From d05691f86fd399a31aedd458e1b4695f95799f8b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C4=93teris=20Caune?= <cuu508@gmail.com>
Date: Mon, 3 Aug 2020 18:00:48 +0300
Subject: [PATCH] SMS and phone calls now have separate "limit reached" email
 templates.

---
 hc/accounts/models.py                         |  7 ++++++
 hc/api/transports.py                          |  2 +-
 hc/lib/emails.py                              |  4 ++++
 .../emails/phone-call-limit-body-html.html    | 22 +++++++++++++++++++
 .../emails/phone-call-limit-body-text.html    |  9 ++++++++
 .../emails/phone-call-limit-subject.html      |  1 +
 templates/emails/sms-limit-subject.html       |  2 +-
 7 files changed, 45 insertions(+), 2 deletions(-)
 create mode 100644 templates/emails/phone-call-limit-body-html.html
 create mode 100644 templates/emails/phone-call-limit-body-text.html
 create mode 100644 templates/emails/phone-call-limit-subject.html

diff --git a/hc/accounts/models.py b/hc/accounts/models.py
index 397e46f7..5a4af9c2 100644
--- a/hc/accounts/models.py
+++ b/hc/accounts/models.py
@@ -135,6 +135,13 @@ class Profile(models.Model):
 
         emails.sms_limit(self.user.email, ctx)
 
+    def send_call_limit_notice(self):
+        ctx = {"limit": self.call_limit}
+        if self.call_limit != 500 and settings.USE_PAYMENTS:
+            ctx["url"] = settings.SITE_ROOT + reverse("hc-pricing")
+
+        emails.call_limit(self.user.email, ctx)
+
     def projects(self):
         """ Return a queryset of all projects we have access to. """
 
diff --git a/hc/api/transports.py b/hc/api/transports.py
index f9967b36..d6cd3dc8 100644
--- a/hc/api/transports.py
+++ b/hc/api/transports.py
@@ -487,7 +487,7 @@ class Call(HttpTransport):
     def notify(self, check):
         profile = Profile.objects.for_user(self.channel.project.owner)
         if not profile.authorize_call():
-            profile.send_sms_limit_notice("phone call")
+            profile.send_call_limit_notice()
             return "Monthly phone call limit exceeded"
 
         url = self.URL % settings.TWILIO_ACCOUNT
diff --git a/hc/lib/emails.py b/hc/lib/emails.py
index 08f5b43d..a05ffd32 100644
--- a/hc/lib/emails.py
+++ b/hc/lib/emails.py
@@ -75,3 +75,7 @@ def deletion_notice(to, ctx, headers={}):
 
 def sms_limit(to, ctx):
     send("sms-limit", to, ctx)
+
+
+def call_limit(to, ctx):
+    send("phone-call-limit", to, ctx)
diff --git a/templates/emails/phone-call-limit-body-html.html b/templates/emails/phone-call-limit-body-html.html
new file mode 100644
index 00000000..70ee3e0b
--- /dev/null
+++ b/templates/emails/phone-call-limit-body-html.html
@@ -0,0 +1,22 @@
+{% extends "emails/base.html" %}
+{% load hc_extras %}
+
+{% block content %}
+Hello,<br />
+<p>
+We could not deliver a phone call notification because your {% site_name %}
+account has reached its monthly phone call limit of
+<strong>{{ limit }} calls per month</strong>. The limit resets at the start of
+each month.
+</p>
+
+{% if url %}
+<p>You can increase the monthly phone call limit by upgrading your billing plan. <a href="{{ url }}">See Pricing</a>.</p>
+{% endif %}
+
+{% endblock %}
+
+{% block content_more %}
+Regards,<br />
+The {% site_name %} Team
+{% endblock %}
diff --git a/templates/emails/phone-call-limit-body-text.html b/templates/emails/phone-call-limit-body-text.html
new file mode 100644
index 00000000..b3e28cb8
--- /dev/null
+++ b/templates/emails/phone-call-limit-body-text.html
@@ -0,0 +1,9 @@
+{% load hc_extras %}Hello,
+
+We could not deliver a phone call notification because your {% site_name %} account has reached its monthly phone call limit of {{ limit }} calls per month. The limit resets at the start of each month.
+{% if url %}You can increase the monthly phone call limit by upgrading your billing plan.
+{% endif %}
+
+--
+Regards,
+The {% site_name %} Team
diff --git a/templates/emails/phone-call-limit-subject.html b/templates/emails/phone-call-limit-subject.html
new file mode 100644
index 00000000..3a5b091e
--- /dev/null
+++ b/templates/emails/phone-call-limit-subject.html
@@ -0,0 +1 @@
+Monthly Phone Call Limit Reached
\ No newline at end of file
diff --git a/templates/emails/sms-limit-subject.html b/templates/emails/sms-limit-subject.html
index 3a18059b..a5c6e634 100644
--- a/templates/emails/sms-limit-subject.html
+++ b/templates/emails/sms-limit-subject.html
@@ -1 +1 @@
-Monthly {% if transport == "phone call" %}Phone Call{% else %}{{ transport }}{% endif %} Limit Reached
\ No newline at end of file
+Monthly {{ transport }} Limit Reached
\ No newline at end of file