Skip to content

Commit

Permalink
feat: retry mails from Outgoing Mail Log Summary report (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r authored Dec 5, 2024
1 parent b9628e2 commit 01feb9e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def push_to_queue(self) -> None:
commit=True,
)

recipients = [r.email for r in self.recipients if r.status != "Blocked"]
recipients = [r.email for r in self.recipients if r.status not in ["Blocked", "Sent"]]

if not recipients:
frappe.throw(_("All recipients are blocked."))
Expand Down Expand Up @@ -461,7 +461,7 @@ def push_emails_to_queue() -> None:
GroupConcat(MLR.email).as_("recipients"),
)
.where(
(MLR.status != "Blocked")
(MLR.status.notin(["Blocked", "Sent"]))
& (OML.failed_count < MAX_FAILED_COUNT)
& ((OML.retry_after.isnull()) | (OML.retry_after <= Now()))
& (OML.status.isin(["Accepted", "Failed"]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,30 @@ frappe.query_reports["Outgoing Mail Log Summary"] = {
default: 0,
},
],

get_datatable_options(options) {
return Object.assign(options, {
checkboxColumn: true,
});
},

onload(report) {
if (!frappe.user_roles.includes("System Manager")) return;

report.page.add_inner_button(__("Retry"), () => {
let indexes = frappe.query_report.datatable.rowmanager.getCheckedRows();
let selected_rows = indexes.map((i) => frappe.query_report.data[i]);

if (!selected_rows.length) {
frappe.throw(__("No rows selected. Please select at least one row to retry."));
}

frappe.call({
method: "mail_server.mail_server.report.outgoing_mail_log_summary.outgoing_mail_log_summary.retry",
args: {
rows: selected_rows,
},
});
});
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from frappe import _
from frappe.query_builder import Order
from frappe.query_builder.functions import Date, IfNull
from frappe.utils import parse_json


def execute(filters: dict | None = None) -> tuple:
Expand Down Expand Up @@ -311,3 +312,40 @@ def get_summary(data: list) -> list[dict]:
"indicator": "grey",
},
]


@frappe.whitelist()
def retry(rows: str | list) -> None:
"""Retry the specified outgoing mail logs."""

frappe.only_for("System Manager")

if isinstance(rows, str):
rows = parse_json(rows)

retried_mails = []
valid_statuses = ["Blocked", "Bounced"]

for row in rows:
row = frappe._dict(row)

if row.name in retried_mails or row.status not in valid_statuses:
continue

doc = frappe.get_doc("Outgoing Mail Log", row.name)

if doc.status == "Blocked":
doc.force_accept()
elif doc.status == "Bounced":
doc.retry_bounced()

retried_mails.append(row.name)

if retried_mails:
frappe.msgprint(_("Retried {0} outgoing mail(s).").format(len(retried_mails)))
else:
frappe.msgprint(
_(
"No outgoing mails were retried. Please ensure the selected mails are eligible for retry (Blocked or Bounced)."
)
)

0 comments on commit 01feb9e

Please sign in to comment.