From be1ea4613a86598e7e880b4397671b2ab2f25a63 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Wed, 11 Aug 2021 12:57:17 -0500 Subject: [PATCH] If expired with auto top up, NOTIFY for a week before sending expiry notice --- bin/billing_monthly_cronjob | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/bin/billing_monthly_cronjob b/bin/billing_monthly_cronjob index 7147a4e..fd5b3f3 100755 --- a/bin/billing_monthly_cronjob +++ b/bin/billing_monthly_cronjob @@ -114,18 +114,19 @@ class Plan end class ExpiredCustomer - def self.for(row) + def self.for(row, db) plan = Plan.from_name(row["plan_name"]) if row["balance"] < plan.price - WithLowBalance.new(row, plan) + WithLowBalance.new(row, plan, db) else - new(row, plan) + new(row, plan, db) end end - def initialize(row, plan) + def initialize(row, plan, db) @row = row @plan = plan + @db = db end def customer_id @@ -145,12 +146,18 @@ class ExpiredCustomer class WithLowBalance < ExpiredCustomer ONE_WEEK = 60 * 60 * 24 * 7 + LAST_WEEK = Time.now - ONE_WEEK def try_renew(_, stats) stats.add(:not_renewed, 1) - return if REDIS.exists?("jmp_customer_low_balance-#{customer_id}") - REDIS.set("jmp_customer_low_balance-#{customer_id}", Time.now, ex: ONE_WEEK) - send_notification + if REDIS.exists?("jmp_customer_auto_top_up_amount-#{customer_id}") && \ + @row["expires_at"] > LAST_WEEK + @db.exec_params("SELECT pg_notify('low_balance', $1)", [customer_id]) + else + return if REDIS.exists?("jmp_customer_low_balance-#{customer_id}") + REDIS.set("jmp_customer_low_balance-#{customer_id}", Time.now, ex: ONE_WEEK) + send_notification + end end protected @@ -201,7 +208,7 @@ db.transaction do WHERE expires_at <= NOW() SQL ).each do |row| - ExpiredCustomer.for(row).try_renew(db, stats) + ExpiredCustomer.for(row, db).try_renew(db, stats) end end -- 2.34.2