@@ -33,42 +33,37 @@ def get_users():
33
33
34
34
@frappe .whitelist ()
35
35
def get_contacts ():
36
- contacts = frappe .get_all (
37
- "Contact" ,
38
- fields = [
39
- "name" ,
40
- "salutation" ,
41
- "first_name" ,
42
- "last_name" ,
43
- "full_name" ,
44
- "gender" ,
45
- "address" ,
46
- "designation" ,
47
- "image" ,
48
- "email_id" ,
49
- "mobile_no" ,
50
- "phone" ,
51
- "company_name" ,
52
- "modified" ,
53
- ],
54
- order_by = "first_name asc" ,
55
- distinct = True ,
56
- )
36
+ from itertools import chain
57
37
58
- for contact in contacts :
59
- contact ["email_ids" ] = frappe .get_all (
60
- "Contact Email" ,
61
- filters = {"parenttype" : "Contact" , "parent" : contact .name },
62
- fields = ["name" , "email_id" , "is_primary" ],
63
- )
38
+ def get_contact_data (filters : dict = None ):
39
+ contacts = get_contact_list (filters )
40
+ contact_names = [c .name for c in contacts ]
41
+ contact_emails = get_contact_emails (contact_names )
42
+ contact_phone = get_contact_phones (contact_names )
43
+ for contact in contacts :
44
+ contact .email_ids = [e for e in contact_emails if e .parent == contact .name ]
45
+ contact .phone_nos = [p for p in contact_phone if p .parent == contact .name ]
64
46
65
- contact ["phone_nos" ] = frappe .get_all (
66
- "Contact Phone" ,
67
- filters = {"parenttype" : "Contact" , "parent" : contact .name },
68
- fields = ["name" , "phone" , "is_primary_phone" , "is_primary_mobile_no" ],
47
+ contact_name_set = {c .name for c in contacts }
48
+ filtered_contact_names = list (
49
+ {
50
+ c .parent
51
+ for c in chain (contact_emails , contact_phone )
52
+ if c .parent not in contact_name_set
53
+ }
69
54
)
55
+ return contacts , filtered_contact_names
56
+
57
+ # Get contacts from `tabContact` which has mobile number set.
58
+ filters = {"mobile_no" : ["is" , "set" ]}
59
+ contacts_1 , filtered_contact_names = get_contact_data (filters )
60
+
61
+ # Get list of contacts to be fetched from `tabContact Email` and `tabContact Phone` combine them, and finally fetch the contacts.
62
+ filters .update ({"name" : ["in" , filtered_contact_names ]})
63
+ contacts_2 , _ = get_contact_data (filters )
70
64
71
- return contacts
65
+ # Combine the contacts from both the queries.
66
+ return list (chain (contacts_1 , contacts_2 ))
72
67
73
68
74
69
@frappe .whitelist ()
@@ -94,3 +89,44 @@ def get_customers():
94
89
).run (as_dict = 1 )
95
90
96
91
return customers
92
+
93
+
94
+ def get_contact_list (filters : dict = None ):
95
+ return frappe .get_all (
96
+ "Contact" ,
97
+ fields = [
98
+ "name" ,
99
+ "salutation" ,
100
+ "first_name" ,
101
+ "last_name" ,
102
+ "full_name" ,
103
+ "gender" ,
104
+ "address" ,
105
+ "designation" ,
106
+ "image" ,
107
+ "email_id" ,
108
+ "mobile_no" ,
109
+ "phone" ,
110
+ "company_name" ,
111
+ "modified" ,
112
+ ],
113
+ order_by = "first_name asc" ,
114
+ filters = filters ,
115
+ distinct = True ,
116
+ )
117
+
118
+
119
+ def get_contact_emails (parent_contact_names : list ):
120
+ return frappe .get_all (
121
+ "Contact Email" ,
122
+ filters = {"parenttype" : "Contact" , "parent" : ["in" , parent_contact_names ]},
123
+ fields = ["name" , "email_id" , "is_primary" , "parent" ],
124
+ )
125
+
126
+
127
+ def get_contact_phones (parent_contact_names : list ):
128
+ return frappe .get_all (
129
+ "Contact Phone" ,
130
+ filters = {"parenttype" : "Contact" , "parent" : ["in" , parent_contact_names ]},
131
+ fields = ["name" , "phone" , "is_primary_phone" , "is_primary_mobile_no" , "parent" ],
132
+ )
0 commit comments