|
| 1 | +import typing as t |
| 2 | + |
| 3 | +from ..hrflow import Hrflow |
| 4 | +from ..schemas import HrFlowProfile, ProfileInfo |
| 5 | + |
| 6 | + |
| 7 | +def is_valid_info_for_searching(info: ProfileInfo) -> bool: |
| 8 | + """ |
| 9 | + Check if the info part of a profile is valid for searching |
| 10 | +
|
| 11 | + Based on the following schemas https://developers.hrflow.ai/docs/profiles-searching |
| 12 | +
|
| 13 | + Args: |
| 14 | + info: <ProfileInfo> |
| 15 | + Info part of the profile |
| 16 | + """ |
| 17 | + if not isinstance(info, ProfileInfo): |
| 18 | + raise ValueError("info must be a ProfileInfo object") |
| 19 | + |
| 20 | + first_name_score = 1 if info.first_name else 0 |
| 21 | + last_name_score = 1 if info.last_name else 0 |
| 22 | + phone_score = 1 if info.phone else 0 |
| 23 | + date_birth_score = 1 if info.date_birth else 0 |
| 24 | + gender_score = 1 if info.gender else 0 |
| 25 | + summary_score = 1 if info.summary else 0 |
| 26 | + urls_score = 1 if info.urls else 0 |
| 27 | + location_score = 1 if info.location else 0 |
| 28 | + |
| 29 | + info_score = ( |
| 30 | + first_name_score |
| 31 | + + last_name_score |
| 32 | + + phone_score |
| 33 | + + date_birth_score |
| 34 | + + gender_score |
| 35 | + + summary_score |
| 36 | + + urls_score |
| 37 | + + location_score |
| 38 | + ) |
| 39 | + info_score = info_score / 8 |
| 40 | + has_person = info.first_name and info.last_name |
| 41 | + |
| 42 | + return info.email or has_person or info_score >= 0.5 |
| 43 | + |
| 44 | + |
| 45 | +def is_valid_for_searching( |
| 46 | + client: Hrflow, |
| 47 | + profile: t.Optional[t.Union[t.Dict, HrFlowProfile]] = None, |
| 48 | + source_key: t.Optional[str] = None, |
| 49 | + profile_key: t.Optional[str] = None, |
| 50 | + profile_reference: t.Optional[str] = None, |
| 51 | +) -> bool: |
| 52 | + """ |
| 53 | + Check if a profile is valid for searching |
| 54 | +
|
| 55 | + Based on the following schemas https://developers.hrflow.ai/docs/profiles-searching |
| 56 | +
|
| 57 | + Args: |
| 58 | + client: <Hrflow> |
| 59 | + Hrflow client |
| 60 | + profile: <dict> or <HrFlowProfile> |
| 61 | + Profile to check. Can be not provided if source_key, |
| 62 | + profile_key or profile_reference are provided |
| 63 | + source_key: <str> |
| 64 | + Source key. If provided, profile_key or |
| 65 | + profile_reference must be also provided. |
| 66 | + profile_key: <str> |
| 67 | + Profile key. If provided, profile_reference must be None |
| 68 | + profile_reference: <str> |
| 69 | + Profile reference. If provided, profile_key must be None |
| 70 | + Return: |
| 71 | + <bool> True if the profile is valid for searching, |
| 72 | + False otherwise |
| 73 | + """ |
| 74 | + # Check parameters and fetch profile if needed |
| 75 | + if profile is None: |
| 76 | + if source_key is None: |
| 77 | + raise ValueError("profile or source_key must be provided") |
| 78 | + elif profile_key is None and profile_reference is None: |
| 79 | + raise ValueError("profile_key or profile_reference must be provided") |
| 80 | + |
| 81 | + response = client.profile.storing.get( |
| 82 | + source_key=source_key, key=profile_key, reference=profile_reference |
| 83 | + ) |
| 84 | + if response["code"] >= 400: |
| 85 | + message = response["message"] |
| 86 | + raise ValueError(f"Error while fetching profile: {message}") |
| 87 | + profile = response["data"] |
| 88 | + else: |
| 89 | + if ( |
| 90 | + source_key is not None |
| 91 | + or profile_key is not None |
| 92 | + or profile_reference is not None |
| 93 | + ): |
| 94 | + |
| 95 | + raise ValueError( |
| 96 | + "If you provide a profile, you can't provide source_key, profile_key " |
| 97 | + "or profile_reference" |
| 98 | + ) |
| 99 | + |
| 100 | + if isinstance(profile, dict): |
| 101 | + profile = HrFlowProfile.parse_obj(profile) |
| 102 | + |
| 103 | + if not isinstance(profile, HrFlowProfile): |
| 104 | + raise ValueError("profile must be a dict or a HrFlowProfile object") |
| 105 | + |
| 106 | + # Check if profile is valid for searching |
| 107 | + return is_valid_info_for_searching(profile.info) and bool(profile.text) |
0 commit comments