Files
Convivo/supabase/migrations/20261020130000_event_feedback_points.sql
T
lago 35b88d50ca feat(points): staff auto-connect never scores; event feedback pays +5
- Migration 20261020120000: staff (type staff / Admin / @appe.at) are
  auto-connected to every participant of the event via connections.auto
  rows inserted directly as accepted - never through accept_connection,
  so zero points by construction; backfill + idempotent helper + trigger.
- Migration 20261020130000: submit_event_feedback awards +5 on the first
  submission per participant x event (edits earn 0), gated by
  _points_eligible; response gains earned column; ACL restored.
- Admin: points move from Contacts to Registrations (active-event rows
  only, in-place save); Dashboard Top points drops emails; Dashboard and
  Activity hide auto connections.
- Attendee: leaderboard masks surnames (Max M.); EventFeedback toast
  shows +5 pts on paid submissions.
- Tests: staff_auto_connections_tests.sql (14 assertions), rpc_tests
  section 11 (4 assertions). Docs + release record 2026-10-20.
- README: document the git-ignored .env provisioning step (missing key
  = white screen).
2026-10-20 22:15:29 +02:00

70 lines
3.2 KiB
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- =============================================================================
-- 20261020130000_event_feedback_points.sql
-- Event-level feedback is worth points: **+5 for the first submission**
-- (per participant × event). Re-submitting edits the existing row and awards
-- nothing, mirroring the session-feedback idempotency rule. Staff / Admin
-- contacts / @appe.at earn nothing — the same `_points_eligible` gate every
-- other award uses. Session feedback is unchanged (+5 rating only, +10 with
-- comment — already live since 20260824000000 / 20261015164546).
-- The response gains an `earned` column so the app can show "+5 pts".
-- =============================================================================
-- =============================================================================
-- The return type gains a column, which `create or replace` cannot do —
-- drop + recreate, then restore the exact ACL the original migrations left
-- (EXECUTE for PUBLIC + authenticated; no direct anon grant).
-- =============================================================================
drop function if exists public.submit_event_feedback(integer, text);
create function public.submit_event_feedback(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_fb_id uuid;
v_points integer;
v_clean text;
v_exists boolean;
v_earned integer;
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 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 not exists (select 1 from public.registrations
where event_id = v_event_id and contact_email = v_email) then
raise exception 'you must be registered for this event to give feedback';
end if;
v_clean := nullif(btrim(coalesce(p_comment, '')), '');
-- First submission only gets the award; an edit of an existing answer
-- (or an ineligible contact) earns nothing.
select exists (select 1 from public.event_feedback
where event_id = v_event_id and contact_email = v_email) into v_exists;
v_earned := case
when v_exists then 0
when not public._points_eligible(v_event_id, v_email) then 0
else 5
end;
insert into public.event_feedback (event_id, contact_email, rating, comment)
values (v_event_id, v_email, p_rating, v_clean)
on conflict (event_id, contact_email) do update
set rating = excluded.rating, comment = excluded.comment
returning id into v_fb_id;
if v_earned > 0 then
perform public._award_points(v_event_id, v_email, v_earned, 'feedback');
end if;
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;
$$;
grant execute on function public.submit_event_feedback(integer, text) to public, authenticated;