Files
faunosteo/supabase/migrations/20261103100000_sheet_draft_last_save.sql
T

95 lines
4.6 KiB
PL/PgSQL

-- V1-2.6 M8 `sheet_draft_last_save`: who saved the draft's current revision.
-- docs/development/V1-2_ANATOMICAL_SHEETS.md §12.3 (M8), §16.7; owner
-- decision 2026-11-03.
--
-- The editor's conflict dialog names the account that saved the draft and
-- when. `get_official_sheet_admin` therefore answers `draft_last_save`:
-- {revision, saved_at, saved_by} read from the newest
-- `anatomical_sheet_version.saved` audit row of the draft whose revision is
-- the draft's current one. Null when the draft has no draft, or when its
-- current revision was not written by a save (a fresh draft, a content write
-- outside `save_sheet_draft`). Read change only: body otherwise identical to
-- M7, no table, policy or privilege change.
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),
'draft_last_save', (
select jsonb_build_object('revision', d.revision, 'saved_at', l.occurred_at,
'saved_by', private.sheet_admin_person(l.actor_user_id))
from public.anatomical_sheet_versions d
join lateral (
select a.occurred_at, a.actor_user_id from audit.audit_logs a
where a.resource_type = 'anatomical_sheet_version'
and a.resource_id = d.id
and a.action = 'anatomical_sheet_version.saved'
and a.metadata ->> 'revision' = d.revision::text
order by a.occurred_at desc limit 1) l on true
where d.sheet_id = get_official_sheet_admin.sheet_id and d.status = 'draft'
order by d.version_number desc limit 1),
'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.get_official_sheet_admin(uuid) is
'Capability holders, live aal2 session: one official sheet''s summary, versions (authors named), rounds (actors named) and who last saved the draft''s current revision; unknown and forbidden ids answer not_found (V1-2 contract §11.1, M7, M8).';
-- Kept by `create or replace`, stated again so this file stands alone.
revoke all on function public.get_official_sheet_admin(uuid) from public, anon, service_role;
grant execute on function public.get_official_sheet_admin(uuid) to authenticated;