Do not catchup low-balance notifications for expired customers

Stephen Paul Weber created

Makes the number of users to do on startup much smaller and slower-growing.
Expired users have been told about their low balance quite a bit already, and
will be notified by billing cronjob etc from here out.

Change summary

sgx_jmp.rb | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)

Detailed changes

sgx_jmp.rb 🔗

@@ -183,10 +183,17 @@ end
 EM.error_handler(&method(:panic))
 
 # Infer anything we might have been notified about while we were down
-def catchup_notify(db)
-	db.query("SELECT customer_id FROM balances WHERE balance < 5").each do |c|
+def catchup_notify_low_balance(db)
+	db.query(<<~SQL).each do |c|
+		SELECT customer_id
+		FROM balances INNER JOIN customer_plans USING (customer_id)
+		WHERE balance < 5 AND expires_at > LOCALTIMESTAMP
+	SQL
 		db.query("SELECT pg_notify('low_balance', $1)", c.values)
 	end
+end
+
+def catchup_notify_possible_renewal
 	db.query(<<~SQL).each do |c|
 		SELECT customer_id
 		FROM customer_plans INNER JOIN balances USING (customer_id)
@@ -227,7 +234,8 @@ when_ready do
 	DB.hold do |conn|
 		conn.query("LISTEN low_balance")
 		conn.query("LISTEN possible_renewal")
-		catchup_notify(conn)
+		catchup_notify_low_balance(conn)
+		catchup_notify_possible_renewal(conn)
 		poll_for_notify(conn)
 	end