-- set_registration_checked_in: admin flips checked_in; un-checking removes the -- attendee's check-in points (and only those). Run inside a transaction that -- is rolled back, like the other suites. \set ON_ERROR_STOP on begin; create temporary table scr_results (name text); grant insert, select on scr_results to authenticated, anon; create function pg_temp.assert_scr(p_condition boolean, p_name text) returns void language plpgsql as $$ begin if p_condition is distinct from true then raise exception 'FAIL: %', p_name; end if; insert into scr_results values (p_name); end; $$; -- ----------------------------------------------------------------------------- -- Fixtures: one active event, an admin, a plain attendee with a mix of points. -- ----------------------------------------------------------------------------- update public.events set is_active = false where is_active; insert into public.events (id, name, start_date, end_date, is_active) values ('c7e10000-0000-0000-0000-000000000001', 'SCR regression', current_date, current_date, true); insert into public.contacts (email, full_name, is_admin) values ('scr-admin@example.test', 'SCR Admin', true), ('scr-user@example.test', 'SCR User', false), ('scr-other@example.test', 'SCR Other', false); insert into public.registrations (id, event_id, contact_email, type, qr_token, checked_in, points) values ('c7e10000-0000-0000-0000-000000000011', 'c7e10000-0000-0000-0000-000000000001', 'scr-user@example.test', 'attendee', 'SCRQR-1', true, 40), ('c7e10000-0000-0000-0000-000000000012', 'c7e10000-0000-0000-0000-000000000001', 'scr-other@example.test', 'attendee', 'SCRQR-2', true, 25); -- 40 pts = 3x checkin (10+10+10) + connection 10 + feedback 10. insert into public.points_log (event_id, contact_email, delta, category) values ('c7e10000-0000-0000-0000-000000000001', 'scr-user@example.test', 10, 'checkin'), ('c7e10000-0000-0000-0000-000000000001', 'scr-user@example.test', 10, 'checkin'), ('c7e10000-0000-0000-0000-000000000001', 'scr-user@example.test', 10, 'checkin'), ('c7e10000-0000-0000-0000-000000000001', 'scr-user@example.test', 10, 'connection'), ('c7e10000-0000-0000-0000-000000000001', 'scr-user@example.test', 10, 'feedback'); -- Other attendee: only connection points, no checkin rows. insert into public.points_log (event_id, contact_email, delta, category) values ('c7e10000-0000-0000-0000-000000000001', 'scr-other@example.test', 25, 'connection'); -- Leaderboard mirrors the starting points (trigger keeps it in sync). select pg_temp.assert_scr( (select points = 40 from public.leaderboard where event_id = 'c7e10000-0000-0000-0000-000000000001' and contact_email = 'scr-user@example.test'), 'leaderboard starts at 40'); -- ----------------------------------------------------------------------------- -- 1. Admin un-checks: check-in points removed, other categories untouched. -- ----------------------------------------------------------------------------- select set_config('request.jwt.claims', '{"email":"scr-admin@example.test","role":"authenticated"}', true); create temporary table scr_off as select * from public.set_registration_checked_in( 'c7e10000-0000-0000-0000-000000000011', false); select pg_temp.assert_scr( (select checked_in = false and points_removed = 30 from scr_off), 'un-check-in reports 30 points removed'); select pg_temp.assert_scr( (select checked_in = false from public.registrations where qr_token = 'SCRQR-1'), 'registration is unchecked'); select pg_temp.assert_scr( (select points = 10 from public.registrations where qr_token = 'SCRQR-1'), 'registration keeps non-checkin points'); select pg_temp.assert_scr( (select count(*) = 0 from public.points_log where contact_email = 'scr-user@example.test' and category = 'checkin'), 'checkin rows deleted from the audit log'); select pg_temp.assert_scr( (select count(*) = 2 from public.points_log where contact_email = 'scr-user@example.test' and category in ('connection', 'feedback')), 'other categories keep their audit rows'); select pg_temp.assert_scr( (select count(*) = 1 and coalesce(min(delta), 0) = -30 from public.points_log where contact_email = 'scr-user@example.test' and category = 'admin'), 'reversal recorded as a single -30 admin row'); select pg_temp.assert_scr( (select points = 10 from public.leaderboard where event_id = 'c7e10000-0000-0000-0000-000000000001' and contact_email = 'scr-user@example.test'), 'leaderboard synced down to 10'); -- ----------------------------------------------------------------------------- -- 2. Un-checking someone with no checkin points: no-op, zero removed. -- ----------------------------------------------------------------------------- create temporary table scr_off2 as select * from public.set_registration_checked_in( 'c7e10000-0000-0000-0000-000000000012', false); select pg_temp.assert_scr( (select checked_in = false and points_removed = 0 from scr_off2), 'un-check-in without checkin points removes nothing'); select pg_temp.assert_scr( (select points = 25 from public.registrations where qr_token = 'SCRQR-2'), 'other attendee keeps their points'); -- ----------------------------------------------------------------------------- -- 3. Re-checking does not re-award anything. -- ----------------------------------------------------------------------------- create temporary table scr_on as select * from public.set_registration_checked_in( 'c7e10000-0000-0000-0000-000000000011', true); select pg_temp.assert_scr( (select checked_in = true and points_removed = 0 from scr_on), 're-check-in awards no points'); select pg_temp.assert_scr( (select points = 10 from public.registrations where qr_token = 'SCRQR-1'), 'points unchanged after re-check-in'); -- ----------------------------------------------------------------------------- -- 4. Unknown registration id is rejected. -- ----------------------------------------------------------------------------- do $$ begin begin perform * from public.set_registration_checked_in('c7e10000-0000-0000-0000-00000000dead', true); raise exception 'missing registration allowed'; exception when raise_exception then if sqlerrm <> 'registration not found' then raise; end if; end; perform pg_temp.assert_scr(true, 'unknown registration rejected'); end; $$; -- ----------------------------------------------------------------------------- -- 5. Non-admin cannot call it (plain RAISE EXCEPTION → raise_exception). -- ----------------------------------------------------------------------------- select set_config('request.jwt.claims', '{"email":"scr-user@example.test","role":"authenticated"}', true); do $$ begin begin perform * from public.set_registration_checked_in( 'c7e10000-0000-0000-0000-000000000011', false); raise exception 'non-admin un-check-in allowed'; exception when raise_exception then if sqlerrm <> 'admin only' then raise; end if; end; perform pg_temp.assert_scr(true, 'un-check-in is admin only'); end; $$; -- ----------------------------------------------------------------------------- -- 6. The anonymous role cannot execute it at all. -- ----------------------------------------------------------------------------- select set_config('request.jwt.claims', '{"role":"anon"}', true); set local role anon; do $$ begin begin perform * from public.set_registration_checked_in( 'c7e10000-0000-0000-0000-000000000011', false); raise exception 'anon un-check-in allowed'; exception when insufficient_privilege then null; end; perform pg_temp.assert_scr(true, 'anon cannot flip check-in'); end; $$; reset role; select count(*) as passed_assertions from scr_results; rollback;