diff --git a/supabase/migrations/20260909020000_appe_at_admin_trigger.sql b/supabase/migrations/20260909020000_appe_at_admin_trigger.sql new file mode 100644 index 0000000..3dd880d --- /dev/null +++ b/supabase/migrations/20260909020000_appe_at_admin_trigger.sql @@ -0,0 +1,35 @@ +-- ============================================================================= +-- Convivo — @appe.at contacts are admins automatically (trigger) +-- +-- Follow-up to 20260909010000 (which made is_admin() domain-aware + backfilled +-- existing rows). That backfill only ran once, so contacts created LATER (e.g. +-- by the Pretix sync) still had is_admin=false — the DB function treated them +-- as admins, but the admin UI's "admin" badge reads the flag, so they looked +-- non-admin. +-- +-- This adds a BEFORE INSERT OR UPDATE trigger that auto-sets is_admin=true +-- whenever a contact's email is on the @appe.at domain. Now the flag, the UI +-- badge, and is_admin() all agree, for every current + future contact. +-- ============================================================================= + +create or replace function public._set_appe_at_admin() +returns trigger +language plpgsql security definer set search_path = public as $$ +begin + if new.email like '%@appe.at' then + new.is_admin := true; + end if; + return new; +end; +$$; + +drop trigger if exists trg_contacts_appe_at_admin on public.contacts; +create trigger trg_contacts_appe_at_admin + before insert or update on public.contacts + for each row execute function public._set_appe_at_admin(); + +-- Re-apply the rule to any rows created since the last backfill (idempotent). +update public.contacts +set is_admin = true +where email like '%@appe.at' + and not is_admin;