diff --git a/crm/api/comment.py b/crm/api/comment.py index 0333d174..8649d85b 100644 --- a/crm/api/comment.py +++ b/crm/api/comment.py @@ -3,6 +3,7 @@ from collections.abc import Iterable import frappe from frappe import _ from bs4 import BeautifulSoup +from crm.fcrm.doctype.crm_notification.crm_notification import notify_user def on_update(self, method): notify_mentions(self) @@ -29,22 +30,17 @@ def notify_mentions(doc): { doc.reference_name } """ - values = frappe._dict( - doctype="CRM Notification", - from_user=doc.owner, - to_user=mention.email, - type="Mention", - message=doc.content, - notification_text=notification_text, - notification_type_doctype="Comment", - notification_type_doc=doc.name, - reference_doctype=doc.reference_doctype, - reference_name=doc.reference_name, - ) - - if frappe.db.exists("CRM Notification", values): - return - frappe.get_doc(values).insert() + notify_user({ + "owner": doc.owner, + "assigned_to": mention.email, + "notification_type": "Mention", + "message": doc.content, + "notification_text": notification_text, + "doctype": "Comment", + "name": doc.name, + "reference_doctype": doc.reference_doctype, + "reference_docname": doc.reference_name, + }) def extract_mentions(html): diff --git a/crm/api/whatsapp.py b/crm/api/whatsapp.py index 6bbc2424..c2a01aef 100644 --- a/crm/api/whatsapp.py +++ b/crm/api/whatsapp.py @@ -2,6 +2,7 @@ import frappe import json from frappe import _ from crm.api.doc import get_assigned_users +from crm.fcrm.doctype.crm_notification.crm_notification import notify_user def validate(doc, method): @@ -29,30 +30,25 @@ def notify_agent(doc): if doctype.startswith("CRM "): doctype = doctype[4:].lower() notification_text = f""" -
- { _('You') } - { _('received a whatsapp message in {0}').format(doctype) } - { doc.reference_name } -
- """ +
+ { _('You') } + { _('received a whatsapp message in {0}').format(doctype) } + { doc.reference_name } +
+ """ assigned_users = get_assigned_users(doc.reference_doctype, doc.reference_name) for user in assigned_users: - values = frappe._dict( - doctype="CRM Notification", - from_user=doc.owner, - to_user=user, - type="WhatsApp", - message=doc.message, - notification_text=notification_text, - notification_type_doctype="WhatsApp Message", - notification_type_doc=doc.name, - reference_doctype=doc.reference_doctype, - reference_name=doc.reference_name, - ) - - if frappe.db.exists("CRM Notification", values): - return - frappe.get_doc(values).insert(ignore_permissions=True) + notify_user({ + "owner": doc.owner, + "assigned_to": user, + "notification_type": "WhatsApp", + "message": doc.message, + "notification_text": notification_text, + "doctype": "WhatsApp Message", + "name": doc.name, + "reference_doctype": doc.reference_doctype, + "reference_docname": doc.reference_name, + }) def get_lead_or_deal_from_number(number): @@ -62,10 +58,10 @@ def get_lead_or_deal_from_number(number): mobile_no = parse_mobile_no(mobile_no) query = f""" - SELECT name, mobile_no - FROM `tab{doctype}` - WHERE CONCAT('+', REGEXP_REPLACE(mobile_no, '[^0-9]', '')) = {mobile_no} - """ + 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 diff --git a/crm/fcrm/doctype/crm_notification/crm_notification.py b/crm/fcrm/doctype/crm_notification/crm_notification.py index 16079b37..0cf00551 100644 --- a/crm/fcrm/doctype/crm_notification/crm_notification.py +++ b/crm/fcrm/doctype/crm_notification/crm_notification.py @@ -33,4 +33,4 @@ def notify_user(args): if frappe.db.exists("CRM Notification", values): return - frappe.get_doc(values).insert() \ No newline at end of file + frappe.get_doc(values).insert(ignore_permissions=True) \ No newline at end of file