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