diff --git a/supabase/migrations/20261116100000_sheet_admin_last_modified.sql b/supabase/migrations/20261116100000_sheet_admin_last_modified.sql new file mode 100644 index 00000000..a33b1770 --- /dev/null +++ b/supabase/migrations/20261116100000_sheet_admin_last_modified.sql @@ -0,0 +1,72 @@ +-- 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; diff --git a/supabase/tests/022_sheet_admin_reads.sql b/supabase/tests/022_sheet_admin_reads.sql index 482f16c3..7587b3d0 100644 --- a/supabase/tests/022_sheet_admin_reads.sql +++ b/supabase/tests/022_sheet_admin_reads.sql @@ -4,7 +4,7 @@ -- unknown, deleted, personal or forbidden sheet gets one answer. Fixtures are -- scoped to the ids created here. begin; -select plan(60); +select plan(72); -- Accounts. O owner, A admin, E admin + editor, V reviewer, P admin + -- publisher, R reader, N no right. Every one has a verified factor and a live @@ -181,6 +181,58 @@ select is(pg_temp.val($$ select public.get_sheet_version(pg_temp.id('SX.v2')) -> select pg_temp.as_user('E', 'aal2'); select is(pg_temp.val($$ select public.get_sheet_version(pg_temp.id('SP.v2')) ->> 'fingerprint' $$), 'ok', 'editor: a draft has no fingerprint'); +-- ============ Last modification date (R3, M9) ============ +-- now() is the transaction's start for the whole file, so these rows are dated +-- in the past at insert (the trigger stamps updates only): a later write then +-- shows as a newer date. +reset role; +select set_config('request.jwt.claims', '{"sub":"22000000-0000-0000-0000-00000000000e"}', true); +insert into pg_temp.k values + ('SL', '22200000-0000-0000-0000-000000000007'), ('SL.v1', '22300000-0000-0000-0000-000000000071'), + ('SN', '22200000-0000-0000-0000-000000000008'), + ('WK', '22200000-0000-0000-0000-000000000009'), ('WK.v1', '22300000-0000-0000-0000-000000000091'); +insert into public.anatomical_sheets (id, species_id, name, slug, is_official, visibility, status, + created_at, updated_at, working_copy_source_version_id) values + (pg_temp.id('SL'), 'equine', 'SL', 'm22-sl', true, 'private', 'active', '2026-01-01T10:00:00Z', '2026-01-01T10:00:00Z', null), + (pg_temp.id('SN'), 'equine', 'SN', 'm22-sn', true, 'private', 'active', '2026-01-03T10:00:00Z', '2026-01-03T10:00:00Z', null), + -- A second, dated working copy of the historical sheet: the single slot is a + -- rule of create_sheet_working_copy, not a constraint, and WC holds it already. + (pg_temp.id('WK'), 'equine', 'WK', 'm22-wk', true, 'private', 'active', '2026-01-01T10:00:00Z', '2026-01-01T10:00:00Z', + pg_temp.id('h.v')); +insert into public.anatomical_sheet_versions (id, sheet_id, version_number, status, document_schema_version, content, + created_at, updated_at) values + (pg_temp.id('SL.v1'), pg_temp.id('SL'), 1, 'draft', 2, pg_temp.doc('Datée'), '2026-01-02T10:00:00Z', '2026-01-02T10:00:00Z'), + (pg_temp.id('WK.v1'), pg_temp.id('WK'), 1, 'draft', 2, pg_temp.doc('Copie datée'), '2026-01-04T10:00:00Z', '2026-01-04T10:00:00Z'); +-- One date of a sheet's admin answer. +create function pg_temp.at(p_key text, p_field text) returns timestamptz language sql as $$ + select (public.get_official_sheet_admin(pg_temp.id(p_key)) ->> p_field)::timestamptz +$$; +grant execute on function pg_temp.at(text, text) to authenticated; + +select pg_temp.as_user('E', 'aal2'); +select is(pg_temp.at('SL', 'last_modified_at'), '2026-01-02T10:00:00Z'::timestamptz, + 'a version newer than the sheet row gives the last modification'); +select is(pg_temp.at('SN', 'last_modified_at'), '2026-01-03T10:00:00Z'::timestamptz, + 'a sheet without a version answers its own date'); +select is(pg_temp.val($$ select public.save_sheet_draft(pg_temp.id('SL.v1'), 0, pg_temp.doc('Datée 2'), 'r3') ->> 'revision' $$), + '1', 'E saves the dated draft'); +select is(pg_temp.at('SL', 'last_modified_at'), now(), 'a draft save moves last_modified_at'); +select is(pg_temp.at('SL', 'updated_at'), '2026-01-01T10:00:00Z'::timestamptz, 'a draft save leaves the sheet row''s date'); +reset role; +update public.anatomical_sheets set name = 'SN 2' where id = pg_temp.id('SN'); +select pg_temp.as_user('E', 'aal2'); +select is(pg_temp.at('SN', 'last_modified_at'), now(), 'a sheet change moves last_modified_at'); +select is(pg_temp.at('SN', 'updated_at'), now(), 'a sheet change moves updated_at too'); +select is((public.get_official_sheet_admin(pg_temp.id('WK')) ->> 'working_copy')::boolean, true, + 'the dated fixture is a working copy'); +select is(pg_temp.at('WK', 'last_modified_at'), '2026-01-04T10:00:00Z'::timestamptz, + 'a working copy is dated by its own draft'); +select is(pg_temp.val($$ select public.save_sheet_draft(pg_temp.id('WK.v1'), 0, pg_temp.doc('Copie datée 2'), 'r3') ->> 'revision' $$), + '1', 'E saves the working copy''s draft'); +select is(pg_temp.at('WK', 'last_modified_at'), now(), 'a working copy''s draft save moves its last_modified_at'); +select is(pg_temp.val($$ select count(*)::text from jsonb_array_elements(public.list_official_sheets_admin()) e + where e ->> 'last_modified_at' is null $$), '0', 'every listed sheet carries last_modified_at'); + -- ============ Privileges and nature ============ reset role; select ok(not has_function_privilege('anon', 'public.list_official_sheets_admin()', 'execute')