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
Co-authored-by: Stephan Charles <[email protected]>
Co-authored-by: Amelia Hernandez <[email protected]>
Co-authored-by: Markus Franz <[email protected]>
Co-authored-by: Sabrina Hota <[email protected]>
Co-authored-by: Nilüfer Doğan <[email protected]>
This commit is contained in:
1 parent
65dcf64c4e
commit
383a80f5ec
6 files changed
+307
-6
No files matched your search
@@ -0,0 +1,68 @@
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- Admin-controlled check-in state, with points reversal on un-check-in.
|
||||
--
|
||||
-- The admin Registrations view flips `registrations.checked_in` with a plain
|
||||
-- UPDATE. That leaves the attendee's session check-in points (category
|
||||
-- 'checkin' in points_log) on their score even though their check-ins were
|
||||
-- (or are being) undone. This RPC becomes the single admin path:
|
||||
--
|
||||
-- * sets checked_in to the requested value;
|
||||
-- * when turning it OFF, removes every point that person earned from
|
||||
-- check-ins in this event (deletes the 'checkin' points_log rows and
|
||||
-- applies the negative delta through _award_points, so the leaderboard
|
||||
-- mirror and the audit trail stay consistent).
|
||||
--
|
||||
-- Re-checking someone in does NOT re-award points — the same person scanning
|
||||
-- again must not farm points; staff re-grant manually if ever needed.
|
||||
-- -----------------------------------------------------------------------------
|
||||
|
||||
create or replace function public.set_registration_checked_in(
|
||||
p_registration_id uuid,
|
||||
p_checked_in boolean
|
||||
)
|
||||
returns table (registration_id uuid, checked_in boolean, points_removed integer)
|
||||
language plpgsql security definer set search_path = public as $$
|
||||
declare
|
||||
v_event_id uuid;
|
||||
v_contact text;
|
||||
v_removed integer;
|
||||
begin
|
||||
if not public.is_admin() then raise exception 'admin only'; end if;
|
||||
|
||||
select event_id, contact_email into v_event_id, v_contact
|
||||
from public.registrations
|
||||
where id = p_registration_id
|
||||
for update;
|
||||
if v_event_id is null then raise exception 'registration not found'; end if;
|
||||
|
||||
update public.registrations
|
||||
set checked_in = p_checked_in
|
||||
where id = p_registration_id;
|
||||
|
||||
v_removed := 0;
|
||||
if not p_checked_in then
|
||||
select coalesce(sum(delta), 0) into v_removed
|
||||
from public.points_log
|
||||
where event_id = v_event_id
|
||||
and contact_email = v_contact
|
||||
and category = 'checkin';
|
||||
|
||||
if v_removed > 0 then
|
||||
delete from public.points_log
|
||||
where event_id = v_event_id
|
||||
and contact_email = v_contact
|
||||
and category = 'checkin';
|
||||
-- Negative delta through the normal award path: clamps at 0, records the
|
||||
-- reversal in points_log (category 'admin') and syncs the leaderboard.
|
||||
perform public._award_points(v_event_id, v_contact, -v_removed, 'admin');
|
||||
end if;
|
||||
end if;
|
||||
|
||||
return query select p_registration_id, p_checked_in, coalesce(v_removed, 0)::integer;
|
||||
end;
|
||||
$$;
|
||||
|
||||
revoke execute on function public.set_registration_checked_in(uuid, boolean) from public, anon;
|
||||
grant execute on function public.set_registration_checked_in(uuid, boolean) to authenticated;
|
||||
|
||||
notify pgrst, 'reload schema';
|
||||
@@ -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;
|
||||
Reference in new issue
Block a user