fix: include unlinked and dedupe multi-link contacts in SMS Center

This commit is contained in:
Shllokkk
2026-10-20 20:21:27 +05:30
parent 85029161c8
commit c10e095a16
2 changed files with 52 additions and 2 deletions
@@ -76,8 +76,6 @@ class SMSCenter(Document):
DynamicLink = frappe.qb.DocType("Dynamic Link")
query = (
frappe.qb.from_(Contact)
.join(DynamicLink)
.on(DynamicLink.parent == Contact.name)
.select(
fn.Concat(fn.IfNull(Contact.first_name, ""), " ", fn.IfNull(Contact.last_name, "")),
Contact.mobile_no,
@@ -85,6 +83,12 @@ class SMSCenter(Document):
.where((fn.IfNull(Contact.mobile_no, "") != "") & (Contact.docstatus != 2))
)
# "All Contact" needs no link; the party options join Dynamic Link and dedupe multi-link contacts
if self.send_to == "All Contact":
return query
query = query.join(DynamicLink).on(DynamicLink.parent == Contact.name).distinct()
if self.send_to == "All Customer Contact":
query = query.where(DynamicLink.link_doctype == "Customer")
query = (
@@ -24,3 +24,49 @@ class TestSMSCenter(FrappeTestCase):
sms_center.receiver_list = "Anne-Marie Roy - 9800000004\n+91-98000-00005\n9800000006"
self.assertEqual(sms_center.get_receiver_nos(), ["9800000004", "+91-98000-00005", "9800000006"])
def test_all_contact_includes_unlinked_and_dedupes_multi_link(self):
customer = frappe.get_doc(
{
"doctype": "Customer",
"customer_name": "SMS Customer",
"customer_type": "Company",
"customer_group": frappe.db.get_value("Customer Group", {"is_group": 0}),
"territory": frappe.db.get_value("Territory", {"is_group": 0}),
}
).insert()
supplier = frappe.get_doc(
{
"doctype": "Supplier",
"supplier_name": "SMS Supplier",
"supplier_type": "Company",
"supplier_group": frappe.db.get_value("Supplier Group", {"is_group": 0}),
}
).insert()
unlinked = frappe.get_doc(
{
"doctype": "Contact",
"first_name": "SMS Unlinked",
"phone_nos": [{"phone": "9800000010", "is_primary_mobile_no": 1}],
}
).insert()
multi = frappe.get_doc(
{
"doctype": "Contact",
"first_name": "SMS Multi",
"phone_nos": [{"phone": "9800000011", "is_primary_mobile_no": 1}],
"links": [
{"link_doctype": "Customer", "link_name": customer.name},
{"link_doctype": "Supplier", "link_name": supplier.name},
],
}
).insert()
sms_center = frappe.new_doc("SMS Center")
sms_center.send_to = "All Contact"
numbers = [row[1] for row in sms_center.get_contact_query_for_all_contacts().run(as_list=1)]
self.assertIn(unlinked.mobile_no, numbers)
self.assertEqual(numbers.count(multi.mobile_no), 1)