feat: add manual feedback AI summaries and secure leaderboard scoring
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:
1 parent
407b878e23
commit
821907b164
37 files changed
+1640
-77
No files matched your search
@@ -0,0 +1,159 @@
|
||||
create or replace function public._points_eligible(p_event_id uuid, p_contact_email text)
|
||||
returns boolean
|
||||
language sql stable security definer set search_path = '' as $$
|
||||
select exists (
|
||||
select 1 from public.registrations r
|
||||
join public.contacts c on c.email = r.contact_email
|
||||
where r.event_id = p_event_id and r.contact_email = p_contact_email
|
||||
and r.type <> 'staff' and not c.is_admin and c.email not like '%@appe.at'
|
||||
);
|
||||
$$;
|
||||
|
||||
create or replace function public._sync_leaderboard(p_event_id uuid, p_contact_email text)
|
||||
returns void
|
||||
language plpgsql security definer set search_path = '' as $$
|
||||
begin
|
||||
if not public._points_eligible(p_event_id, p_contact_email) then
|
||||
delete from public.leaderboard where event_id = p_event_id and contact_email = p_contact_email;
|
||||
return;
|
||||
end if;
|
||||
insert into public.leaderboard (event_id, contact_email, full_name, points)
|
||||
select r.event_id, c.email, c.full_name, r.points
|
||||
from public.registrations r join public.contacts c on c.email = r.contact_email
|
||||
where r.event_id = p_event_id and r.contact_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;
|
||||
$$;
|
||||
|
||||
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 = '' as $$
|
||||
begin
|
||||
perform 1 from public.registrations
|
||||
where event_id = p_event_id and contact_email = p_contact_email for update;
|
||||
if not found or not public._points_eligible(p_event_id, p_contact_email) then
|
||||
return;
|
||||
end if;
|
||||
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);
|
||||
end;
|
||||
$$;
|
||||
|
||||
create or replace function public._guard_registration_points()
|
||||
returns trigger
|
||||
language plpgsql security definer set search_path = '' as $$
|
||||
begin
|
||||
if new.type = 'staff' or exists (
|
||||
select 1 from public.contacts c where c.email = new.contact_email
|
||||
and (c.is_admin or c.email like '%@appe.at')
|
||||
) then
|
||||
new.points := 0;
|
||||
end if;
|
||||
return new;
|
||||
end;
|
||||
$$;
|
||||
|
||||
create trigger z_registrations_points_guard
|
||||
before insert or update on public.registrations
|
||||
for each row execute function public._guard_registration_points();
|
||||
|
||||
create or replace function public._refresh_registration_leaderboard()
|
||||
returns trigger
|
||||
language plpgsql security definer set search_path = '' as $$
|
||||
begin
|
||||
if tg_op = 'DELETE' then
|
||||
delete from public.leaderboard where event_id = old.event_id and contact_email = old.contact_email;
|
||||
return old;
|
||||
end if;
|
||||
if tg_op = 'UPDATE' then
|
||||
if old.event_id is distinct from new.event_id or old.contact_email is distinct from new.contact_email then
|
||||
delete from public.leaderboard where event_id = old.event_id and contact_email = old.contact_email;
|
||||
end if;
|
||||
end if;
|
||||
perform public._sync_leaderboard(new.event_id, new.contact_email);
|
||||
return new;
|
||||
end;
|
||||
$$;
|
||||
|
||||
create trigger registrations_leaderboard_refresh
|
||||
after insert or update or delete on public.registrations
|
||||
for each row execute function public._refresh_registration_leaderboard();
|
||||
|
||||
create or replace function public._refresh_contact_leaderboards()
|
||||
returns trigger
|
||||
language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
registration_event uuid;
|
||||
begin
|
||||
if new.is_admin or new.email like '%@appe.at' then
|
||||
update public.registrations set points = 0 where contact_email = new.email;
|
||||
end if;
|
||||
for registration_event in select event_id from public.registrations where contact_email = new.email loop
|
||||
perform public._sync_leaderboard(registration_event, new.email);
|
||||
end loop;
|
||||
return new;
|
||||
end;
|
||||
$$;
|
||||
|
||||
create trigger contacts_leaderboard_refresh
|
||||
after update of is_admin, full_name on public.contacts
|
||||
for each row execute function public._refresh_contact_leaderboards();
|
||||
|
||||
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 = '' 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 and e.is_active
|
||||
join public.registrations r on r.event_id = l.event_id and r.contact_email = l.contact_email
|
||||
join public.contacts c on c.email = r.contact_email
|
||||
where r.type <> 'staff' and not c.is_admin and c.email not like '%@appe.at'
|
||||
order by l.points desc, l.full_name asc, l.contact_email asc;
|
||||
$$;
|
||||
|
||||
do $$
|
||||
declare
|
||||
definition text := pg_get_functiondef('public.submit_feedback(uuid,integer,text)'::regprocedure);
|
||||
original text := 'v_earned := case when v_clean is not null then 10 else 5 end;';
|
||||
begin
|
||||
if strpos(definition, original) = 0 then
|
||||
raise exception 'submit_feedback scoring definition changed; review migration';
|
||||
end if;
|
||||
execute replace(definition, original,
|
||||
'v_earned := case when not public._points_eligible(v_event_id, v_email) then 0 when v_clean is not null then 10 else 5 end;');
|
||||
end;
|
||||
$$;
|
||||
|
||||
update public.registrations r set points = 0
|
||||
from public.contacts c where c.email = r.contact_email
|
||||
and (r.type = 'staff' or c.is_admin or c.email like '%@appe.at') and r.points <> 0;
|
||||
|
||||
delete from public.leaderboard l where not public._points_eligible(l.event_id, l.contact_email);
|
||||
insert into public.leaderboard (event_id, contact_email, full_name, points)
|
||||
select r.event_id, c.email, c.full_name, r.points
|
||||
from public.registrations r join public.contacts c on c.email = r.contact_email
|
||||
where r.type <> 'staff' and not c.is_admin and c.email not like '%@appe.at'
|
||||
on conflict (event_id, contact_email) do update
|
||||
set full_name = excluded.full_name, points = excluded.points, updated_at = now();
|
||||
|
||||
revoke all on function public._points_eligible(uuid,text),
|
||||
public._award_points(uuid,text,integer,text), public._sync_leaderboard(uuid,text),
|
||||
public._guard_registration_points(), public._refresh_registration_leaderboard(),
|
||||
public._refresh_contact_leaderboards() from public, anon, authenticated;
|
||||
revoke all on function public.get_leaderboard() from public, anon;
|
||||
grant execute on function public.get_leaderboard() to authenticated;
|
||||
|
||||
do $$
|
||||
begin
|
||||
if exists (select 1 from pg_publication where pubname = 'supabase_realtime')
|
||||
and not exists (
|
||||
select 1 from pg_publication_tables
|
||||
where pubname = 'supabase_realtime' and schemaname = 'public' and tablename = 'leaderboard'
|
||||
) then
|
||||
alter publication supabase_realtime add table public.leaderboard;
|
||||
end if;
|
||||
end;
|
||||
$$;
|
||||
Reference in new issue
Block a user