From f09364ee948d4fe49af8525b47fb0be6e2ef438b Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Mon, 23 Sep 2024 19:20:55 +0530 Subject: [PATCH] fix: assign & send notification if task is created --- .../crm_notification/crm_notification.json | 4 +- .../crm_notification/crm_notification.py | 30 +++++++++++- crm/fcrm/doctype/crm_task/crm_task.py | 46 ++++++++++++++++++- 3 files changed, 75 insertions(+), 5 deletions(-) diff --git a/crm/fcrm/doctype/crm_notification/crm_notification.json b/crm/fcrm/doctype/crm_notification/crm_notification.json index be7ac1f69..cc4302b36 100644 --- a/crm/fcrm/doctype/crm_notification/crm_notification.json +++ b/crm/fcrm/doctype/crm_notification/crm_notification.json @@ -34,7 +34,7 @@ "fieldtype": "Select", "in_list_view": 1, "label": "Type", - "options": "Mention\nWhatsApp", + "options": "Mention\nTask\nWhatsApp", "reqd": 1 }, { @@ -116,7 +116,7 @@ ], "index_web_pages_for_search": 1, "links": [], - "modified": "2024-04-25 16:26:07.484857", + "modified": "2024-09-23 17:22:06.973897", "modified_by": "Administrator", "module": "FCRM", "name": "CRM Notification", diff --git a/crm/fcrm/doctype/crm_notification/crm_notification.py b/crm/fcrm/doctype/crm_notification/crm_notification.py index 28f4ab80f..16079b378 100644 --- a/crm/fcrm/doctype/crm_notification/crm_notification.py +++ b/crm/fcrm/doctype/crm_notification/crm_notification.py @@ -2,9 +2,35 @@ # For license information, please see license.txt import frappe +from frappe import _ from frappe.model.document import Document class CRMNotification(Document): - def on_update(self): - frappe.publish_realtime("crm_notification") + def on_update(self): + frappe.publish_realtime("crm_notification") + +def notify_user(args): + """ + Notify the assigned user + """ + args = frappe._dict(args) + if args.owner == args.assigned_to: + return + + values = frappe._dict( + doctype="CRM Notification", + from_user=args.owner, + to_user=args.assigned_to, + type=args.notification_type, + message=args.message, + notification_text=args.notification_text, + notification_type_doctype=args.doctype, + notification_type_doc=args.name, + reference_doctype=args.reference_doctype, + reference_name=args.reference_docname, + ) + + if frappe.db.exists("CRM Notification", values): + return + frappe.get_doc(values).insert() \ No newline at end of file diff --git a/crm/fcrm/doctype/crm_task/crm_task.py b/crm/fcrm/doctype/crm_task/crm_task.py index cf1bc963a..73ed845b2 100644 --- a/crm/fcrm/doctype/crm_task/crm_task.py +++ b/crm/fcrm/doctype/crm_task/crm_task.py @@ -1,11 +1,55 @@ # Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors # For license information, please see license.txt -# import frappe +import frappe +from frappe import _ from frappe.model.document import Document +from frappe.desk.form.assign_to import add as assign +from crm.fcrm.doctype.crm_notification.crm_notification import notify_user class CRMTask(Document): + def after_insert(self): + self.assign_to() + + def assign_to(self): + if self.assigned_to: + assign({ + "assign_to": [self.assigned_to], + "doctype": self.doctype, + "name": self.name, + "description": self.title or self.description, + }) + self.notify_assigned_user() + + def notify_assigned_user(self): + """ + Notify the assigned user about the task assignment + """ + + owner = frappe.get_cached_value("User", self.owner, "full_name") + notification_text = f""" +
+ { owner } + { _('assigned a new task {0} to you').format( + f'{ self.title }' + ) } +
+ """ + + notify_user({ + "owner": self.owner, + "assigned_to": self.assigned_to, + "notification_type": "Task", + "message": self.description, + "notification_text": notification_text, + "doctype": self.doctype, + "name": self.name, + "reference_doctype": self.reference_doctype, + "reference_docname": self.reference_docname, + }) + + @staticmethod def default_list_data(): columns = [