diff --git a/supabase/migrations/20260909030000_admin_registration_staff.sql b/supabase/migrations/20260909030000_admin_registration_staff.sql new file mode 100644 index 0000000..077641d --- /dev/null +++ b/supabase/migrations/20260909030000_admin_registration_staff.sql @@ -0,0 +1,47 @@ +-- ============================================================================= +-- Convivo — admin registrations are always `staff` (no exception) +-- +-- Business rule: if a contact is an admin (is_admin flag OR @appe.at domain — +-- the same rule as public.is_admin()), then their registration for ANY event +-- must always be type `staff` (staff is merged into admin in this app). No +-- exception. +-- +-- Enforced at the DB transaction level via a BEFORE INSERT OR UPDATE trigger on +-- public.registrations, so it applies to every write path — the Pretix sync +-- RPC, the Sessionize sync, manual admin edits, direct Postgres, etc. +-- +-- Also backfills the existing rows so current admin registrations are corrected +-- immediately. +-- ============================================================================= + +create or replace function public._admin_registration_staff() +returns trigger +language plpgsql security definer set search_path = public as $$ +begin + -- If the registrant is an admin (flag or @appe.at domain), force the + -- registration type to `staff`. No exception. + if exists ( + select 1 from public.contacts + where email = new.contact_email + and (is_admin or email like '%@appe.at') + ) then + new.type := 'staff'::public.registration_type; + end if; + return new; +end; +$$; + +drop trigger if exists trg_registrations_admin_staff on public.registrations; +create trigger trg_registrations_admin_staff + before insert or update on public.registrations + for each row execute function public._admin_registration_staff(); + +-- Backfill: correct any existing admin registrations (idempotent). +update public.registrations r +set type = 'staff'::public.registration_type +where r.type <> 'staff'::public.registration_type + and exists ( + select 1 from public.contacts c + where c.email = r.contact_email + and (c.is_admin or c.email like '%@appe.at') + );