- Phase 3: new migration 20260824000000_redesign.sql (contacts/events/registrations/sponsors, slot types, session speakers + feedback/material), seed rewrite - Phase 4: APPE Admin rebrand + Contacts/Registrations/Sponsors/Schedule/Activity/Events views - Phase 5: Convivo App (24h login, 5s polling, QR type badge, red dot, speaker material) - Phase 6: APPE favicon + PWA icons for both apps - Docs: schema/rpcs/rls/seed/screens/api-client/admin-app/architecture/next-steps/README/roadmap updated 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]>
1100 lines
51 KiB
PL/PgSQL
1100 lines
51 KiB
PL/PgSQL
-- =============================================================================
|
|
-- Convivo — Phase 3 (Redesign): multi-event data model
|
|
--
|
|
-- Replaces the `profiles`-based single-event model with a multi-event model:
|
|
-- contacts / events / registrations / sponsors (+ slot types, session
|
|
-- speakers, session feedback/material fields).
|
|
--
|
|
-- Key decisions (locked with the user):
|
|
-- * Staff = admin (merged). A contact has `is_admin` + a QR token (its login
|
|
-- password). `is_admin` grants both staff powers (check-in) AND APPE Admin
|
|
-- access.
|
|
-- * contacts is keyed by `email` (natural key, mirrors auth.users.email).
|
|
-- * Points are kept, scoped per event, keyed on the registration.
|
|
-- * Event aggregates are maintained columns (triggers + feedback RPC).
|
|
-- * AI feedback is a stub this phase (field + state machine + admin RPC).
|
|
-- =============================================================================
|
|
create extension if not exists "pgcrypto";
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 1. DROP OLD TABLES FIRST (cascades RLS policies, indexes, FKs).
|
|
-- Policies depend on the helper functions, so the tables must go before the
|
|
-- functions can be dropped.
|
|
-- -----------------------------------------------------------------------------
|
|
drop table if exists public.points_log cascade;
|
|
drop table if exists public.leaderboard cascade;
|
|
drop table if exists public.feedback cascade;
|
|
drop table if exists public.checkins cascade;
|
|
drop table if exists public.connections cascade;
|
|
drop table if exists public.preferences cascade;
|
|
drop table if exists public.sessions cascade;
|
|
drop table if exists public.slots cascade;
|
|
drop table if exists public.profiles cascade;
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 2. DROP OLD FUNCTIONS (they reference `profiles`)
|
|
-- -----------------------------------------------------------------------------
|
|
drop function if exists public.get_public_profiles();
|
|
drop function if exists public.lanyard_lookup(text);
|
|
drop function if exists public.set_preference(uuid);
|
|
drop function if exists public.clear_preference(integer);
|
|
drop function if exists public.create_connection_request(text);
|
|
drop function if exists public.accept_connection(uuid);
|
|
drop function if exists public.reject_connection(uuid);
|
|
drop function if exists public.checkin_attendee(uuid, text);
|
|
drop function if exists public.submit_feedback(uuid, integer, text);
|
|
drop function if exists public.adjust_points(uuid, integer, text);
|
|
drop function if exists public.get_agenda();
|
|
drop function if exists public.is_admin();
|
|
drop function if exists public.is_staff_or_admin();
|
|
drop function if exists public._sync_leaderboard(uuid);
|
|
drop function if exists public._award_points(uuid, integer, text);
|
|
drop function if exists public.reset_qr_token(uuid);
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 3. NEW TABLES
|
|
-- -----------------------------------------------------------------------------
|
|
|
|
-- Source of truth for people. PK = email (natural key, mirrors auth.users).
|
|
-- `qr_token` is the lanyard token; it doubles as the Auth password for the
|
|
-- QR + email login flow (no email is ever sent).
|
|
create table public.contacts (
|
|
email text primary key,
|
|
full_name text not null,
|
|
linkedin_url text,
|
|
show_email boolean not null default false,
|
|
show_linkedin boolean not null default false,
|
|
is_admin boolean not null default false,
|
|
qr_token text unique,
|
|
created_at timestamptz not null default now()
|
|
);
|
|
|
|
-- A conference. At most one active at a time (partial unique index + trigger).
|
|
create table public.events (
|
|
id uuid primary key default gen_random_uuid(),
|
|
name text not null,
|
|
start_date date not null,
|
|
end_date date not null,
|
|
location text,
|
|
is_active boolean not null default false,
|
|
-- Maintained aggregates (kept in sync by triggers + the feedback RPC).
|
|
avg_feedback numeric,
|
|
num_sessions integer not null default 0,
|
|
num_speakers integer not null default 0,
|
|
num_registrations integer not null default 0,
|
|
num_checked_in_registrations integer not null default 0,
|
|
created_at timestamptz not null default now()
|
|
);
|
|
create unique index events_one_active on public.events (is_active) where is_active;
|
|
|
|
-- A contact's participation in an event. Points are scoped here (per event).
|
|
create table public.registrations (
|
|
id uuid primary key default gen_random_uuid(),
|
|
event_id uuid not null references public.events (id) on delete cascade,
|
|
contact_email text not null references public.contacts (email) on delete cascade,
|
|
type text not null default 'attendee'
|
|
check (type in ('attendee', 'speaker', 'staff', 'sponsor')),
|
|
checked_in boolean not null default false,
|
|
points integer not null default 0 check (points >= 0),
|
|
created_at timestamptz not null default now(),
|
|
unique (event_id, contact_email)
|
|
);
|
|
|
|
-- A sponsor for an event. Unpaid sponsors are hidden from non-admins (RLS).
|
|
create table public.sponsors (
|
|
id uuid primary key default gen_random_uuid(),
|
|
event_id uuid not null references public.events (id) on delete cascade,
|
|
type text not null
|
|
check (type in ('headline', 'platinum', 'gold', 'silver',
|
|
'community', 'swag', 'catering', 'diversity')),
|
|
name text not null,
|
|
email text,
|
|
phone text,
|
|
paid boolean not null default false,
|
|
amount_eur numeric,
|
|
contact_email text references public.contacts (email) on delete set null,
|
|
created_at timestamptz not null default now()
|
|
);
|
|
|
|
-- A time block in an event.
|
|
create table public.slots (
|
|
id integer primary key,
|
|
event_id uuid not null references public.events (id) on delete cascade,
|
|
type text not null default 'session'
|
|
check (type in ('session', 'lunch_break', 'coffee_break',
|
|
'keynote', 'close_up')),
|
|
name text not null,
|
|
slot_date date not null,
|
|
start_time time not null,
|
|
end_time time not null
|
|
);
|
|
|
|
-- A talk within a slot.
|
|
create table public.sessions (
|
|
id uuid primary key default gen_random_uuid(),
|
|
event_id uuid not null references public.events (id) on delete cascade,
|
|
slot_id integer not null references public.slots (id) on delete cascade,
|
|
title text not null,
|
|
description text,
|
|
room text,
|
|
capacity integer,
|
|
average_feedback numeric,
|
|
ai_generated_feedback text,
|
|
material_path text,
|
|
created_at timestamptz not null default now()
|
|
);
|
|
|
|
-- M:N between sessions and contacts (a session must have >= 1 speaker).
|
|
create table public.session_speakers (
|
|
session_id uuid not null references public.sessions (id) on delete cascade,
|
|
contact_email text not null references public.contacts (email) on delete cascade,
|
|
primary key (session_id, contact_email)
|
|
);
|
|
|
|
-- One preferred session per slot per contact per event.
|
|
create table public.preferences (
|
|
id uuid primary key default gen_random_uuid(),
|
|
event_id uuid not null references public.events (id) on delete cascade,
|
|
contact_email text not null references public.contacts (email) on delete cascade,
|
|
session_id uuid not null references public.sessions (id) on delete cascade,
|
|
slot_id integer not null,
|
|
created_at timestamptz not null default now(),
|
|
unique (event_id, contact_email, slot_id)
|
|
);
|
|
|
|
-- The custom M:N networking relationship, scoped to an event.
|
|
create table public.connections (
|
|
id uuid primary key default gen_random_uuid(),
|
|
event_id uuid not null references public.events (id) on delete cascade,
|
|
from_email text not null references public.contacts (email) on delete cascade,
|
|
to_email text not null references public.contacts (email) on delete cascade,
|
|
status text not null default 'pending'
|
|
check (status in ('pending', 'accepted', 'rejected')),
|
|
created_at timestamptz not null default now(),
|
|
resolved_at timestamptz
|
|
);
|
|
create unique index connections_pair_uniq
|
|
on public.connections (event_id, least(from_email, to_email), greatest(from_email, to_email));
|
|
|
|
-- Staff check-in of a contact into a session.
|
|
create table public.checkins (
|
|
id uuid primary key default gen_random_uuid(),
|
|
event_id uuid not null references public.events (id) on delete cascade,
|
|
contact_email text not null references public.contacts (email) on delete cascade,
|
|
session_id uuid not null references public.sessions (id) on delete cascade,
|
|
staff_email text not null references public.contacts (email) on delete cascade,
|
|
created_at timestamptz not null default now(),
|
|
unique (event_id, contact_email, session_id)
|
|
);
|
|
|
|
-- Post-session feedback.
|
|
create table public.feedback (
|
|
id uuid primary key default gen_random_uuid(),
|
|
event_id uuid not null references public.events (id) on delete cascade,
|
|
contact_email text not null references public.contacts (email) on delete cascade,
|
|
session_id uuid not null references public.sessions (id) on delete cascade,
|
|
rating integer not null check (rating between 1 and 5),
|
|
comment text,
|
|
created_at timestamptz not null default now(),
|
|
unique (event_id, contact_email, session_id)
|
|
);
|
|
|
|
-- Public, Realtime-friendly mirror of points per event (live leaderboard).
|
|
create table public.leaderboard (
|
|
event_id uuid not null references public.events (id) on delete cascade,
|
|
contact_email text not null references public.contacts (email) on delete cascade,
|
|
full_name text not null,
|
|
points integer not null default 0,
|
|
updated_at timestamptz not null default now(),
|
|
primary key (event_id, contact_email)
|
|
);
|
|
|
|
-- Audit trail powering the per-category points breakdown (per event).
|
|
create table public.points_log (
|
|
id uuid primary key default gen_random_uuid(),
|
|
event_id uuid not null references public.events (id) on delete cascade,
|
|
contact_email text not null references public.contacts (email) on delete cascade,
|
|
delta integer not null,
|
|
category text not null
|
|
check (category in ('checkin', 'connection', 'feedback', 'admin')),
|
|
created_at timestamptz not null default now()
|
|
);
|
|
|
|
create index sessions_event_id_idx on public.sessions (event_id);
|
|
create index sessions_slot_id_idx on public.sessions (slot_id);
|
|
create index slots_event_id_idx on public.slots (event_id);
|
|
create index registrations_event_id_idx on public.registrations (event_id);
|
|
create index checkins_session_id_idx on public.checkins (session_id);
|
|
create index points_log_event_id_idx on public.points_log (event_id);
|
|
create index contacts_qr_token_idx on public.contacts (qr_token);
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 4. HELPER FUNCTIONS
|
|
-- -----------------------------------------------------------------------------
|
|
|
|
-- The current user's email (contacts is keyed by email).
|
|
create or replace function public.current_email() returns text
|
|
language sql stable as $$
|
|
select auth.jwt() ->> 'email';
|
|
$$;
|
|
|
|
create or replace function public.is_admin() returns boolean
|
|
language sql stable security definer set search_path = public as $$
|
|
select exists (select 1 from public.contacts
|
|
where email = auth.jwt() ->> 'email' and is_admin);
|
|
$$;
|
|
|
|
-- Staff is merged into admin: the check-in guard is `is_admin`.
|
|
create or replace function public.is_staff_or_admin() returns boolean
|
|
language sql stable security definer set search_path = public as $$
|
|
select public.is_admin();
|
|
$$;
|
|
|
|
-- For the `materials` storage bucket: session speakers + admins can write.
|
|
-- A "speaker" here means a contact assigned to at least one session of the
|
|
-- active event (via session_speakers — matches the app's getMySpeakerSessions),
|
|
-- or a contact whose ticket type is 'speaker', or an admin.
|
|
create or replace function public.is_speaker_or_admin() returns boolean
|
|
language sql stable security definer set search_path = public as $$
|
|
select public.is_admin()
|
|
or exists (
|
|
select 1 from public.session_speakers ss
|
|
join public.sessions s on s.id = ss.session_id
|
|
where ss.contact_email = auth.jwt() ->> 'email'
|
|
and s.event_id = (select id from public.events where is_active limit 1)
|
|
)
|
|
or exists (
|
|
select 1 from public.registrations r
|
|
join public.events e on e.id = r.event_id
|
|
where r.contact_email = auth.jwt() ->> 'email'
|
|
and r.type = 'speaker'
|
|
and e.is_active
|
|
);
|
|
$$;
|
|
|
|
-- The single active event (or no rows if none).
|
|
create or replace function public.get_active_event()
|
|
returns public.events
|
|
language sql stable security definer set search_path = public as $$
|
|
select * from public.events where is_active limit 1;
|
|
$$;
|
|
|
|
-- Recompute all maintained aggregates for an event.
|
|
create or replace function public._recalc_event_aggregates(p_event_id uuid) returns void
|
|
language plpgsql security definer set search_path = public as $$
|
|
begin
|
|
update public.events e set
|
|
num_sessions = (select count(*) from public.sessions s where s.event_id = p_event_id),
|
|
num_speakers = (select count(distinct ss.contact_email)
|
|
from public.session_speakers ss
|
|
join public.sessions s on s.id = ss.session_id
|
|
where s.event_id = p_event_id),
|
|
num_registrations = (select count(*) from public.registrations r where r.event_id = p_event_id),
|
|
num_checked_in_registrations = (select count(*) from public.registrations r
|
|
where r.event_id = p_event_id and r.checked_in),
|
|
avg_feedback = (select avg(rating) from public.feedback f where f.event_id = p_event_id)
|
|
where e.id = p_event_id;
|
|
end;
|
|
$$;
|
|
|
|
-- Keep the leaderboard mirror in sync for one (event, contact).
|
|
create or replace function public._sync_leaderboard(p_event_id uuid, p_contact_email text) returns void
|
|
language plpgsql security definer set search_path = public as $$
|
|
begin
|
|
insert into public.leaderboard (event_id, contact_email, full_name, points)
|
|
select p_event_id, c.email, c.full_name, r.points
|
|
from public.contacts c
|
|
join public.registrations r on r.contact_email = c.email and r.event_id = p_event_id
|
|
where c.email = p_contact_email
|
|
on conflict (event_id, contact_email) do update
|
|
set full_name = excluded.full_name,
|
|
points = excluded.points,
|
|
updated_at = now();
|
|
end;
|
|
$$;
|
|
|
|
-- Transactional points award: registration + audit log + leaderboard mirror.
|
|
create or replace function public._award_points(p_event_id uuid, p_contact_email text, p_delta integer, p_category text) returns void
|
|
language plpgsql security definer set search_path = public as $$
|
|
begin
|
|
update public.registrations
|
|
set points = greatest(0, points + p_delta)
|
|
where event_id = p_event_id and contact_email = p_contact_email;
|
|
insert into public.points_log (event_id, contact_email, delta, category)
|
|
values (p_event_id, p_contact_email, p_delta, p_category);
|
|
perform public._sync_leaderboard(p_event_id, p_contact_email);
|
|
end;
|
|
$$;
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 5. TRIGGERS (maintain event aggregates + invariants)
|
|
-- -----------------------------------------------------------------------------
|
|
|
|
-- When an event is set active, deactivate all others (single-active invariant).
|
|
create or replace function public._trg_event_single_active() returns trigger
|
|
language plpgsql security definer set search_path = public as $$
|
|
begin
|
|
if new.is_active then
|
|
update public.events set is_active = false where id <> new.id and is_active;
|
|
end if;
|
|
return new;
|
|
end;
|
|
$$;
|
|
create trigger event_single_active
|
|
before update on public.events
|
|
for each row execute function public._trg_event_single_active();
|
|
|
|
-- Recompute aggregates when registrations change (count + checked-in).
|
|
create or replace function public._trg_registrations_agg() returns trigger
|
|
language plpgsql security definer set search_path = public as $$
|
|
begin
|
|
perform public._recalc_event_aggregates(coalesce(new.event_id, old.event_id));
|
|
return null;
|
|
end;
|
|
$$;
|
|
create trigger registrations_agg
|
|
after insert or update or delete on public.registrations
|
|
for each row execute function public._trg_registrations_agg();
|
|
|
|
-- Recompute aggregates when sessions change (count).
|
|
create or replace function public._trg_sessions_agg() returns trigger
|
|
language plpgsql security definer set search_path = public as $$
|
|
begin
|
|
perform public._recalc_event_aggregates(coalesce(new.event_id, old.event_id));
|
|
return null;
|
|
end;
|
|
$$;
|
|
create trigger sessions_agg
|
|
after insert or update or delete on public.sessions
|
|
for each row execute function public._trg_sessions_agg();
|
|
|
|
-- Recompute aggregates when session speakers change (distinct speaker count).
|
|
create or replace function public._trg_session_speakers_agg() returns trigger
|
|
language plpgsql security definer set search_path = public as $$
|
|
declare v_event_id uuid;
|
|
begin
|
|
select event_id into v_event_id
|
|
from public.sessions where id = coalesce(new.session_id, old.session_id);
|
|
if v_event_id is not null then
|
|
perform public._recalc_event_aggregates(v_event_id);
|
|
end if;
|
|
return null;
|
|
end;
|
|
$$;
|
|
create trigger session_speakers_agg
|
|
after insert or update or delete on public.session_speakers
|
|
for each row execute function public._trg_session_speakers_agg();
|
|
|
|
-- Recompute the session average + event aggregates when feedback changes.
|
|
create or replace function public._trg_feedback_agg() returns trigger
|
|
language plpgsql security definer set search_path = public as $$
|
|
declare v_session_id uuid; v_event_id uuid;
|
|
begin
|
|
select session_id into v_session_id from (
|
|
select coalesce(new.session_id, old.session_id) as session_id
|
|
) x;
|
|
select event_id into v_event_id from public.sessions where id = v_session_id;
|
|
if v_session_id is not null then
|
|
update public.sessions set
|
|
average_feedback = (select avg(rating) from public.feedback where session_id = v_session_id)
|
|
where id = v_session_id;
|
|
end if;
|
|
if v_event_id is not null then
|
|
perform public._recalc_event_aggregates(v_event_id);
|
|
end if;
|
|
return null;
|
|
end;
|
|
$$;
|
|
create trigger feedback_agg
|
|
after insert or update or delete on public.feedback
|
|
for each row execute function public._trg_feedback_agg();
|
|
|
|
-- A session must keep at least one speaker (block deleting the last one).
|
|
-- Skipped during a cascade delete of the session itself.
|
|
create or replace function public._trg_session_needs_speaker() returns trigger
|
|
language plpgsql security definer set search_path = public as $$
|
|
begin
|
|
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;
|
|
$$;
|
|
create trigger session_needs_speaker
|
|
before delete on public.session_speakers
|
|
for each row execute function public._trg_session_needs_speaker();
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 6. PUBLIC READ FUNCTIONS
|
|
-- -----------------------------------------------------------------------------
|
|
|
|
-- Leaderboard for the active event (initial load; Realtime is the live source).
|
|
create or replace function public.get_leaderboard()
|
|
returns table (event_id uuid, contact_email text, full_name text, points integer)
|
|
language sql stable security definer set search_path = public as $$
|
|
select l.event_id, l.contact_email, l.full_name, l.points
|
|
from public.leaderboard l
|
|
join public.events e on e.id = l.event_id
|
|
where e.is_active
|
|
order by l.points desc, l.full_name asc;
|
|
$$;
|
|
|
|
-- Resolve a scanned lanyard QR token to public contact info + registration type
|
|
-- for the active event, respecting the contact's visibility toggles.
|
|
create or replace function public.lanyard_lookup(p_qr_token text)
|
|
returns table (email text, full_name text, linkedin_url text,
|
|
show_email boolean, registration_type text)
|
|
language sql stable security definer set search_path = public as $$
|
|
select c.email,
|
|
c.full_name,
|
|
case when c.show_linkedin then c.linkedin_url else null end as linkedin_url,
|
|
c.show_email,
|
|
r.type as registration_type
|
|
from public.contacts c
|
|
left join public.registrations r
|
|
on r.contact_email = c.email
|
|
and r.event_id = (select id from public.events where is_active limit 1)
|
|
where c.qr_token = p_qr_token;
|
|
$$;
|
|
|
|
-- Public directory of contacts registered for the active event. Used by the
|
|
-- attendee app to resolve connection names. Respects visibility toggles:
|
|
-- email only when show_email, linkedin only when show_linkedin.
|
|
create or replace function public.get_public_contacts()
|
|
returns table (email text, full_name text, linkedin_url text,
|
|
show_email boolean, registration_type text)
|
|
language sql stable security definer set search_path = public as $$
|
|
select c.email,
|
|
c.full_name,
|
|
case when c.show_linkedin then c.linkedin_url else null end as linkedin_url,
|
|
c.show_email,
|
|
r.type as registration_type
|
|
from public.contacts c
|
|
join public.registrations r
|
|
on r.contact_email = c.email
|
|
and r.event_id = (select id from public.events where is_active limit 1)
|
|
order by c.full_name;
|
|
$$;
|
|
|
|
-- Full agenda for the active event: slots (ordered) each with their sessions.
|
|
create or replace function public.get_agenda()
|
|
returns table (
|
|
slot_id integer, slot_name text, slot_date date, start_time time, end_time time,
|
|
slot_type text,
|
|
session_id uuid, title text, description text, room text, capacity integer,
|
|
average_feedback numeric, ai_generated_feedback text, material_path text,
|
|
speakers text
|
|
)
|
|
language sql stable security definer set search_path = public as $$
|
|
select s.id, s.name, s.slot_date, s.start_time, s.end_time, s.type,
|
|
se.id, se.title, se.description, se.room, se.capacity,
|
|
se.average_feedback, se.ai_generated_feedback, se.material_path,
|
|
(select string_agg(c.full_name, ', ' order by c.full_name)
|
|
from public.session_speakers ss
|
|
join public.contacts c on c.email = ss.contact_email
|
|
where ss.session_id = se.id) as speakers
|
|
from public.slots s
|
|
left join public.sessions se on se.slot_id = s.id
|
|
where s.event_id = (select id from public.events where is_active limit 1)
|
|
order by s.id, se.title;
|
|
$$;
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 7. RPCs
|
|
-- -----------------------------------------------------------------------------
|
|
|
|
-- Set (or change) the preferred session for the slot that p_session_id belongs to.
|
|
create or replace function public.set_preference(p_session_id uuid)
|
|
returns public.preferences
|
|
language plpgsql security definer set search_path = public as $$
|
|
declare
|
|
v_email text := public.current_email();
|
|
v_slot_id integer;
|
|
v_event_id uuid;
|
|
v_pref public.preferences;
|
|
begin
|
|
if v_email is null then raise exception 'not authenticated'; end if;
|
|
select slot_id, event_id into v_slot_id, v_event_id from public.sessions where id = p_session_id;
|
|
if v_slot_id is null then raise exception 'session not found'; end if;
|
|
delete from public.preferences
|
|
where event_id = v_event_id and contact_email = v_email and slot_id = v_slot_id;
|
|
insert into public.preferences (event_id, contact_email, session_id, slot_id)
|
|
values (v_event_id, v_email, p_session_id, v_slot_id)
|
|
returning * into v_pref;
|
|
return v_pref;
|
|
end;
|
|
$$;
|
|
|
|
create or replace function public.clear_preference(p_slot_id integer)
|
|
returns void
|
|
language plpgsql security definer set search_path = public as $$
|
|
declare
|
|
v_email text := public.current_email();
|
|
v_event_id uuid;
|
|
begin
|
|
if v_email is null then raise exception 'not authenticated'; end if;
|
|
select event_id into v_event_id from public.slots where id = p_slot_id;
|
|
if v_event_id is null then raise exception 'slot not found'; end if;
|
|
delete from public.preferences
|
|
where event_id = v_event_id and contact_email = v_email and slot_id = p_slot_id;
|
|
end;
|
|
$$;
|
|
|
|
-- Create a pending connection to the contact whose lanyard QR was scanned.
|
|
-- Idempotent: returns the existing connection if the pair already has one.
|
|
create or replace function public.create_connection_request(p_target_qr text)
|
|
returns public.connections
|
|
language plpgsql security definer set search_path = public as $$
|
|
declare
|
|
v_email text := public.current_email();
|
|
v_target text;
|
|
v_event_id uuid;
|
|
v_conn public.connections;
|
|
begin
|
|
if v_email is null then raise exception 'not authenticated'; end if;
|
|
select email into v_target from public.contacts where qr_token = p_target_qr;
|
|
if v_target is null then raise exception 'no attendee found for that QR code'; end if;
|
|
if v_target = v_email then raise exception 'you cannot connect with yourself'; 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 * into v_conn from public.connections
|
|
where event_id = v_event_id
|
|
and ((from_email = v_email and to_email = v_target)
|
|
or (from_email = v_target and to_email = v_email))
|
|
limit 1;
|
|
if found then
|
|
return v_conn;
|
|
end if;
|
|
insert into public.connections (event_id, from_email, to_email, status)
|
|
values (v_event_id, v_email, v_target, 'pending')
|
|
returning * into v_conn;
|
|
return v_conn;
|
|
end;
|
|
$$;
|
|
|
|
-- Accept an incoming connection request. Awards +5 to both parties.
|
|
create or replace function public.accept_connection(p_connection_id uuid)
|
|
returns public.connections
|
|
language plpgsql security definer set search_path = public as $$
|
|
declare
|
|
v_email text := public.current_email();
|
|
v_conn public.connections;
|
|
begin
|
|
if v_email is null then raise exception 'not authenticated'; end if;
|
|
select * into v_conn from public.connections where id = p_connection_id for update;
|
|
if not found then raise exception 'connection not found'; end if;
|
|
if v_conn.to_email <> v_email then raise exception 'you can only accept incoming requests'; end if;
|
|
if v_conn.status <> 'pending' then raise exception 'request already resolved'; end if;
|
|
update public.connections set status = 'accepted', resolved_at = now() where id = p_connection_id;
|
|
perform public._award_points(v_conn.event_id, v_conn.from_email, 5, 'connection');
|
|
perform public._award_points(v_conn.event_id, v_conn.to_email, 5, 'connection');
|
|
select * into v_conn from public.connections where id = p_connection_id;
|
|
return v_conn;
|
|
end;
|
|
$$;
|
|
|
|
create or replace function public.reject_connection(p_connection_id uuid)
|
|
returns public.connections
|
|
language plpgsql security definer set search_path = public as $$
|
|
declare
|
|
v_email text := public.current_email();
|
|
v_conn public.connections;
|
|
begin
|
|
if v_email is null then raise exception 'not authenticated'; end if;
|
|
select * into v_conn from public.connections where id = p_connection_id for update;
|
|
if not found then raise exception 'connection not found'; end if;
|
|
if v_conn.to_email <> v_email then raise exception 'you can only reject incoming requests'; end if;
|
|
if v_conn.status <> 'pending' then raise exception 'request already resolved'; end if;
|
|
update public.connections set status = 'rejected', resolved_at = now() where id = p_connection_id;
|
|
select * into v_conn from public.connections where id = p_connection_id;
|
|
return v_conn;
|
|
end;
|
|
$$;
|
|
|
|
-- Staff (admin) checks a contact into a session. Awards +10 to the contact and
|
|
-- marks their registration checked-in. Returns already=true if already checked in.
|
|
create or replace function public.checkin_attendee(p_session_id uuid, p_attendee_qr text)
|
|
returns table (checkin_id uuid, attendee_email text, attendee_name text,
|
|
points integer, 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_new_id uuid;
|
|
v_points integer;
|
|
v_already boolean := false;
|
|
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 email, full_name into v_attendee, v_name from public.contacts where qr_token = p_attendee_qr;
|
|
if v_attendee is null then raise exception 'no attendee found for that QR code'; end if;
|
|
select event_id into v_event_id from public.sessions where id = p_session_id;
|
|
if v_event_id is null then raise exception 'session not found'; end if;
|
|
insert into public.checkins (event_id, contact_email, session_id, staff_email)
|
|
values (v_event_id, v_attendee, p_session_id, v_staff)
|
|
on conflict (event_id, contact_email, session_id) do nothing
|
|
returning id into v_new_id;
|
|
if v_new_id is null then
|
|
v_already := true;
|
|
select id into v_new_id from public.checkins
|
|
where event_id = v_event_id and contact_email = v_attendee and session_id = p_session_id;
|
|
else
|
|
perform public._award_points(v_event_id, v_attendee, 10, 'checkin');
|
|
update public.registrations set checked_in = true
|
|
where event_id = v_event_id and contact_email = v_attendee;
|
|
end if;
|
|
select r.points into v_points from public.registrations r
|
|
where r.event_id = v_event_id and r.contact_email = v_attendee;
|
|
return query select v_new_id, v_attendee, v_name, coalesce(v_points, 0), v_already;
|
|
end;
|
|
$$;
|
|
|
|
-- Post-session feedback. Requires the contact to be checked in.
|
|
-- Awards +5 (rating only) or +10 (rating + comment). Idempotent per session.
|
|
-- Also recalcs the session average + AI-feedback state + event aggregates.
|
|
create or replace function public.submit_feedback(p_session_id uuid, p_rating integer, p_comment text)
|
|
returns table (feedback_id uuid, points integer, earned integer)
|
|
language plpgsql security definer set search_path = public as $$
|
|
declare
|
|
v_email text := public.current_email();
|
|
v_event_id uuid;
|
|
v_earned integer;
|
|
v_fb_id uuid;
|
|
v_points integer;
|
|
v_clean text;
|
|
v_exists boolean;
|
|
begin
|
|
if v_email is null then raise exception 'not authenticated'; end if;
|
|
if p_rating is null or p_rating < 1 or p_rating > 5 then raise exception 'rating must be between 1 and 5'; end if;
|
|
select event_id into v_event_id from public.sessions where id = p_session_id;
|
|
if v_event_id is null then raise exception 'session not found'; end if;
|
|
if not exists (select 1 from public.checkins
|
|
where event_id = v_event_id and contact_email = v_email and session_id = p_session_id) then
|
|
raise exception 'you must be checked in to this session to give feedback';
|
|
end if;
|
|
v_clean := nullif(btrim(coalesce(p_comment, '')), '');
|
|
v_earned := case when v_clean is not null then 10 else 5 end;
|
|
|
|
select exists (select 1 from public.feedback
|
|
where event_id = v_event_id and contact_email = v_email and session_id = p_session_id) into v_exists;
|
|
if v_exists then
|
|
update public.feedback set rating = p_rating, comment = v_clean
|
|
where event_id = v_event_id and contact_email = v_email and session_id = p_session_id;
|
|
select id into v_fb_id from public.feedback
|
|
where event_id = v_event_id and contact_email = v_email and session_id = p_session_id;
|
|
v_earned := 0; -- already rewarded on first submission
|
|
else
|
|
insert into public.feedback (event_id, contact_email, session_id, rating, comment)
|
|
values (v_event_id, v_email, p_session_id, p_rating, v_clean)
|
|
returning id into v_fb_id;
|
|
perform public._award_points(v_event_id, v_email, v_earned, 'feedback');
|
|
end if;
|
|
|
|
-- Recalc the session's average feedback.
|
|
update public.sessions set
|
|
average_feedback = (select avg(rating) from public.feedback where session_id = p_session_id)
|
|
where id = p_session_id;
|
|
|
|
-- AI-feedback state machine (stub): no comments vs comments pending evaluation.
|
|
update public.sessions set
|
|
ai_generated_feedback = case
|
|
when exists (select 1 from public.feedback
|
|
where session_id = p_session_id and comment is not null)
|
|
then 'Pending AI Evaluation'
|
|
else 'No additional comments were given'
|
|
end
|
|
where id = p_session_id;
|
|
|
|
-- Recalc event aggregates (avg_feedback).
|
|
perform public._recalc_event_aggregates(v_event_id);
|
|
|
|
select r.points into v_points from public.registrations r
|
|
where r.event_id = v_event_id and r.contact_email = v_email;
|
|
return query select v_fb_id, coalesce(v_points, 0), v_earned;
|
|
end;
|
|
$$;
|
|
|
|
-- Admin-only manual points adjustment (for the active event).
|
|
create or replace function public.adjust_points(p_contact_email text, p_delta integer, p_reason text)
|
|
returns void
|
|
language plpgsql security definer set search_path = public as $$
|
|
declare
|
|
v_email text := public.current_email();
|
|
v_event_id uuid;
|
|
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;
|
|
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;
|
|
perform public._award_points(v_event_id, p_contact_email, p_delta, 'admin');
|
|
end;
|
|
$$;
|
|
|
|
-- Reset a contact's QR token (also the Auth password). Admin only.
|
|
create or replace function public.reset_qr_token(p_contact_email text)
|
|
returns table (email text, qr_token text)
|
|
language plpgsql security definer set search_path = public as $$
|
|
declare
|
|
v_uid uuid;
|
|
v_token text;
|
|
v_salt text;
|
|
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 not exists (select 1 from public.contacts where email = p_contact_email) then
|
|
raise exception 'contact not found';
|
|
end if;
|
|
v_token := 'CVV-' || upper(substr(md5(random()::text), 1, 3)) || '-'
|
|
|| lpad((floor(random() * 10000))::int::text, 4, '0');
|
|
update public.contacts set qr_token = v_token where email = p_contact_email;
|
|
select id into v_uid from auth.users where email = p_contact_email;
|
|
if v_uid is not null then
|
|
v_salt := extensions.gen_salt('bf');
|
|
update auth.users
|
|
set encrypted_password = extensions.crypt(v_token, v_salt), updated_at = now()
|
|
where id = v_uid;
|
|
delete from auth.sessions where user_id = v_uid;
|
|
end if;
|
|
return query select p_contact_email, v_token;
|
|
end;
|
|
$$;
|
|
|
|
-- Create-if-not-exists a contact by email (the interface flow, e.g. Pretix).
|
|
-- Also creates/updates the matching auth.user (QR token = password).
|
|
create or replace function public.upsert_contact(
|
|
p_email text, p_full_name text, p_linkedin_url text,
|
|
p_is_admin boolean, p_qr_token text
|
|
)
|
|
returns public.contacts
|
|
language plpgsql security definer set search_path = public as $$
|
|
declare
|
|
v_uid uuid;
|
|
v_token text;
|
|
v_contact public.contacts;
|
|
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_email is null or btrim(p_email) = '' then raise exception 'email is required'; end if;
|
|
if p_full_name is null or btrim(p_full_name) = '' then raise exception 'full_name is required'; end if;
|
|
|
|
-- Keep the existing token unless a new one is supplied.
|
|
select qr_token into v_token from public.contacts where email = p_email;
|
|
v_token := coalesce(p_qr_token, v_token,
|
|
'CVV-' || upper(substr(md5(random()::text), 1, 3)) || '-'
|
|
|| lpad((floor(random() * 10000))::int::text, 4, '0'));
|
|
|
|
insert into public.contacts (email, full_name, linkedin_url, is_admin, qr_token)
|
|
values (p_email, p_full_name, p_linkedin_url, coalesce(p_is_admin, false), v_token)
|
|
on conflict (email) do update
|
|
set full_name = excluded.full_name,
|
|
linkedin_url = excluded.linkedin_url,
|
|
is_admin = excluded.is_admin,
|
|
qr_token = excluded.qr_token
|
|
returning * into v_contact;
|
|
|
|
-- Sync auth.users (create or update the password).
|
|
select id into v_uid from auth.users where email = p_email;
|
|
if v_uid is null then
|
|
insert into auth.users (instance_id, id, aud, role, email, encrypted_password,
|
|
email_confirmed_at, confirmation_token, recovery_token, email_change_token_new,
|
|
email_change, raw_app_meta_data, raw_user_meta_data,
|
|
created_at, updated_at, confirmation_sent_at, last_sign_in_at)
|
|
values ('00000000-0000-0000-0000-000000000000', gen_random_uuid(), 'authenticated',
|
|
'authenticated', p_email, extensions.crypt(v_token, extensions.gen_salt('bf')),
|
|
now(), '', '', '', '', '{"provider":"email","providers":["email"]}',
|
|
jsonb_build_object('full_name', p_full_name), now(), now(), now(), now())
|
|
returning id into v_uid;
|
|
insert into auth.identities (id, provider_id, identity_data, provider, user_id,
|
|
created_at, updated_at, last_sign_in_at)
|
|
values (v_uid, v_uid::text,
|
|
jsonb_build_object('sub', v_uid::text, 'email', p_email),
|
|
'email', v_uid, now(), now(), now());
|
|
else
|
|
update auth.users
|
|
set encrypted_password = extensions.crypt(v_token, extensions.gen_salt('bf')),
|
|
updated_at = now()
|
|
where id = v_uid;
|
|
delete from auth.sessions where user_id = v_uid;
|
|
end if;
|
|
|
|
return v_contact;
|
|
end;
|
|
$$;
|
|
|
|
-- Create (or update) a registration for the active event. Admin only.
|
|
-- Enforces the sponsor rule: a `sponsor` type requires a sponsor lookup.
|
|
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)
|
|
on conflict (event_id, contact_email) do update set type = excluded.type
|
|
returning * into v_reg;
|
|
return v_reg;
|
|
end;
|
|
$$;
|
|
|
|
-- Change a registration's type for the active event. Admin only.
|
|
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
|
|
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;
|
|
$$;
|
|
|
|
-- Admin stub for AI feedback evaluation (real LLM integration comes later).
|
|
create or replace function public.evaluate_session_feedback(p_session_id uuid)
|
|
returns public.sessions
|
|
language plpgsql security definer set search_path = public as $$
|
|
declare
|
|
v_email text := public.current_email();
|
|
v_session public.sessions;
|
|
v_comment_count integer;
|
|
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;
|
|
select * into v_session from public.sessions where id = p_session_id;
|
|
if not found then raise exception 'session not found'; end if;
|
|
select count(*) into v_comment_count
|
|
from public.feedback where session_id = p_session_id and comment is not null;
|
|
update public.sessions set
|
|
ai_generated_feedback = 'AI evaluation stub: ' || v_comment_count
|
|
|| ' comment(s) received. (LLM integration pending.)'
|
|
where id = p_session_id
|
|
returning * into v_session;
|
|
return v_session;
|
|
end;
|
|
$$;
|
|
|
|
-- Set a session's material_path. Allowed for admins and for any speaker of the
|
|
-- session (the sessions table itself is admin-write only, so speakers need this
|
|
-- security-definer RPC to attach their uploaded material).
|
|
create or replace function public.set_session_material(p_session_id uuid, p_material_path text)
|
|
returns public.sessions
|
|
language plpgsql security definer set search_path = public as $$
|
|
declare
|
|
v_email text := public.current_email();
|
|
v_session public.sessions;
|
|
begin
|
|
if v_email is null then raise exception 'not authenticated'; end if;
|
|
select * into v_session from public.sessions where id = p_session_id;
|
|
if not found then raise exception 'session not found'; end if;
|
|
if not (public.is_admin() or exists (
|
|
select 1 from public.session_speakers ss
|
|
where ss.session_id = p_session_id and ss.contact_email = v_email)) then
|
|
raise exception 'not a speaker of this session';
|
|
end if;
|
|
update public.sessions set material_path = p_material_path
|
|
where id = p_session_id
|
|
returning * into v_session;
|
|
return v_session;
|
|
end;
|
|
$$;
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 8. ROW LEVEL SECURITY
|
|
-- -----------------------------------------------------------------------------
|
|
alter table public.contacts enable row level security;
|
|
alter table public.events enable row level security;
|
|
alter table public.registrations enable row level security;
|
|
alter table public.sponsors enable row level security;
|
|
alter table public.slots enable row level security;
|
|
alter table public.sessions enable row level security;
|
|
alter table public.session_speakers enable row level security;
|
|
alter table public.preferences enable row level security;
|
|
alter table public.connections enable row level security;
|
|
alter table public.checkins enable row level security;
|
|
alter table public.feedback enable row level security;
|
|
alter table public.leaderboard enable row level security;
|
|
alter table public.points_log enable row level security;
|
|
|
|
-- contacts: full row only for self or admin (protects email + qr_token).
|
|
create policy "contacts_select_own_or_admin" on public.contacts for select to authenticated
|
|
using (email = public.current_email() or public.is_admin());
|
|
create policy "contacts_admin_insert" on public.contacts for insert to authenticated
|
|
with check (public.is_admin());
|
|
create policy "contacts_admin_update" on public.contacts for update to authenticated
|
|
using (public.is_admin()) with check (public.is_admin());
|
|
create policy "contacts_admin_delete" on public.contacts for delete to authenticated
|
|
using (public.is_admin());
|
|
|
|
-- events: readable by all authenticated; writable by admin.
|
|
create policy "events_select_all" on public.events for select to authenticated using (true);
|
|
create policy "events_admin_insert" on public.events for insert to authenticated with check (public.is_admin());
|
|
create policy "events_admin_update" on public.events for update to authenticated
|
|
using (public.is_admin()) with check (public.is_admin());
|
|
create policy "events_admin_delete" on public.events for delete to authenticated using (public.is_admin());
|
|
|
|
-- registrations: own or admin.
|
|
create policy "registrations_select_own_or_admin" on public.registrations for select to authenticated
|
|
using (contact_email = public.current_email() or public.is_admin());
|
|
create policy "registrations_admin_insert" on public.registrations for insert to authenticated
|
|
with check (public.is_admin());
|
|
create policy "registrations_admin_update" on public.registrations for update to authenticated
|
|
using (public.is_admin()) with check (public.is_admin());
|
|
create policy "registrations_admin_delete" on public.registrations for delete to authenticated
|
|
using (public.is_admin());
|
|
|
|
-- sponsors: admin sees all; non-admin sees only paid sponsors.
|
|
create policy "sponsors_select" on public.sponsors for select to authenticated
|
|
using (public.is_admin() or paid);
|
|
create policy "sponsors_admin_insert" on public.sponsors for insert to authenticated
|
|
with check (public.is_admin());
|
|
create policy "sponsors_admin_update" on public.sponsors for update to authenticated
|
|
using (public.is_admin()) with check (public.is_admin());
|
|
create policy "sponsors_admin_delete" on public.sponsors for delete to authenticated
|
|
using (public.is_admin());
|
|
|
|
-- slots: readable by all authenticated; writable by admin.
|
|
create policy "slots_select_all" on public.slots for select to authenticated using (true);
|
|
create policy "slots_admin_insert" on public.slots for insert to authenticated with check (public.is_admin());
|
|
create policy "slots_admin_update" on public.slots for update to authenticated
|
|
using (public.is_admin()) with check (public.is_admin());
|
|
create policy "slots_admin_delete" on public.slots for delete to authenticated using (public.is_admin());
|
|
|
|
-- sessions: readable by all authenticated; writable by admin.
|
|
create policy "sessions_select_all" on public.sessions for select to authenticated using (true);
|
|
create policy "sessions_admin_insert" on public.sessions for insert to authenticated with check (public.is_admin());
|
|
create policy "sessions_admin_update" on public.sessions for update to authenticated
|
|
using (public.is_admin()) with check (public.is_admin());
|
|
create policy "sessions_admin_delete" on public.sessions for delete to authenticated using (public.is_admin());
|
|
|
|
-- session_speakers: readable by all authenticated; writable by admin.
|
|
create policy "session_speakers_select_all" on public.session_speakers for select to authenticated using (true);
|
|
create policy "session_speakers_admin_insert" on public.session_speakers for insert to authenticated
|
|
with check (public.is_admin());
|
|
create policy "session_speakers_admin_update" on public.session_speakers for update to authenticated
|
|
using (public.is_admin()) with check (public.is_admin());
|
|
create policy "session_speakers_admin_delete" on public.session_speakers for delete to authenticated
|
|
using (public.is_admin());
|
|
|
|
-- preferences: read own; writes happen via RPC (security definer).
|
|
create policy "prefs_select_own" on public.preferences for select to authenticated
|
|
using (contact_email = public.current_email());
|
|
|
|
-- connections: read any row you're part of (or all if admin); writes happen via RPC.
|
|
create policy "conn_select_involved_or_admin" on public.connections for select to authenticated
|
|
using (from_email = public.current_email() or to_email = public.current_email() or public.is_admin());
|
|
|
|
-- checkins: read own, or all if admin; writes happen via RPC.
|
|
create policy "checkins_select_own_or_admin" on public.checkins for select to authenticated
|
|
using (contact_email = public.current_email() or public.is_admin());
|
|
|
|
-- feedback: read own, or all if admin; writes happen via RPC.
|
|
create policy "feedback_select_own_or_admin" on public.feedback for select to authenticated
|
|
using (contact_email = public.current_email() or public.is_admin());
|
|
|
|
-- leaderboard: public to all authenticated (Realtime source).
|
|
create policy "leaderboard_select_all" on public.leaderboard for select to authenticated using (true);
|
|
|
|
-- points_log: read own or admin.
|
|
create policy "points_log_select_own_or_admin" on public.points_log for select to authenticated
|
|
using (contact_email = public.current_email() or public.is_admin());
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 9. GRANTS
|
|
-- The Supabase CLI does NOT auto-expose new tables to the Data API roles,
|
|
-- so grant explicitly. RLS still governs what each role can actually see.
|
|
-- -----------------------------------------------------------------------------
|
|
grant usage on schema public to anon, authenticated, service_role;
|
|
grant select on public.contacts, public.events, public.registrations, public.sponsors,
|
|
public.slots, public.sessions, public.session_speakers, public.preferences,
|
|
public.connections, public.checkins, public.feedback,
|
|
public.leaderboard, public.points_log
|
|
to anon, authenticated, service_role;
|
|
grant insert, update, delete on public.contacts, public.events, public.registrations, public.sponsors,
|
|
public.slots, public.sessions, public.session_speakers, public.preferences,
|
|
public.connections, public.checkins, public.feedback,
|
|
public.leaderboard, public.points_log
|
|
to authenticated, service_role;
|
|
|
|
grant execute on function public.current_email() to authenticated;
|
|
grant execute on function public.is_admin() to authenticated;
|
|
grant execute on function public.is_staff_or_admin() to authenticated;
|
|
grant execute on function public.is_speaker_or_admin() to authenticated;
|
|
grant execute on function public.get_active_event() to authenticated;
|
|
grant execute on function public.get_leaderboard() to authenticated;
|
|
grant execute on function public.lanyard_lookup(text) to authenticated;
|
|
grant execute on function public.get_public_contacts() to authenticated;
|
|
grant execute on function public.set_preference(uuid) to authenticated;
|
|
grant execute on function public.clear_preference(integer) to authenticated;
|
|
grant execute on function public.create_connection_request(text) to authenticated;
|
|
grant execute on function public.accept_connection(uuid) to authenticated;
|
|
grant execute on function public.reject_connection(uuid) to authenticated;
|
|
grant execute on function public.checkin_attendee(uuid, text) to authenticated;
|
|
grant execute on function public.submit_feedback(uuid, integer, text) to authenticated;
|
|
grant execute on function public.adjust_points(text, integer, text) to authenticated;
|
|
grant execute on function public.get_agenda() to authenticated;
|
|
grant execute on function public.reset_qr_token(text) to authenticated;
|
|
grant execute on function public.upsert_contact(text, text, text, boolean, text) to authenticated;
|
|
grant execute on function public.create_registration(text, text) to authenticated;
|
|
grant execute on function public.set_registration_type(text, text) to authenticated;
|
|
grant execute on function public.evaluate_session_feedback(uuid) to authenticated;
|
|
grant execute on function public.set_session_material(uuid, text) to authenticated;
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 10. STORAGE: `materials` bucket (session materials)
|
|
-- Read for event attendees; write for speakers + admins.
|
|
-- -----------------------------------------------------------------------------
|
|
insert into storage.buckets (id, name, public)
|
|
values ('materials', 'materials', true)
|
|
on conflict (id) do nothing;
|
|
|
|
drop policy if exists "materials_read" on storage.objects;
|
|
drop policy if exists "materials_write" on storage.objects;
|
|
drop policy if exists "materials_update" on storage.objects;
|
|
drop policy if exists "materials_delete" on storage.objects;
|
|
|
|
create policy "materials_read" on storage.objects for select to authenticated
|
|
using (bucket_id = 'materials');
|
|
create policy "materials_write" on storage.objects for insert to authenticated
|
|
with check (bucket_id = 'materials' and public.is_speaker_or_admin());
|
|
create policy "materials_update" on storage.objects for update to authenticated
|
|
using (bucket_id = 'materials' and public.is_speaker_or_admin());
|
|
create policy "materials_delete" on storage.objects for delete to authenticated
|
|
using (bucket_id = 'materials' and public.is_speaker_or_admin());
|