Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
corporate: Send a reminder email 2 months before the end date.
Prakhar Pratyush2f8e4a71d631
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.pyindex eb6e774877c..9bc3f2ab951 100644--- a/corporate/lib/stripe.py+++ b/corporate/lib/stripe.py@@ -1477,7 +1477,28 @@ class BillingSession(ABC):"property": "next_invoice_date",}plan.next_invoice_date = new_end_date- plan.save(update_fields=["end_date", "next_invoice_date"])+ # Currently, we send a reminder email 2 months before the end date.+ # Reset it when we are extending the end_date.+ reminder_to_review_plan_email_sent_changed_extra_data = None+ if (+ plan.reminder_to_review_plan_email_sent+ and old_end_date is not None # for mypy+ and new_end_date > old_end_date+ ):+ plan.reminder_to_review_plan_email_sent = False+ reminder_to_review_plan_email_sent_changed_extra_data = {+ "old_value": True,+ "new_value": False,+ "plan_id": plan.id,+ "property": "reminder_to_review_plan_email_sent",+ }+ plan.save(+ update_fields=[+ "end_date",+ "next_invoice_date",+ "reminder_to_review_plan_email_sent",+ ]+ )def write_to_audit_log_plan_property_changed(extra_data: Dict[str, Any]) -> None:extra_data["plan_id"] = plan.id@@ -1497,6 +1518,11 @@ class BillingSession(ABC):if next_invoice_date_changed_extra_data:write_to_audit_log_plan_property_changed(next_invoice_date_changed_extra_data)+ if reminder_to_review_plan_email_sent_changed_extra_data:+ write_to_audit_log_plan_property_changed(+ reminder_to_review_plan_email_sent_changed_extra_data+ )+return f"Current plan for {self.billing_entity_display_name} updated to end on {end_date_string}."raise SupportRequestError(f"No current plan for {self.billing_entity_display_name}."@@ -4815,6 +4841,27 @@ def invoice_plans_as_needed(event_time: Optional[datetime] = None) -> None:if remote_server:assert plan.next_invoice_date is not None+ if (+ plan.fixed_price is not None+ and not plan.reminder_to_review_plan_email_sent+ and plan.end_date is not None # for mypy+ # The max gap between two months is 62 days. (1 Jul - 1 Sep)+ and plan.end_date - plan.next_invoice_date <= timedelta(days=62)+ ):+ context = {+ "billing_entity": billing_session.billing_entity_display_name,+ "end_date": plan.end_date.strftime("%Y-%m-%d"),+ "support_url": billing_session.support_url(),+ }+ send_email(+ "zerver/emails/reminder_to_review_plan",+ to_emails=[BILLING_SUPPORT_EMAIL],+ from_address=FromAddress.tokenized_no_reply_address(),+ context=context,+ )+ plan.reminder_to_review_plan_email_sent = True+ plan.save(update_fields=["reminder_to_review_plan_email_sent"])+last_audit_log_update = remote_server.last_audit_log_updateif last_audit_log_update is None or plan.next_invoice_date > last_audit_log_update:if (diff --git a/corporate/migrations/0040_customerplan_reminder_to_review_plan_email_sent.py b/corporate/migrations/0040_customerplan_reminder_to_review_plan_email_sent.pynew file mode 100644index 00000000000..0f59baa4d64--- /dev/null+++ b/corporate/migrations/0040_customerplan_reminder_to_review_plan_email_sent.py@@ -0,0 +1,18 @@+# Generated by Django 4.2.9 on 2024-02-15 06:47++from django.db import migrations, models+++class Migration(migrations.Migration):++ dependencies = [+ ("corporate", "0039_backfill_end_date_for_fixed_price_plans"),+ ]++ operations = [+ migrations.AddField(+ model_name="customerplan",+ name="reminder_to_review_plan_email_sent",+ field=models.BooleanField(default=False),+ ),+ ]diff --git a/corporate/models.py b/corporate/models.pyindex 69b25b96716..262b8691019 100644--- a/corporate/models.py+++ b/corporate/models.py@@ -353,6 +353,11 @@ class CustomerPlan(AbstractCustomerPlan):# and not every time when cron run.invoice_overdue_email_sent = models.BooleanField(default=False)+ # Flag to track if an email has been sent to Zulip team to+ # review the pricing, 60 days before the end date. Helps to send+ # an email only once and not every time when cron run.+ reminder_to_review_plan_email_sent = models.BooleanField(default=False)+# On next_invoice_date, we go through ledger entries that were# created after invoiced_through and process them by generating# invoices for any additional users and/or plan renewal. Once thediff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.pyindex acd6d44d41b..e0650f1b44f 100644--- a/corporate/tests/test_stripe.py+++ b/corporate/tests/test_stripe.py@@ -6834,14 +6834,37 @@ class TestRemoteRealmBillingFlow(StripeTestCase, RemoteRealmBillingTestCase):self.assertEqual(current_plan.billing_schedule, CustomerPlan.BILLING_SCHEDULE_MONTHLY)self.assertEqual(current_plan.end_date, end_date)- # Invoice for february to november- for invoice_count in range(1, 11):+ # Invoice for february to october+ for invoice_count in range(1, 10):with time_machine.travel(add_months(self.now, invoice_count), tick=False):send_server_data_to_push_bouncer(consider_usage_statistics=False)invoice_plans_as_needed()billing_entity = self.billing_session.billing_entity_display_name+ # Cron runs 60 days before the end date (november) & sends a reminder email.+ self.assertFalse(current_plan.reminder_to_review_plan_email_sent)+ with time_machine.travel(add_months(self.now, 10), tick=False):+ send_server_data_to_push_bouncer(consider_usage_statistics=False)+ invoice_plans_as_needed()+ current_plan.refresh_from_db()+ self.assertTrue(current_plan.reminder_to_review_plan_email_sent)++ from django.core.mail import outbox++ messages_count = len(outbox)+ message = outbox[-1]+ self.assert_length(message.to, 1)+ self.assertEqual(message.to[0], "[email protected]")+ self.assertIn(+ f"Support URL: {self.billing_session.support_url()}",+ message.body,+ )+ self.assertEqual(+ f"Fixed-price plan for {billing_entity} ends on {end_date.strftime('%Y-%m-%d')}",+ message.subject,+ )+self.logout()self.login("iago")@@ -6862,6 +6885,9 @@ class TestRemoteRealmBillingFlow(StripeTestCase, RemoteRealmBillingTestCase):send_server_data_to_push_bouncer(consider_usage_statistics=False)invoice_plans_as_needed()+ # Verify that we don't send another email to Zulip team.+ self.assert_length(outbox, messages_count)+# All the monthly invoices are processed, now we can schedule a plan.updated_annual_fixed_price = annual_fixed_price + 500result = self.client_post(diff --git a/corporate/tests/test_support_views.py b/corporate/tests/test_support_views.pyindex 4bf8b34862e..0edb953ca82 100644--- a/corporate/tests/test_support_views.py+++ b/corporate/tests/test_support_views.py@@ -351,6 +351,8 @@ class TestRemoteServerSupportEndpoint(ZulipTestCase):customer = Customer.objects.get(remote_realm=remote_realm)plan = get_current_plan_by_customer(customer)assert plan is not None+ plan.reminder_to_review_plan_email_sent = True+ plan.save(update_fields=["reminder_to_review_plan_email_sent"])self.assertEqual(plan.status, CustomerPlan.ACTIVE)self.assertEqual(plan.end_date, datetime(2050, 2, 1, tzinfo=timezone.utc))self.assertEqual(plan.next_invoice_date, datetime(2050, 2, 1, tzinfo=timezone.utc))@@ -359,7 +361,7 @@ class TestRemoteServerSupportEndpoint(ZulipTestCase):self.login_user(cordelia)result = self.client_post("/activity/remote/support",- {"realm_id": f"{remote_realm.id}", "end_date": "2040-01-01"},+ {"realm_id": f"{remote_realm.id}", "end_date": "2050-03-01"},)self.assertEqual(result.status_code, 302)self.assertEqual(result["Location"], "/login/")@@ -369,29 +371,41 @@ class TestRemoteServerSupportEndpoint(ZulipTestCase):result = self.client_post("/activity/remote/support",- {"remote_realm_id": f"{remote_realm.id}", "plan_end_date": "2040-01-01"},+ {"remote_realm_id": f"{remote_realm.id}", "plan_end_date": "2050-03-01"},)self.assert_in_success_response(- ["Current plan for realm-name-5 updated to end on 2040-01-01."], result+ ["Current plan for realm-name-5 updated to end on 2050-03-01."], result)plan.refresh_from_db()- self.assertEqual(plan.end_date, datetime(2040, 1, 1, tzinfo=timezone.utc))- self.assertEqual(plan.next_invoice_date, datetime(2040, 1, 1, tzinfo=timezone.utc))+ self.assertEqual(plan.end_date, datetime(2050, 3, 1, tzinfo=timezone.utc))+ self.assertEqual(plan.next_invoice_date, datetime(2050, 3, 1, tzinfo=timezone.utc))+ self.assertFalse(plan.reminder_to_review_plan_email_sent)audit_logs = RemoteRealmAuditLog.objects.filter(event_type=RemoteRealmAuditLog.CUSTOMER_PLAN_PROPERTY_CHANGED).order_by("-id")assert audit_logs.exists()- next_invoice_date_changed_audit_log = audit_logs[0]- end_date_changed_audit_log = audit_logs[1]+ reminder_to_review_plan_email_sent_changed_audit_log = audit_logs[0]+ next_invoice_date_changed_audit_log = audit_logs[1]+ end_date_changed_audit_log = audit_logs[2]expected_extra_data = {"old_value": "2050-02-01T00:00:00Z",- "new_value": "2040-01-01T00:00:00Z",+ "new_value": "2050-03-01T00:00:00Z","property": "end_date","plan_id": plan.id,}self.assertEqual(end_date_changed_audit_log.extra_data, expected_extra_data)expected_extra_data["property"] = "next_invoice_date"self.assertEqual(next_invoice_date_changed_audit_log.extra_data, expected_extra_data)+ expected_extra_data.update(+ {+ "old_value": True,+ "new_value": False,+ "property": "reminder_to_review_plan_email_sent",+ }+ )+ self.assertEqual(+ reminder_to_review_plan_email_sent_changed_audit_log.extra_data, expected_extra_data+ )result = self.client_post("/activity/remote/support",diff --git a/templates/zerver/emails/reminder_to_review_plan.html b/templates/zerver/emails/reminder_to_review_plan.htmlnew file mode 100644index 00000000000..67902cc453b--- /dev/null+++ b/templates/zerver/emails/reminder_to_review_plan.html@@ -0,0 +1,10 @@+{% extends "zerver/emails/email_base_default.html" %}++{% block content %}+<p>Reminder to re-evaluate the pricing and configure a new fixed-price plan accordingly.</p>++<br /><br />++<b>Support URL</b>: <a href="{{ support_url }}">{{ support_url }}</a>++{% endblock %}diff --git a/templates/zerver/emails/reminder_to_review_plan.subject.txt b/templates/zerver/emails/reminder_to_review_plan.subject.txtnew file mode 100644index 00000000000..c5ea2f9702f--- /dev/null+++ b/templates/zerver/emails/reminder_to_review_plan.subject.txt@@ -0,0 +1 @@+Fixed-price plan for {{billing_entity}} ends on {{end_date}}diff --git a/templates/zerver/emails/reminder_to_review_plan.txt b/templates/zerver/emails/reminder_to_review_plan.txtnew file mode 100644index 00000000000..2fa99af1321--- /dev/null+++ b/templates/zerver/emails/reminder_to_review_plan.txt@@ -0,0 +1,3 @@+Reminder to re-evaluate the pricing and configure a new fixed-price plan accordingly.++Support URL: {{ support_url }}