fix: use notify_user method in comment & whatsapp

This commit is contained in:
Shariq Ansari 2024-09-23 19:26:34 +05:30
parent f09364ee94
commit f3b57c6240
3 changed files with 35 additions and 43 deletions

View File

@ -3,6 +3,7 @@ from collections.abc import Iterable
import frappe import frappe
from frappe import _ from frappe import _
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from crm.fcrm.doctype.crm_notification.crm_notification import notify_user
def on_update(self, method): def on_update(self, method):
notify_mentions(self) notify_mentions(self)
@ -29,22 +30,17 @@ def notify_mentions(doc):
<span class="font-medium text-gray-900">{ doc.reference_name }</span> <span class="font-medium text-gray-900">{ doc.reference_name }</span>
</div> </div>
""" """
values = frappe._dict( notify_user({
doctype="CRM Notification", "owner": doc.owner,
from_user=doc.owner, "assigned_to": mention.email,
to_user=mention.email, "notification_type": "Mention",
type="Mention", "message": doc.content,
message=doc.content, "notification_text": notification_text,
notification_text=notification_text, "doctype": "Comment",
notification_type_doctype="Comment", "name": doc.name,
notification_type_doc=doc.name, "reference_doctype": doc.reference_doctype,
reference_doctype=doc.reference_doctype, "reference_docname": doc.reference_name,
reference_name=doc.reference_name, })
)
if frappe.db.exists("CRM Notification", values):
return
frappe.get_doc(values).insert()
def extract_mentions(html): def extract_mentions(html):

View File

@ -2,6 +2,7 @@ import frappe
import json import json
from frappe import _ from frappe import _
from crm.api.doc import get_assigned_users from crm.api.doc import get_assigned_users
from crm.fcrm.doctype.crm_notification.crm_notification import notify_user
def validate(doc, method): def validate(doc, method):
@ -29,30 +30,25 @@ def notify_agent(doc):
if doctype.startswith("CRM "): if doctype.startswith("CRM "):
doctype = doctype[4:].lower() doctype = doctype[4:].lower()
notification_text = f""" notification_text = f"""
<div class="mb-2 leading-5 text-gray-600"> <div class="mb-2 leading-5 text-gray-600">
<span class="font-medium text-gray-900">{ _('You') }</span> <span class="font-medium text-gray-900">{ _('You') }</span>
<span>{ _('received a whatsapp message in {0}').format(doctype) }</span> <span>{ _('received a whatsapp message in {0}').format(doctype) }</span>
<span class="font-medium text-gray-900">{ doc.reference_name }</span> <span class="font-medium text-gray-900">{ doc.reference_name }</span>
</div> </div>
""" """
assigned_users = get_assigned_users(doc.reference_doctype, doc.reference_name) assigned_users = get_assigned_users(doc.reference_doctype, doc.reference_name)
for user in assigned_users: for user in assigned_users:
values = frappe._dict( notify_user({
doctype="CRM Notification", "owner": doc.owner,
from_user=doc.owner, "assigned_to": user,
to_user=user, "notification_type": "WhatsApp",
type="WhatsApp", "message": doc.message,
message=doc.message, "notification_text": notification_text,
notification_text=notification_text, "doctype": "WhatsApp Message",
notification_type_doctype="WhatsApp Message", "name": doc.name,
notification_type_doc=doc.name, "reference_doctype": doc.reference_doctype,
reference_doctype=doc.reference_doctype, "reference_docname": doc.reference_name,
reference_name=doc.reference_name, })
)
if frappe.db.exists("CRM Notification", values):
return
frappe.get_doc(values).insert(ignore_permissions=True)
def get_lead_or_deal_from_number(number): 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) mobile_no = parse_mobile_no(mobile_no)
query = f""" query = f"""
SELECT name, mobile_no SELECT name, mobile_no
FROM `tab{doctype}` FROM `tab{doctype}`
WHERE CONCAT('+', REGEXP_REPLACE(mobile_no, '[^0-9]', '')) = {mobile_no} WHERE CONCAT('+', REGEXP_REPLACE(mobile_no, '[^0-9]', '')) = {mobile_no}
""" """
data = frappe.db.sql(query + where, as_dict=True) data = frappe.db.sql(query + where, as_dict=True)
return data[0].name if data else None return data[0].name if data else None

View File

@ -33,4 +33,4 @@ def notify_user(args):
if frappe.db.exists("CRM Notification", values): if frappe.db.exists("CRM Notification", values):
return return
frappe.get_doc(values).insert() frappe.get_doc(values).insert(ignore_permissions=True)