- contacts.password now auto-syncs to auth.users.encrypted_password via trg_contact_password_sync, so setting a password on the contact row in the Supabase UI makes it work with signInWithPassword immediately (fixes staff like Bruno not being able to log in with a UI-set password). One-time reconciliation in the migration. - Staff excluded from the leaderboard: _sync_leaderboard skips/removes staff rows, get_leaderboard() filters type='staff', seed no longer inserts staff. Affects the attendee Leaderboard and the admin Dashboard 'Top points'. - Home/Agenda/Profile identical for all roles: Home quick-action always 'Scan & connect' (no 'Check-in' relabel), footer role badge removed, tab label always 'Scan' (staff keep check-in via the Scan screen mode toggle). - Profile: removed the duplicate Staff pill (type pill already shows Staff). - Agenda session sheet: 'Download material' moved to the bottom of the button stack with spacing. - Docs updated (schema, rpcs, seed, screens, architecture, next-steps). 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]>
109 lines
4.7 KiB
PL/PgSQL
109 lines
4.7 KiB
PL/PgSQL
-- =============================================================================
|
|
-- Convivo — Staff exclusions + contact-password → auth sync
|
|
--
|
|
-- 1. `contacts.password` now drives the Auth password. Setting it (e.g. from the
|
|
-- Supabase Table Editor) re-hashes it into `auth.users.encrypted_password`,
|
|
-- so an admin/staff member can log in with that password. This is what makes
|
|
-- "set the password on the contact row in Supabase" actually work for login.
|
|
--
|
|
-- 2. Staff are excluded from the leaderboard (the "top points" board). They can
|
|
-- still hold points on their registration, but they never appear on the
|
|
-- public leaderboard or the admin Dashboard's "Top points".
|
|
-- =============================================================================
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 1. CONTACT PASSWORD → AUTH PASSWORD SYNC
|
|
-- -----------------------------------------------------------------------------
|
|
-- When `contacts.password` is set/changed, re-hash it as the Auth password so
|
|
-- the value works with `signInWithPassword`. Security-definer (owner postgres)
|
|
-- so it can write `auth.users`. pgcrypto lives in the `extensions` schema.
|
|
create or replace function public._sync_contact_password()
|
|
returns trigger
|
|
language plpgsql security definer set search_path = public as $$
|
|
begin
|
|
if new.password is not null then
|
|
update auth.users
|
|
set encrypted_password = extensions.crypt(new.password, extensions.gen_salt('bf'))
|
|
where email = new.email;
|
|
end if;
|
|
return new;
|
|
end;
|
|
$$;
|
|
|
|
drop trigger if exists trg_contact_password_sync on public.contacts;
|
|
create trigger trg_contact_password_sync
|
|
after update of password on public.contacts
|
|
for each row
|
|
when (new.password is distinct from old.password and new.password is not null)
|
|
execute function public._sync_contact_password();
|
|
|
|
comment on column public.contacts.password is
|
|
'Admin/staff login password (event-independent). Setting this value re-hashes '
|
|
'it into auth.users.encrypted_password (see trg_contact_password_sync), so it '
|
|
'works with signInWithPassword. Set it directly in Supabase.';
|
|
|
|
-- One-time reconciliation: any contact that already has a password set (e.g.
|
|
-- entered in the Supabase Table Editor before this migration) gets it synced
|
|
-- into auth.users now, so it works immediately.
|
|
update auth.users u
|
|
set encrypted_password = extensions.crypt(c.password, extensions.gen_salt('bf'))
|
|
from public.contacts c
|
|
where u.email = c.email
|
|
and c.password is not null;
|
|
|
|
-- -----------------------------------------------------------------------------
|
|
-- 2. LEADERBOARD — EXCLUDE STAFF
|
|
-- -----------------------------------------------------------------------------
|
|
-- `_sync_leaderboard` is the single writer of the leaderboard mirror. Make it
|
|
-- skip staff (and remove any existing staff row), so staff never appear on the
|
|
-- board even if they earn points.
|
|
create or replace function public._sync_leaderboard(p_event_id uuid, p_contact_email text) returns void
|
|
language plpgsql security definer set search_path = public as $$
|
|
declare
|
|
v_type text;
|
|
begin
|
|
select r.type into v_type
|
|
from public.registrations r
|
|
where r.contact_email = p_contact_email and r.event_id = p_event_id;
|
|
|
|
-- Staff (or no registration) are excluded from the leaderboard.
|
|
if v_type is null or v_type = 'staff' 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 p_event_id, c.email, c.full_name, r.points
|
|
from public.contacts c
|
|
join public.registrations r on r.contact_email = c.email and r.event_id = p_event_id
|
|
where c.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;
|
|
$$;
|
|
|
|
-- Defense in depth: the read function also filters staff, so the board is
|
|
-- correct even if a stray staff row exists in the table.
|
|
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 = public 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
|
|
left join public.registrations r
|
|
on r.contact_email = l.contact_email and r.event_id = l.event_id
|
|
where e.is_active
|
|
and coalesce(r.type, 'attendee') <> 'staff'
|
|
order by l.points desc, l.full_name asc;
|
|
$$;
|
|
|
|
-- One-time cleanup: remove any staff rows already in the leaderboard.
|
|
delete from public.leaderboard l
|
|
using public.registrations r
|
|
where r.event_id = l.event_id
|
|
and r.contact_email = l.contact_email
|
|
and r.type = 'staff';
|