@@ -425,27 +425,10 @@ def retrieve_contacts(self, session: SessionInfo, user: UserInfo) -> list[Contac
425425 contacts .append (Serializer .get_contact_info_by_account (account , i_account ))
426426 case PortaSwitchContactsSelectingMode .PHONEBOOK :
427427 phonebook = self ._account_api .get_phonebook_list (access_token , 1 , 100 )['phonebook_rec_list' ]
428- number_to_accounts = {}
429- # Retrieve accounts for a pre-defined list of customers to map them to phonebook records
430- with ThreadPoolExecutor (max_workers = 10 ) as executor :
431- future_to_customer_id = {
432- executor .submit (lambda cid : self ._admin_api .get_account_list (int (cid ))["account_list" ],
433- cid ): cid
434- for cid in self ._portaswitch_settings .CONTACTS_SELECTING_PHONEBOOK_CUSTOMER_IDS
435- }
436-
437- for future in as_completed (future_to_customer_id ):
438- try :
439- accounts = future .result ()
440- for account in accounts :
441- number_to_accounts [account ["id" ]] = account
442- except Exception as e :
443- logging .warning (
444- f"Error fetching accounts for customer { future_to_customer_id [future ]} : { e } " )
445-
446- contacts = []
428+ number_to_accounts = self ._get_number_to_customer_accounts_map ()
429+
447430 for record in phonebook :
448- # Normalize phone number by removing '+' prefix
431+ # Normalize phone number by removing the '+' prefix
449432 phonebook_record_number = record .get ("phone_number" ).replace ("+" , "" )
450433 phonebook_contact_info = Serializer .get_contact_info_by_phonebook_record (record )
451434
@@ -463,6 +446,40 @@ def retrieve_contacts(self, session: SessionInfo, user: UserInfo) -> list[Contac
463446 if contact .is_current_user is not True :
464447 contacts .append (contact )
465448
449+ case PortaSwitchContactsSelectingMode .PHONE_DIRECTORY :
450+ phone_directories = self ._account_api .get_phone_directory_list (access_token , 1 , 100 )[
451+ 'phone_directory_list' ]
452+ number_to_accounts = self ._get_number_to_customer_accounts_map ()
453+
454+ for directory in phone_directories :
455+ directory_info = self ._account_api .get_phone_directory_info (
456+ access_token ,
457+ directory ['i_ua_config_directory' ],
458+ 1 ,
459+ 10_000
460+ )['phone_directory_info' ]
461+ for record in directory_info ['directory_records' ]:
462+ # Normalize phone number by removing the '+' prefix
463+ phone_directory_record_number = record .get ("office_number" ).replace ("+" , "" )
464+ phone_directory_contact_info = Serializer .get_contact_info_by_phone_directory_record (record ,
465+ directory_info [
466+ 'name' ])
467+
468+ if account := number_to_accounts .get (phone_directory_record_number ):
469+ # If we found a matching account, use its contact info but update with phone directory data
470+ contact = Serializer .get_contact_info_by_account (account , i_account )
471+ contact .first_name = phone_directory_contact_info .first_name
472+ contact .last_name = phone_directory_contact_info .last_name
473+ contact .numbers .main = phone_directory_record_number
474+ else :
475+ # No matching account found, use phone directory contact info as is
476+ contact = phone_directory_contact_info
477+ if contact .numbers .main :
478+ contact .numbers .main = contact .numbers .main .replace ("+" , "" )
479+
480+ if contact .is_current_user is not True :
481+ contacts .append (contact )
482+
466483 # Extend the contact list with custom entries
467484 contacts .extend ([Serializer .get_contact_info_by_custom_entry (entry ) for entry in
468485 self ._portaswitch_settings .CONTACTS_CUSTOM ])
@@ -806,3 +823,25 @@ def _check_allowed_addons(self, account_info: dict):
806823
807824 if not allowed_addons .intersection (assigned_addon_names ):
808825 raise WebTritErrorException (403 , "Access denied: required add-on not assigned" , code = "addon_required" )
826+
827+ def _get_number_to_customer_accounts_map (self ) -> dict [str , dict ]:
828+ """Return a mapping of phone numbers to customer accounts."""
829+ number_to_accounts = {}
830+ # Retrieve accounts for a pre-defined list of customers to map them to phonebook records
831+ with ThreadPoolExecutor (max_workers = 10 ) as executor :
832+ future_to_customer_id = {
833+ executor .submit (lambda cid : self ._admin_api .get_account_list (int (cid ))["account_list" ],
834+ cid ): cid
835+ for cid in self ._portaswitch_settings .CONTACTS_SELECTING_CUSTOMER_IDS
836+ }
837+
838+ for future in as_completed (future_to_customer_id ):
839+ try :
840+ accounts = future .result ()
841+ for account in accounts :
842+ number_to_accounts [account ["id" ]] = account
843+ except Exception as e :
844+ logging .warning (
845+ f"Error fetching accounts for customer { future_to_customer_id [future ]} : { e } " )
846+
847+ return number_to_accounts
0 commit comments