201 lines
11 KiB
PL/PgSQL
201 lines
11 KiB
PL/PgSQL
alter table public.registrations add column import_source text
|
|
check (import_source in ('sessionize'));
|
|
|
|
update public.registrations registration set import_source = 'sessionize'
|
|
from public.contacts contact
|
|
where contact.email = registration.contact_email
|
|
and nullif(btrim(contact.external_id), '') is not null
|
|
and nullif(btrim(registration.qr_token), '') is null
|
|
and nullif(btrim(registration.external_id), '') is null
|
|
and registration.type in ('speaker', 'staff');
|
|
|
|
create table public.ticket_merges (
|
|
event_id uuid not null references public.events(id) on delete cascade,
|
|
source_email text not null references public.contacts(email) on delete cascade,
|
|
target_email text not null references public.contacts(email) on delete cascade,
|
|
transferred_type public.registration_type not null,
|
|
merged_by uuid not null,
|
|
merged_at timestamptz not null default now(),
|
|
primary key (event_id, source_email),
|
|
check (source_email <> target_email),
|
|
check (transferred_type in ('speaker', 'staff'))
|
|
);
|
|
alter table public.ticket_merges enable row level security;
|
|
revoke all on public.ticket_merges from public, anon, authenticated;
|
|
grant select on public.ticket_merges to authenticated;
|
|
create policy ticket_merges_admin_read on public.ticket_merges
|
|
for select to authenticated using (public.is_admin());
|
|
|
|
create function public._ticket_email(p_event_id uuid, p_email text)
|
|
returns text language sql stable security definer set search_path = '' as $$
|
|
select coalesce((select target_email from public.ticket_merges
|
|
where event_id = p_event_id and source_email = p_email), p_email);
|
|
$$;
|
|
revoke all on function public._ticket_email(uuid, text) from public, anon, authenticated;
|
|
|
|
create function public._apply_ticket_merge_registration()
|
|
returns trigger language plpgsql security definer set search_path = '' as $$
|
|
declare
|
|
mapping public.ticket_merges;
|
|
begin
|
|
select * into mapping from public.ticket_merges
|
|
where event_id = new.event_id and source_email = new.contact_email;
|
|
if found then
|
|
if nullif(btrim(new.qr_token), '') is not null then
|
|
raise exception 'This imported account was merged; reconcile its new ticket manually';
|
|
end if;
|
|
update public.registrations set type = case
|
|
when type = 'staff' or mapping.transferred_type = 'staff' then 'staff'::public.registration_type
|
|
else 'speaker'::public.registration_type end
|
|
where event_id = mapping.event_id and contact_email = mapping.target_email;
|
|
if not found then raise exception 'Merged ticket no longer exists'; end if;
|
|
return null;
|
|
end if;
|
|
select * into mapping from public.ticket_merges
|
|
where event_id = new.event_id and target_email = new.contact_email
|
|
order by (transferred_type = 'staff') desc limit 1;
|
|
if found then
|
|
new.type := case when new.type = 'staff' or mapping.transferred_type = 'staff'
|
|
then 'staff'::public.registration_type else 'speaker'::public.registration_type end;
|
|
end if;
|
|
return new;
|
|
end;
|
|
$$;
|
|
revoke all on function public._apply_ticket_merge_registration() from public, anon, authenticated;
|
|
create trigger ticket_merge_registration before insert or update on public.registrations
|
|
for each row execute function public._apply_ticket_merge_registration();
|
|
|
|
do $$
|
|
declare
|
|
function_name text;
|
|
definition text;
|
|
needle text := E'insert into public.registrations (event_id, contact_email, type)\n values (p_event_id, v_email, ''speaker''::public.registration_type)';
|
|
begin
|
|
foreach function_name in array array['sync_sessionize_event', 'sync_sessionize_excel'] loop
|
|
definition := pg_get_functiondef(to_regprocedure('public.' || function_name || '(uuid,jsonb)'));
|
|
if function_name = 'sync_sessionize_excel' and position(needle in definition) = 0 then
|
|
raise exception 'Unexpected Sessionize registration definition: %', function_name;
|
|
end if;
|
|
if position('values (v_session.id, v_spk)' in definition) = 0
|
|
or position('where e = ss.contact_email' in definition) = 0 then
|
|
raise exception 'Unexpected Sessionize speaker definition: %', function_name;
|
|
end if;
|
|
definition := replace(definition, needle,
|
|
E'insert into public.registrations (event_id, contact_email, type, import_source)\n values (p_event_id, v_email, ''speaker''::public.registration_type, ''sessionize'')');
|
|
if function_name = 'sync_sessionize_excel' then
|
|
definition := replace(definition, E' v_email := v_item ->> ''email'';',
|
|
E' v_email := public._ticket_email(p_event_id, v_item ->> ''email'');');
|
|
definition := replace(definition, 'set type = case when public.registrations.type',
|
|
E'set import_source = ''sessionize'',\n type = case when public.registrations.type');
|
|
definition := replace(definition, E'\n where public.registrations.type not in (''staff'', ''sponsor'');', ';');
|
|
end if;
|
|
definition := replace(definition, 'values (v_session.id, v_spk)',
|
|
'values (v_session.id, public._ticket_email(p_event_id, v_spk))');
|
|
definition := replace(definition, 'where e = ss.contact_email',
|
|
'where public._ticket_email(p_event_id, e) = ss.contact_email');
|
|
execute definition;
|
|
end loop;
|
|
end;
|
|
$$;
|
|
|
|
create or replace function public._recalc_event_aggregates(p_event_id uuid) returns void
|
|
language plpgsql security definer set search_path = '' as $$
|
|
begin
|
|
update public.events set
|
|
num_sessions = (select count(*) from public.sessions where event_id = p_event_id),
|
|
num_speakers = (select count(distinct speaker.contact_email) from public.session_speakers speaker
|
|
join public.sessions talk on talk.id = speaker.session_id where talk.event_id = p_event_id),
|
|
num_registrations = (select count(*) from public.registrations
|
|
where event_id = p_event_id and nullif(btrim(qr_token), '') is not null),
|
|
num_checked_in_registrations = (select count(*) from public.registrations
|
|
where event_id = p_event_id and checked_in and nullif(btrim(qr_token), '') is not null),
|
|
avg_feedback = (select avg(rating) from public.feedback where event_id = p_event_id)
|
|
where id = p_event_id;
|
|
end;
|
|
$$;
|
|
|
|
create function public.merge_tickets(p_source_id uuid, p_target_id uuid)
|
|
returns jsonb language plpgsql security definer set search_path = '' as $$
|
|
declare
|
|
source_registration public.registrations;
|
|
target_registration public.registrations;
|
|
transferred_type public.registration_type;
|
|
session_count integer;
|
|
begin
|
|
if auth.uid() is null or not public.is_admin() then
|
|
raise exception 'admin only' using errcode = '42501';
|
|
end if;
|
|
if p_source_id is null or p_target_id is null or p_source_id = p_target_id then
|
|
raise exception 'Pick two different registrations';
|
|
end if;
|
|
select * into source_registration from public.registrations where id = p_source_id;
|
|
if not found then raise exception 'Imported registration no longer exists'; end if;
|
|
perform 1 from public.events where id = source_registration.event_id for update;
|
|
perform 1 from public.registrations where id in (p_source_id, p_target_id) order by id for update;
|
|
select * into source_registration from public.registrations where id = p_source_id;
|
|
if not found then raise exception 'Imported registration no longer exists'; end if;
|
|
select * into target_registration from public.registrations where id = p_target_id;
|
|
if not found then raise exception 'Ticket no longer exists'; end if;
|
|
if source_registration.event_id <> target_registration.event_id then
|
|
raise exception 'Tickets must belong to the same event';
|
|
end if;
|
|
if source_registration.import_source is distinct from 'sessionize'
|
|
or nullif(btrim(source_registration.qr_token), '') is not null
|
|
or source_registration.type not in ('speaker', 'staff') then
|
|
raise exception 'Source must be an imported speaker/staff registration without a QR';
|
|
end if;
|
|
if nullif(btrim(target_registration.qr_token), '') is null
|
|
or nullif(btrim(target_registration.external_id), '') is null then
|
|
raise exception 'Destination must be a purchased Pretix ticket';
|
|
end if;
|
|
if exists (select 1 from public.ticket_merges where event_id = source_registration.event_id
|
|
and (target_email = source_registration.contact_email or source_email = target_registration.contact_email)) then
|
|
raise exception 'Merge chains are not supported';
|
|
end if;
|
|
perform 1 from public.contacts
|
|
where email in (source_registration.contact_email, target_registration.contact_email)
|
|
order by email for update;
|
|
if source_registration.checked_in or source_registration.points <> 0
|
|
or exists (select 1 from public.preferences where event_id = source_registration.event_id and contact_email = source_registration.contact_email)
|
|
or exists (select 1 from public.checkins where event_id = source_registration.event_id and contact_email = source_registration.contact_email)
|
|
or exists (select 1 from public.feedback where event_id = source_registration.event_id and contact_email = source_registration.contact_email)
|
|
or exists (select 1 from public.points_log where event_id = source_registration.event_id and contact_email = source_registration.contact_email)
|
|
or exists (select 1 from public.connections where event_id = source_registration.event_id
|
|
and (from_email = source_registration.contact_email or to_email = source_registration.contact_email)) then
|
|
raise exception 'Imported account has attendee activity; reconcile it before merging';
|
|
end if;
|
|
transferred_type := case when source_registration.type = 'staff' or target_registration.type = 'staff'
|
|
then 'staff'::public.registration_type else 'speaker'::public.registration_type end;
|
|
perform 1 from public.sessions where event_id = source_registration.event_id order by id for update;
|
|
perform 1 from public.session_speakers speaker join public.sessions talk on talk.id = speaker.session_id
|
|
where talk.event_id = source_registration.event_id order by speaker.session_id, speaker.contact_email for update of speaker;
|
|
select count(*) into session_count from public.session_speakers speaker
|
|
join public.sessions talk on talk.id = speaker.session_id
|
|
where talk.event_id = source_registration.event_id and speaker.contact_email = source_registration.contact_email;
|
|
insert into public.session_speakers(session_id, contact_email)
|
|
select speaker.session_id, target_registration.contact_email from public.session_speakers speaker
|
|
join public.sessions talk on talk.id = speaker.session_id
|
|
where talk.event_id = source_registration.event_id and speaker.contact_email = source_registration.contact_email
|
|
on conflict (session_id, contact_email) do nothing;
|
|
delete from public.session_speakers speaker using public.sessions talk
|
|
where talk.id = speaker.session_id and talk.event_id = source_registration.event_id
|
|
and speaker.contact_email = source_registration.contact_email;
|
|
update public.registrations set type = transferred_type where id = p_target_id;
|
|
insert into public.ticket_merges(event_id, source_email, target_email, transferred_type, merged_by)
|
|
values (source_registration.event_id, source_registration.contact_email, target_registration.contact_email, transferred_type, auth.uid());
|
|
delete from public.registrations where id = p_source_id;
|
|
delete from public.leaderboard where event_id = source_registration.event_id and contact_email = source_registration.contact_email;
|
|
return jsonb_build_object('source_id', p_source_id, 'target_id', p_target_id,
|
|
'target_email', target_registration.contact_email, 'type', transferred_type, 'sessions_transferred', session_count);
|
|
end;
|
|
$$;
|
|
revoke all on function public.merge_tickets(uuid, uuid) from public, anon, authenticated;
|
|
grant execute on function public.merge_tickets(uuid, uuid) to authenticated;
|
|
|
|
do $$ declare event_row record; begin
|
|
for event_row in select id from public.events loop
|
|
perform public._recalc_event_aggregates(event_row.id);
|
|
end loop;
|
|
end; $$;
|
|
notify pgrst, 'reload schema';
|