Files
faunosteo/supabase/migrations/20261116100000_sheet_admin_last_modified.sql
T
bchanotandClaude e3495df088 feat(supabase): last_modified_at in the admin sheet summary (M9)
Admin sheet summary answers last_modified_at: newest of the sheet row and
its versions' updated_at. Read change only, body otherwise identical to M7.
pgTAP 022: plan 60 to 72, 12 assertions added, none changed.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-11-16 01:15:49 +01:00

73 lines
3.6 KiB
PL/PgSQL

-- V1-2.6 R3 M9 `sheet_admin_last_modified`: a true last-modification date in
-- the administration reads. docs/development/V1-2-6_R3_SQL_CHECKPOINT.md
-- (design accepted by the owner 2026-11-09, BDR-109, arbitration A7).
--
-- The admin list dated a sheet by `anatomical_sheets.updated_at`, which a
-- draft save does not move: `save_sheet_draft` writes
-- `anatomical_sheet_versions` only. The summary now also answers
-- `last_modified_at`, the newest of the sheet row's `updated_at` and its
-- versions' `updated_at`, both kept by the existing `set_updated_at`
-- triggers. Review rounds and audit rows do not count. Read change only: body
-- otherwise identical to M7, no table, policy, index or privilege change;
-- `updated_at` keeps its meaning.
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,
'last_modified_at', greatest(s.updated_at, (
select max(v.updated_at) from public.anatomical_sheet_versions v where v.sheet_id = s.id)),
'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;
$$;
-- Kept by `create or replace`, stated again so this file stands alone.
revoke all on function private.official_sheet_admin_summary(uuid) from public, anon, authenticated, service_role;