@@ -590,43 +590,38 @@ def retrieve_contacts_v2(
590590 type .value for type in
591591 self ._portaswitch_settings .CONTACTS_SELECTING_EXTENSION_TYPES
592592 }
593-
593+
594594 # For EXTENSIONS mode, we need to get extensions first
595595 extensions = self ._admin_api .get_extensions_list (i_customer )["extensions_list" ]
596-
596+
597597 # Filter extensions by allowed types and exclude current user
598598 filtered_extensions = [
599599 ext for ext in extensions
600600 if ext ["type" ] in allowed_ext_types and ext .get ("i_account" ) != i_account
601601 ]
602-
602+
603603 # If search is provided, filter extensions
604604 if search :
605- search_lower = search .lower ()
605+ search_fields = ["id" , "firstname" , "lastname" , "name" , "email" ]
606+
606607 filtered_extensions = [
607608 ext for ext in filtered_extensions
608- if (search_lower in (ext .get ("firstname" , "" ) or "" ).lower () or
609- search_lower in (ext .get ("lastname" , "" ) or "" ).lower () or
610- search_lower in (ext .get ("extension_name" , "" ) or "" ).lower () or
611- search_lower in (ext .get ("email" , "" ) or "" ).lower ())
609+ if any (search .lower () in ext .get (field , "" ).lower () for field in search_fields )
612610 ]
613-
614- # Get accounts for extensions (we need to get all accounts to map extensions)
615- # For pagination, we'll get accounts in batches if needed
616- account_ids = {ext .get ("i_account" ) for ext in filtered_extensions if ext .get ("i_account" )}
617-
611+
618612 # Get accounts for these extension account IDs
619613 all_accounts = self ._get_all_accounts_by_customer (i_customer )
620- account_to_aliases = {account ["i_account" ]: account .get ("alias_list" , []) for account in all_accounts }
621-
614+ account_to_aliases = {account ["i_account" ]: account .get ("alias_list" , []) for account in
615+ all_accounts }
616+
622617 # Build contacts from filtered extensions
623618 for ext in filtered_extensions :
624619 aliases = account_to_aliases .get (ext .get ("i_account" ), [])
625620 contacts .append (Serializer .get_contact_info_by_extension (ext , aliases , i_account ))
626-
621+
627622 # Add custom contacts before pagination
628623 custom_contacts = [Serializer .get_contact_info_by_custom_entry (entry ) for entry in
629- self ._portaswitch_settings .CONTACTS_CUSTOM ]
624+ self ._portaswitch_settings .CONTACTS_CUSTOM ]
630625 if search :
631626 search_lower = search .lower ()
632627 custom_contacts = [
@@ -638,19 +633,19 @@ def retrieve_contacts_v2(
638633 search_lower in (contact .numbers .main or "" ).lower ())
639634 ]
640635 contacts .extend (custom_contacts )
641-
636+
642637 # Apply pagination
643638 total_count = len (contacts )
644639 start_idx = (page - 1 ) * items_per_page
645640 end_idx = start_idx + items_per_page
646641 contacts = contacts [start_idx :end_idx ]
647-
642+
648643 case PortaSwitchContactsSelectingMode .ACCOUNTS :
649644 # Get custom contacts (needed for both search and non-search modes)
650645 custom_contacts = [Serializer .get_contact_info_by_custom_entry (entry ) for entry in
651- self ._portaswitch_settings .CONTACTS_CUSTOM ]
646+ self ._portaswitch_settings .CONTACTS_CUSTOM ]
652647 custom_contacts_count = len (custom_contacts )
653-
648+
654649 if search :
655650 # Search mode: call get_account_list 4 times with different search parameters
656651 search_pattern = f"%{ search } %"
@@ -661,31 +656,31 @@ def retrieve_contacts_v2(
661656 accounts_main_number = result_main_number .get ("account_list" , [])
662657 for account in accounts_main_number :
663658 accounts_dict [account ["i_account" ]] = account
664-
659+
665660 # Search by firstname
666661 result_firstname = self ._admin_api .get_account_list (i_customer , firstname = search_pattern )
667662 accounts_firstname = result_firstname .get ("account_list" , [])
668663 for account in accounts_firstname :
669664 accounts_dict [account ["i_account" ]] = account
670-
665+
671666 # Search by lastname
672667 result_lastname = self ._admin_api .get_account_list (i_customer , lastname = search_pattern )
673668 accounts_lastname = result_lastname .get ("account_list" , [])
674669 for account in accounts_lastname :
675670 accounts_dict [account ["i_account" ]] = account
676-
671+
677672 # Search by extension_name
678673 result_extension = self ._admin_api .get_account_list (i_customer , extension_name = search_pattern )
679674 accounts_extension = result_extension .get ("account_list" , [])
680675 for account in accounts_extension :
681676 accounts_dict [account ["i_account" ]] = account
682-
677+
683678 # Search by email
684679 result_email = self ._admin_api .get_account_list (i_customer , email = search_pattern )
685680 accounts_email = result_email .get ("account_list" , [])
686681 for account in accounts_email :
687682 accounts_dict [account ["i_account" ]] = account
688-
683+
689684 # Use accounts directly from search results
690685 accounts = list (accounts_dict .values ())
691686 else :
@@ -699,7 +694,7 @@ def retrieve_contacts_v2(
699694 api_limit = items_per_page
700695 offset = (page - 1 ) * items_per_page - custom_contacts_count
701696 offset = max (0 , offset ) # Ensure offset is not negative
702-
697+
703698 # Get accounts from API with pagination
704699 result = self ._admin_api .get_account_list (
705700 i_customer ,
@@ -708,22 +703,23 @@ def retrieve_contacts_v2(
708703 )
709704 accounts = result .get ("account_list" , [])
710705 total_count_from_api = result .get ("total" , 0 )
711-
706+
712707 # Filter accounts
713708 filtered_accounts = []
714709 for account in accounts :
715710 if (status := account .get ("status" )) and status == "blocked" :
716711 continue
717712 dual_version_system = PortaSwitchDualVersionSystem (account .get ("dual_version_system" ))
718713 if dual_version_system != PortaSwitchDualVersionSystem .SOURCE :
719- if (not self ._portaswitch_settings .CONTACTS_SKIP_WITHOUT_EXTENSION or account .get ("extension_id" )) and account ["i_account" ] != i_account :
714+ if (not self ._portaswitch_settings .CONTACTS_SKIP_WITHOUT_EXTENSION or account .get (
715+ "extension_id" )) and account ["i_account" ] != i_account :
720716 filtered_accounts .append (account )
721-
717+
722718 # Build contacts from accounts
723719 account_contacts = []
724720 for account in filtered_accounts :
725721 account_contacts .append (Serializer .get_contact_info_by_account (account , i_account ))
726-
722+
727723 # Filter custom contacts if search is provided
728724 if search :
729725 search_lower = search .lower ()
@@ -735,11 +731,11 @@ def retrieve_contacts_v2(
735731 search_lower in (contact .email or "" ).lower () or
736732 search_lower in (contact .numbers .main or "" ).lower ())
737733 ]
738-
734+
739735 # Add custom contacts (only on first page for non-search mode)
740736 if search or page == 1 :
741737 account_contacts .extend (custom_contacts )
742-
738+
743739 # Apply pagination
744740 if search :
745741 # For search mode, apply manual pagination after filtering
@@ -753,25 +749,25 @@ def retrieve_contacts_v2(
753749 contacts = account_contacts [:items_per_page ]
754750 # Total count from API plus custom contacts (only count them once)
755751 total_count = total_count_from_api + (len (custom_contacts ) if page == 1 else 0 )
756-
752+
757753 case PortaSwitchContactsSelectingMode .PHONEBOOK :
758754 phonebook = self ._account_api .get_phonebook_list (access_token , 1 , 100 )['phonebook_rec_list' ]
759-
755+
760756 # Extract phone numbers from phonebook records
761757 phonebook_numbers = set ()
762758 for record in phonebook :
763759 phone_number = record .get ("phone_number" , "" ).replace ("+" , "" )
764760 if phone_number :
765761 phonebook_numbers .add (phone_number )
766-
762+
767763 # Get account mapping only for phonebook numbers (on-demand)
768764 number_to_accounts = self ._get_number_to_customer_accounts_map_for_numbers (phonebook_numbers )
769-
765+
770766 for record in phonebook :
771767 # Normalize phone number by removing the '+' prefix
772768 phonebook_record_number = record .get ("phone_number" ).replace ("+" , "" )
773769 phonebook_contact_info = Serializer .get_contact_info_by_phonebook_record (record )
774-
770+
775771 if account := number_to_accounts .get (phonebook_record_number ):
776772 # If we found a matching account, use its contact info but update with phonebook data
777773 contact = Serializer .get_contact_info_by_account (account , i_account )
@@ -782,37 +778,39 @@ def retrieve_contacts_v2(
782778 contact = phonebook_contact_info
783779 if contact .numbers .main :
784780 contact .numbers .main = contact .numbers .main .replace ("+" , "" )
785-
781+
786782 if contact .is_current_user is not True :
787783 contacts .append (contact )
788-
784+
789785 # Add custom contacts before filtering and pagination
790786 custom_contacts = [Serializer .get_contact_info_by_custom_entry (entry ) for entry in
791- self ._portaswitch_settings .CONTACTS_CUSTOM ]
787+ self ._portaswitch_settings .CONTACTS_CUSTOM ]
792788 contacts .extend (custom_contacts )
793-
794- # Apply search filter if provided
789+
790+ # If search is provided, filter extensions
795791 if search :
796792 search_lower = search .lower ()
797793 contacts = [
798794 contact for contact in contacts
799- if (search_lower in (contact .first_name or "" ).lower () or
800- search_lower in (contact .last_name or "" ).lower () or
801- search_lower in (contact .alias_name or "" ).lower () or
802- search_lower in (contact .email or "" ).lower () or
803- search_lower in (contact .numbers .main or "" ).lower ())
795+ if any (search_lower in str (value or "" ).lower () for value in [
796+ contact .numbers .main if contact .numbers else None ,
797+ contact .first_name ,
798+ contact .last_name ,
799+ contact .alias_name ,
800+ contact .email
801+ ])
804802 ]
805-
803+
806804 # Apply pagination
807805 total_count = len (contacts )
808806 start_idx = (page - 1 ) * items_per_page
809807 end_idx = start_idx + items_per_page
810808 contacts = contacts [start_idx :end_idx ]
811-
809+
812810 case PortaSwitchContactsSelectingMode .PHONE_DIRECTORY :
813811 phone_directories = self ._account_api .get_phone_directory_list (access_token , 1 , 100 )[
814812 'phone_directory_list' ]
815-
813+
816814 # Extract phone numbers from phone directory records
817815 phone_directory_numbers = set ()
818816 for directory in phone_directories :
@@ -826,10 +824,10 @@ def retrieve_contacts_v2(
826824 office_number = record .get ("office_number" , "" ).replace ("+" , "" )
827825 if office_number :
828826 phone_directory_numbers .add (office_number )
829-
827+
830828 # Get account mapping only for phone directory numbers (on-demand)
831829 number_to_accounts = self ._get_number_to_customer_accounts_map_for_numbers (phone_directory_numbers )
832-
830+
833831 for directory in phone_directories :
834832 directory_info = self ._account_api .get_phone_directory_info (
835833 access_token ,
@@ -841,8 +839,9 @@ def retrieve_contacts_v2(
841839 # Normalize phone number by removing the '+' prefix
842840 phone_directory_record_number = record .get ("office_number" ).replace ("+" , "" )
843841 phone_directory_contact_info = Serializer .get_contact_info_by_phone_directory_record (record ,
844- directory_info ['name' ])
845-
842+ directory_info [
843+ 'name' ])
844+
846845 if account := number_to_accounts .get (phone_directory_record_number ):
847846 # If we found a matching account, use its contact info but update with phone directory data
848847 contact = Serializer .get_contact_info_by_account (account , i_account )
@@ -854,33 +853,35 @@ def retrieve_contacts_v2(
854853 contact = phone_directory_contact_info
855854 if contact .numbers .main :
856855 contact .numbers .main = contact .numbers .main .replace ("+" , "" )
857-
856+
858857 if contact .is_current_user is not True :
859858 contacts .append (contact )
860-
859+
861860 # Add custom contacts before filtering and pagination
862861 custom_contacts = [Serializer .get_contact_info_by_custom_entry (entry ) for entry in
863- self ._portaswitch_settings .CONTACTS_CUSTOM ]
862+ self ._portaswitch_settings .CONTACTS_CUSTOM ]
864863 contacts .extend (custom_contacts )
865-
864+
866865 # Apply search filter if provided
867866 if search :
868867 search_lower = search .lower ()
869868 contacts = [
870869 contact for contact in contacts
871- if (search_lower in (contact .first_name or "" ).lower () or
872- search_lower in (contact .last_name or "" ).lower () or
873- search_lower in (contact .alias_name or "" ).lower () or
874- search_lower in (contact .email or "" ).lower () or
875- search_lower in (contact .numbers .main or "" ).lower ())
870+ if any (search_lower in str (value or "" ).lower () for value in [
871+ contact .numbers .main if contact .numbers else None ,
872+ contact .first_name ,
873+ contact .last_name ,
874+ contact .alias_name ,
875+ contact .email
876+ ])
876877 ]
877-
878+
878879 # Apply pagination
879880 total_count = len (contacts )
880881 start_idx = (page - 1 ) * items_per_page
881882 end_idx = start_idx + items_per_page
882883 contacts = contacts [start_idx :end_idx ]
883-
884+
884885 return contacts , total_count
885886
886887 except WebTritErrorException as error :
0 commit comments