feat: add admin raffle with confetti and top-3/staff exclusion

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]>
This commit is contained in:
committed 2026-10-15 21:39:57 +02:00
1 parent 4cf09cd739
commit 4ec03410ff
14 files changed
+680 -2

No files matched your search

@@ -0,0 +1,87 @@
-- -----------------------------------------------------------------------------
-- Admin raffle.
--
-- Draws eligible attendees one at a time for the active event. The pool
-- excludes staff/Admin contacts (same rule as scoring eligibility), the top 3
-- of the leaderboard, and anyone already drawn in the current run. A run is
-- the set of winners recorded for the event; `reset_raffle` clears it so a new
-- run can begin. Winner selection is authoritative server-side (random), so
-- the client roulette animation only has to decelerate onto the returned name.
-- -----------------------------------------------------------------------------
create table public.raffle_draws (
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,
draw_number integer not null check (draw_number > 0),
drawn_at timestamptz not null default now(),
primary key (event_id, contact_email)
);
create index raffle_draws_event_id_idx on public.raffle_draws (event_id);
alter table public.raffle_draws enable row level security;
revoke all on public.raffle_draws from public, anon, authenticated;
grant select on public.raffle_draws to authenticated;
create policy raffle_draws_admin_read on public.raffle_draws
for select to authenticated using ((select public.is_admin()));
-- Eligible attendees for the active event: non-staff, non-Admin, not in the
-- leaderboard top 3, and not already drawn in the current run.
create or replace function public.get_raffle_pool()
returns table (contact_email text, full_name text, points integer)
language sql stable security definer set search_path = '' as $$
select c.email, c.full_name, r.points
from public.registrations r
join public.contacts c on c.email = r.contact_email
join public.events e on e.id = r.event_id and e.is_active
where public.is_admin()
and r.type <> 'staff' and not c.is_admin and c.email not like '%@appe.at'
and c.email not in (select l.contact_email from public.get_leaderboard() l limit 3)
and c.email not in (select d.contact_email from public.raffle_draws d)
order by c.full_name, c.email;
$$;
-- Draw one random eligible attendee and record them as the next winner.
create or replace function public.draw_raffle_winner()
returns table (contact_email text, full_name text, points integer, draw_number integer)
language plpgsql security definer set search_path = '' as $$
declare
v_event uuid;
v_email text;
v_name text;
v_points integer;
v_num integer;
begin
if not public.is_admin() then raise exception 'admin only'; end if;
select id into v_event from public.events where is_active limit 1;
if v_event is null then raise exception 'no active event'; end if;
select p.contact_email, p.full_name, p.points into v_email, v_name, v_points
from public.get_raffle_pool() p order by random() limit 1;
if v_email is null then raise exception 'No eligible attendees left to draw'; end if;
select coalesce(max(d.draw_number), 0) + 1 into v_num
from public.raffle_draws d where d.event_id = v_event;
insert into public.raffle_draws (event_id, contact_email, full_name, draw_number)
values (v_event, v_email, v_name, v_num);
return query select v_email, v_name, v_points, v_num;
end;
$$;
-- Clear the current run's winners so a new run can begin.
create or replace function public.reset_raffle()
returns void
language plpgsql security definer set search_path = '' as $$
declare
v_event uuid;
begin
if not public.is_admin() then raise exception 'admin only'; end if;
select id into v_event from public.events where is_active limit 1;
if v_event is null then raise exception 'no active event'; end if;
delete from public.raffle_draws where event_id = v_event;
end;
$$;
revoke all on function public.get_raffle_pool(), public.draw_raffle_winner(), public.reset_raffle()
from public, anon;
grant execute on function public.get_raffle_pool(), public.draw_raffle_winner(), public.reset_raffle()
to authenticated;