Files
Convivo/supabase/migrations/20261020120000_staff_auto_connections.sql
3ee91b5ad6 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).

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]>
2026-10-20 22:15:29 +02:00

109 lines
5.4 KiB
PL/PgSQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- =============================================================================
-- 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;