fix(admin): per-row check-in toggle; feat(db): front-desk gate + badge copies

Registrations table felt buggy because both row actions did
setBusy(true) -> write -> reloadRegistrations() -> setBusy(false):
the list is sorted by points, so a reload made the row you just
touched jump (or vanish under a Checked-in filter), and the single
`busy` flag disabled every control in the table for the whole
request. The fix is per-row in-flight state (busyIds + withRowBusy)
and no reload at all: the toggle patches its own row's checked_in
plus the points delta the RPC returns, the type change patches
`type`; a rejected write rolls both back. Only the touched row is
disabled, shows a "Saving…" pulse, and two rows can save at once.
Toggle hit area is now 40x41 px with a focus-visible ring.

Front-desk gate: checkin_attendee reads registrations.checked_in
and raises "This attendee needs to be checked in at the front desk
first." when it is not true, and no longer sets checked_in as a
side effect. checkins has no INSERT policy, so the RPC is the only
write path and the gate cannot be bypassed. The scanner shows a
dedicated "Not checked in yet" sheet instead of a toast.

Badge copies: events.badge_copies (1..4, default 1). welcome_checkin
sends that many pgmq jobs in one transaction (the Pi prints one
label per job, so N jobs = N labels; no worker change) and clamps
the value again itself. preview_welcome_checkin gained a `copies`
column (drop + recreate) so the popup states the real count. Manual
reprint still prints exactly one. Set from Settings -> Badge
printing (active event) and the Events form, always as a
single-column update; the Settings badge section now renders even
when the LLM settings call fails.

Tests: badge_print 113 (+6), leaderboard 47 (+1), new
registration-checkin-toggle (6), front-desk-gate (3),
welcome-badge-copies (3); the stale registrations-PATCH mock in
registration-ticket-merge now targets set_registration_checked_in,
the ambiguous Save button is "Save copies", and the print-copy
assertion tolerates the typographic apostrophe. Docs updated
(rpcs, schema, printing, tests, admin-app, screens) with a release
record; NOT deployed - see docs/releases/2026-10-19.md.
This commit is contained in:
Lago committed 2026-10-19 15:10:20 +02:00
1 parent c857204cfe
commit c49a94fe80
27 files changed
+1378 -73

No files matched your search

@@ -0,0 +1,194 @@
-- -----------------------------------------------------------------------------
-- Front-desk gate + configurable welcome badge copies.
--
-- 1. Front-desk gate: an attendee must be checked in to the EVENT (the welcome
-- check-in at the front desk sets registrations.checked_in) before they can
-- be checked into any SESSION. Previously checkin_attendee silently marked
-- the attendee checked in as a side effect; that is removed and replaced by
-- a hard requirement with a clear message.
--
-- 2. Badge copies: events.badge_copies (1..4, default 1) controls how many
-- labels a welcome check-in prints. welcome_checkin enqueues that many
-- identical jobs (the Pi worker prints one label per job, so no worker
-- change is needed). Manual reprint (enqueue_badge_print) still prints one.
-- -----------------------------------------------------------------------------
alter table public.events
add column if not exists badge_copies integer not null default 1
check (badge_copies between 1 and 4);
comment on column public.events.badge_copies is
'How many badge labels a welcome (front-desk) check-in prints, 1..4.';
-- -----------------------------------------------------------------------------
-- 1. checkin_attendee — require a prior front-desk (welcome) check-in.
-- Recreated from 20260833000000 with two changes:
-- * resolves the registration's checked_in and raises a clear message when
-- the attendee has not been checked in at the front desk;
-- * no longer sets checked_in = true (that is the welcome check-in's job).
-- -----------------------------------------------------------------------------
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)
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;
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
perform public._award_points(v_event_id, v_attendee, 10, 'checkin');
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;
end;
$$;
-- -----------------------------------------------------------------------------
-- 2. welcome_checkin — enqueue events.badge_copies labels on a first check-in.
-- Recreated from 20261018170000 with the copies loop; everything else is
-- unchanged (guard, resolution, already flag, transactional enqueue).
-- -----------------------------------------------------------------------------
create or replace function public.welcome_checkin(p_attendee_qr text)
returns table (attendee_email text, attendee_name text, already 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;
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;
-- Badge printing: only for a genuine first check-in, and only when the event
-- actually has a badge layout. No payload -> no job. Print badge_copies labels.
if v_checked is not true then
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;
return query select v_attendee, v_name, coalesce(v_checked, false);
end;
$$;
-- -----------------------------------------------------------------------------
-- 3. preview_welcome_checkin — now also reports HOW MANY labels a confirm will
-- print, so the scanner popup states the truth instead of "this person's
-- label". Recreated from 20261019090000 with the extra `copies` column; the
-- signature changes, so it has to be dropped first (Postgres will not
-- replace a function with a different return type). Everything else —
-- guards, resolution, `already`, and the `will_print` condition that mirrors
-- welcome_checkin — is unchanged.
-- -----------------------------------------------------------------------------
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)
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;
return query select v_attendee, v_name, coalesce(v_checked, false), will_print, copies;
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';
+44 -1
View File
@@ -238,7 +238,9 @@ select pg_temp.assert_bp(
(select count(*) = 1 from pgmq.q_badge_print), 'event without a layout enqueues nothing');
update public.events set is_active = true where id = 'b9d00001-0000-0000-0000-00000000000a';
-- The regular session check-in must not print badges.
-- The regular session check-in must not print badges. It now requires a prior
-- front-desk (welcome) check-in, so mark the attendee checked in first.
update public.registrations set checked_in = true where contact_email = '[email protected]';
select public.checkin_attendee('b9d00003-0000-0000-0000-00000000000a', 'BPQR-ATT-3');
select pg_temp.assert_bp(
(select count(*) = 1 from public.checkins where session_id = 'b9d00003-0000-0000-0000-00000000000a'),
@@ -246,6 +248,18 @@ select pg_temp.assert_bp(
select pg_temp.assert_bp(
(select count(*) = 1 from pgmq.q_badge_print), 'session check-in enqueues nothing');
-- Front-desk gate: an attendee not checked in at the event cannot be scanned
-- into a session (raises a clear message, records nothing).
do $$ begin
begin
perform public.checkin_attendee('b9d00003-0000-0000-0000-00000000000a', 'BPQR-ATT-5');
raise exception 'front-desk gate allowed a session check-in';
exception when raise_exception then
if sqlerrm not like '%front desk%' then raise; end if;
end;
perform pg_temp.assert_bp(true, 'session check-in requires a front-desk check-in');
end; $$;
-- A direct write of checked_in (admin edit) must not print badges either.
update public.registrations set checked_in = true where contact_email = '[email protected]';
select pg_temp.assert_bp(
@@ -262,6 +276,20 @@ select pg_temp.assert_bp(
(select not checked_in from public.registrations where contact_email = '[email protected]'),
'rolled back check-in leaves the attendee unchecked');
-- Badge copies: a welcome check-in prints events.badge_copies labels (default
-- 1 is covered by the first check-in above). Isolated in a savepoint so the
-- queue returns to one for the rest of the run.
savepoint bp_copies;
update public.events set badge_copies = 3 where id = 'b9d00001-0000-0000-0000-00000000000a';
select public.welcome_checkin('BPQR-ATT-5');
select pg_temp.assert_bp(
(select count(*) = 4 from pgmq.q_badge_print),
'welcome check-in enqueues badge_copies (3) labels');
rollback to savepoint bp_copies;
select pg_temp.assert_bp(
(select count(*) = 1 from pgmq.q_badge_print),
'copies test rolled back cleanly');
-- -----------------------------------------------------------------------------
-- 2b. WELCOME CHECK-IN PREVIEW (read-only: resolves the name + will_print)
-- -----------------------------------------------------------------------------
@@ -291,6 +319,21 @@ select pg_temp.assert_bp(
(select already = true and will_print = false from bp_prev2),
'preview of a checked-in attendee reports no print');
-- The preview also states how many labels a confirm would print, so the scanner
-- popup can be truthful about the copy count instead of "this person's label".
select pg_temp.assert_bp(
(select copies = 1 from bp_prev1),
'preview reports copies = 1 by default');
select pg_temp.assert_bp(
(select copies = 0 from bp_prev2),
'preview reports no copies when nothing will print');
savepoint bp_prev_copies;
update public.events set badge_copies = 3 where id = 'b9d00001-0000-0000-0000-00000000000a';
select pg_temp.assert_bp(
(select copies = 3 from public.preview_welcome_checkin('BPQR-ATT-5')),
'preview reports the event badge_copies');
rollback to savepoint bp_prev_copies;
-- An event without a badge layout: check-in would work but nothing prints.
-- (bp-attb was checked in by the section-2 scan; reset it so the preview
-- isolates the "no layout" reason rather than "already checked in".)
+21 -2
View File
@@ -19,11 +19,12 @@ values ('[email protected]', 'Player', false), ('[email protected]', 'Pe
('[email protected]', 'Other', false), ('[email protected]', 'Speaker', false),
('[email protected]', 'Staff', false), ('[email protected]', 'Admin', true),
('[email protected]', 'Domain Admin', false), ('[email protected]', 'Orphan', false);
insert into public.registrations (event_id, contact_email, type, qr_token, points)
insert into public.registrations (event_id, contact_email, type, qr_token, points, checked_in)
select 'a0150000-0000-0000-0000-000000000001'::uuid, email,
case when email = '[email protected]' then 'staff'
when email = '[email protected]' then 'speaker' else 'attendee' end::public.registration_type,
'LB-' || email, case when is_admin or email = '[email protected]' then 99 else 0 end
'LB-' || email, case when is_admin or email = '[email protected]' then 99 else 0 end,
true
from public.contacts where email in ('[email protected]', '[email protected]', '[email protected]',
'[email protected]', '[email protected]', '[email protected]', '[email protected]');
insert into public.registrations (event_id, contact_email, type, qr_token, points)
@@ -36,6 +37,13 @@ values ('a0150000-0000-0000-0000-000000000011', 'a0150000-0000-0000-0000-0000000
('a0150000-0000-0000-0000-000000000012', 'a0150000-0000-0000-0000-000000000001', -151001, 'Talk two'),
('a0150000-0000-0000-0000-000000000013', 'a0150000-0000-0000-0000-000000000001', -151002, 'Service');
-- A front-desk-UNCHECKED attendee: has a registration but checked_in = false,
-- so a session check-in must be blocked by the front-desk gate.
insert into public.contacts (email, full_name, is_admin)
values ('[email protected]', 'Fresh Face', false);
insert into public.registrations (event_id, contact_email, type, qr_token, points, checked_in)
values ('a0150000-0000-0000-0000-000000000001', '[email protected]', 'attendee', '[email protected]', 0, false);
do $$
declare
event_one uuid := 'a0150000-0000-0000-0000-000000000001';
@@ -63,6 +71,17 @@ begin
if sqlerrm <> 'service sessions cannot be checked in' then raise; end if;
end;
perform pg_temp.assert_board(true, 'service check-in rejected');
-- Front-desk gate: an attendee not checked in at the event cannot be scanned
-- into a session; the RPC raises a clear message.
begin
perform public.checkin_attendee(talk_one, '[email protected]');
raise exception 'front-desk gate allowed a session check-in';
exception when raise_exception then
if sqlerrm not like '%front desk%' then raise; end if;
end;
perform pg_temp.assert_board(
(select not exists (select 1 from public.checkins where contact_email = '[email protected]')),
'front-desk gate blocks session check-in');
foreach excluded_email in array array['[email protected]','[email protected]','[email protected]'] loop
perform set_config('request.jwt.claims', json_build_object('email', excluded_email)::text, true);
select * into feedback_result from public.submit_feedback(talk_one, 5, 'Review');
+7 -1
View File
@@ -61,6 +61,9 @@ declare
begin
select id into v_session from public.sessions where slot_id = 2 limit 1;
perform set_user('[email protected]');
-- Front-desk (welcome) check-in first: a session check-in now requires the
-- attendee to be checked in to the event.
perform public.welcome_checkin('CVV-ATT-1000');
select points into v_res_points from public.checkin_attendee(v_session, 'CVV-ATT-1000');
select points into v_points from public.registrations
where contact_email = '[email protected]'
@@ -72,7 +75,7 @@ begin
v_ok := exists (select 1 from public.registrations
where contact_email = '[email protected]' and checked_in);
v_msg := 'registration not marked checked_in';
insert into test_results values ('checkin marks checked_in', v_ok, case when v_ok then null else v_msg end);
insert into test_results values ('front-desk check-in marks checked_in', v_ok, case when v_ok then null else v_msg end);
exception when others then
insert into test_results values ('checkin (1)', false, 'error: ' || sqlerrm);
end;
@@ -119,6 +122,9 @@ begin
where slot_id in (select id from public.slots where type = 'service_session')
limit 1;
perform set_user('[email protected]');
-- Front-desk check-in first so the service-session guard (not the front-desk
-- gate) is what rejects this scan.
perform public.welcome_checkin('CVV-ATT-1001');
begin
perform public.checkin_attendee(v_session, 'CVV-ATT-1001');
insert into test_results values ('service session check-in rejected', false,