~singpolyma/jmp-schemas

5fa89f8c950cc059772447c3ee5fb6ce71bb4403 — root21 1 year, 8 months ago dfb88d5
Added dispositions NO ANSWER and FAILED to cdr_with_charge ignore list
A deploy/cdr_with_charge_ignoring_failed_noanswer.sql => deploy/cdr_with_charge_ignoring_failed_noanswer.sql +26 -0
@@ 0,0 1,26 @@
-- Deploy jmp:cdr_with_charge_ignoring_failed_noanswer to pg
-- requires: cdr_with_charge_rate_when_no_plan

BEGIN;

DROP VIEW cdr_with_charge;

CREATE VIEW cdr_with_charge AS
	SELECT DISTINCT ON (cdr_id, customer_id)
		cdr.*,
		rate,
		(CASE WHEN cdr.disposition IN ('VOICEMAIL', 'NO ANSWER', 'FAILED') THEN 0
		ELSE rate * ceiling(billsec / 60.0) END) AS charge
	FROM
		cdr
		LEFT JOIN customer_plans USING (customer_id)
		LEFT JOIN call_rates ON
			cdr.direction = call_rates.direction AND
			(
				customer_plans.plan_name IS NULL OR
				customer_plans.plan_name = call_rates.plan_name
			) AND
			cdr.tel LIKE call_rates.prefix || '%'
	ORDER BY cdr_id, customer_id, prefix DESC, rate DESC;

COMMIT;

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

BEGIN;

DROP VIEW cdr_with_charge;

CREATE VIEW cdr_with_charge AS
	SELECT DISTINCT ON (cdr_id, customer_id)
		cdr.*,
		rate,
		(CASE WHEN cdr.disposition = 'VOICEMAIL' THEN 0
		ELSE rate * ceiling(billsec / 60.0) END) AS charge
	FROM
		cdr
		LEFT JOIN customer_plans USING (customer_id)
		LEFT JOIN call_rates ON
			cdr.direction = call_rates.direction AND
			(
				customer_plans.plan_name IS NULL OR
				customer_plans.plan_name = call_rates.plan_name
			) AND
			cdr.tel LIKE call_rates.prefix || '%'
	ORDER BY cdr_id, customer_id, prefix DESC, rate DESC;

COMMIT;

M sqitch.plan => sqitch.plan +2 -0
@@ 54,3 54,5 @@ sim_nicks 2023-02-01T19:02:47Z Nico <nico@pop-os> # Add column for SIM nickname.

snikket_instances 2023-03-28T18:47:40Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # Store association between billing account and snikket instance
@2023087 2023-03-28T20:00:44Z Stephen Paul Weber,,, <singpolyma@singpolyma-beefy> # Deploy snikket_instances table

cdr_with_charge_ignoring_failed_noanswer [cdr_with_charge] 2023-03-28T21:35:43Z Nico <nico@pop-os> # Added dispositions NO ANSWER and FAILED to cdr_with_charge ignore list

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

BEGIN;

SELECT rate, charge FROM cdr_with_charge;

INSERT INTO plan_log
    (customer_id, plan_name, date_range)
    VALUES
    ('test', 'test_plan', tsrange(LOCALTIMESTAMP, LOCALTIMESTAMP + '10 seconds'));

INSERT INTO cdr
    (cdr_id, customer_id, start, billsec, disposition, tel)
    VALUES
    ('test1', 'test', LOCALTIMESTAMP, 10000, 'VOICEMAIL', '+15551234567');

INSERT INTO cdr
    (cdr_id, customer_id, start, billsec, disposition, tel)
    VALUES
    ('test2', 'test', LOCALTIMESTAMP, 10000, 'NO ANSWER', '+15551234567');

INSERT INTO cdr
    (cdr_id, customer_id, start, billsec, disposition, tel)
    VALUES
    ('test3', 'test', LOCALTIMESTAMP, 10000, 'FAILED', '+15551234567');

DO $$
DECLARE
    c numeric;
BEGIN
    SELECT charge INTO c FROM cdr_with_charge WHERE cdr_id='test1';
    IF c IS NULL or c <> 0 THEN
        RAISE EXCEPTION 'Expected VOICEMAIL to charge 0, but got: %', c;
    END IF;

    SELECT charge INTO c FROM cdr_with_charge WHERE cdr_id='test2';
    IF c IS NULL or c <> 0 THEN
        RAISE EXCEPTION 'Expected NO ANSWER to charge 0, but got: %', c;
    END IF;

    SELECT charge INTO c FROM cdr_with_charge WHERE cdr_id='test3';
    IF c IS NULL or c <> 0 THEN
        RAISE EXCEPTION 'Expected FAILED to charge 0, but got: %', c;
    END IF;
END $$;

ROLLBACK;