feat(points): staff auto-connect never scores; event feedback pays +5

- Migration 20261020120000: staff (type staff / Admin / @appe.at) are
  auto-connected to every participant of the event via connections.auto
  rows inserted directly as accepted - never through accept_connection,
  so zero points by construction; backfill + idempotent helper + trigger.
- Migration 20261020130000: submit_event_feedback awards +5 on the first
  submission per participant x event (edits earn 0), gated by
  _points_eligible; response gains earned column; ACL restored.
- Admin: points move from Contacts to Registrations (active-event rows
  only, in-place save); Dashboard Top points drops emails; Dashboard and
  Activity hide auto connections.
- Attendee: leaderboard masks surnames (Max M.); EventFeedback toast
  shows +5 pts on paid submissions.
- Tests: staff_auto_connections_tests.sql (14 assertions), rpc_tests
  section 11 (4 assertions). Docs + release record 2026-10-20.
- README: document the git-ignored .env provisioning step (missing key
  = white screen).
This commit is contained in:
lago committed 2026-10-20 22:15:29 +02:00
1 parent b7c2abb73b
commit 35b88d50ca
22 files changed
+809 -166

No files matched your search

@@ -0,0 +1,108 @@
-- =============================================================================
-- Convivo — Staff are automatically connected to everyone (never for points)
--
-- Every staff member is an automatic connection for every other participant
-- of the same event. Rows are inserted straight into `connections` as
-- 'accepted' by a trigger on `registrations`; existing events are backfilled
-- at the bottom of this file. Three properties matter:
--
-- 1. NO POINTS ARE EVER AWARDED. The rows never pass through
-- accept_connection, so _award_points is never called for a staff
-- pairing — and the staff side is points-exempt by _points_eligible
-- anyway, so even a later manual path cannot score them.
-- 2. "Staff" mirrors the points-exemption set exactly: registration type
-- 'staff', Admin contacts, or an @appe.at address (the organiser
-- domain). Everyone else (attendees, speakers, sponsors) is "people".
-- Pairs are staff × people only — staff × staff pairs are not created.
-- 3. Rows are marked auto = true so the admin console can tell them apart
-- from people who actually connected (Dashboard activity feed and the
-- Activity view hide them; the attendee app shows them normally).
--
-- Idempotent by construction: the unique pair index turns every re-run into
-- a no-op, and an existing pair — including a manually rejected one — is
-- never touched. If a registration changes type, the trigger pairs the new
-- role; previously created rows are kept (deleting them could destroy a
-- genuine manual connection of the same pair).
-- =============================================================================
alter table public.connections add column if not exists auto boolean not null default false;
-- Full pairing pass for one event (idempotent). Used by the backfill below
-- and safe to re-run manually after a bulk import.
create or replace function public._autoconnect_staff_event(p_event_id uuid)
returns void
language plpgsql security definer set search_path = '' as $$
begin
insert into public.connections (event_id, from_email, to_email, status, resolved_at, auto)
select p_event_id, s.email, o.email, 'accepted', now(), true
from (
select r.contact_email as email
from public.registrations r
join public.contacts c on c.email = r.contact_email
where r.event_id = p_event_id
and (r."type" = 'staff' or c.is_admin or r.contact_email like '%@appe.at')
group by 1
) s
join (
select r.contact_email as email
from public.registrations r
join public.contacts c on c.email = r.contact_email
where r.event_id = p_event_id
and r."type" <> 'staff' and not c.is_admin and r.contact_email not like '%@appe.at'
) o on o.email <> s.email
on conflict (event_id, least(from_email, to_email), greatest(from_email, to_email)) do nothing;
end;
$$;
-- A new registration (or a type change) is paired with the opposite side of
-- its event immediately, in whichever direction it arrives: a staff row
-- pairs with every existing person, a person pairs with every existing
-- staff member. Multi-row INSERTs are safe — AFTER ROW triggers run once the
-- statement has landed, so rows of the same statement see each other.
create or replace function public._registrations_autoconnect_staff()
returns trigger
language plpgsql security definer set search_path = '' as $$
declare
v_staff boolean;
begin
-- The FK on registrations.contact_email guarantees the contact exists.
select (new."type" = 'staff' or c.is_admin or new.contact_email like '%@appe.at')
into v_staff
from public.contacts c
where c.email = new.contact_email;
if v_staff then
insert into public.connections (event_id, from_email, to_email, status, resolved_at, auto)
select new.event_id, new.contact_email, r.contact_email, 'accepted', now(), true
from public.registrations r
join public.contacts c on c.email = r.contact_email
where r.event_id = new.event_id
and r.contact_email <> new.contact_email
and r."type" <> 'staff' and not c.is_admin and r.contact_email not like '%@appe.at'
on conflict (event_id, least(from_email, to_email), greatest(from_email, to_email)) do nothing;
else
insert into public.connections (event_id, from_email, to_email, status, resolved_at, auto)
select new.event_id, r.contact_email, new.contact_email, 'accepted', now(), true
from public.registrations r
join public.contacts c on c.email = r.contact_email
where r.event_id = new.event_id
and r.contact_email <> new.contact_email
and (r."type" = 'staff' or c.is_admin or r.contact_email like '%@appe.at')
on conflict (event_id, least(from_email, to_email), greatest(from_email, to_email)) do nothing;
end if;
return new;
end;
$$;
drop trigger if exists registrations_autoconnect_staff on public.registrations;
create trigger registrations_autoconnect_staff
after insert or update of "type", event_id on public.registrations
for each row execute function public._registrations_autoconnect_staff();
-- Backfill every event already in the database.
select public._autoconnect_staff_event(e.id) from public.events e;
-- Internal helpers: never callable through the API (the trigger runs as the
-- function owner, postgres, which is what bypasses RLS on `connections`).
revoke all on function public._autoconnect_staff_event(uuid), public._registrations_autoconnect_staff()
from public, anon, authenticated;
@@ -0,0 +1,69 @@
-- =============================================================================
-- 20261020130000_event_feedback_points.sql
-- Event-level feedback is worth points: **+5 for the first submission**
-- (per participant × event). Re-submitting edits the existing row and awards
-- nothing, mirroring the session-feedback idempotency rule. Staff / Admin
-- contacts / @appe.at earn nothing — the same `_points_eligible` gate every
-- other award uses. Session feedback is unchanged (+5 rating only, +10 with
-- comment — already live since 20260824000000 / 20261015164546).
-- The response gains an `earned` column so the app can show "+5 pts".
-- =============================================================================
-- =============================================================================
-- The return type gains a column, which `create or replace` cannot do —
-- drop + recreate, then restore the exact ACL the original migrations left
-- (EXECUTE for PUBLIC + authenticated; no direct anon grant).
-- =============================================================================
drop function if exists public.submit_event_feedback(integer, text);
create function public.submit_event_feedback(p_rating integer, p_comment text)
returns table (feedback_id uuid, points integer, earned integer)
language plpgsql security definer set search_path = public as $$
declare
v_email text := public.current_email();
v_event_id uuid;
v_fb_id uuid;
v_points integer;
v_clean text;
v_exists boolean;
v_earned integer;
begin
if v_email is null then raise exception 'not authenticated'; end if;
if p_rating is null or p_rating < 1 or p_rating > 5 then
raise exception 'rating must be between 1 and 5';
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;
if not exists (select 1 from public.registrations
where event_id = v_event_id and contact_email = v_email) then
raise exception 'you must be registered for this event to give feedback';
end if;
v_clean := nullif(btrim(coalesce(p_comment, '')), '');
-- First submission only gets the award; an edit of an existing answer
-- (or an ineligible contact) earns nothing.
select exists (select 1 from public.event_feedback
where event_id = v_event_id and contact_email = v_email) into v_exists;
v_earned := case
when v_exists then 0
when not public._points_eligible(v_event_id, v_email) then 0
else 5
end;
insert into public.event_feedback (event_id, contact_email, rating, comment)
values (v_event_id, v_email, p_rating, v_clean)
on conflict (event_id, contact_email) do update
set rating = excluded.rating, comment = excluded.comment
returning id into v_fb_id;
if v_earned > 0 then
perform public._award_points(v_event_id, v_email, v_earned, 'feedback');
end if;
select r.points into v_points from public.registrations r
where r.event_id = v_event_id and r.contact_email = v_email;
return query select v_fb_id, coalesce(v_points, 0), v_earned;
end;
$$;
grant execute on function public.submit_event_feedback(integer, text) to public, authenticated;
+68
View File
@@ -441,6 +441,74 @@ exception when others then
end;
$$;
-- =============================================================================
-- 11. submit_event_feedback: first submission +5, re-submit idempotent,
-- ineligible contacts earn nothing.
-- Self-contained fixtures (seed-independent) — everything rolls back.
-- =============================================================================
do $$
declare
v_event uuid;
v_before integer;
v_after integer;
v_earned integer;
v_count integer;
v_ok boolean;
v_msg text;
begin
v_event := (select id from public.events where is_active limit 1);
if v_event is null then
insert into test_results values ('event feedback (11)', false, 'no active event');
return;
end if;
insert into public.contacts (email, full_name) values
('[email protected]', 'Effie Probe'),
('[email protected]', 'Stella Probe');
insert into public.registrations (event_id, contact_email, type) values
(v_event, '[email protected]', 'attendee'),
(v_event, '[email protected]', 'staff');
-- First submission (attendee) -> +5.
perform set_user('[email protected]');
select points into v_before from public.registrations
where contact_email = '[email protected]' and event_id = v_event;
select earned into v_earned from public.submit_event_feedback(4, 'Well organised');
select points into v_after from public.registrations
where contact_email = '[email protected]' and event_id = v_event;
v_ok := (v_earned = 5 and v_after = v_before + 5);
v_msg := format('expected earned=5/points=%s, got earned=%s points=%s', v_before + 5, v_earned, v_after);
insert into test_results values ('event feedback first submit earns +5', v_ok, case when v_ok then null else v_msg end);
-- Edit the answer: row updates, no second award.
select earned into v_earned from public.submit_event_feedback(5, 'Minor tweaks');
select points into v_after from public.registrations
where contact_email = '[email protected]' and event_id = v_event;
v_ok := (v_earned = 0 and v_after = v_before + 5);
v_msg := format('expected earned=0/points=%s, got earned=%s points=%s', v_before + 5, v_earned, v_after);
insert into test_results values ('event feedback edit earns nothing', v_ok, case when v_ok then null else v_msg end);
-- Exactly one +5 'feedback' points_log row.
select count(*) into v_count from public.points_log
where event_id = v_event and contact_email = '[email protected]'
and category = 'feedback' and delta = 5;
v_ok := (v_count = 1);
insert into test_results values ('event feedback logs one +5 row', v_ok,
case when v_ok then null else 'expected 1 points_log row, got ' || v_count end);
-- Staff registration -> eligible check fails, earns nothing, no log row.
perform set_user('[email protected]');
select earned into v_earned from public.submit_event_feedback(5, 'Staff view');
select count(*) into v_count from public.points_log
where event_id = v_event and contact_email = '[email protected]';
v_ok := (v_earned = 0 and v_count = 0);
v_msg := format('expected earned=0/log=0, got earned=%s log=%s', v_earned, v_count);
insert into test_results values ('event feedback staff earns nothing', v_ok, case when v_ok then null else v_msg end);
exception when others then
insert into test_results values ('event feedback (11)', false, 'error: ' || sqlerrm);
end;
$$;
-- =============================================================================
-- Summary (printed BEFORE the rollback, since the temp table is dropped by it)
-- =============================================================================
@@ -0,0 +1,162 @@
\set ON_ERROR_STOP on
begin;
-- Regression suite for 20261020120000_staff_auto_connections.sql.
-- Run against a database that already has the migration applied:
-- docker exec -i supabase_db_Convivo psql -X -U postgres -d postgres -v ON_ERROR_STOP=1 \
-- < supabase/tests/staff_auto_connections_tests.sql
-- Everything is created inside this transaction and rolled back at the end.
create temporary table staff_conn_results (name text);
create function pg_temp.assert_conn(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 staff_conn_results values (p_name);
end;
$$;
-- ---------------------------------------------------------------------------
-- Fixture: one disposable (inactive) event. Staff side = type 'staff', an
-- is_admin contact, or an @appe.at address; people side = everyone else.
-- ---------------------------------------------------------------------------
insert into public.events (id, name, start_date, end_date, is_active)
values ('a0170000-0000-0000-0000-000000000001', 'Staff autoconnect regression', current_date, current_date, false);
insert into public.contacts (email, full_name, is_admin)
values ('[email protected]', 'Staff One', false),
('[email protected]', 'Admin Person', true),
('[email protected]', 'Domain Person', false),
('[email protected]', 'Att One', false),
('[email protected]', 'Att Two', false),
('[email protected]', 'Speaker One', false);
insert into public.registrations (event_id, contact_email, type, qr_token, points)
values ('a0170000-0000-0000-0000-000000000001', '[email protected]', 'staff', 'SC-1', 0),
('a0170000-0000-0000-0000-000000000001', '[email protected]', 'attendee', 'SC-2', 0),
('a0170000-0000-0000-0000-000000000001', '[email protected]', 'attendee', 'SC-3', 0),
('a0170000-0000-0000-0000-000000000001', '[email protected]', 'attendee', 'SC-4', 0),
('a0170000-0000-0000-0000-000000000001', '[email protected]', 'attendee', 'SC-5', 0),
('a0170000-0000-0000-0000-000000000001', '[email protected]', 'speaker', 'SC-6', 0);
-- 3 staff-side registrations × 3 people = 9 auto pairs, all accepted+auto.
select pg_temp.assert_conn(
(select count(*) from public.connections
where event_id = 'a0170000-0000-0000-0000-000000000001'
and status = 'accepted' and auto and resolved_at is not null) = 9,
'staff x people pairs created by the trigger');
-- Every auto pair crosses the staff/people boundary exactly — no staff ×
-- staff and no people × people rows were ever created.
select pg_temp.assert_conn(
(with staff_side(e) as (values ('[email protected]'), ('[email protected]'), ('[email protected]')),
auto_rows(a, b) as (
select least(c.from_email, c.to_email), greatest(c.from_email, c.to_email)
from public.connections c
where c.event_id = 'a0170000-0000-0000-0000-000000000001' and c.auto)
select count(*) from auto_rows r
where (r.a in (select e from staff_side)) = (r.b in (select e from staff_side))) = 0,
'every auto pair is exactly one staff side and one person');
select pg_temp.assert_conn(
exists (select 1 from public.connections
where event_id = 'a0170000-0000-0000-0000-000000000001'
and from_email = '[email protected]' and to_email = '[email protected]'
and status = 'accepted' and auto),
'staff-to-attendee row stored accepted and auto');
-- ---------------------------------------------------------------------------
-- NO POINTS: the whole feature must never touch points or points_log.
-- ---------------------------------------------------------------------------
select pg_temp.assert_conn(
(select count(*) from public.points_log
where event_id = 'a0170000-0000-0000-0000-000000000001') = 0,
'auto-connect writes no points_log rows');
select pg_temp.assert_conn(
(select bool_and(points = 0) from public.registrations
where event_id = 'a0170000-0000-0000-0000-000000000001'),
'auto-connect leaves every points value at 0');
-- ---------------------------------------------------------------------------
-- Late arrivals pair in both directions.
-- ---------------------------------------------------------------------------
insert into public.contacts (email, full_name, is_admin) values ('[email protected]', 'Late Person', false);
insert into public.registrations (event_id, contact_email, type, qr_token, points)
values ('a0170000-0000-0000-0000-000000000001', '[email protected]', 'attendee', 'SC-7', 0);
select pg_temp.assert_conn(
(select count(*) from public.connections
where event_id = 'a0170000-0000-0000-0000-000000000001' and auto) = 12,
'a person registered after the staff pairs with all three staff');
insert into public.contacts (email, full_name, is_admin) values ('[email protected]', 'Staff Two', false);
insert into public.registrations (event_id, contact_email, type, qr_token, points)
values ('a0170000-0000-0000-0000-000000000001', '[email protected]', 'staff', 'SC-8', 0);
select pg_temp.assert_conn(
(select count(*) from public.connections
where event_id = 'a0170000-0000-0000-0000-000000000001' and auto) = 16,
'a staff member registered later pairs with all four people');
-- ---------------------------------------------------------------------------
-- Existing manual pairs are never touched.
-- ---------------------------------------------------------------------------
-- People x people stay connectable by hand and stay pending.
insert into public.connections (event_id, from_email, to_email, status)
values ('a0170000-0000-0000-0000-000000000001', '[email protected]', '[email protected]', 'pending');
-- A staff that was manually rejected before registering: the trigger must
-- not flip the rejection back to accepted (and must still pair the rest).
insert into public.contacts (email, full_name, is_admin) values ('[email protected]', 'Staff Three', false);
insert into public.connections (event_id, from_email, to_email, status, resolved_at)
values ('a0170000-0000-0000-0000-000000000001', '[email protected]', '[email protected]', 'rejected', now());
insert into public.registrations (event_id, contact_email, type, qr_token, points)
values ('a0170000-0000-0000-0000-000000000001', '[email protected]', 'staff', 'SC-9', 0);
select public._autoconnect_staff_event('a0170000-0000-0000-0000-000000000001'); -- idempotent re-run
select pg_temp.assert_conn(
(select count(*) from public.connections
where event_id = 'a0170000-0000-0000-0000-000000000001') = 21,
'backfill re-run inserts nothing new (19 auto + pending + rejected)');
select pg_temp.assert_conn(
(select count(*) from public.connections
where event_id = 'a0170000-0000-0000-0000-000000000001' and auto) = 19,
'late staff pairs with three people and skips the rejected pair');
select pg_temp.assert_conn(
exists (select 1 from public.connections
where event_id = 'a0170000-0000-0000-0000-000000000001'
and least(from_email, to_email) = '[email protected]'
and greatest(from_email, to_email) = '[email protected]'
and status = 'pending'),
'manual pending people-pair untouched by the backfill');
select pg_temp.assert_conn(
exists (select 1 from public.connections
where event_id = 'a0170000-0000-0000-0000-000000000001'
and least(from_email, to_email) = '[email protected]'
and greatest(from_email, to_email) = '[email protected]'
and status = 'rejected'),
'manually rejected staff pair is not re-accepted');
-- ---------------------------------------------------------------------------
-- Type change: attendee becomes staff and pairs with the remaining people.
-- ---------------------------------------------------------------------------
update public.registrations set "type" = 'staff'
where event_id = 'a0170000-0000-0000-0000-000000000001' and contact_email = '[email protected]';
select pg_temp.assert_conn(
exists (select 1 from public.connections
where event_id = 'a0170000-0000-0000-0000-000000000001'
and from_email = '[email protected]' and to_email = '[email protected]'
and status = 'accepted' and auto),
'type flip to staff creates the new staff-side pairs');
select pg_temp.assert_conn(
(select count(*) from public.points_log
where event_id = 'a0170000-0000-0000-0000-000000000001') = 0,
'type flip still awards no points');
-- ---------------------------------------------------------------------------
-- Helpers stay internal.
-- ---------------------------------------------------------------------------
select pg_temp.assert_conn(
not has_function_privilege('authenticated', 'public._autoconnect_staff_event(uuid)', 'execute')
and not has_function_privilege('anon', 'public._registrations_autoconnect_staff()', 'execute'),
'API roles cannot call the autoconnect helpers');
select count(*) as passed_assertions from staff_conn_results;
rollback;