diff --git a/scripts/manual_team_sync.py b/scripts/manual_team_sync.py index abdc73d..9474c2d 100644 --- a/scripts/manual_team_sync.py +++ b/scripts/manual_team_sync.py @@ -125,41 +125,49 @@ def enlist_teams_to_fix( cosec_team_list: list, ) -> list: + """ + Gives you a list of TCAOFF team members whose Cosec details are missing or malformed. + This does NOT enlist missing names. Th efocus of this function is only fixing. + :param tcaoff_team_list: The list of team members from TCAOFF's '/team/list' API. + :param cosec_team_list: The list of team members from Cosec's Muster Roll report. + :return: The list of TCAOFF team members with read-to-use correct data. + """ + # Prepare the variables: teams_to_fix = [] cosec_id_to_cosec_team_map = {ct["User ID"]: ct for ct in cosec_team_list} - print("COSEC_MAP:", json.to_string(cosec_id_to_cosec_team_map)) for tt in tcaoff_team_list: - # Start by assuming everything is alright, - # then extract the notes: - need_to_sync = False + # Extract Cosec notes: json_notes = json.from_string(tt.get("json_notes", "{}")) applicant_notes = json_notes.get("applicantNotes") or {} cosec_notes = applicant_notes.get("cosec") or {} - # Now check if any fix is required: + # Now check if any fix is required. + # Fixes will be required when: + # 1. The team member is present in TCAOFF, + # 2. the team member either has no cosec notes, or the notes are malformed. user_id = cosec_notes.get("User ID", cosec_notes.get("UserID")) if not user_id: - print("NEED TO FIX:", f"{tt['full_name']} ({tt['pseudonym']})") - - # if tt['pseudonym'] == "VIS07070": - # print("RAKESH:", json.to_string(tt)) - # print("RAKESH:", user_id) - # print("RAKESH:", json.to_string(cosec_notes)) + if (valid_cose_notes := cosec_id_to_cosec_team_map.get(tt["pseudonym"])) is not None: + applicant_notes["cosec"] = valid_cose_notes + tt.update({"applicantNotes": applicant_notes}) + teams_to_fix.append(tt) + # Done here: + return teams_to_fix # --------------------------------------------------------------------------------------------------------------------- -def enlist_missing_teams( - tcaoff_team_list: list, - cosec_team_list: list -) -> list: - - pass +# def enlist_missing_teams( +# tcaoff_team_list: list, +# cosec_team_list: list +# ) -> list: +# +# pass # --------------------------------------------------------------------------------------------------------------------- @@ -172,6 +180,16 @@ async def manual_team_sync( test_mode: bool = False, ) -> None: + """ + To manually sync. teams between Cosec (source) and TCAOFF (dest). + :param cosec_creds: The credentials to log into Cosec. + :param tcaoff_client: The credentials to log into TCAOFF. + :param on_date: The date for which the teams need to be synchronized. That day's Cosec Muster Roll will be fetched + and those records will be sync'd. + :param test_mode: Enable this during development or local testing. + :return: None. + """ + printer("MANUAL TEAM SYNC") # Try the whole process once: @@ -188,26 +206,35 @@ async def manual_team_sync( now = date_time.get_current_ist_date_time() if not on_date: on_date = now - # # Get the previous day's In/Out Summary and then wait - # # for the driver's resources to get freed: - # success = common.get_muster_roll( - # cosec_creds = cosec_creds, - # on_date = on_date, - # cache_file = common.get_manual_muster_roll_cache_file_path, - # test_mode = test_mode - # ) + # Get the previous day's In/Out Summary and then wait + # for the driver's resources to get freed: + success = common.get_muster_roll( + cosec_creds = cosec_creds, + on_date = on_date, + cache_file = common.get_manual_muster_roll_cache_file_path, + test_mode = test_mode + ) success = True # Sync data between Cosec and TCAOFF: if success: + # --- Data Fetch: printer("MUSTER ROLL: Sync'ing with TCAOFF") - print("SYNC FROM:", common.get_manual_muster_roll_cache_file_path()) cosec_teams = json.from_file(common.get_manual_muster_roll_cache_file_path())["report"] - # print("COSEC:", json.to_string(cosec_teams)) tcaoff_teams = await tcaoff_client.team_list() - # print("TCAOFF:", json.to_string(tcaoff_teams)) - # --- + # --- Team Fix: teams_to_fix = enlist_teams_to_fix(tcaoff_teams, cosec_teams) + for t in teams_to_fix: + printer("Fixing", t["user_id"], t["full_name"]) + await tcaoff_client.team_update( + user_id = t["user_id"], + dept_id = t["department_id"], + team_name = t["full_name"], + email = t["email"], + phone_no = t["phone_number"], + role = t["role"], + applicant_notes = t["applicantNotes"], + ) # If something goes wrong in the COSEC step: diff --git a/tcaoff/async_tcaoff.py b/tcaoff/async_tcaoff.py index 26d222a..91068a6 100644 --- a/tcaoff/async_tcaoff.py +++ b/tcaoff/async_tcaoff.py @@ -531,7 +531,7 @@ class AsyncTheCAOffice: role: str, username: str, password: str, - applicant_notes: dict | list | str = None, + applicant_notes: dict | list = None, raise_exception: bool = False, ) -> bool: @@ -604,6 +604,81 @@ class AsyncTheCAOffice: # Done here: return success + async def team_update( + self, + user_id: int, + dept_id: int, + team_name: str, + email: str, + phone_no: str, + role: str, + applicant_notes: dict | list | str = None, + raise_exception: bool = False, + ) -> bool: + + """ + Update an existing team member. + :param user_id: The id of the team member. + :param dept_id: The id of the department that this team member is working in. + :param team_name: The name of the team member. This is the full display name. Can be the same as others. + :param email: The email id of the team member. + :param phone_no: The phone no. of the team member. + :param role: The role of the team member in the organization. + :param applicant_notes: Optional notes about the team member. + :param raise_exception: Whether to raise any exceptions, or to suppress them. + :return: True if successful, else False. + """ + + # Start by assuming failure: + success = False + + try: + + # Make the API call: + response = await self._http_client.post( + url = self.TEAM_UPDATE_URL, + headers = {"X-Session-Token": self.__session_token}, + json = { + "idUser": user_id, + "idDepartment": dept_id, + "name": team_name, + "email": email, + "phoneNo": phone_no, + "role": role, + "hierarchy": 1, + "applicantNotes": applicant_notes + } + ) + + # If the call succeeded: + if response.status_code in [200]: + self._printer( + "Team updated.", + user_id, + team_name, + ) + success = True + + # If the call failed: + else: + response_json = response.json() + self._err_printer( + "Team NOT updated.", + user_id, + team_name, + response_json + ) + success = False + + # If something goes wrong: + except Exception as exception: + self._err_printer("Team NOT updated.", exception) + if raise_exception: raise + success = False + + # Done here: + return success + # ┏┓ ┓ # ┣┫╋╋┏┓┏┓┏┫┏┓┏┓┏┏┓ # ┛┗┗┗┗ ┛┗┗┻┗┻┛┗┗┗