fix: find correct lead/deal doc to attach the call after call end
This commit is contained in:
parent
75c4b35d08
commit
473e9c3e4a
@ -4,6 +4,7 @@ import json
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
from .twilio_handler import Twilio, IncomingCall, TwilioCallDetails
|
from .twilio_handler import Twilio, IncomingCall, TwilioCallDetails
|
||||||
|
from .utils import parse_mobile_no
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def is_enabled():
|
def is_enabled():
|
||||||
@ -149,17 +150,32 @@ def add_note_to_call_log(call_sid, note):
|
|||||||
def get_lead_or_deal_from_number(call):
|
def get_lead_or_deal_from_number(call):
|
||||||
"""Get lead/deal from the given number.
|
"""Get lead/deal from the given number.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def find_record(doctype, mobile_no):
|
||||||
|
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
|
||||||
|
)
|
||||||
|
return data[0].name if data else None
|
||||||
|
|
||||||
doctype = "CRM Lead"
|
doctype = "CRM Lead"
|
||||||
doc = None
|
doc = None
|
||||||
|
to_number = call.get('to')
|
||||||
|
from_number = call.get('from')
|
||||||
if call.type == 'Outgoing':
|
if call.type == 'Outgoing':
|
||||||
doc = frappe.get_cached_value(doctype, { "mobile_no": call.get('to') })
|
doc = find_record(doctype, to_number)
|
||||||
if not doc:
|
if not doc:
|
||||||
doctype = "CRM Deal"
|
doctype = "CRM Deal"
|
||||||
doc = frappe.get_cached_value(doctype, { "mobile_no": call.get('to') })
|
doc = find_record(doctype, to_number)
|
||||||
else:
|
else:
|
||||||
doc = frappe.get_cached_value(doctype, { "mobile_no": call.get('from') })
|
doc = find_record(doctype, from_number)
|
||||||
if not doc:
|
if not doc:
|
||||||
doctype = "CRM Deal"
|
doctype = "CRM Deal"
|
||||||
doc = frappe.get_cached_value(doctype, { "mobile_no": call.get('from') })
|
doc = find_record(doctype, from_number)
|
||||||
|
|
||||||
return doc, doctype
|
return doc, doctype
|
||||||
@ -13,4 +13,11 @@ def merge_dicts(d1: dict, d2: dict):
|
|||||||
)
|
)
|
||||||
... {'name1': {'age': 20, 'phone': '+xxx'}, 'name2': {'age': 30, 'phone': '+yyy'}}
|
... {'name1': {'age': 20, 'phone': '+xxx'}, 'name2': {'age': 30, 'phone': '+yyy'}}
|
||||||
"""
|
"""
|
||||||
return {k:{**v, **d2.get(k, {})} for k, v in d1.items()}
|
return {k:{**v, **d2.get(k, {})} for k, v in d1.items()}
|
||||||
|
|
||||||
|
def parse_mobile_no(mobile_no: str):
|
||||||
|
"""Parse mobile number to remove spaces, brackets, etc.
|
||||||
|
>>> parse_mobile_no('+91 (766) 667 6666')
|
||||||
|
... '+917666676666'
|
||||||
|
"""
|
||||||
|
return ''.join([c for c in mobile_no if c.isdigit() or c == '+'])
|
||||||
Loading…
x
Reference in New Issue
Block a user