Files
faunosteo/supabase/migrations/20261102100000_sheet_admin_reads.sql
T
bchanot b0a4cbe588 feat(supabase): M7 admin read RPCs for official sheets (V1-2.5 B)
list_official_sheets_admin and get_official_sheet_admin: badges, version and round states, named actors; capability holders at aal2, one answer for unknown or forbidden ids, no write path. pgTAP 022 (40); sandbox fresh and upgrade from M6: 1938 pass, types match.
2026-11-02 16:19:48 +01:00

191 lines
8.8 KiB
PL/PgSQL

-- V1-2.5 M7 `sheet_admin_reads`: the read RPCs of the administration space.
-- docs/development/V1-2_ANATOMICAL_SHEETS.md §11.1 (`/app/admin/sheets`, its
-- overview), §12.3 (M7), §16.6; owner decision 2026-11-02 (BDR-097).
--
-- The admin list shows the historical and working-copy badges, the version
-- and round states and the rounds awaiting review. The reader column grants
-- of M4 leave out `working_copy_source_version_id`, `last_version_number`,
-- `updated_at` and the version authors on purpose (§5.3), so the admin space
-- reads them here, never through a wider grant:
-- * list_official_sheets_admin: every official sheet that is not deleted,
-- one summary each;
-- * get_official_sheet_admin: one sheet's summary, its versions and its
-- rounds, with the names of the people who acted.
-- Capability holders at aal2 only (`caller_is_catalog_staff`, the predicate
-- of the draft reads); an unknown or forbidden sheet gets one answer. No
-- write, no grant, no policy and no rule changes here.
-- ===========================================================================
-- 1. Helpers
-- ===========================================================================
-- An account as the admin space names it: id and display name.
create or replace function private.sheet_admin_person(p_user_id uuid)
returns jsonb
language sql
stable
security definer
set search_path = ''
as $$
select case when p_user_id is null then null else jsonb_build_object(
'user_id', p_user_id,
'display_name', (select p.display_name from public.profiles p where p.id = p_user_id)
) end;
$$;
-- One official sheet as the admin list shows it.
create or replace function private.official_sheet_admin_summary(p_sheet_id uuid)
returns jsonb
language sql
stable
security definer
set search_path = ''
as $$
select jsonb_build_object(
'sheet_id', s.id,
'species_id', s.species_id,
'slug', s.slug,
'status', s.status,
'archived_at', s.archived_at,
'created_at', s.created_at,
'updated_at', s.updated_at,
'name', coalesce(latest.content -> 'name', jsonb_build_object('fr', s.name, 'en', s.name)),
'historical', s.id = private.historical_sheet_id(),
'working_copy', private.is_historical_working_copy(s.id),
-- The single slot of §14.3, read where the action is offered.
'working_copy_active', case when s.id = private.historical_sheet_id()
then private.historical_working_copy_active(null) end,
'latest_version', case when latest.id is not null then jsonb_build_object(
'version_id', latest.id, 'version_number', latest.version_number,
'status', latest.status, 'revision', latest.revision) end,
'current_version', case when cur.id is not null then jsonb_build_object(
'version_id', cur.id, 'version_number', cur.version_number, 'published_at', cur.published_at) end,
'draft_version', (
select jsonb_build_object('version_id', d.id, 'version_number', d.version_number, 'revision', d.revision)
from public.anatomical_sheet_versions d
where d.sheet_id = s.id and d.status = 'draft'
order by d.version_number desc limit 1),
'version_statuses', coalesce((
select jsonb_agg(distinct v.status) from public.anatomical_sheet_versions v where v.sheet_id = s.id),
'[]'::jsonb),
'open_rounds', (
select count(*) from public.anatomical_sheet_reviews r
join public.anatomical_sheet_versions v on v.id = r.version_id
where v.sheet_id = s.id and r.status = 'open'),
'latest_round', (
select jsonb_build_object('review_id', r.id, 'version_number', v.version_number, 'round', r.round,
'status', r.status, 'submitted_at', r.submitted_at)
from public.anatomical_sheet_reviews r
join public.anatomical_sheet_versions v on v.id = r.version_id
where v.sheet_id = s.id
order by r.submitted_at desc nulls last, v.version_number desc, r.round desc limit 1)
)
from public.anatomical_sheets s
left join public.anatomical_sheet_versions cur on cur.id = s.current_version_id
left join lateral (
select v.* from public.anatomical_sheet_versions v
where v.sheet_id = s.id order by v.version_number desc limit 1
) latest on true
where s.id = p_sheet_id;
$$;
-- ===========================================================================
-- 2. Read RPCs
-- ===========================================================================
create or replace function public.list_official_sheets_admin()
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
begin
perform private.require_catalog_actor();
if not private.caller_is_catalog_staff() then
raise exception 'rights.capability' using errcode = '42501';
end if;
return coalesce((
select jsonb_agg(private.official_sheet_admin_summary(s.id) order by s.species_id, s.slug)
from public.anatomical_sheets s
where s.is_official and s.status <> 'deleted'
), '[]'::jsonb);
end;
$$;
create or replace function public.get_official_sheet_admin(sheet_id uuid)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
#variable_conflict use_column
begin
perform private.require_catalog_actor();
-- Unknown, deleted, personal and forbidden sheets: one answer (§10).
if get_official_sheet_admin.sheet_id is null
or not private.caller_can_see_official_sheet(get_official_sheet_admin.sheet_id) then
raise exception 'not_found' using errcode = '42501';
end if;
return private.official_sheet_admin_summary(get_official_sheet_admin.sheet_id) || jsonb_build_object(
'working_copy_source', (
select jsonb_build_object('version_id', src.id, 'version_number', src.version_number)
from public.anatomical_sheets s
join public.anatomical_sheet_versions src on src.id = s.working_copy_source_version_id
where s.id = get_official_sheet_admin.sheet_id),
'versions', coalesce((
select jsonb_agg(jsonb_build_object(
'version_id', v.id,
'version_number', v.version_number,
'status', v.status,
'revision', v.revision,
'change_note', v.change_note,
'created_at', v.created_at,
'published_at', v.published_at,
'deprecated_at', v.deprecated_at,
'source_version_number', (select src.version_number from public.anatomical_sheet_versions src
where src.id = v.source_version_id),
'authors', coalesce((select jsonb_agg(private.sheet_admin_person(a.id) order by a.ord)
from unnest(v.authors) with ordinality a(id, ord)), '[]'::jsonb)
) order by v.version_number desc)
from public.anatomical_sheet_versions v
where v.sheet_id = get_official_sheet_admin.sheet_id), '[]'::jsonb),
'rounds', coalesce((
select jsonb_agg(jsonb_build_object(
'review_id', r.id,
'version_id', v.id,
'version_number', v.version_number,
'round', r.round,
'revision', r.revision,
'status', r.status,
'submitted_by', private.sheet_admin_person(r.submitted_by),
'submitted_at', r.submitted_at,
'verified_at', r.verified_at,
'failure_outcome', r.failure_outcome,
'failure_code', r.failure_code,
'decided_by', private.sheet_admin_person(r.decided_by),
'decided_at', r.decided_at,
'withdrawn_at', r.withdrawn_at
) order by r.submitted_at desc nulls last, v.version_number desc, r.round desc)
from public.anatomical_sheet_reviews r
join public.anatomical_sheet_versions v on v.id = r.version_id
where v.sheet_id = get_official_sheet_admin.sheet_id), '[]'::jsonb)
);
end;
$$;
comment on function public.list_official_sheets_admin() is
'Capability holders, live aal2 session: every official sheet that is not deleted, with its historical and working-copy badges, version and round states (V1-2 contract §11.1, M7).';
comment on function public.get_official_sheet_admin(uuid) is
'Capability holders, live aal2 session: one official sheet''s summary, versions (authors named) and rounds (actors named); unknown and forbidden ids answer not_found (V1-2 contract §11.1, M7).';
-- ===========================================================================
-- 3. Privileges
-- ===========================================================================
revoke all on function private.sheet_admin_person(uuid) from public, anon, authenticated, service_role;
revoke all on function private.official_sheet_admin_summary(uuid) from public, anon, authenticated, service_role;
revoke all on function public.list_official_sheets_admin() from public, anon, service_role;
revoke all on function public.get_official_sheet_admin(uuid) from public, anon, service_role;
grant execute on function public.list_official_sheets_admin() to authenticated;
grant execute on function public.get_official_sheet_admin(uuid) to authenticated;