Sidhra_Audit_System/sidrah_business/patches/create_resolution_notifications.py
2026-10-09 12:16:48 +05:30

66 lines
2.3 KiB
Python

import frappe
# Declarative Frappe Notification (Email Alert) records — same shape/sender account as
# other apps on this bench (see e.g. "Training Feedback"). These cover the one thing our
# own code-driven notifications (sidrah_business.api._send_notification, called throughout
# the workflow functions) don't already: telling the original *creator* their submission
# reached a resolved/closed state. They fire natively off a normal `doc.save()`, which is
# safe here — unlike workflow_state (advanced via frappe.db.set_value to dodge Frappe's
# Workflow permission check), the `status` field on these three doctypes is always set via
# a plain `doc.status = ...` + `doc.save()`, so Frappe's own "Value Change" event dispatch
# fires reliably without any extra plumbing.
_SENDER_ACCOUNT = 'Asset Notifications'
_NOTIFICATIONS = [
{
'name': 'SB Incident Resolved',
'document_type': 'SB Incident',
'subject': 'Your reported event {{ doc.name }} has been resolved and closed',
'condition': 'doc.status == "Executed & Closed"',
},
{
'name': 'SB Idea Resolved',
'document_type': 'SB Idea',
'subject': 'Your submitted idea/study {{ doc.name }} has been resolved and closed',
'condition': 'doc.status == "Executed & Closed"',
},
{
'name': 'SB Order Closed',
'document_type': 'SB Order',
'subject': 'Outsourcing order {{ doc.name }} has been closed',
'condition': 'doc.status == "Closed"',
},
]
def execute():
if not frappe.db.exists('Email Account', _SENDER_ACCOUNT):
# No shared outgoing account configured on this bench — skip rather than create
# alerts that can never send.
return
for spec in _NOTIFICATIONS:
if frappe.db.exists('Notification', spec['name']):
continue
doc = frappe.get_doc({
'doctype': 'Notification',
'name': spec['name'],
'subject': spec['subject'],
'document_type': spec['document_type'],
'event': 'Value Change',
'value_changed': 'status',
'condition': spec['condition'],
'channel': 'Email',
'sender': _SENDER_ACCOUNT,
'send_system_notification': 1,
'message_type': 'Markdown',
'message': '<p>{{ doc.title or doc.name }} — status: {{ doc.status }}.</p>',
'attach_print': 0,
'recipients': [
{'receiver_by_document_field': 'owner'},
],
})
doc.insert(ignore_permissions=True)
frappe.db.commit()