fix(scanner): camera scans used stale first-render handler (welcome mode ignored); feat(admin): un-check-in removes check-in points
- Scanner: invoke onResult through a ref refreshed each render; the camera
callback captured the mount-time closure, so scans kept running session
check-in even with the Welcome toggle on (manual entry was unaffected)
- new RPC set_registration_checked_in(id, bool): admin-only; on un-check,
deletes the event's 'checkin' points_log rows and applies the negative
delta via _award_points('admin') so leaderboard + audit stay consistent;
re-check awards nothing; returns points_removed
- admin Registrations toggle now calls the RPC, refreshes rows and toasts
the removed points
- supabase/tests/set_registration_checked_in_tests.sql: 15 assertions
This commit is contained in:
1 parent
9a6e2e5c2a
commit
316f1316e6
6 files changed
+307
-6
No files matched your search
@@ -171,10 +171,18 @@ export async function setRegistrationType(contactEmail: string, type: Registrati
|
|||||||
await rpc('set_registration_type', { p_contact_email: contactEmail, p_type: type });
|
await rpc('set_registration_type', { p_contact_email: contactEmail, p_type: type });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function setCheckedIn(registrationId: string, checkedIn: boolean): Promise<void> {
|
/**
|
||||||
const { data, error } = await supabase.from('registrations').update({ checked_in: checkedIn }).eq('id', registrationId).select('id').single();
|
* Flip an attendee's event check-in state. When turning it OFF, the backend
|
||||||
if (error) throw new ApiError(error.message);
|
* also removes every point this person earned from check-ins in the event
|
||||||
if (!data) throw new ApiError('Registration no longer exists');
|
* (see set_registration_checked_in). Returns the number of points removed.
|
||||||
|
*/
|
||||||
|
export async function setCheckedIn(registrationId: string, checkedIn: boolean): Promise<number> {
|
||||||
|
const rows = await rpc<{ registration_id: string; checked_in: boolean; points_removed: number }[]>(
|
||||||
|
'set_registration_checked_in',
|
||||||
|
{ p_registration_id: registrationId, p_checked_in: checkedIn },
|
||||||
|
);
|
||||||
|
if (!rows?.length) throw new ApiError('Registration no longer exists');
|
||||||
|
return rows[0].points_removed;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const mergeTickets = (sourceId: string, targetId: string) =>
|
export const mergeTickets = (sourceId: string, targetId: string) =>
|
||||||
|
|||||||
@@ -198,7 +198,13 @@ export function Registrations() {
|
|||||||
setBusy(true);
|
setBusy(true);
|
||||||
setRegistrations((rows) => rows.map((row) => row.id === r.id ? { ...row, checked_in: v } : row));
|
setRegistrations((rows) => rows.map((row) => row.id === r.id ? { ...row, checked_in: v } : row));
|
||||||
try {
|
try {
|
||||||
await setCheckedIn(r.id, v);
|
const removed = await setCheckedIn(r.id, v);
|
||||||
|
if (removed > 0) {
|
||||||
|
push('success', `${byEmail.get(r.contact_email)?.full_name ?? r.contact_email}: un-checked in, −${removed} check-in pts removed`);
|
||||||
|
}
|
||||||
|
// The RPC may have changed this attendee's points — refresh so the
|
||||||
|
// points column and leaderboard stay truthful.
|
||||||
|
await reloadRegistrations(r.event_id);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setRegistrations((rows) => rows.map((row) => row.id === r.id ? { ...row, checked_in: r.checked_in } : row));
|
setRegistrations((rows) => rows.map((row) => row.id === r.id ? { ...row, checked_in: r.checked_in } : row));
|
||||||
push('error', e instanceof Error ? e.message : 'Update failed');
|
push('error', e instanceof Error ? e.message : 'Update failed');
|
||||||
|
|||||||
@@ -90,6 +90,12 @@ export function Scanner({ onResult, label = 'Scan a lanyard QR', entryLabel = 'Q
|
|||||||
const [camError, setCamError] = useState<string | null>(null);
|
const [camError, setCamError] = useState<string | null>(null);
|
||||||
const scannerRef = useRef<Html5Qrcode | null>(null);
|
const scannerRef = useRef<Html5Qrcode | null>(null);
|
||||||
const [camOn, setCamOn] = useState(false);
|
const [camOn, setCamOn] = useState(false);
|
||||||
|
// The camera callback below is registered once on mount; without a ref it
|
||||||
|
// would keep calling the FIRST render's onResult, whose closures over
|
||||||
|
// parent state (welcome mode, selected session) are stale. Always invoke
|
||||||
|
// the latest handler through this ref.
|
||||||
|
const onResultRef = useRef(onResult);
|
||||||
|
onResultRef.current = onResult;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
@@ -119,7 +125,7 @@ export function Scanner({ onResult, label = 'Scan a lanyard QR', entryLabel = 'Q
|
|||||||
if (cancelled) return;
|
if (cancelled) return;
|
||||||
setCamOn(false);
|
setCamOn(false);
|
||||||
scanner?.stop().catch(() => {});
|
scanner?.stop().catch(() => {});
|
||||||
onResult(text);
|
onResultRef.current(text);
|
||||||
},
|
},
|
||||||
() => {},
|
() => {},
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -61,3 +61,60 @@ and still never prints, per the product decision):
|
|||||||
changed files — `api.ts`, `types.ts`, `Scan.tsx` — were byte-identical to the
|
changed files — `api.ts`, `types.ts`, `Scan.tsx` — were byte-identical to the
|
||||||
deployed baseline, so whole-file copy was safe). Only `convivo-app` recreated;
|
deployed baseline, so whole-file copy was safe). Only `convivo-app` recreated;
|
||||||
admin, sidecar and every unrelated container ID preserved.
|
admin, sidecar and every unrelated container ID preserved.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# Follow-up: camera scans ignored Welcome mode + un-check-in removes points (2026-10-19)
|
||||||
|
|
||||||
|
## Symptoms (reported from production)
|
||||||
|
|
||||||
|
1. With the "Check-in type" toggle switched to **Welcome**, a camera scan still
|
||||||
|
produced the *session* result sheet ("Already checked in / No extra points")
|
||||||
|
and no confirmation popup.
|
||||||
|
2. Feature request: when an admin flips the **checked-in** toggle OFF in the
|
||||||
|
admin Registrations view, the points that person earned from check-ins
|
||||||
|
should be removed too.
|
||||||
|
|
||||||
|
## Root cause of (1)
|
||||||
|
|
||||||
|
`Scanner` (`apps/convivo/src/components/ui.tsx`) registers the html5-qrcode
|
||||||
|
success callback once in a `useEffect(…, [])`. That callback captured the
|
||||||
|
`onResult` prop **from the first render**, so it kept calling a `handleScan`
|
||||||
|
closure in which `welcomeMode` was still `false` (and the session selector
|
||||||
|
still empty). Toggling Welcome afterwards had no effect on camera scans — every
|
||||||
|
scan ran the session branch. The manual-entry field called the *current* prop,
|
||||||
|
which is why keyboard-driven tests and the earlier smoke test passed.
|
||||||
|
|
||||||
|
**Fix:** keep `onResult` in a ref refreshed on every render and invoke
|
||||||
|
`onResultRef.current(text)` from the camera callback.
|
||||||
|
|
||||||
|
## Design of (2)
|
||||||
|
|
||||||
|
New admin RPC `set_registration_checked_in(p_registration_id, p_checked_in)`
|
||||||
|
(`20261019100000_set_registration_checked_in.sql`), the single admin path for
|
||||||
|
the toggle:
|
||||||
|
|
||||||
|
- admin-only (`is_admin()`), locks the registration row;
|
||||||
|
- sets `checked_in` to the requested value;
|
||||||
|
- when turning it **off**, sums the attendee's `points_log` rows of category
|
||||||
|
`checkin` for that event, deletes them, and applies the negative delta
|
||||||
|
through `_award_points(…, 'admin')` — so the leaderboard mirror and the audit
|
||||||
|
trail stay consistent (the reversal itself is logged as an `admin` row);
|
||||||
|
- returns `points_removed`. Re-checking in does **not** re-award points (no
|
||||||
|
point farming by toggling); the admin `points` column refreshes after the
|
||||||
|
call and a toast reports the removal.
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
|
||||||
|
- `supabase/tests/set_registration_checked_in_tests.sql`: **15 assertions** —
|
||||||
|
un-check removes exactly the checkin points (40 → 10, other categories
|
||||||
|
untouched), leaderboard syncs, reversal logged once as −30 `admin`,
|
||||||
|
no-checkin-points un-check is a no-op, re-check awards nothing, unknown id
|
||||||
|
rejected, non-admin and anon rejected.
|
||||||
|
- Existing suites re-run clean: leaderboard 46, badge_print 109, rpc_tests.
|
||||||
|
|
||||||
|
## Deployment
|
||||||
|
|
||||||
|
**Deployed to production 2026-10-19**: migration applied + ledger (40 → 41);
|
||||||
|
`convivo-app` and `convivo-admin` rebuilt from HEAD behind the drift guard (all
|
||||||
|
three changed files byte-identical to the deployed baseline).
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
-- -----------------------------------------------------------------------------
|
||||||
|
-- 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';
|
||||||
@@ -0,0 +1,156 @@
|
|||||||
|
-- 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 ON_ERROR_STOP on
|
||||||
|
begin;
|
||||||
|
|
||||||
|
create temporary table scr_results (name text);
|
||||||
|
grant insert, select on scr_results to authenticated, anon;
|
||||||
|
create function pg_temp.assert_scr(p_condition boolean, p_name text) returns void
|
||||||
|
language plpgsql as $$
|
||||||
|
begin
|
||||||
|
if p_condition is distinct from true then raise exception 'FAIL: %', p_name; end if;
|
||||||
|
insert into scr_results values (p_name);
|
||||||
|
end;
|
||||||
|
$$;
|
||||||
|
|
||||||
|
-- -----------------------------------------------------------------------------
|
||||||
|
-- Fixtures: one active event, an admin, a plain attendee with a mix of points.
|
||||||
|
-- -----------------------------------------------------------------------------
|
||||||
|
update public.events set is_active = false where is_active;
|
||||||
|
insert into public.events (id, name, start_date, end_date, is_active)
|
||||||
|
values ('c7e10000-0000-0000-0000-000000000001', 'SCR regression', current_date, current_date, true);
|
||||||
|
|
||||||
|
insert into public.contacts (email, full_name, is_admin)
|
||||||
|
values ('[email protected]', 'SCR Admin', true),
|
||||||
|
('[email protected]', 'SCR User', false),
|
||||||
|
('[email protected]', 'SCR Other', false);
|
||||||
|
|
||||||
|
insert into public.registrations (id, event_id, contact_email, type, qr_token, checked_in, points)
|
||||||
|
values ('c7e10000-0000-0000-0000-000000000011', 'c7e10000-0000-0000-0000-000000000001', '[email protected]', 'attendee', 'SCRQR-1', true, 40),
|
||||||
|
('c7e10000-0000-0000-0000-000000000012', 'c7e10000-0000-0000-0000-000000000001', '[email protected]', 'attendee', 'SCRQR-2', true, 25);
|
||||||
|
|
||||||
|
-- 40 pts = 3x checkin (10+10+10) + connection 10 + feedback 10.
|
||||||
|
insert into public.points_log (event_id, contact_email, delta, category)
|
||||||
|
values ('c7e10000-0000-0000-0000-000000000001', '[email protected]', 10, 'checkin'),
|
||||||
|
('c7e10000-0000-0000-0000-000000000001', '[email protected]', 10, 'checkin'),
|
||||||
|
('c7e10000-0000-0000-0000-000000000001', '[email protected]', 10, 'checkin'),
|
||||||
|
('c7e10000-0000-0000-0000-000000000001', '[email protected]', 10, 'connection'),
|
||||||
|
('c7e10000-0000-0000-0000-000000000001', '[email protected]', 10, 'feedback');
|
||||||
|
-- Other attendee: only connection points, no checkin rows.
|
||||||
|
insert into public.points_log (event_id, contact_email, delta, category)
|
||||||
|
values ('c7e10000-0000-0000-0000-000000000001', '[email protected]', 25, 'connection');
|
||||||
|
|
||||||
|
-- Leaderboard mirrors the starting points (trigger keeps it in sync).
|
||||||
|
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 starts at 40');
|
||||||
|
|
||||||
|
-- -----------------------------------------------------------------------------
|
||||||
|
-- 1. Admin un-checks: check-in points removed, other categories untouched.
|
||||||
|
-- -----------------------------------------------------------------------------
|
||||||
|
select set_config('request.jwt.claims', '{"email":"[email protected]","role":"authenticated"}', true);
|
||||||
|
|
||||||
|
create temporary table scr_off as
|
||||||
|
select * from public.set_registration_checked_in(
|
||||||
|
'c7e10000-0000-0000-0000-000000000011', false);
|
||||||
|
select pg_temp.assert_scr(
|
||||||
|
(select checked_in = false and points_removed = 30 from scr_off),
|
||||||
|
'un-check-in reports 30 points removed');
|
||||||
|
select pg_temp.assert_scr(
|
||||||
|
(select checked_in = false from public.registrations where qr_token = 'SCRQR-1'),
|
||||||
|
'registration is unchecked');
|
||||||
|
select pg_temp.assert_scr(
|
||||||
|
(select points = 10 from public.registrations where qr_token = 'SCRQR-1'),
|
||||||
|
'registration keeps non-checkin points');
|
||||||
|
select pg_temp.assert_scr(
|
||||||
|
(select count(*) = 0 from public.points_log
|
||||||
|
where contact_email = '[email protected]' and category = 'checkin'),
|
||||||
|
'checkin rows deleted from the audit log');
|
||||||
|
select pg_temp.assert_scr(
|
||||||
|
(select count(*) = 2 from public.points_log
|
||||||
|
where contact_email = '[email protected]' and category in ('connection', 'feedback')),
|
||||||
|
'other categories keep their audit rows');
|
||||||
|
select pg_temp.assert_scr(
|
||||||
|
(select count(*) = 1 and coalesce(min(delta), 0) = -30 from public.points_log
|
||||||
|
where contact_email = '[email protected]' and category = 'admin'),
|
||||||
|
'reversal recorded as a single -30 admin row');
|
||||||
|
select pg_temp.assert_scr(
|
||||||
|
(select points = 10 from public.leaderboard
|
||||||
|
where event_id = 'c7e10000-0000-0000-0000-000000000001' and contact_email = '[email protected]'),
|
||||||
|
'leaderboard synced down to 10');
|
||||||
|
|
||||||
|
-- -----------------------------------------------------------------------------
|
||||||
|
-- 2. Un-checking someone with no checkin points: no-op, zero removed.
|
||||||
|
-- -----------------------------------------------------------------------------
|
||||||
|
create temporary table scr_off2 as
|
||||||
|
select * from public.set_registration_checked_in(
|
||||||
|
'c7e10000-0000-0000-0000-000000000012', false);
|
||||||
|
select pg_temp.assert_scr(
|
||||||
|
(select checked_in = false and points_removed = 0 from scr_off2),
|
||||||
|
'un-check-in without checkin points removes nothing');
|
||||||
|
select pg_temp.assert_scr(
|
||||||
|
(select points = 25 from public.registrations where qr_token = 'SCRQR-2'),
|
||||||
|
'other attendee keeps their points');
|
||||||
|
|
||||||
|
-- -----------------------------------------------------------------------------
|
||||||
|
-- 3. Re-checking does not re-award anything.
|
||||||
|
-- -----------------------------------------------------------------------------
|
||||||
|
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 pg_temp.assert_scr(
|
||||||
|
(select points = 10 from public.registrations where qr_token = 'SCRQR-1'),
|
||||||
|
'points unchanged after re-check-in');
|
||||||
|
|
||||||
|
-- -----------------------------------------------------------------------------
|
||||||
|
-- 4. Unknown registration id is rejected.
|
||||||
|
-- -----------------------------------------------------------------------------
|
||||||
|
do $$ begin
|
||||||
|
begin
|
||||||
|
perform * from public.set_registration_checked_in('c7e10000-0000-0000-0000-00000000dead', true);
|
||||||
|
raise exception 'missing registration allowed';
|
||||||
|
exception when raise_exception then
|
||||||
|
if sqlerrm <> 'registration not found' then raise; end if;
|
||||||
|
end;
|
||||||
|
perform pg_temp.assert_scr(true, 'unknown registration rejected');
|
||||||
|
end; $$;
|
||||||
|
|
||||||
|
-- -----------------------------------------------------------------------------
|
||||||
|
-- 5. Non-admin cannot call it (plain RAISE EXCEPTION → raise_exception).
|
||||||
|
-- -----------------------------------------------------------------------------
|
||||||
|
select set_config('request.jwt.claims', '{"email":"[email protected]","role":"authenticated"}', true);
|
||||||
|
do $$ begin
|
||||||
|
begin
|
||||||
|
perform * from public.set_registration_checked_in(
|
||||||
|
'c7e10000-0000-0000-0000-000000000011', false);
|
||||||
|
raise exception 'non-admin un-check-in allowed';
|
||||||
|
exception when raise_exception then
|
||||||
|
if sqlerrm <> 'admin only' then raise; end if;
|
||||||
|
end;
|
||||||
|
perform pg_temp.assert_scr(true, 'un-check-in is admin only');
|
||||||
|
end; $$;
|
||||||
|
|
||||||
|
-- -----------------------------------------------------------------------------
|
||||||
|
-- 6. The anonymous role cannot execute it at all.
|
||||||
|
-- -----------------------------------------------------------------------------
|
||||||
|
select set_config('request.jwt.claims', '{"role":"anon"}', true);
|
||||||
|
set local role anon;
|
||||||
|
do $$ begin
|
||||||
|
begin
|
||||||
|
perform * from public.set_registration_checked_in(
|
||||||
|
'c7e10000-0000-0000-0000-000000000011', false);
|
||||||
|
raise exception 'anon un-check-in allowed';
|
||||||
|
exception when insufficient_privilege then
|
||||||
|
null;
|
||||||
|
end;
|
||||||
|
perform pg_temp.assert_scr(true, 'anon cannot flip check-in');
|
||||||
|
end; $$;
|
||||||
|
reset role;
|
||||||
|
|
||||||
|
select count(*) as passed_assertions from scr_results;
|
||||||
|
rollback;
|
||||||
Reference in new issue
Block a user