Files
Convivo/supabase/migrations/20260910010000_bulk_delete_contacts_rpc.sql
0b6d1f2b6f Post-event reporting + talk networking incentives
- Talk networking: per-event talk_networking_bonus awarded when two people
  who shared a checked-in talk accept a connection (accept_connection
  re-created, shared_talk flag, get_talk_cohort RPC, points_log category
  check widened); Convivo Networking sub-tab inside the Connect screen
  (Home quick action deep-links in; Connect button reflects existing
  connection state and is disabled for sent/connected/declined); admin
  Events view bonus input (0-10).
- Reports: sidecar POST /export (CSV / multi-sheet XLSX / PDF report via
  new pdfkit dep); APPE Admin Reports view (any event: on-screen summary,
  session feedback summary, top-5, CSV/Excel/PDF downloads); Activity
  session-feedback per-session aggregate header.
- Also carried: @appe.at admin trigger + default password, bulk contact
  delete, session level/area + event-level feedback, agenda level/area
  filters, admin password self-service, schedule level/area fields.
- 18/18 RPC tests; both apps build; exports verified end-to-end.

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-09-21 22:34:25 +02:00

74 lines
3.2 KiB
PL/PgSQL

-- =============================================================================
-- 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;