-- ----------------------------------------------------------------------------- -- Points correctness pass. -- -- Three defects, all in the check-in points path: -- -- 1. The front-desk (welcome) check-in awarded NO points, while a session scan -- awarded +10. "Check me in" therefore paid out inconsistently depending on -- WHICH check-in a person got. The welcome check-in is a real check-in, so it -- now awards +10 (category `checkin`) on a genuine first check-in. -- checkin_attendee keeps awarding +10 per session scan; the front-desk award -- is once-per-person because `checked_in` is already guarded that way. -- -- 2. `set_registration_checked_in(false)` deleted every `checkin` point but left -- the session `checkins` ROWS in place. Re-checking the same person in awarded -- nothing (documented as "no farming"), so their session points were lost -- PERMANENTLY while the attendance records still existed — and re-scanning -- could not bring them back either ("already checked in, no extra points"). -- Turning the flag back ON now recomputes what those existing rows are worth -- and re-awards the difference. The toggle becomes reversible in both -- directions, and it still cannot farm points: the total is derived from the -- rows that actually exist, never from the number of clicks. -- -- 3. Staff and Admin contacts earn nothing at all, silently: `_points_eligible` -- excludes `type = 'staff'`, `is_admin` and any `@appe.at` email, and -- `_award_points` no-ops without an audit row. That exclusion is deliberate -- (they must not rank on the public leaderboard) and STAYS. What was wrong is -- that it was invisible — a staff test check-in looks like a broken feature. -- `welcome_checkin` now reports whether points were actually awarded, so the -- scanner says "Checked in" instead of claiming "+10 pts" that never landed, -- and the admin Registrations table labels these people as not earning points. -- ----------------------------------------------------------------------------- -- ----------------------------------------------------------------------------- -- 0. checkin_attendee — same visibility fix. It returns `points`, and the -- scanner renders "+10 pts · now {points}" whenever `already` is false, so an -- ineligible attendee got a sheet claiming "+10 pts · now 0". `points_awarded` -- tells the truth. Return type grows -> drop first. Body is otherwise -- identical to 20261019110000 (front-desk gate, no checked_in side effect). -- ----------------------------------------------------------------------------- drop function if exists public.checkin_attendee(uuid, text); create or replace function public.checkin_attendee(p_session_id uuid, p_attendee_qr text) returns table (checkin_id uuid, attendee_email text, attendee_name text, points integer, already boolean, points_awarded boolean) language plpgsql security definer set search_path = public as $$ declare v_staff text := public.current_email(); v_attendee text; v_name text; v_event_id uuid; v_slot_type public.slot_type; v_new_id uuid; v_points integer; v_already boolean := false; v_checked boolean; v_award boolean := false; begin if v_staff is null then raise exception 'not authenticated'; end if; if not public.is_staff_or_admin() then raise exception 'staff or admin only'; end if; select r.contact_email, c.full_name, r.checked_in into v_attendee, v_name, v_checked from public.registrations r join public.contacts c on c.email = r.contact_email where r.qr_token = p_attendee_qr and r.event_id = (select id from public.events where is_active limit 1); if v_attendee is null then raise exception 'no attendee found for that QR code'; end if; -- Front-desk gate: no session check-in before the welcome check-in. if coalesce(v_checked, false) is not true then raise exception 'This attendee needs to be checked in at the front desk first.'; end if; select se.event_id, s.type into v_event_id, v_slot_type from public.sessions se join public.slots s on s.id = se.slot_id where se.id = p_session_id; if v_event_id is null then raise exception 'session not found'; end if; if v_slot_type = 'service_session' then raise exception 'service sessions cannot be checked in'; end if; insert into public.checkins (event_id, contact_email, session_id, staff_email) values (v_event_id, v_attendee, p_session_id, v_staff) on conflict (event_id, contact_email, session_id) do nothing returning id into v_new_id; if v_new_id is null then v_already := true; select id into v_new_id from public.checkins where event_id = v_event_id and contact_email = v_attendee and session_id = p_session_id; else -- Ask before awarding: _award_points skips staff/Admin contacts silently, -- and the sheet must not promise a +10 that never lands. v_award := public._points_eligible(v_event_id, v_attendee); if v_award then perform public._award_points(v_event_id, v_attendee, 10, 'checkin'); end if; end if; select r.points into v_points from public.registrations r where r.event_id = v_event_id and r.contact_email = v_attendee; return query select v_new_id, v_attendee, v_name, coalesce(v_points, 0), v_already, v_award; end; $$; revoke execute on function public.checkin_attendee(uuid, text) from public, anon; grant execute on function public.checkin_attendee(uuid, text) to authenticated; -- ----------------------------------------------------------------------------- -- 1. welcome_checkin — award +10 on a first front-desk check-in, and report the -- resulting balance. The return type grows, so the function has to be dropped -- first (Postgres cannot change the row type of an existing function). -- ----------------------------------------------------------------------------- drop function if exists public.welcome_checkin(text); create or replace function public.welcome_checkin(p_attendee_qr text) returns table (attendee_email text, attendee_name text, already boolean, points integer, points_awarded boolean) language plpgsql security definer set search_path = public as $$ declare v_staff text := public.current_email(); v_attendee text; v_name text; v_event_id uuid; v_checked boolean; v_reg_id uuid; v_payload jsonb; v_copies integer; v_i integer; v_award boolean := false; v_total integer; begin if v_staff is null then raise exception 'not authenticated'; end if; if not public.is_staff_or_admin() then raise exception 'staff or admin only'; end if; select id into v_event_id from public.events where is_active limit 1; if v_event_id is null then raise exception 'no active event'; end if; select r.contact_email, c.full_name, r.id into v_attendee, v_name, v_reg_id from public.registrations r join public.contacts c on c.email = r.contact_email where r.qr_token = p_attendee_qr and r.event_id = v_event_id; if v_attendee is null then raise exception 'no attendee found for that QR code'; end if; select checked_in into v_checked from public.registrations where event_id = v_event_id and contact_email = v_attendee; update public.registrations set checked_in = true where event_id = v_event_id and contact_email = v_attendee; -- A genuine first check-in: award the front-desk +10 and print badges. if v_checked is not true then -- _award_points silently skips staff/Admin contacts, so ask first; the UI -- must not promise points that will not arrive. v_award := public._points_eligible(v_event_id, v_attendee); if v_award then perform public._award_points(v_event_id, v_attendee, 10, 'checkin'); end if; v_payload := public._badge_print_payload(v_reg_id, 'checkin', v_staff); if v_payload is not null then select coalesce(e.badge_copies, 1) into v_copies from public.events e where e.id = v_event_id; v_copies := greatest(1, least(4, coalesce(v_copies, 1))); v_i := 0; while v_i < v_copies loop perform pgmq.send('badge_print', v_payload); v_i := v_i + 1; end loop; end if; end if; select r.points into v_total from public.registrations r where r.event_id = v_event_id and r.contact_email = v_attendee; return query select v_attendee, v_name, coalesce(v_checked, false), coalesce(v_total, 0), v_award; end; $$; revoke execute on function public.welcome_checkin(text) from public, anon; grant execute on function public.welcome_checkin(text) to authenticated; -- ----------------------------------------------------------------------------- -- 2. set_registration_checked_in — OFF removes check-in points (unchanged), ON -- now restores the points owed by the check-in rows that still exist. -- `points_added` is new: the admin toggle patches its own row optimistically, -- so it needs the award in the response, not just the reversal. The return -- type grows, so the function is dropped first. -- ----------------------------------------------------------------------------- drop function if exists public.set_registration_checked_in(uuid, boolean); 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, points_added integer) language plpgsql security definer set search_path = public as $$ declare v_event_id uuid; v_contact text; v_removed integer; v_added integer := 0; v_expected integer; v_current 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; else -- Turning someone back on: their session check-in rows were kept, so the -- check-in points those rows represent must come back. Recompute from the -- rows themselves (10 per session check-in + 10 for the front desk) and -- award only the shortfall, so no sequence of clicks can exceed what the -- attendance actually justifies. Staff/Admin contacts are excluded by -- _points_eligible, so the target stays 0 for them and nothing is claimed. if public._points_eligible(v_event_id, v_contact) then select 10 * (count(*) + 1) into v_expected from public.checkins where event_id = v_event_id and contact_email = v_contact; select coalesce(sum(delta), 0) into v_current from public.points_log where event_id = v_event_id and contact_email = v_contact and category = 'checkin'; if coalesce(v_expected, 10) > coalesce(v_current, 0) then v_added := coalesce(v_expected, 10) - coalesce(v_current, 0); perform public._award_points(v_event_id, v_contact, v_added, 'checkin'); end if; end if; end if; return query select p_registration_id, p_checked_in, coalesce(v_removed, 0)::integer, coalesce(v_added, 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; -- ----------------------------------------------------------------------------- -- 4. preview_welcome_checkin — reports whether confirming will award points, so -- the confirmation sheet can promise "+10 pts" only when it is true. Return -- type grows, so the function is dropped first. Guards, resolution, `already` -- and the `will_print`/`copies` logic are unchanged from 20261019110000. -- ----------------------------------------------------------------------------- drop function if exists public.preview_welcome_checkin(text); create or replace function public.preview_welcome_checkin(p_attendee_qr text) returns table (attendee_email text, attendee_name text, already boolean, will_print boolean, copies integer, points_awarded boolean) language plpgsql stable security definer set search_path = public as $$ declare v_staff text := public.current_email(); v_attendee text; v_name text; v_event_id uuid; v_checked boolean; v_reg_id uuid; begin if v_staff is null then raise exception 'not authenticated'; end if; if not public.is_staff_or_admin() then raise exception 'staff or admin only'; end if; select id into v_event_id from public.events where is_active limit 1; if v_event_id is null then raise exception 'no active event'; end if; select r.contact_email, c.full_name, r.id into v_attendee, v_name, v_reg_id from public.registrations r join public.contacts c on c.email = r.contact_email where r.qr_token = p_attendee_qr and r.event_id = v_event_id; if v_attendee is null then raise exception 'no attendee found for that QR code'; end if; select checked_in into v_checked from public.registrations where event_id = v_event_id and contact_email = v_attendee; -- A badge prints only on a genuine first check-in AND when the event has a -- layout with elements (the same condition welcome_checkin uses to enqueue). will_print := coalesce(v_checked, false) is not true and public._badge_print_payload(v_reg_id, 'checkin', v_staff) is not null; -- Same clamp welcome_checkin applies, so the preview never over-promises. copies := case when will_print then greatest(1, least(4, coalesce( (select e.badge_copies from public.events e where e.id = v_event_id), 1))) else 0 end; -- Points come only from a genuine first check-in, and only for someone -- allowed to earn them — exactly what welcome_checkin will do. points_awarded := coalesce(v_checked, false) is not true and public._points_eligible(v_event_id, v_attendee); return query select v_attendee, v_name, coalesce(v_checked, false), will_print, copies, points_awarded; end; $$; revoke execute on function public.preview_welcome_checkin(text) from public, anon; grant execute on function public.preview_welcome_checkin(text) to authenticated; notify pgrst, 'reload schema';