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

This commit is contained in:
Lago committed 2026-10-15 21:39:57 +02:00
1 parent 5117a25372
commit 3e2f8ff0ed
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;
+127
View File
@@ -0,0 +1,127 @@
\set ON_ERROR_STOP on
begin;
create temporary table raffle_results (name text);
grant insert on raffle_results to authenticated, anon;
create function pg_temp.assert_raffle(p_condition boolean, p_name text) returns void
language plpgsql as $$
begin
if p_condition is distinct from true then raise exception 'FAIL: %', p_name; end if;
insert into raffle_results values (p_name);
end;
$$;
-- Isolate a single active event for the run.
update public.events set is_active = false where is_active;
insert into public.events (id, name, start_date, end_date, is_active)
values ('b0ff1e00-0000-0000-0000-000000000001', 'Raffle regression', current_date, current_date, true);
insert into public.contacts (email, full_name, is_admin) values
('[email protected]', 'Top One', false),
('[email protected]', 'Top Two', false),
('[email protected]', 'Top Three', false),
('[email protected]', 'Low One', false),
('[email protected]', 'Low Two', false),
('[email protected]', 'Staff Person', false),
('[email protected]', 'Admin Person', true),
('[email protected]', 'Domain Person', false),
('[email protected]', 'Plain Viewer', false);
insert into public.registrations (event_id, contact_email, type, points) values
('b0ff1e00-0000-0000-0000-000000000001', '[email protected]', 'attendee', 100),
('b0ff1e00-0000-0000-0000-000000000001', '[email protected]', 'attendee', 90),
('b0ff1e00-0000-0000-0000-000000000001', '[email protected]', 'attendee', 80),
('b0ff1e00-0000-0000-0000-000000000001', '[email protected]', 'attendee', 10),
('b0ff1e00-0000-0000-0000-000000000001', '[email protected]', 'attendee', 5),
('b0ff1e00-0000-0000-0000-000000000001', '[email protected]', 'staff', 999),
('b0ff1e00-0000-0000-0000-000000000001', '[email protected]', 'attendee', 500),
('b0ff1e00-0000-0000-0000-000000000001', '[email protected]', 'attendee', 500);
-- Act as the raffle admin.
select set_config('request.jwt.claims', '{"email":"[email protected]","role":"authenticated"}', true);
-- The pool excludes staff, Admin, the @appe.at domain and the leaderboard top 3,
-- leaving exactly the two low-point eligible attendees.
select pg_temp.assert_raffle(
(select array_agg(contact_email order by contact_email) =
array['[email protected]','[email protected]'] from public.get_raffle_pool()),
'pool excludes staff, admin, domain and top 3');
-- Draw one: winner is an eligible non-top attendee, recorded with draw_number 1.
create temporary table draw1 as select * from public.draw_raffle_winner();
select pg_temp.assert_raffle(
(select contact_email in ('[email protected]','[email protected]') from draw1),
'first draw returns an eligible attendee');
select pg_temp.assert_raffle((select draw_number = 1 from draw1), 'first draw number is one');
select pg_temp.assert_raffle(
(select count(*) = 1 from public.get_raffle_pool()), 'drawn attendee leaves the pool');
-- Draw two: the remaining eligible attendee, draw_number 2, pool now empty.
create temporary table draw2 as select * from public.draw_raffle_winner();
select pg_temp.assert_raffle(
(select contact_email <> (select contact_email from draw1)
and contact_email in ('[email protected]','[email protected]') from draw2),
'second draw is a different eligible attendee');
select pg_temp.assert_raffle((select draw_number = 2 from draw2), 'second draw number is two');
select pg_temp.assert_raffle((select count(*) = 0 from public.get_raffle_pool()), 'pool empty after all eligible drawn');
-- A third draw with no eligible attendees left fails clearly.
do $$ begin
begin
perform public.draw_raffle_winner();
raise exception 'over-draw allowed';
exception when raise_exception then
if sqlerrm <> 'No eligible attendees left to draw' then raise; end if;
end;
perform pg_temp.assert_raffle(true, 'over-draw rejected');
end; $$;
-- Winners persisted, none excluded, sequential numbers.
select pg_temp.assert_raffle(
(select count(*) = 2 and bool_and(contact_email in ('[email protected]','[email protected]'))
and count(distinct draw_number) = 2 from public.raffle_draws),
'winners persisted without excluded attendees');
-- Reset clears the run so the same attendees become eligible again.
select public.reset_raffle();
select pg_temp.assert_raffle(
(select count(*) = 0 from public.raffle_draws), 'reset clears winners');
select pg_temp.assert_raffle(
(select count(*) = 2 from public.get_raffle_pool()), 'reset restores the pool');
-- Non-admin authenticated users cannot see the pool or draw.
select set_config('request.jwt.claims', '{"email":"[email protected]","role":"authenticated"}', true);
select pg_temp.assert_raffle((select count(*) = 0 from public.get_raffle_pool()), 'non-admin sees empty pool');
select pg_temp.assert_raffle((select count(*) = 0 from public.raffle_draws), 'non-admin cannot read winners');
do $$ begin
begin
perform public.draw_raffle_winner();
raise exception 'non-admin draw allowed';
exception when raise_exception then
if sqlerrm <> 'admin only' then raise; end if;
end;
begin
perform public.reset_raffle();
raise exception 'non-admin reset allowed';
exception when raise_exception then
if sqlerrm <> 'admin only' then raise; end if;
end;
perform pg_temp.assert_raffle(true, 'non-admin cannot draw or reset');
end; $$;
-- The anonymous API role has no execute grant at all.
reset role;
select set_config('request.jwt.claims', '{"role":"anon"}', true);
set local role anon;
do $$ begin
begin
perform public.draw_raffle_winner();
raise exception 'anon draw allowed';
exception when insufficient_privilege then null;
end;
perform pg_temp.assert_raffle(true, 'anon cannot execute raffle functions');
end; $$;
reset role;
select count(*) as passed_assertions from raffle_results;
rollback;