~singpolyma/jmp-schemas

8e704d587f92885c87b2e45675f8bdfce0fb3d7a — Stephen Paul Weber 2 years ago 876fd48
Maybe renew a customer when they top up

If they are expired and they top up, ask sgx-jmp to check if they and registered
and bill/renew them.
A deploy/notify_possible_renewal_on_expired_top_up.sql => deploy/notify_possible_renewal_on_expired_top_up.sql +28 -0
@@ 0,0 1,28 @@
-- Deploy jmp:notify_possible_renewal_on_expired_top_up to pg
-- requires: transactions
-- requires: customer_plans

BEGIN;

CREATE OR REPLACE FUNCTION check_and_notify_possible_renewal_on_expired_top_up_trigger() RETURNS TRIGGER AS $$
	DECLARE
		expired BOOLEAN;
	BEGIN
		SELECT expires_at < LOCALTIMESTAMP INTO expired
		FROM customer_plans WHERE customer_id=NEW.customer_id;

		IF expired THEN
			PERFORM pg_notify('possible_renewal', NEW.customer_id);
		END IF;

		RETURN NEW;
	END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER notify_possible_renewal_on_expired_top_up
	AFTER INSERT ON transactions
	FOR EACH ROW
	WHEN (NEW.amount > 0)
	EXECUTE PROCEDURE check_and_notify_possible_renewal_on_expired_top_up_trigger();

COMMIT;

A revert/notify_possible_renewal_on_expired_top_up.sql => revert/notify_possible_renewal_on_expired_top_up.sql +8 -0
@@ 0,0 1,8 @@
-- Revert jmp:notify_possible_renewal_on_expired_top_up from pg

BEGIN;

DROP TRIGGER notify_possible_renewal_on_expired_top_up ON transactions;
DROP FUNCTION IF EXISTS check_and_notify_possible_renewal_on_expired_top_up_trigger;

COMMIT;

M sqitch.plan => sqitch.plan +2 -0
@@ 38,3 38,5 @@ settled_after [transactions] 2022-04-11T17:34:18Z Stephen Paul Weber,,, <singpol

require_settled_after [settled_after insert_charge_for_cdr] 2022-04-12T18:45:50Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # Make sure settled_after is NOT NULL
@2022103 2022-04-13T15:05:58Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # Tag settled_after NOT NULL

notify_possible_renewal_on_expired_top_up [transactions customer_plans] 2022-04-19T17:53:50Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # When an expired customer tops up, notify to try renewal

A verify/notify_possible_renewal_on_expired_top_up.sql => verify/notify_possible_renewal_on_expired_top_up.sql +7 -0
@@ 0,0 1,7 @@
-- Verify jmp:notify_possible_renewal_on_expired_top_up on pg

BEGIN;

-- Cannot LISTEN from inside DB

ROLLBACK;