Files
Convivo/supabase/migrations/20261019100000_set_registration_checked_in.sql
T
383a80f5ec 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]>
2026-10-19 10:35:10 +02:00

69 lines
2.6 KiB
PL/PgSQL

-- -----------------------------------------------------------------------------
-- 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';