Skip to content

Commit

Permalink
refactor: outgoing_mail.py
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r committed Jan 10, 2025
1 parent 07f78e9 commit ffbd4be
Show file tree
Hide file tree
Showing 13 changed files with 614 additions and 130 deletions.
10 changes: 5 additions & 5 deletions mail/api/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from frappe.translate import get_all_translations
from frappe.utils import is_html

from mail.utils.cache import get_user_default_mailbox
from mail.utils.user import get_user_mailboxes, has_role, is_system_manager
from mail.utils.cache import get_user_default_mail_account
from mail.utils.user import get_user_email_addresses, has_role, is_system_manager


def check_app_permission() -> bool:
Expand Down Expand Up @@ -63,7 +63,7 @@ def get_translations() -> dict:
def get_incoming_mails(start: int = 0) -> list:
"""Returns incoming mails for the current user."""

mailboxes = get_user_mailboxes(frappe.session.user, "Incoming")
mailboxes = get_user_email_addresses(frappe.session.user, "Incoming")

mails = frappe.get_all(
"Incoming Mail",
Expand Down Expand Up @@ -105,7 +105,7 @@ def get_draft_mails(start: int = 0) -> list:
def get_outgoing_mails(status: str, start: int = 0) -> list:
"""Returns outgoing mails for the current user."""

mailboxes = get_user_mailboxes(frappe.session.user, "Outgoing")
mailboxes = get_user_email_addresses(frappe.session.user, "Outgoing")

if status == "Draft":
docstatus = 0
Expand Down Expand Up @@ -388,7 +388,7 @@ def get_mail_contacts(txt=None) -> list:
def get_default_outgoing() -> str | None:
"""Returns default outgoing mailbox."""

return get_user_default_mailbox(frappe.session.user)
return get_user_default_mail_account(frappe.session.user)


@frappe.whitelist()
Expand Down
4 changes: 2 additions & 2 deletions mail/mail/doctype/incoming_mail/incoming_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from mail.utils.cache import get_postmaster_for_domain
from mail.utils.dt import add_or_update_tzinfo, parse_iso_datetime
from mail.utils.email_parser import EmailParser, extract_ip_and_host
from mail.utils.user import get_user_mailboxes, is_mail_account_owner, is_system_manager
from mail.utils.user import get_user_email_addresses, is_mail_account_owner, is_system_manager

if TYPE_CHECKING:
from mail.mail.doctype.outgoing_mail.outgoing_mail import OutgoingMail
Expand Down Expand Up @@ -238,7 +238,7 @@ def get_permission_query_condition(user: str | None = None) -> str:
if is_system_manager(user):
return ""

if mailboxes := ", ".join(repr(m) for m in get_user_mailboxes(user)):
if mailboxes := ", ".join(repr(m) for m in get_user_email_addresses(user)):
return f"(`tabIncoming Mail`.`receiver` IN ({mailboxes})) AND (`tabIncoming Mail`.`docstatus` = 1)"
else:
return "1=0"
Expand Down
9 changes: 8 additions & 1 deletion mail/mail/doctype/mail_recipient/mail_recipient.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"field_order": [
"type",
"email",
"check_deliverability",
"display_name",
"column_break_iqoo",
"status",
Expand Down Expand Up @@ -98,12 +99,18 @@
"label": "Error Message",
"no_copy": 1,
"read_only": 1
},
{
"depends_on": "eval: doc.parenttype == \"Outgoing Mail\"",
"fieldname": "check_deliverability",
"fieldtype": "Button",
"label": "Check Deliverability"
}
],
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2024-12-03 18:07:48.333983",
"modified": "2025-01-09 17:19:51.731199",
"modified_by": "Administrator",
"module": "Mail",
"name": "Mail Recipient",
Expand Down
184 changes: 148 additions & 36 deletions mail/mail/doctype/outgoing_mail/outgoing_mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,74 @@ frappe.ui.form.on("Outgoing Mail", {
},

add_actions(frm) {
if (frm.doc.docstatus === 1) {
if (frm.doc.status === "In Progress") {
frm.add_custom_button(
__("Transfer Now"),
() => {
frm.trigger("transfer_to_mail_agent");
},
__("Actions")
);
} else if (frm.doc.status === "Failed" && frm.doc.failed_count < 5) {
frm.add_custom_button(
__("Retry"),
() => {
frm.trigger("retry_failed");
},
__("Actions")
);
} else if (frm.doc.status === "Sent") {
frm.add_custom_button(
__("Reply"),
() => {
frm.trigger("reply");
},
__("Actions")
);
frm.add_custom_button(
__("Reply All"),
() => {
frm.trigger("reply_all");
},
__("Actions")
);
}
if (frm.doc.docstatus !== 1) return;

if (["In Progress", "Blocked"].includes(frm.doc.status)) {
if (!frappe.user_roles.includes("System Manager")) return;

frm.add_custom_button(
__("Force Accept"),
() => {
frm.trigger("force_accept");
},
__("Actions")
);
} else if (frm.doc.status === "Accepted") {
frm.add_custom_button(
__("Transfer to Agent"),
() => {
frm.trigger("transfer_to_mail_agent");
},
__("Actions")
);
} else if (frm.doc.status === "Failed" && frm.doc.failed_count < 5) {
frm.add_custom_button(
__("Retry"),
() => {
frm.trigger("retry_failed");
},
__("Actions")
);
} else if (frm.doc.status === "Transferring") {
if (!frappe.user_roles.includes("System Manager")) return;

frm.add_custom_button(
__("Force Transfer to Agent"),
() => {
frappe.confirm(
__(
"Are you sure you want to force transfer this email to the agent? It may cause duplicate emails to be sent."
),
() => frm.trigger("force_transfer_to_mail_agent")
);
},
__("Actions")
);
} else if (frm.doc.status === "Sent") {
frm.add_custom_button(
__("Reply"),
() => {
frm.trigger("reply");
},
__("Actions")
);
frm.add_custom_button(
__("Reply All"),
() => {
frm.trigger("reply_all");
},
__("Actions")
);
} else if (frm.doc.status === "Bounced") {
if (!frappe.user_roles.includes("System Manager")) return;

frm.add_custom_button(
__("Retry"),
() => {
frm.trigger("retry_bounced");
},
__("Actions")
);
}
},

Expand All @@ -81,12 +116,12 @@ frappe.ui.form.on("Outgoing Mail", {
}
},

transfer_to_mail_agent(frm) {
force_accept(frm) {
frappe.call({
doc: frm.doc,
method: "transfer_to_mail_agent",
method: "force_accept",
freeze: true,
freeze_message: __("Transferring..."),
freeze_message: __("Force Accepting..."),
callback: (r) => {
if (!r.exc) {
frm.refresh();
Expand All @@ -109,6 +144,34 @@ frappe.ui.form.on("Outgoing Mail", {
});
},

transfer_to_mail_agent(frm) {
frappe.call({
doc: frm.doc,
method: "transfer_to_mail_agent",
freeze: true,
freeze_message: __("Transferring..."),
callback: (r) => {
if (!r.exc) {
frm.refresh();
}
},
});
},

force_transfer_to_mail_agent(frm) {
frappe.call({
doc: frm.doc,
method: "force_transfer_to_mail_agent",
freeze: true,
freeze_message: __("Force Transferring..."),
callback: (r) => {
if (!r.exc) {
frm.refresh();
}
},
});
},

reply(frm) {
frappe.model.open_mapped_doc({
method: "mail.mail.doctype.outgoing_mail.outgoing_mail.reply_to_mail",
Expand All @@ -128,4 +191,53 @@ frappe.ui.form.on("Outgoing Mail", {
},
});
},

retry_bounced(frm) {
frappe.call({
doc: frm.doc,
method: "retry_bounced",
freeze: true,
freeze_message: __("Retrying..."),
callback: (r) => {
if (!r.exc) {
frm.refresh();
}
},
});
},
});

frappe.ui.form.on("Mail Recipient", {
check_deliverability(frm, cdt, cdn) {
let recipient = locals[cdt][cdn];
let email = recipient.email;

if (!email) return;

frappe.call({
method: "mail.utils.check_deliverability",
args: {
email: email,
},
freeze: true,
freeze_message: __("Checking email deliverability..."),
callback: (r) => {
if (!r.exc) {
if (r.message) {
frappe.show_alert({
message: __("The email address {0} is deliverable.", [email.bold()]),
indicator: "green",
});
} else {
frappe.show_alert({
message: __("The email address {0} appears to be invalid.", [
email.bold(),
]),
indicator: "red",
});
}
}
},
});
},
});
Loading

0 comments on commit ffbd4be

Please sign in to comment.