-- ----------------------------------------------------------------------------- -- Welcome check-in preview. -- -- The staff scanner now shows a confirmation before a welcome check-in: -- "You are checking in [Name]. By confirming, you will print this person's -- label." This read-only RPC resolves a scanned QR token to the attendee and -- reports whether a badge would actually print, so the popup can show the real -- name and be honest about the print (a person already checked in, or an event -- without a badge layout, will NOT print — mirroring welcome_checkin exactly). -- -- It does NOT check anyone in and does NOT enqueue anything; it is a pure -- lookup. Staff/admin only, same guard as welcome_checkin. -- ----------------------------------------------------------------------------- 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) 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; return query select v_attendee, v_name, coalesce(v_checked, false), will_print; 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';