-- ----------------------------------------------------------------------------- -- 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;