-- ============================================================================= -- Convivo — Talk networking incentives -- -- A per-event configurable bonus awarded when two people who were checked into -- the SAME session (talk) accept a connection. This rewards "networking around -- talks": meeting someone who was in the same talk and making a connection. -- -- 1. `events.talk_networking_bonus` (integer, default 2) — the bonus points -- awarded to EACH party when they share a checked-in session and accept a -- connection. **0 disables the feature** (no bonus, the +5 connection award -- still applies). -- -- 2. `accept_connection(uuid)` is re-created (append-only migrations can't -- edit it, and the return type changes, so we drop + recreate) to: -- - keep the existing +5-to-both connection award, -- - compute whether both parties share a checked-in session, -- - if they do AND `talk_networking_bonus > 0`, award the bonus to each -- party (category `talk_networking`), -- - return a new `shared_talk` column (so the app can show a "met at -- {talk}" note). -- -- Idempotency: `accept_connection` is one-shot (row locked, status must be -- `pending`), so the bonus can never double-award. Documented limitation: -- check-ins that happen AFTER acceptance do not retro-award the bonus — the -- bonus reflects shared attendance at acceptance time. -- -- The bonus flows through `_award_points`, so the leaderboard sync + staff -- exclusion are inherited automatically (no change to `_sync_leaderboard`). -- ============================================================================= -- ----------------------------------------------------------------------------- -- 1. events.talk_networking_bonus -- ----------------------------------------------------------------------------- alter table public.events add column if not exists talk_networking_bonus integer not null default 2; comment on column public.events.talk_networking_bonus is 'Bonus points awarded to EACH party when two people who were checked into the ' 'same session (talk) accept a connection. 0 disables the feature. ' 'Awarded with category "talk_networking".'; -- Widen the points_log.category CHECK to include the new `talk_networking` -- category (the original constraint only allowed the four original categories). alter table public.points_log drop constraint if exists points_log_category_check; alter table public.points_log add constraint points_log_category_check check (category in ('checkin', 'connection', 'feedback', 'admin', 'talk_networking')); -- ----------------------------------------------------------------------------- -- 2. accept_connection — re-created to add the talk networking bonus -- (The return type changes from `public.connections` to a table that adds -- `shared_talk`, so we must drop the existing function first.) -- ----------------------------------------------------------------------------- drop function if exists public.accept_connection(uuid); create or replace function public.accept_connection(p_connection_id uuid) returns table ( event_id uuid, from_email text, to_email text, status text, created_at timestamptz, resolved_at timestamptz, shared_talk boolean ) language plpgsql security definer set search_path = public as $$ declare v_email text := public.current_email(); v_conn public.connections; v_shared boolean; v_bonus integer; 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'); -- Talk networking bonus: do the two parties share a checked-in session (talk)? select count(*) > 0 into v_shared from public.checkins a where a.event_id = v_conn.event_id and a.contact_email = v_conn.from_email and exists ( select 1 from public.checkins b where b.event_id = v_conn.event_id and b.contact_email = v_conn.to_email and b.session_id = a.session_id ); select coalesce(talk_networking_bonus, 0) into v_bonus from public.events where id = v_conn.event_id; if v_shared and v_bonus > 0 then perform public._award_points(v_conn.event_id, v_conn.from_email, v_bonus, 'talk_networking'); perform public._award_points(v_conn.event_id, v_conn.to_email, v_bonus, 'talk_networking'); end if; select * into v_conn from public.connections where id = p_connection_id; -- A single final `return query` (a `return query` inside an `if` block would -- not terminate the function and would double-award). return query select v_conn.event_id, v_conn.from_email, v_conn.to_email, v_conn.status, v_conn.created_at, v_conn.resolved_at, v_shared; end; $$; grant execute on function public.accept_connection(uuid) to authenticated; -- ----------------------------------------------------------------------------- -- 3. get_talk_cohort(p_session_id) — who else was checked into the same talk -- The `checkins` RLS policy is own-or-admin, so an attendee cannot read -- other people's check-ins directly. This security-definer RPC returns the -- other attendees (email + full_name + registration type + QR token, for -- the Connect action) who were checked into the given session. The caller's -- own row is excluded. Staff are excluded (they are not "attendees" of the -- networking game). -- ----------------------------------------------------------------------------- create or replace function public.get_talk_cohort(p_session_id uuid) returns table (email text, full_name text, registration_type public.registration_type, qr_token text) language sql stable security definer set search_path = public as $$ select c.email, c.full_name, r."type", r.qr_token from public.checkins ck join public.registrations r on r.event_id = ck.event_id and r.contact_email = ck.contact_email join public.contacts c on c.email = ck.contact_email where ck.session_id = p_session_id and r."type" <> 'staff' and c.email <> public.current_email(); $$; grant execute on function public.get_talk_cohort(uuid) to authenticated;