Skip to content

Commit

Permalink
fix: use notify_user method in comment & whatsapp
Browse files Browse the repository at this point in the history
  • Loading branch information
shariquerik committed Sep 23, 2024
1 parent f09364e commit f3b57c6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 43 deletions.
28 changes: 12 additions & 16 deletions crm/api/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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)
Expand All @@ -29,22 +30,17 @@ def notify_mentions(doc):
<span class="font-medium text-gray-900">{ doc.reference_name }</span>
</div>
"""
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):
Expand Down
48 changes: 22 additions & 26 deletions crm/api/whatsapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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):
Expand Down Expand Up @@ -29,30 +30,25 @@ def notify_agent(doc):
if doctype.startswith("CRM "):
doctype = doctype[4:].lower()
notification_text = f"""
<div class="mb-2 leading-5 text-gray-600">
<span class="font-medium text-gray-900">{ _('You') }</span>
<span>{ _('received a whatsapp message in {0}').format(doctype) }</span>
<span class="font-medium text-gray-900">{ doc.reference_name }</span>
</div>
"""
<div class="mb-2 leading-5 text-gray-600">
<span class="font-medium text-gray-900">{ _('You') }</span>
<span>{ _('received a whatsapp message in {0}').format(doctype) }</span>
<span class="font-medium text-gray-900">{ doc.reference_name }</span>
</div>
"""
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):
Expand All @@ -62,10 +58,10 @@ def find_record(doctype, mobile_no, where=""):
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
Expand Down
2 changes: 1 addition & 1 deletion crm/fcrm/doctype/crm_notification/crm_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ def notify_user(args):

if frappe.db.exists("CRM Notification", values):
return
frappe.get_doc(values).insert()
frappe.get_doc(values).insert(ignore_permissions=True)

0 comments on commit f3b57c6

Please sign in to comment.