Registration optionset + speaker auto-promote + welcome check-in

- registrations.type is now a Postgres ENUM (registration_type)
- session_speakers trigger auto-promotes attendee -> speaker (never overrides staff/sponsor)
- new welcome_checkin RPC: staff marks an attendee present at the event
- admin Registrations: QR token is final (no reset); failed sponsor change opens in-place sponsor lookup
- Convivo Home: Now/Next cards always render; red pending-material pill + popup for speakers
- Convivo Agenda: speakers can delete their material to re-upload
- Convivo Scan: staff Session/Welcome check-in toggle (uiverse switch)
- seed: staff (Bruno/Rita) get a password so staff QR login works after reset
This commit is contained in:
Lago committed 2026-08-23 21:33:44 +02:00
1 parent 1bffae9777
commit 1cbbd1b529
16 files changed
+730 -142

No files matched your search

@@ -0,0 +1,164 @@
-- =============================================================================
-- Convivo — Registration type optionset + speaker auto-promote + welcome check-in
--
-- 1. `registrations.type` becomes a Postgres ENUM (`registration_type`) — a true
-- optionset of the four ticket types, enforced at the database level.
-- 2. A trigger auto-promotes a contact's registration from 'attendee' to
-- 'speaker' when they're added to `session_speakers` (never overrides
-- staff/sponsor — those are explicit ticket types).
-- 3. New `welcome_checkin` RPC: staff scan an attendee's lanyard to mark them
-- checked in to the EVENT (registrations.checked_in = true). Distinct from
-- `checkin_attendee`, which checks someone into a specific session.
-- =============================================================================
-- -----------------------------------------------------------------------------
-- 1. REGISTRATION TYPE OPTIONSET (Postgres ENUM)
-- -----------------------------------------------------------------------------
do $$
begin
if not exists (select 1 from pg_type where typname = 'registration_type') then
create type public.registration_type as enum ('attendee', 'speaker', 'staff', 'sponsor');
end if;
end $$;
-- Convert the column to the enum. Existing values ('attendee'/'speaker'/'staff'/
-- 'sponsor') all map cleanly. The `type` column name is quoted to avoid any
-- keyword ambiguity in the ALTER; the USING clause does the explicit text→enum
-- cast (Postgres won't cast text to an enum implicitly). The old text CHECK
-- constraint and text default can't be auto-cast, so drop both first, convert,
-- then set the enum default.
alter table public.registrations drop constraint if exists registrations_type_check;
alter table public.registrations alter column "type" drop default;
alter table public.registrations
alter column "type" type public.registration_type using "type"::public.registration_type;
alter table public.registrations
alter column "type" set default 'attendee'::public.registration_type;
-- The admin RPCs take `p_type text` and write it to the (now enum) column.
-- Re-create them with an explicit cast so the text param maps to the enum.
create or replace function public.create_registration(p_contact_email text, p_type text)
returns public.registrations
language plpgsql security definer set search_path = public as $$
declare
v_email text := public.current_email();
v_event_id uuid;
v_reg public.registrations;
begin
if v_email is null then raise exception 'not authenticated'; end if;
if not public.is_admin() then raise exception 'admin only'; end if;
if not exists (select 1 from public.contacts where email = p_contact_email) then
raise exception 'contact not found';
end if;
if p_type is null or p_type not in ('attendee', 'speaker', 'staff', 'sponsor') then
raise exception 'invalid registration type';
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 p_type = 'sponsor' and not exists (
select 1 from public.sponsors where event_id = v_event_id and contact_email = p_contact_email
) then
raise exception 'a sponsor registration requires a sponsor lookup';
end if;
insert into public.registrations (event_id, contact_email, type)
values (v_event_id, p_contact_email, p_type::public.registration_type)
on conflict (event_id, contact_email) do update set type = excluded.type
returning * into v_reg;
return v_reg;
end;
$$;
create or replace function public.set_registration_type(p_contact_email text, p_type text)
returns public.registrations
language plpgsql security definer set search_path = public as $$
declare
v_email text := public.current_email();
v_event_id uuid;
v_reg public.registrations;
begin
if v_email is null then raise exception 'not authenticated'; end if;
if not public.is_admin() then raise exception 'admin only'; end if;
if p_type is null or p_type not in ('attendee', 'speaker', 'staff', 'sponsor') then
raise exception 'invalid registration type';
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 p_type = 'sponsor' and not exists (
select 1 from public.sponsors where event_id = v_event_id and contact_email = p_contact_email
) then
raise exception 'a sponsor registration requires a sponsor lookup';
end if;
update public.registrations set type = p_type::public.registration_type
where event_id = v_event_id and contact_email = p_contact_email
returning * into v_reg;
if not found then raise exception 'registration not found'; end if;
return v_reg;
end;
$$;
-- -----------------------------------------------------------------------------
-- 2. SPEAKER AUTO-PROMOTE TRIGGER
-- -----------------------------------------------------------------------------
-- When a contact is added as a speaker of a session, promote their active-event
-- registration from 'attendee' to 'speaker'. This is one-way: removing a speaker
-- never demotes them, and it never overrides an explicit staff/sponsor ticket.
create or replace function public._trg_speaker_promote() returns trigger
language plpgsql security definer set search_path = public as $$
declare
v_event_id uuid;
begin
if tg_op = 'INSERT' then
select s.event_id into v_event_id
from public.sessions s where s.id = new.session_id;
if v_event_id is not null then
update public.registrations
set "type" = 'speaker'
where event_id = v_event_id
and contact_email = new.contact_email
and "type" = 'attendee';
end if;
end if;
return new;
end;
$$;
drop trigger if exists session_speakers_speaker_promote on public.session_speakers;
create trigger session_speakers_speaker_promote
after insert on public.session_speakers
for each row execute function public._trg_speaker_promote();
-- -----------------------------------------------------------------------------
-- 3. WELCOME CHECK-IN (staff marks an attendee present at the event)
-- -----------------------------------------------------------------------------
-- Resolves the scanned lanyard QR to the attendee's active-event registration and
-- marks them checked in to the event. No points are awarded (that's the session
-- check-in's job); this just records that the person is on site.
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;
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 into v_attendee, v_name
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;
return query select v_attendee, v_name, coalesce(v_checked, false);
end;
$$;
grant execute on function public.welcome_checkin(text) to authenticated;