-- ============================================================================= -- Convivo — Bulk contact delete (admin) that bypasses the session-speaker guard -- -- The admin "Delete all contacts for this event" action deletes a batch of -- contacts in one PostgREST call. Deleting a contact cascades to their -- session_speakers rows, which fires the `session_needs_speaker` BEFORE DELETE -- trigger — and that trigger raises "a session must have at least one speaker" -- when the contact is the sole speaker of a session. So a bulk wipe that -- includes a lone speaker fails with P0001. -- -- This adds an admin-only RPC that deletes the batch in a single transaction -- with the guard temporarily disabled (the "at least one speaker" rule is a -- data-integrity nicety for single edits; a deliberate bulk wipe is allowed to -- leave a session speaker-less). The caller's own email is excluded by the app -- (never passed in), so the admin can't delete themselves. -- ============================================================================= create or replace function public.bulk_delete_contacts(p_emails text[]) returns integer language plpgsql security definer set search_path = public as $$ declare v_deleted integer; begin if auth.uid() is null then raise exception 'not authenticated'; end if; if not public.is_admin() then raise exception 'admin only'; end if; if p_emails is null or array_length(p_emails, 1) is null then raise exception 'no emails provided'; end if; -- Disable the guard for the duration of this transaction, then delete the -- batch (cascades to registrations, session_speakers, checkins, connections, -- feedback, leaderboard, points_log, preferences, sponsors). perform public._disable_session_speaker_guard(); delete from public.contacts where email = any (p_emails); get diagnostics v_deleted = row_count; perform public._enable_session_speaker_guard(); return v_deleted; end; $$; -- The session_needs_speaker trigger is a normal (non-deferrable) trigger, so we -- toggle it with a GUC the trigger function checks. This keeps the trigger -- object intact (no drop/create) and is safe to call from a security-definer -- function. create or replace function public._disable_session_speaker_guard() returns void language sql security definer set search_path = public as $$ select set_config('convivo.session_speaker_guard', 'off', false); $$; create or replace function public._enable_session_speaker_guard() returns void language sql security definer set search_path = public as $$ select set_config('convivo.session_speaker_guard', 'on', false); $$; -- Re-create the guard function to honor the GUC (off = skip the check). create or replace function public._trg_session_needs_speaker() returns trigger language plpgsql security definer set search_path = public as $$ begin if coalesce(current_setting('convivo.session_speaker_guard', true), 'on') = 'off' then return old; end if; if exists (select 1 from public.sessions where id = old.session_id) and (select count(*) from public.session_speakers where session_id = old.session_id) <= 1 then raise exception 'a session must have at least one speaker'; end if; return old; end; $$; grant execute on function public.bulk_delete_contacts(text[]) to authenticated;