Repository: gratipay/gratipay.com

Issue:
upgrade payday email notification to use subscriptions instead of tips
The email notification sent to supporters during payday lists the top one or two people you give to, but it's still using the old tips table instead of the new subscriptions table.

https://gratipay.freshdesk.com/helpdesk/tickets/2233



Discussion:

Resolving patch:
diff --git a/gratipay/billing/payday.py b/gratipay/billing/payday.py
--- a/gratipay/billing/payday.py
+++ b/gratipay/billing/payday.py
@@ -542,32 +542,39 @@ def notify_participants(self):
             if p.notify_charge & i == 0:
                 continue
             username = p.username
-            ntippees, top_tippee = self.db.one("""
+            nteams, top_team = self.db.one("""
                 WITH tippees AS (
-                         SELECT p.username, amount
-                           FROM ( SELECT DISTINCT ON (tippee) tippee, amount
-                                    FROM tips
+                         SELECT t.slug, amount
+                           FROM ( SELECT DISTINCT ON (team) team, amount
+                                    FROM subscriptions
                                    WHERE mtime < %(ts_start)s
-                                     AND tipper = %(username)s
-                                ORDER BY tippee, mtime DESC
-                                ) t
-                           JOIN participants p ON p.username = t.tippee
-                          WHERE t.amount > 0
-                            AND p.is_suspicious IS NOT true
-                            AND p.claimed_time < %(ts_start)s
+                                     AND subscriber = %(username)s
+                                ORDER BY team, mtime DESC
+                                ) s
+                           JOIN teams t ON s.team = t.slug
+                           JOIN participants p ON t.owner = p.username
+                          WHERE s.amount > 0
+                            AND t.is_approved IS true
+                            AND t.is_closed IS NOT true
+                            AND (SELECT count(*)
+                                   FROM current_exchange_routes er
+                                  WHERE er.participant = p.id
+                                    AND network IN ('balanced-ba', 'paypal')
+                                    AND error = ''
+                                ) > 0
                      )
-                SELECT ( SELECT count(*) FROM tippees ) AS ntippees
-                     , ( SELECT username
+                SELECT ( SELECT count(*) FROM tippees ) AS nteams
+                     , ( SELECT slug
                            FROM tippees
                        ORDER BY amount DESC
                           LIMIT 1
-                       ) AS top_tippee
+                       ) AS top_team
             """, locals())
             p.queue_email(
                 'charge_'+e.status,
                 exchange=dict(id=e.id, amount=e.amount, fee=e.fee, note=e.note),
-                ntippees=ntippees,
-                top_tippee=top_tippee,
+                nteams=nteams,
+                top_team=top_team,
             )
 
 


Test patch:
diff --git a/tests/py/test_billing_payday.py b/tests/py/test_billing_payday.py
--- a/tests/py/test_billing_payday.py
+++ b/tests/py/test_billing_payday.py
@@ -570,8 +570,8 @@ class TestNotifyParticipants(EmailHarness):
     def test_it_notifies_participants(self):
         kalel = self.make_participant('kalel', claimed_time='now', is_suspicious=False,
                                       email_address='kalel@example.net', notify_charge=3)
-        lily = self.make_participant('lily', claimed_time='now', is_suspicious=False)
-        kalel.set_tip_to(lily, 10)
+        team = self.make_team('Gratiteam', is_approved=True)
+        kalel.set_subscription_to(team, 10)
 
         for status in ('failed', 'succeeded'):
             payday = Payday.start()
@@ -584,3 +584,4 @@ def test_it_notifies_participants(self):
 
             Participant.dequeue_emails()
             assert self.get_last_email()['to'][0]['email'] == 'kalel@example.net'
+            assert 'Gratiteam' in self.get_last_email()['text']