fix(scanner): camera scans used stale first-render handler (welcome mode ignored); feat(admin): un-check-in removes check-in points

- Scanner: invoke onResult through a ref refreshed each render; the camera
  callback captured the mount-time closure, so scans kept running session
  check-in even with the Welcome toggle on (manual entry was unaffected)
- new RPC set_registration_checked_in(id, bool): admin-only; on un-check,
  deletes the event's 'checkin' points_log rows and applies the negative
  delta via _award_points('admin') so leaderboard + audit stay consistent;
  re-check awards nothing; returns points_removed
- admin Registrations toggle now calls the RPC, refreshes rows and toasts
  the removed points
- supabase/tests/set_registration_checked_in_tests.sql: 15 assertions
This commit is contained in:
Lago committed 2026-10-19 10:35:10 +02:00
1 parent 9a6e2e5c2a
commit 316f1316e6
6 files changed
+307 -6

No files matched your search

@@ -0,0 +1,156 @@
-- 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 ('[email protected]', 'SCR Admin', true),
('[email protected]', 'SCR User', false),
('[email protected]', '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', '[email protected]', 'attendee', 'SCRQR-1', true, 40),
('c7e10000-0000-0000-0000-000000000012', 'c7e10000-0000-0000-0000-000000000001', '[email protected]', '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', '[email protected]', 10, 'checkin'),
('c7e10000-0000-0000-0000-000000000001', '[email protected]', 10, 'checkin'),
('c7e10000-0000-0000-0000-000000000001', '[email protected]', 10, 'checkin'),
('c7e10000-0000-0000-0000-000000000001', '[email protected]', 10, 'connection'),
('c7e10000-0000-0000-0000-000000000001', '[email protected]', 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', '[email protected]', 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 = '[email protected]'),
'leaderboard starts at 40');
-- -----------------------------------------------------------------------------
-- 1. Admin un-checks: check-in points removed, other categories untouched.
-- -----------------------------------------------------------------------------
select set_config('request.jwt.claims', '{"email":"[email protected]","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 = '[email protected]' 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 = '[email protected]' 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 = '[email protected]' 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 = '[email protected]'),
'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":"[email protected]","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;