From 05480970d0d42dcf267fa60e09a1a7fbcee36171 Mon Sep 17 00:00:00 2001 From: javad Date: Sat, 12 Sep 2026 08:58:25 +0330 Subject: [PATCH] fix sms org name --- ...010_use_actual_unit_in_staff_ticket_sms.py | 38 +++++++++++ tests/test_domain.py | 2 +- tests/test_organizational_ticketing.py | 67 +++++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 apps/notifications/migrations/0010_use_actual_unit_in_staff_ticket_sms.py diff --git a/apps/notifications/migrations/0010_use_actual_unit_in_staff_ticket_sms.py b/apps/notifications/migrations/0010_use_actual_unit_in_staff_ticket_sms.py new file mode 100644 index 0000000..32c8253 --- /dev/null +++ b/apps/notifications/migrations/0010_use_actual_unit_in_staff_ticket_sms.py @@ -0,0 +1,38 @@ +from django.db import migrations + + +OLD_PHRASE = "توسط واحد فروش برای شما ثبت شد" +NEW_PHRASE = "توسط واحد {unit_name} برای شما ثبت شد" + + +def use_actual_unit_name(apps, schema_editor): + sms_template = apps.get_model("notifications", "SMSTemplate") + templates = sms_template.objects.filter( + event_type="STAFF_TICKET_CREATED", + body__contains=OLD_PHRASE, + ) + + for template in templates.iterator(): + template.body = template.body.replace(OLD_PHRASE, NEW_PHRASE) + allowed_tags = list(template.allowed_tags or []) + if "unit_name" not in allowed_tags: + allowed_tags.append("unit_name") + template.allowed_tags = allowed_tags + template.save(update_fields=["body", "allowed_tags"]) + + +def restore_legacy_phrase(apps, schema_editor): + sms_template = apps.get_model("notifications", "SMSTemplate") + templates = sms_template.objects.filter( + event_type="STAFF_TICKET_CREATED", + body__contains=NEW_PHRASE, + ) + for template in templates.iterator(): + template.body = template.body.replace(NEW_PHRASE, OLD_PHRASE) + template.save(update_fields=["body"]) + + +class Migration(migrations.Migration): + dependencies = [("notifications", "0009_ensure_global_sms_templates")] + + operations = [migrations.RunPython(use_actual_unit_name, restore_legacy_phrase)] diff --git a/tests/test_domain.py b/tests/test_domain.py index bb76e72..c375364 100644 --- a/tests/test_domain.py +++ b/tests/test_domain.py @@ -327,7 +327,7 @@ def test_sms_templates_are_database_managed_and_distinguish_ticket_creator(): staff_text=SMSTemplate.objects.get(event_type="STAFF_TICKET_CREATED").body assert customer_text!=staff_text assert "از ثبت درخواست شما متشکریم" in customer_text - assert "توسط واحد فروش برای شما ثبت شد" in staff_text + assert "توسط واحد {unit_name} برای شما ثبت شد" in staff_text assert "{magic_link}" in customer_text and "{first_name}" in customer_text @pytest.mark.django_db(transaction=True) diff --git a/tests/test_organizational_ticketing.py b/tests/test_organizational_ticketing.py index 81e80eb..87f80f5 100644 --- a/tests/test_organizational_ticketing.py +++ b/tests/test_organizational_ticketing.py @@ -70,6 +70,73 @@ def test_group_dispatch_creates_one_isolated_ticket_and_sms_job_per_contact(): assert NotificationJob.objects.filter(ticket__in=tickets, channel="SMS", event_type="STAFF_TICKET_CREATED").count() == 2 +@pytest.mark.django_db +def test_staff_created_ticket_sms_uses_its_actual_organizational_unit(monkeypatch, settings): + from apps.integrations.sms.base import SMSResult + from apps.notifications.management.commands import process_notifications + + settings.PUBLIC_BASE_URL = "https://support.example.com" + finance = OrganizationalUnit.objects.create(name="حسابداری", slug="sms-finance") + staff = make_staff("finance-sms-agent", finance) + customer = make_contact("finance-sms-customer", "09120000501") + category = TicketCategory.objects.create(unit=finance, title="اسناد مالی", slug="sms-financial-documents") + ticket = create_ticket( + customer=customer, + created_by=staff, + unit=finance, + category=category, + subject="ارسال سند مالی", + body="لطفاً سند را بررسی کنید.", + ) + captured = {} + + class Provider: + def send_message(self, mobile, message): + captured.update(mobile=mobile, message=message) + return SMSResult(True, "sent") + + monkeypatch.setattr(process_notifications, "get_sms_provider", lambda: Provider()) + process_notifications._send_sms(NotificationJob.objects.get(ticket=ticket)) + + assert "توسط واحد حسابداری برای شما ثبت شد" in captured["message"] + assert "توسط واحد فروش" not in captured["message"] + + +@pytest.mark.django_db +def test_group_dispatch_sms_uses_dispatch_organizational_unit(monkeypatch, settings): + from apps.integrations.sms.base import SMSResult + from apps.notifications.management.commands import process_notifications + + settings.PUBLIC_BASE_URL = "https://support.example.com" + warehouse = OrganizationalUnit.objects.get(name="بارگیری انبار") + warehouse.permissions.add(Permission.objects.get(codename="dispatch_group_tickets", content_type__app_label="tickets")) + staff = make_staff("warehouse-sms-agent", warehouse) + customer = make_contact("warehouse-sms-customer", "09120000502") + group = ContactGroup.objects.create(name="رانندگان", unit=warehouse) + customer.customer_profile.groups.add(group) + category = TicketCategory.objects.create(unit=warehouse, title="هماهنگی بار", slug="sms-loading") + _, tickets = dispatch_group_tickets( + user=staff, + unit=warehouse, + contact_group=group, + category=category, + subject="هماهنگی بارگیری", + body="زمان بارگیری را اعلام کنید.", + ) + captured = [] + + class Provider: + def send_message(self, mobile, message): + captured.append(message) + return SMSResult(True, "sent") + + monkeypatch.setattr(process_notifications, "get_sms_provider", lambda: Provider()) + process_notifications._send_sms(NotificationJob.objects.get(ticket=tickets[0])) + + assert "توسط واحد بارگیری انبار برای شما ثبت شد" in captured[0] + assert "توسط واحد فروش" not in captured[0] + + @pytest.mark.django_db def test_group_dispatch_copies_shared_document_into_each_private_ticket(): unit=OrganizationalUnit.objects.create(name="مالی گروهی",slug="bulk-finance")