fix(points): welcome check-in awards +10; add Sessions view, AI connection test, silent reload

The front-desk check-in set checked_in and printed badges but awarded nothing,
so the +10 depended on which check-in a person happened to get. Every award
path was reviewed:

- welcome_checkin awards +10 (category checkin) on a genuine first check-in and
  returns points + points_awarded; checkin_attendee and preview_welcome_checkin
  return points_awarded too.
- set_registration_checked_in(false) deleted all checkin points while keeping
  the checkins rows, and switching back on awarded nothing -- one mistaken
  toggle destroyed those points permanently. ON now recomputes from the rows
  that still exist and awards only the shortfall: reversible, still unfarmable.
- The staff/Admin/@appe.at exclusion stays (product decision) but is no longer
  silent: three-way points sheets in the scanner, "no points" with a tooltip in
  Registrations, and the Contacts points modal refuses to apply rather than
  letting adjust_points (void) no-op quietly.

Also in this change:
- admin silent refresh (5s poll + 500ms after writes) across all data views;
  Registrations patches in place so rows never slide under the cursor
- Settings "Validate AI connection" via a new sidecar POST /llm-test verdict
  endpoint (never leaks the key, distinguishes "no usage block" from "off")
- new Sessions view between Schedule and Activity: speakers, attendance,
  ratings, attendee filter, and one button that summarises every session with
  feedback (one request each, concurrency 3, UI blocked until all settle)
- provider requests disable thinking through one shared body builder sending
  both reasoning_effort:"none" and chat_template_kwargs.enable_thinking

SQL: 113 / 57 / 24 assertions (set_registration_checked_in section 3 was
rewritten, it asserted the bug). Sidecar 34 unit tests. Full e2e 202 passed,
0 failed. Migration applied locally only -- not deployed.

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]>
This commit is contained in:
committed 2026-10-19 20:14:03 +02:00
1 parent cdb4793273
commit d3083b7940
35 files changed
+2778 -154

No files matched your search

@@ -0,0 +1,323 @@
-- -----------------------------------------------------------------------------
-- 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';
+37
View File
@@ -51,6 +51,7 @@ declare
talk_two uuid := 'a0150000-0000-0000-0000-000000000012';
excluded_email text;
feedback_result record;
welcome_result record;
begin
perform pg_temp.assert_board((select bool_and(points = 0) from public.registrations
where contact_email in ('[email protected]','[email protected]','[email protected]')), 'excluded inserts force zero');
@@ -82,6 +83,42 @@ begin
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');
-- The front-desk welcome check-in is a real check-in: it awards the same +10
-- as a session scan, once per person, and says whether it paid out.
select * into welcome_result from public.welcome_checkin('[email protected]');
perform pg_temp.assert_board(
welcome_result.already = false and welcome_result.points_awarded
and welcome_result.points = 10,
'welcome check-in awards ten');
perform pg_temp.assert_board(
(select points = 10 and checked_in from public.registrations where contact_email = '[email protected]'),
'welcome check-in lands on the registration');
select * into welcome_result from public.welcome_checkin('[email protected]');
perform pg_temp.assert_board(
welcome_result.already and welcome_result.points = 10,
'welcome check-in is idempotent');
-- The gate is open for that attendee now, and both awards stack.
perform public.checkin_attendee(talk_one, '[email protected]');
perform pg_temp.assert_board(
(select points = 20 from public.registrations where contact_email = '[email protected]'),
'session scan after welcome totals twenty');
-- Staff and Admin-domain contacts are welcomed too, but earn nothing and the
-- RPC must not claim otherwise (that silence is what made points look broken).
-- Clear the flag first so this exercises the award branch, not the
-- already-checked-in shortcut.
update public.registrations set checked_in = false
where contact_email in ('[email protected]','[email protected]','[email protected]');
foreach excluded_email in array array['[email protected]','[email protected]','[email protected]'] loop
select * into welcome_result from public.welcome_checkin('LB-' || excluded_email);
perform pg_temp.assert_board(
welcome_result.already = false and welcome_result.points_awarded = false
and welcome_result.points = 0,
excluded_email || ' welcome awards zero and reports it');
perform pg_temp.assert_board(
(select checked_in from public.registrations where contact_email = excluded_email),
excluded_email || ' is still marked checked in');
end loop;
perform set_config('request.jwt.claims', '{"email":"[email protected]"}', true);
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');
@@ -1,6 +1,7 @@
-- set_registration_checked_in: admin flips checked_in; un-checking removes the
-- attendee's check-in points (and only those). Run inside a transaction that
-- is rolled back, like the other suites.
-- set_registration_checked_in: admin flips checked_in. Un-checking removes the
-- attendee's check-in points (and only those); re-checking restores the points
-- that the surviving check-in rows justify, without letting repeated clicks
-- farm points. Run inside a transaction that is rolled back, like other suites.
\set ON_ERROR_STOP on
begin;
@@ -95,17 +96,97 @@ select pg_temp.assert_scr(
'other attendee keeps their points');
-- -----------------------------------------------------------------------------
-- 3. Re-checking does not re-award anything.
-- 3. Re-checking RESTORES the points the surviving check-in rows justify.
-- Section 1 deleted the audit rows but the attendance records remain, so the
-- old behaviour (award nothing, permanently) left this person 30 points short
-- with no way back. Toggling on now recomputes from the rows: 10 per session
-- check-in + 10 for the front desk.
-- -----------------------------------------------------------------------------
-- Real attendance for scr-user: two sessions. Negative slot ids keep these
-- fixtures out of every real event's schedule.
insert into public.slots (id, event_id, name, slot_date, start_time, end_time, type)
values (-152001, 'c7e10000-0000-0000-0000-000000000001', 'SCR Talk A', current_date, '09:00', '10:00', 'session');
insert into public.sessions (id, event_id, slot_id, title)
values ('c7e10000-0000-0000-0000-0000000000a1', 'c7e10000-0000-0000-0000-000000000001', -152001, 'SCR Session one'),
('c7e10000-0000-0000-0000-0000000000a2', 'c7e10000-0000-0000-0000-000000000001', -152001, 'SCR Session two');
insert into public.checkins (event_id, contact_email, session_id, staff_email)
values ('c7e10000-0000-0000-0000-000000000001', '[email protected]', 'c7e10000-0000-0000-0000-0000000000a1', '[email protected]'),
('c7e10000-0000-0000-0000-000000000001', '[email protected]', 'c7e10000-0000-0000-0000-0000000000a2', '[email protected]');
create temporary table scr_on as
select * from public.set_registration_checked_in(
'c7e10000-0000-0000-0000-000000000011', true);
select pg_temp.assert_scr(
(select checked_in = true and points_removed = 0 from scr_on),
're-check-in awards no points');
(select checked_in = true and points_removed = 0 and points_added = 30 from scr_on),
're-check-in restores 30 points (2 sessions + front desk)');
select pg_temp.assert_scr(
(select points = 10 from public.registrations where qr_token = 'SCRQR-1'),
'points unchanged after re-check-in');
(select points = 40 from public.registrations where qr_token = 'SCRQR-1'),
'points back to the pre-uncheck total after re-check-in');
select pg_temp.assert_scr(
(select points = 40 from public.leaderboard
where event_id = 'c7e10000-0000-0000-0000-000000000001' and contact_email = '[email protected]'),
'leaderboard synced back up to 40');
-- Toggling on again is idempotent: the award already matches what the rows
-- justify, so repeated clicks cannot farm points.
create temporary table scr_on2 as
select * from public.set_registration_checked_in(
'c7e10000-0000-0000-0000-000000000011', true);
select pg_temp.assert_scr(
(select points_added = 0 from scr_on2),
'second re-check-in awards nothing (not farmable)');
select pg_temp.assert_scr(
(select points = 40 from public.registrations where qr_token = 'SCRQR-1'),
'points unchanged by a repeat toggle on');
-- Full round trip is symmetric: off removes exactly what on gives back.
create temporary table scr_off3 as
select * from public.set_registration_checked_in(
'c7e10000-0000-0000-0000-000000000011', false);
select pg_temp.assert_scr(
(select points_removed = 30 from scr_off3),
'un-check-in again removes the same 30');
create temporary table scr_on4 as
select * from public.set_registration_checked_in(
'c7e10000-0000-0000-0000-000000000011', true);
select pg_temp.assert_scr(
(select points_added = 30 from scr_on4),
'and toggling on restores it once more');
-- Someone with attendance but no session check-ins at all earns only the
-- front-desk +10 on the way back on (SCRQR-2 has connection points, no rows).
create temporary table scr_on3 as
select * from public.set_registration_checked_in(
'c7e10000-0000-0000-0000-000000000012', true);
select pg_temp.assert_scr(
(select points_added = 10 from scr_on3),
'front-desk-only restore awards 10');
select pg_temp.assert_scr(
(select points = 35 from public.registrations where qr_token = 'SCRQR-2'),
'front-desk +10 lands on top of connection points');
-- -----------------------------------------------------------------------------
-- 3b. Staff and Admin-domain contacts stay at zero in both directions.
-- -----------------------------------------------------------------------------
insert into public.contacts (email, full_name, is_admin)
values ('[email protected]', 'SCR Staff', false);
insert into public.registrations (id, event_id, contact_email, type, qr_token, checked_in, points)
values ('c7e10000-0000-0000-0000-000000000013', 'c7e10000-0000-0000-0000-000000000001',
'[email protected]', 'staff', 'SCRQR-3', true, 0);
insert into public.checkins (event_id, contact_email, session_id, staff_email)
values ('c7e10000-0000-0000-0000-000000000001', '[email protected]', 'c7e10000-0000-0000-0000-0000000000a1', '[email protected]');
create temporary table scr_staff_off as
select * from public.set_registration_checked_in(
'c7e10000-0000-0000-0000-000000000013', false);
create temporary table scr_staff_on as
select * from public.set_registration_checked_in(
'c7e10000-0000-0000-0000-000000000013', true);
select pg_temp.assert_scr(
((select points_removed = 0 from scr_staff_off) and (select points_added = 0 from scr_staff_on)),
'staff toggle moves no points in either direction');
select pg_temp.assert_scr(
(select points = 0 from public.registrations where qr_token = 'SCRQR-3'),
'staff registration stays at zero');
-- -----------------------------------------------------------------------------
-- 4. Unknown registration id is rejected.