Skip to content

Commit 4424342

Browse files
authored
Merge pull request #66 from rtCamp/pref/get-contacts
Reduce execution time for get_contacts api
2 parents 7d933aa + 86bd677 commit 4424342

File tree

1 file changed

+68
-32
lines changed

1 file changed

+68
-32
lines changed

next_crm/api/session.py

+68-32
Original file line numberDiff line numberDiff line change
@@ -33,42 +33,37 @@ def get_users():
3333

3434
@frappe.whitelist()
3535
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
5737

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]
6446

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+
}
6954
)
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)
7064

71-
return contacts
65+
# Combine the contacts from both the queries.
66+
return list(chain(contacts_1, contacts_2))
7267

7368

7469
@frappe.whitelist()
@@ -94,3 +89,44 @@ def get_customers():
9489
).run(as_dict=1)
9590

9691
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

Comments
 (0)