From 481d097563236f1b48e8a18076cdb4cf89bea71f Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Wed, 13 Mar 2024 16:49:41 +0530 Subject: [PATCH] refactor: updated find_record logic --- crm/twilio/api.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/crm/twilio/api.py b/crm/twilio/api.py index ce1ff21c..7acaec91 100644 --- a/crm/twilio/api.py +++ b/crm/twilio/api.py @@ -157,24 +157,26 @@ def get_lead_or_deal_from_number(call): """Get lead/deal from the given number. """ - def find_record(doctype, mobile_no): + def find_record(doctype, mobile_no, where=''): mobile_no = parse_mobile_no(mobile_no) - data = frappe.db.sql( - """ - SELECT name, mobile_no - FROM `tab{doctype}` - WHERE CONCAT('+', REGEXP_REPLACE(mobile_no, '[^0-9]', '')) = {mobile_no} - """.format(doctype=doctype, mobile_no=mobile_no), - as_dict=True - ) + + query = f""" + SELECT name, mobile_no + FROM `tab{doctype}` + WHERE CONCAT('+', REGEXP_REPLACE(mobile_no, '[^0-9]', '')) = {mobile_no} + """ + + data = frappe.db.sql(query + where, as_dict=True) return data[0].name if data else None - doctype = "CRM Lead" + doctype = "CRM Deal" number = call.get('to') if call.type == 'Outgoing' else call.get('from') doc = find_record(doctype, number) or None if not doc: - doctype = "CRM Deal" - doc = find_record(doctype, number) + doctype = "CRM Lead" + doc = find_record(doctype, number, 'AND converted is not True') + if not doc: + doc = find_record(doctype, number) return doc, doctype \ No newline at end of file