diff --git a/src/hrflow_connectors/connectors/hrflow/warehouse/profile.py b/src/hrflow_connectors/connectors/hrflow/warehouse/profile.py index 13f3e4929..636db472d 100644 --- a/src/hrflow_connectors/connectors/hrflow/warehouse/profile.py +++ b/src/hrflow_connectors/connectors/hrflow/warehouse/profile.py @@ -174,6 +174,30 @@ def write( return failed +def merge_info(base: dict, info: dict) -> dict: + if not info: + return base + + info_parsed = base.get("info", {}) + existing_urls = info_parsed.get("urls", []) + + if isinstance(info.get("urls"), list): + for new_url in info["urls"]: + if new_url not in existing_urls: + existing_urls.append(new_url) + + info_parsed["urls"] = existing_urls + + for key, value in info.items(): + if value and key != "location" and key != "urls": + info_parsed[key] = value + elif key == "location" and isinstance(value, dict) and any(value.values()): + info_parsed[key] = value + + base["info"] = info_parsed + return base + + def write_parsing( adapter: LoggerAdapter, parameters: WriteProfileParsingParameters, @@ -184,6 +208,7 @@ def write_parsing( api_secret=parameters.api_secret, api_user=parameters.api_user ) for profile in profiles: + profile_info = profile.pop("info", {}) if parameters.only_insert and hrflow_client.profile.indexing.get( source_key=parameters.source_key, reference=profile["reference"] ).get("data"): @@ -193,7 +218,7 @@ def write_parsing( ) continue - response = hrflow_client.profile.parsing.add_file( + parsing_response = hrflow_client.profile.parsing.add_file( source_key=parameters.source_key, profile_file=profile["resume"]["raw"], profile_content_type=profile["resume"]["content_type"], @@ -202,13 +227,40 @@ def write_parsing( metadatas=profile["metadatas"], created_at=profile["created_at"], ) - if response["code"] != 202: + if parsing_response["code"] not in [202, 201]: # 202: Accepted, 201: Created adapter.error( "Failed to parse profile with reference={} response={}".format( - profile["reference"], response + profile["reference"], parsing_response ) ) failed.append(profile) + continue + source_response = hrflow_client.source.get( + key=parameters.source_key + ) # Get source to check if sync_parsing is enabled + if source_response["code"] != 200: + adapter.warning( + "Failed to get source with key={} response={}, won't be able to update" + " profile parsed with profile json".format( + parameters.source_key, source_response + ) + ) + elif source_response["data"]["sync_parsing"] is True: + current_profile = parsing_response["data"]["profile"] + profile_result = merge_info(current_profile, profile_info) + edit_response = hrflow_client.profile.indexing.edit( + source_key=parameters.source_key, + key=profile_result["key"], + profile_json=profile_result, + ) + if edit_response["code"] != 200: + adapter.warning( + "Failed to update profile after parsing, reference={}" + " response={}".format(profile["reference"], edit_response) + ) + failed.append(profile) + continue + return failed