Skip to content

Commit

Permalink
feat: add option to send notification daily based on selected date field
Browse files Browse the repository at this point in the history
  • Loading branch information
shridarpatil committed Aug 14, 2024
1 parent a120747 commit 83ef07f
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,56 @@
// Copyright (c) 2022, Shridhar Patil and contributors
// For license information, please see license.txt
frappe.notification = {
setup_fieldname_select: function (frm) {
// get the doctype to update fields
if (!frm.doc.reference_doctype) {
return;
}

frappe.model.with_doctype(frm.doc.reference_doctype, function () {
let get_select_options = function (df, parent_field) {
// Append parent_field name along with fieldname for child table fields
let select_value = parent_field ? df.fieldname + "," + parent_field : df.fieldname;
let path = parent_field ? parent_field + " > " + df.fieldname : df.fieldname;

return {
value: select_value,
label: path + " (" + __(df.label, null, df.parent) + ")",
};
};

let get_date_change_options = function () {
let date_options = $.map(fields, function (d) {
return d.fieldtype == "Date" || d.fieldtype == "Datetime"
? get_select_options(d)
: null;
});
// append creation and modified date to Date Change field
return date_options.concat([
{ value: "creation", label: `creation (${__("Created On")})` },
{ value: "modified", label: `modified (${__("Last Modified Date")})` },
]);
};

let fields = frappe.get_doc("DocType", frm.doc.reference_doctype).fields;
let options = $.map(fields, function (d) {
return frappe.model.no_value_type.includes(d.fieldtype)
? null
: get_select_options(d);
});

// set date changed options
frm.set_df_property("date_changed", "options", get_date_change_options());

});
}
};


frappe.ui.form.on('WhatsApp Notification', {
refresh: function(frm) {
frm.trigger("load_template")
frappe.notification.setup_fieldname_select(frm);
},
template: function(frm){
frm.trigger("load_template")
Expand Down Expand Up @@ -55,5 +102,8 @@ frappe.ui.form.on('WhatsApp Notification', {
if(['DOCUMENT', "IMAGE"].includes(frm.doc.header_type)){
frm.set_value("custom_attachment", !frm.doc.attach_document_print)
}
}
},
reference_doctype: function (frm) {
frappe.notification.setup_fieldname_select(frm);
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"field_name",
"event_frequency",
"doctype_event",
"days_in_advance",
"date_changed",
"column_break_3",
"disabled",
"template",
Expand Down Expand Up @@ -52,7 +54,7 @@
"fieldname": "doctype_event",
"fieldtype": "Select",
"label": "DocType Event",
"options": "Before Insert\nBefore Validate\nBefore Save\nAfter Insert\nAfter Save\nBefore Submit\nAfter Submit\nBefore Cancel\nAfter Cancel\nBefore Delete\nAfter Delete\nBefore Save (Submitted Document)\nAfter Save (Submitted Document)"
"options": "Before Insert\nBefore Validate\nBefore Save\nAfter Insert\nAfter Save\nBefore Submit\nAfter Submit\nBefore Cancel\nAfter Cancel\nBefore Delete\nAfter Delete\nBefore Save (Submitted Document)\nAfter Save (Submitted Document)\nDays After\nDays Before"
},
{
"fieldname": "column_break_3",
Expand Down Expand Up @@ -164,11 +166,26 @@
"fieldname": "attach_from_field",
"fieldtype": "Data",
"label": "Attach from field "
},
{
"depends_on": "eval:doc.doctype_event==='Days Before' || doc.doctype_event==='Days After'",
"description": "Send days before or after the reference date",
"fieldname": "days_in_advance",
"fieldtype": "Int",
"label": "Days Before or After",
"mandatory_depends_on": "eval:doc.doctype_event==='Days Before' || doc.doctype_event==='Days After'"
},
{
"depends_on": "eval:doc.doctype_event==='Days Before' || doc.doctype_event==='Days After'",
"fieldname": "date_changed",
"fieldtype": "Select",
"label": "Reference Date",
"mandatory_depends_on": "eval:doc.doctype_event==='Days Before' || doc.doctype_event==='Days After'"
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2023-11-23 17:18:54.663077",
"modified": "2024-08-11 20:11:35.462365",
"modified_by": "Administrator",
"module": "Frappe Whatsapp",
"name": "WhatsApp Notification",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from frappe.utils.safe_exec import get_safe_globals, safe_exec
from frappe.integrations.utils import make_post_request
from frappe.desk.form.utils import get_pdf_link
from frappe.utils import add_to_date, nowdate


class WhatsAppNotification(Document):
Expand Down Expand Up @@ -243,3 +244,58 @@ def format_number(self, number):
number = number[1:len(number)]

return number


def get_documents_for_today(self):
"""get list of documents that will be triggered today"""
docs = []

diff_days = self.days_in_advance
if self.doctype_event == "Days After":
diff_days = -diff_days

reference_date = add_to_date(nowdate(), days=diff_days)
reference_date_start = reference_date + " 00:00:00.000000"
reference_date_end = reference_date + " 23:59:59.000000"

doc_list = frappe.get_all(
self.reference_doctype,
fields="name",
filters=[
{self.date_changed: (">=", reference_date_start)},
{self.date_changed: ("<=", reference_date_end)},
],
)

for d in doc_list:
doc = frappe.get_doc(self.reference_doctype, d.name)
self.send_template_message(doc)
# print(doc.name)


def trigger_notifications(method="daily"):
if frappe.flags.in_import or frappe.flags.in_patch:
# don't send notifications while syncing or patching
return

if method == "daily":
doc_list = frappe.get_all(
"WhatsApp Notification", filters={"doctype_event": ("in", ("Days Before", "Days After")), "disabled": 0}
)
for d in doc_list:
alert = frappe.get_doc("WhatsApp Notification", d.name)

alert.get_documents_for_today()
# doc.name
# evaluate_alert(doc, alert, alert.event)
# frappe.db.commit()



def template():
notification = frappe.get_doc("WhatsApp Notification", "WN-0001")
notification.disabled = 0
doc = frappe.get_doc("User", "Administrator")
doc.mobile_no = "+919741094468"
notification.send_template_message(doc)

Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-05-03 13:18:08.326567",
"modified": "2024-08-14 19:39:39.532098",
"modified_by": "Administrator",
"module": "Frappe Whatsapp",
"name": "WhatsApp Templates",
Expand Down
13 changes: 5 additions & 8 deletions frappe_whatsapp/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,20 @@
# Scheduled Tasks
# ---------------

# scheduler_events = {
scheduler_events = {
# "all": [
# "frappe_whatsapp.tasks.all"
# ],
# "daily": [
# "frappe_whatsapp.tasks.daily"
# ],
# "hourly": [
# "frappe_whatsapp.tasks.hourly"
# ],
"daily": [
"frappe_whatsapp.frappe_whatsapp.doctype.whatsapp_notification.whatsapp_notification.trigger_notifications"
],
# "weekly": [
# "frappe_whatsapp.tasks.weekly"
# ],
# "monthly": [
# "frappe_whatsapp.tasks.monthly"
# ],
# }
}

# Testing
# -------
Expand Down

0 comments on commit 83ef07f

Please sign in to comment.