feat: add per-event badge editor, pretix company import, smart guides
Badge Designer (admin): per-event WYSIWYG badge/PDF layout editor with mm canvas, text/QR/image elements, multi-select, Word+PowerPoint alignment, PowerPoint-style smart guides with snapping, Delete-key, preview and JSON views. Migration 20261015194531_badge_editor (badge_layouts + admin-only save/set-default/delete RPCs). Company import: migration 20261015200719_events_company adds events.company; the sidecar pretix sync now fetches the organizer display name and stores it (best-effort). Bindable on badges as Company/organizer. Tests: 15 SQL rollback assertions + 13 mocked browser tests. Docs synced.
This commit is contained in:
1 parent
3e2f8ff0ed
commit
ab25c102dc
16 files changed
+1914
-9
No files matched your search
@@ -0,0 +1,162 @@
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- Badge editor (per-event WYSIWYG badge / PDF layout, modelled on pretix's
|
||||
-- "PDF Editor / Badge layout").
|
||||
--
|
||||
-- Each event owns one or more named badge layouts. A layout is a page size
|
||||
-- (millimetres) plus an ordered list of elements stored as JSON. The Admin app
|
||||
-- renders/edits the layout on a canvas and previews it against a real sample
|
||||
-- attendee; the element geometry is millimetre-based so it maps 1:1 to print.
|
||||
--
|
||||
-- `elements` is a JSON array; each element is an object:
|
||||
-- {
|
||||
-- "id": string, -- stable client id (uuid-ish)
|
||||
-- "type": "text" | "qr" | "image",
|
||||
-- "x": mm, "y": mm, "width": mm, "height": mm, "rotation": deg,
|
||||
-- -- type = "text":
|
||||
-- "content": "attendee_name" | "full_name" | "email" | "ticket_type"
|
||||
-- | "event_name" | "qr_token" | "static",
|
||||
-- "text": string, -- literal text when content = "static"
|
||||
-- "font": string, "font_size": pt, "line_height": number,
|
||||
-- "color": "#rrggbb", "bold": bool, "italic": bool,
|
||||
-- "align_h": "left" | "center" | "right",
|
||||
-- "align_v": "top" | "middle" | "bottom",
|
||||
-- "transform": "none" | "uppercase" | "lowercase" | "capitalize",
|
||||
-- -- type = "qr":
|
||||
-- "value": "qr_token" | "checkin_url" | "email" | "attendee_name" | "static",
|
||||
-- "static_value": string, -- literal payload when value = "static"
|
||||
-- -- type = "image":
|
||||
-- "src": string, -- absolute URL or data: URI
|
||||
-- "fit": "contain" | "cover" | "fill"
|
||||
-- }
|
||||
-- -----------------------------------------------------------------------------
|
||||
|
||||
create table public.badge_layouts (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
event_id uuid not null references public.events (id) on delete cascade,
|
||||
name text not null check (btrim(name) <> ''),
|
||||
is_default boolean not null default false,
|
||||
page_width_mm numeric not null default 100 check (page_width_mm > 0 and page_width_mm <= 1000),
|
||||
page_height_mm numeric not null default 70 check (page_height_mm > 0 and page_height_mm <= 1000),
|
||||
elements jsonb not null default '[]'::jsonb check (jsonb_typeof(elements) = 'array'),
|
||||
created_at timestamptz not null default now(),
|
||||
updated_at timestamptz not null default now(),
|
||||
unique (event_id, name)
|
||||
);
|
||||
|
||||
create index badge_layouts_event_id_idx on public.badge_layouts (event_id);
|
||||
-- At most one default layout per event.
|
||||
create unique index badge_layouts_one_default on public.badge_layouts (event_id) where is_default;
|
||||
|
||||
alter table public.badge_layouts enable row level security;
|
||||
revoke all on public.badge_layouts from public, anon, authenticated;
|
||||
grant select on public.badge_layouts to authenticated;
|
||||
grant select, insert, update, delete on public.badge_layouts to service_role;
|
||||
|
||||
create policy badge_layouts_admin_read on public.badge_layouts
|
||||
for select to authenticated using (public.is_admin());
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- Write RPCs. All are admin-only (checked inside, mirroring the raffle RPCs) so
|
||||
-- the exclusive-default invariant is enforced transactionally rather than by
|
||||
-- racing client writes.
|
||||
-- -----------------------------------------------------------------------------
|
||||
|
||||
-- Insert or update a layout (matched on event_id + name). When p_is_default is
|
||||
-- true, every other layout for the event is demoted first.
|
||||
create or replace function public.save_badge_layout(
|
||||
p_event_id uuid,
|
||||
p_name text,
|
||||
p_page_width_mm numeric,
|
||||
p_page_height_mm numeric,
|
||||
p_elements jsonb,
|
||||
p_is_default boolean default false
|
||||
) returns public.badge_layouts
|
||||
language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
v_row public.badge_layouts;
|
||||
begin
|
||||
if not public.is_admin() then
|
||||
raise exception 'admin only' using errcode = '42501';
|
||||
end if;
|
||||
if p_name is null or btrim(p_name) = '' then
|
||||
raise exception 'layout name is required';
|
||||
end if;
|
||||
if jsonb_typeof(p_elements) <> 'array' then
|
||||
raise exception 'elements must be a JSON array';
|
||||
end if;
|
||||
|
||||
if p_is_default then
|
||||
update public.badge_layouts
|
||||
set is_default = false
|
||||
where event_id = p_event_id and name <> btrim(p_name);
|
||||
end if;
|
||||
|
||||
insert into public.badge_layouts
|
||||
(event_id, name, is_default, page_width_mm, page_height_mm, elements, updated_at)
|
||||
values
|
||||
(p_event_id, btrim(p_name), p_is_default, p_page_width_mm, p_page_height_mm, p_elements, now())
|
||||
on conflict (event_id, name) do update
|
||||
set is_default = excluded.is_default,
|
||||
page_width_mm = excluded.page_width_mm,
|
||||
page_height_mm = excluded.page_height_mm,
|
||||
elements = excluded.elements,
|
||||
updated_at = now()
|
||||
returning * into v_row;
|
||||
return v_row;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Promote a layout to the event's default (demoting the previous one).
|
||||
create or replace function public.set_default_badge_layout(p_id uuid)
|
||||
returns void
|
||||
language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
v_event uuid;
|
||||
begin
|
||||
if not public.is_admin() then
|
||||
raise exception 'admin only' using errcode = '42501';
|
||||
end if;
|
||||
select event_id into v_event from public.badge_layouts where id = p_id;
|
||||
if v_event is null then
|
||||
raise exception 'badge layout not found';
|
||||
end if;
|
||||
update public.badge_layouts set is_default = false where event_id = v_event and id <> p_id;
|
||||
update public.badge_layouts set is_default = true, updated_at = now() where id = p_id;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Delete a layout. The default layout may not be removed while other layouts
|
||||
-- exist (there must always be a usable default).
|
||||
create or replace function public.delete_badge_layout(p_id uuid)
|
||||
returns void
|
||||
language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
v_event uuid;
|
||||
v_is_default boolean;
|
||||
v_count integer;
|
||||
begin
|
||||
if not public.is_admin() then
|
||||
raise exception 'admin only' using errcode = '42501';
|
||||
end if;
|
||||
select event_id, is_default into v_event, v_is_default
|
||||
from public.badge_layouts where id = p_id;
|
||||
if v_event is null then
|
||||
raise exception 'badge layout not found';
|
||||
end if;
|
||||
if v_is_default then
|
||||
select count(*) into v_count from public.badge_layouts where event_id = v_event;
|
||||
if v_count > 1 then
|
||||
raise exception 'cannot delete the default layout while other layouts exist; set another as default first';
|
||||
end if;
|
||||
end if;
|
||||
delete from public.badge_layouts where id = p_id;
|
||||
end;
|
||||
$$;
|
||||
|
||||
revoke all on function public.save_badge_layout(uuid, text, numeric, numeric, jsonb, boolean)
|
||||
from public, anon, authenticated;
|
||||
revoke all on function public.set_default_badge_layout(uuid) from public, anon, authenticated;
|
||||
revoke all on function public.delete_badge_layout(uuid) from public, anon, authenticated;
|
||||
grant execute on function public.save_badge_layout(uuid, text, numeric, numeric, jsonb, boolean) to authenticated;
|
||||
grant execute on function public.set_default_badge_layout(uuid) to authenticated;
|
||||
grant execute on function public.delete_badge_layout(uuid) to authenticated;
|
||||
@@ -0,0 +1,12 @@
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- Organizer / company name for an event.
|
||||
--
|
||||
-- The Pretix organizer resource carries the owning organisation's display name
|
||||
-- (e.g. "Austrian Power Platform Enthusiasts"). The sidecar now pulls it during
|
||||
-- the Pretix sync and stores it here so badges (and other print material) can
|
||||
-- bind a "Company / organizer" field, independent of the event's own name.
|
||||
-- -----------------------------------------------------------------------------
|
||||
|
||||
alter table public.events add column if not exists company text;
|
||||
comment on column public.events.company is
|
||||
'The owning organisation / company (e.g. the Pretix organizer name). Synced from Pretix; also bindable on badges.';
|
||||
@@ -0,0 +1,144 @@
|
||||
\set ON_ERROR_STOP on
|
||||
begin;
|
||||
|
||||
create temporary table badge_results (name text);
|
||||
grant insert on badge_results to authenticated, anon;
|
||||
create function pg_temp.assert_badge(p_condition boolean, p_name text) returns void
|
||||
language plpgsql as $$
|
||||
begin
|
||||
if p_condition is distinct from true then raise exception 'FAIL: %', p_name; end if;
|
||||
insert into badge_results values (p_name);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Two isolated events so per-event scoping can be checked.
|
||||
update public.events set is_active = false where is_active;
|
||||
insert into public.events (id, name, start_date, end_date, is_active) values
|
||||
('b0de0001-0000-0000-0000-000000000001', 'Badge event A', current_date, current_date, true),
|
||||
('b0de0002-0000-0000-0000-000000000002', 'Badge event B', current_date, current_date, false);
|
||||
|
||||
insert into public.contacts (email, full_name, is_admin) values
|
||||
('[email protected]', 'Badge Admin', true),
|
||||
('[email protected]', 'Plain Viewer', false);
|
||||
|
||||
-- Act as the badge admin.
|
||||
select set_config('request.jwt.claims', '{"email":"[email protected]","role":"authenticated"}', true);
|
||||
|
||||
-- Save a default layout for event A.
|
||||
select pg_temp.assert_badge(
|
||||
(select is_default from public.save_badge_layout(
|
||||
'b0de0001-0000-0000-0000-000000000001'::uuid, 'Default', 100, 70,
|
||||
'[{"id":"a","type":"text","x":10,"y":10,"width":60,"height":10,"rotation":0,"content":"attendee_name","text":"","font":"Open Sans","font_size":13,"line_height":1,"color":"#000000","bold":false,"italic":false,"align_h":"left","align_v":"middle","transform":"none"}]'::jsonb, true)),
|
||||
'save creates a default layout');
|
||||
|
||||
-- Upsert the same (event, name) updates in place rather than duplicating.
|
||||
select public.save_badge_layout('b0de0001-0000-0000-0000-000000000001'::uuid, 'Default', 105, 75, '[]'::jsonb, true);
|
||||
select pg_temp.assert_badge(
|
||||
(select count(*) = 1 from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and name = 'Default'),
|
||||
're-saving the same name upserts');
|
||||
select pg_temp.assert_badge(
|
||||
(select page_width_mm = 105 from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and name = 'Default'),
|
||||
'upsert updates page size');
|
||||
|
||||
-- A second default layout demotes the first (only one default per event).
|
||||
select public.save_badge_layout('b0de0001-0000-0000-0000-000000000001'::uuid, 'VIP', 90, 50, '[]'::jsonb, true);
|
||||
select pg_temp.assert_badge(
|
||||
(select count(*) = 1 from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and is_default),
|
||||
'only one default per event after saving a new default');
|
||||
select pg_temp.assert_badge(
|
||||
(select is_default from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and name = 'VIP'),
|
||||
'newest default is the VIP layout');
|
||||
|
||||
-- Layouts are scoped per event: event B has none of its own.
|
||||
select public.save_badge_layout('b0de0002-0000-0000-0000-000000000002'::uuid, 'Default', 100, 70, '[]'::jsonb, true);
|
||||
select pg_temp.assert_badge(
|
||||
(select count(*) = 1 from public.badge_layouts where event_id = 'b0de0002-0000-0000-0000-000000000002'),
|
||||
'event B has its own layout');
|
||||
select pg_temp.assert_badge(
|
||||
(select count(*) = 2 from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001'),
|
||||
'event A still has two layouts');
|
||||
|
||||
-- set_default_badge_layout promotes the target and demotes the rest.
|
||||
select public.set_default_badge_layout(
|
||||
(select id from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and name = 'Default'));
|
||||
select pg_temp.assert_badge(
|
||||
(select is_default from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and name = 'Default'),
|
||||
'set_default promotes the chosen layout');
|
||||
select pg_temp.assert_badge(
|
||||
(select not is_default from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and name = 'VIP'),
|
||||
'set_default demotes the previous default');
|
||||
|
||||
-- The default layout cannot be deleted while other layouts exist.
|
||||
do $$ begin
|
||||
begin
|
||||
perform public.delete_badge_layout(
|
||||
(select id from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and name = 'Default'));
|
||||
raise exception 'default layout delete allowed';
|
||||
exception when raise_exception then
|
||||
if sqlerrm not like 'cannot delete the default layout%' then raise; end if;
|
||||
end;
|
||||
perform pg_temp.assert_badge(true, 'cannot delete default while others exist');
|
||||
end; $$;
|
||||
|
||||
-- A non-default layout deletes cleanly.
|
||||
select public.delete_badge_layout(
|
||||
(select id from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001' and name = 'VIP'));
|
||||
select pg_temp.assert_badge(
|
||||
(select count(*) = 1 from public.badge_layouts where event_id = 'b0de0001-0000-0000-0000-000000000001'),
|
||||
'non-default layout deletes');
|
||||
|
||||
-- Empty / blank names are rejected.
|
||||
do $$ begin
|
||||
begin
|
||||
perform public.save_badge_layout('b0de0001-0000-0000-0000-000000000001'::uuid, ' ', 100, 70, '[]'::jsonb, false);
|
||||
raise exception 'blank name allowed';
|
||||
exception when raise_exception then
|
||||
if sqlerrm <> 'layout name is required' then raise; end if;
|
||||
end;
|
||||
perform pg_temp.assert_badge(true, 'blank layout name rejected');
|
||||
end; $$;
|
||||
|
||||
-- Non-admins cannot write.
|
||||
select set_config('request.jwt.claims', '{"email":"[email protected]","role":"authenticated"}', true);
|
||||
do $$ begin
|
||||
begin
|
||||
perform public.save_badge_layout('b0de0001-0000-0000-0000-000000000001'::uuid, 'Hacked', 100, 70, '[]'::jsonb, false);
|
||||
raise exception 'non-admin save allowed';
|
||||
exception when insufficient_privilege then
|
||||
if sqlerrm <> 'admin only' then raise; end if;
|
||||
end;
|
||||
begin
|
||||
perform public.delete_badge_layout(
|
||||
(select id from public.badge_layouts limit 1));
|
||||
raise exception 'non-admin delete allowed';
|
||||
exception when insufficient_privilege then
|
||||
if sqlerrm <> 'admin only' then raise; end if;
|
||||
end;
|
||||
perform pg_temp.assert_badge(true, 'non-admin cannot save or delete');
|
||||
end; $$;
|
||||
|
||||
-- Non-admins cannot read layouts either (admin-read RLS). Run as the real
|
||||
-- `authenticated` role so RLS is enforced (the superuser would bypass it).
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"email":"[email protected]","role":"authenticated"}', true);
|
||||
select pg_temp.assert_badge(
|
||||
(select count(*) = 0 from public.badge_layouts),
|
||||
'non-admin sees no layouts via RLS');
|
||||
reset role;
|
||||
|
||||
-- The anonymous API role has no execute grant at all.
|
||||
reset role;
|
||||
select set_config('request.jwt.claims', '{"role":"anon"}', true);
|
||||
set local role anon;
|
||||
do $$ begin
|
||||
begin
|
||||
perform public.save_badge_layout('b0de0001-0000-0000-0000-000000000001'::uuid, 'Anon', 100, 70, '[]'::jsonb, false);
|
||||
raise exception 'anon save allowed';
|
||||
exception when insufficient_privilege then null;
|
||||
end;
|
||||
perform pg_temp.assert_badge(true, 'anon cannot execute badge functions');
|
||||
end; $$;
|
||||
reset role;
|
||||
|
||||
select count(*) as passed_assertions from badge_results;
|
||||
rollback;
|
||||
Reference in new issue
Block a user