From 2f824a947cfbf584832e17cbbe10881c4d2ad6a4 Mon Sep 17 00:00:00 2001 From: bchanot Date: Wed, 4 Nov 2026 11:40:22 +0100 Subject: [PATCH] feat(supabase): add draft_last_save to the sheet admin read (M8, sandbox only) --- apps/app/tests/unit/routes.admin.test.tsx | 5 + docs/development/V1-2_ANATOMICAL_SHEETS.md | 4 +- packages/supabase/src/sheets.ts | 4 + packages/supabase/tests/sheets-admin.test.ts | 6 + packages/supabase/tests/sheets.test.ts | 3 +- .../20261103100000_sheet_draft_last_save.sql | 94 +++++++++++ supabase/tests/023_sheet_draft_last_save.sql | 155 ++++++++++++++++++ 7 files changed, 269 insertions(+), 2 deletions(-) create mode 100644 supabase/migrations/20261103100000_sheet_draft_last_save.sql create mode 100644 supabase/tests/023_sheet_draft_last_save.sql diff --git a/apps/app/tests/unit/routes.admin.test.tsx b/apps/app/tests/unit/routes.admin.test.tsx index 7d8eabdd..3b13e75b 100644 --- a/apps/app/tests/unit/routes.admin.test.tsx +++ b/apps/app/tests/unit/routes.admin.test.tsx @@ -748,6 +748,7 @@ describe('Sheet creation (A1)', () => { data: { ...summary({ sheet_id: NEW_ID }), working_copy_source: null, + draft_last_save: null, versions: [], rounds: [], }, @@ -826,6 +827,7 @@ describe('Sheet overview (H3)', () => { }, }), working_copy_source: { version_id: 'v-h', version_number: 1 }, + draft_last_save: null, versions: [ { version_id: 'v-1', @@ -919,6 +921,7 @@ describe('Sheet overview (H3)', () => { data: { ...historical(), working_copy_source: null, + draft_last_save: null, versions: [], rounds: [], }, @@ -943,6 +946,7 @@ describe('Sheet overview (H3)', () => { ...historical(), working_copy_active: false, working_copy_source: null, + draft_last_save: null, versions: [], rounds: [], }, @@ -976,6 +980,7 @@ describe('Sheet overview (H3)', () => { ...historical(), working_copy_active: false, working_copy_source: null, + draft_last_save: null, versions: [], rounds: [], }, diff --git a/docs/development/V1-2_ANATOMICAL_SHEETS.md b/docs/development/V1-2_ANATOMICAL_SHEETS.md index 5b1e5196..78954ef6 100644 --- a/docs/development/V1-2_ANATOMICAL_SHEETS.md +++ b/docs/development/V1-2_ANATOMICAL_SHEETS.md @@ -1760,6 +1760,7 @@ of the document's JSON text in one form, the same for every layer: | M5 `sheet_review_workflow` | V1-2.1 | version columns `revision`, `authors`, `source_version_id`, `published_review_id`, `document_fingerprint`; sheet column `working_copy_source_version_id`; revision, author and freeze triggers, the historical-sheet write rule; extended immutability guard (§6.8); rounds, comments, acknowledgments; `create_official_sheet`, `create_sheet_draft` adapted, `save_sheet_draft`, submit, withdraw, comment, acknowledge, decide, publish; `create_sheet_working_copy`; species RPCs; catalogue and version read RPCs; upload: `register_sheet_asset` extended, `confirm_sheet_asset_upload` with the storage check (F7), INSERT policy, bucket limit 2 MiB; mirror removal and status check (§12.2); superseded signatures dropped; the content-size CHECK replaced by the text measure (`octet_length(content::text)`, §12.2) | new columns take their defaults on the equine v1 row (`revision` 0, empty author set); its document, status, number and assets unchanged; the new size CHECK is validated against the existing rows when M5 is applied (the equine document measures 184 105 bytes as `content::text`, measured on the sandbox); bucket limit lowered; no round, no grant | | M6 `sheet_verification_jobs` | V1-2.3 | worker RPCs (claim, input, complete with the verdict structure check, fail), service role only, with the retry delay (hidden `not_before` column on the round, `private.sheet_verification_retry_delay`: owner decision 2026-11-01, replaces « without backoff ») and the retry by cause (definitive codes close at the first occurrence, `private.sheet_worker_final_codes`); written as `20261101100000_sheet_verification_jobs.sql`, pgTAP `021`, rehearsed on the sandbox from the state before M4 (M4 to M6 in one `migration up`) and from the state before M6 | none | | M7 `sheet_admin_reads` | V1-2.5 | read RPCs of the admin space (owner decision 2026-11-02, BDR-097): the official sheet list with its badges (historical, working copy), statuses and round counts, and one sheet's overview (versions, rounds); `get_sheet_version` redefined to return the fingerprint of deprecated versions too (BDR-098); the admin reads for capability holders at `aal2` only (`caller_can_see_official_sheet`); no new write path (it redefines two existing write RPCs), no new grant, no policy change; the one rule change is the species sort order: `create_species` and `update_inactive_species` are redefined to refuse an order outside 0..9 999 with `invalid_input` (null keeps today's meaning, owner 2026-11-02), `security definer`, `search_path = ''` and their privileges restated; pgTAP allowed and denied cases | none | +| M8 `sheet_draft_last_save` | V1-2.6 | read migration (owner decision 2026-11-03, sandbox only): `get_official_sheet_admin` redefined to answer `draft_last_save` = `{revision, saved_at, saved_by}` (the account that saved the draft's current revision, from the newest `anatomical_sheet_version.saved` audit row of that revision), null when no save wrote it; the list and the summary helper untouched; no write path, no new grant, no policy change, privileges restated as M7; pgTAP allowed and denied cases | none | Each migration ships its pgTAP file (allowed and denied cases), regenerated `database.types.ts`, a `db:sandbox up upgrade` run from the DEV state (with a @@ -2315,7 +2316,8 @@ V1-2.7 on. - Dependencies: V1-2.1, V1-2.2, V1-2.4, V1-2.5 (not V1-2.3). - Security prerequisites: stored bytes never inserted into the page; only core-composed trees rendered. -- Application changes: the draft editor. SQL changes: none. +- Application changes: the draft editor. SQL changes: M8 `sheet_draft_last_save`, a read + migration for the conflict dialog (§12.3, owner decision 2026-11-03; was « none »). - Tests: `e2e:shell` A2, A3, A4 with a conflict and a local recovery; keyboard path through numeric fields; axe; read-only below 1 024 px. - Acceptance: a sheet for a species other than the horse, with a drawing diff --git a/packages/supabase/src/sheets.ts b/packages/supabase/src/sheets.ts index 9cf63010..f88a9965 100644 --- a/packages/supabase/src/sheets.ts +++ b/packages/supabase/src/sheets.ts @@ -445,6 +445,10 @@ export type SheetAdminRound = z.infer const OfficialSheetAdminSchema = OfficialSheetAdminSummarySchema.extend({ working_copy_source: z.object({ version_id: z.string(), version_number: z.number() }).nullable(), + /** Who saved the draft's current revision, null when no save wrote it (M8). */ + draft_last_save: z + .object({ revision: z.number(), saved_at: z.string(), saved_by: PersonSchema.nullable() }) + .nullable(), versions: z.array(SheetAdminVersionSchema), rounds: z.array(SheetAdminRoundSchema), }) diff --git a/packages/supabase/tests/sheets-admin.test.ts b/packages/supabase/tests/sheets-admin.test.ts index f0e7cf3f..25aa80b0 100644 --- a/packages/supabase/tests/sheets-admin.test.ts +++ b/packages/supabase/tests/sheets-admin.test.ts @@ -95,6 +95,7 @@ const summary = { const overview = { ...summary, working_copy_source: null, + draft_last_save: { revision: 3, saved_at: '2026-11-02T11:00:00Z', saved_by: person }, versions: [{ version_id: VERSION, version_number: 1, status: 'published', revision: 1, change_note: null, created_at: '2026-11-01T09:00:00Z', published_at: '2026-11-01T10:00:00Z', deprecated_at: null, @@ -172,6 +173,11 @@ describe('sheet admin wrappers: answers', () => { expect(rpcCalls).toEqual([['get_official_sheet_admin', { sheet_id: SHEET }]]) }) + it('accepts a draft nobody saved: draft_last_save is null', async () => { + const { client } = stubClient({ rpc: { data: { ...overview, draft_last_save: null } } }) + expect((await getOfficialSheetAdmin(client, SHEET)).draft_last_save).toBeNull() + }) + it('parses grants, the account lookup and the species', async () => { expect(await listCatalogGrants(stubClient({ rpc: { data: [grant] } }).client)).toEqual([grant]) const account = { diff --git a/packages/supabase/tests/sheets.test.ts b/packages/supabase/tests/sheets.test.ts index 3939fd78..a99c5a99 100644 --- a/packages/supabase/tests/sheets.test.ts +++ b/packages/supabase/tests/sheets.test.ts @@ -5,7 +5,7 @@ import { migrationsDir } from './db/harness' /** * The closed failure kinds of the sheet workflow (V1-2.1). The census reads - * every closed code M4, M5 and M7 raise (`raise exception ''`, a lowercase + * every closed code M4, M5, M7 and M8 raise (`raise exception ''`, a lowercase * dotted word; free-text messages are not codes) so a code added on the server * can never reach a page as `unavailable` for want of a kind. */ @@ -13,6 +13,7 @@ const SHEET_MIGRATIONS = [ '20261028100000_sheet_access_hardening.sql', '20261028110000_sheet_review_workflow.sql', '20261102100000_sheet_admin_reads.sql', + '20261103100000_sheet_draft_last_save.sql', ] async function raisedCodes(): Promise { diff --git a/supabase/migrations/20261103100000_sheet_draft_last_save.sql b/supabase/migrations/20261103100000_sheet_draft_last_save.sql new file mode 100644 index 00000000..8c89ccbc --- /dev/null +++ b/supabase/migrations/20261103100000_sheet_draft_last_save.sql @@ -0,0 +1,94 @@ +-- 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; diff --git a/supabase/tests/023_sheet_draft_last_save.sql b/supabase/tests/023_sheet_draft_last_save.sql new file mode 100644 index 00000000..b48282aa --- /dev/null +++ b/supabase/tests/023_sheet_draft_last_save.sql @@ -0,0 +1,155 @@ +-- V1-2.6 M8: `get_official_sheet_admin` names who saved the draft's current +-- revision (contract §12.3, §16.7). Null on a fresh draft, on a sheet without +-- draft and after a content write that is not a save; same refusals as M7, no +-- new privilege. Fixtures are scoped to the ids created here. +begin; +select plan(27); + +-- Accounts. O owner, A admin, E and F admin + editor, R reader, N no right. +create table pg_temp.who (label text primary key, id uuid not null); +insert into pg_temp.who values + ('O', '23000000-0000-0000-0000-000000000001'), ('A', '23000000-0000-0000-0000-00000000000a'), + ('E', '23000000-0000-0000-0000-00000000000e'), ('F', '23000000-0000-0000-0000-00000000000f'), + ('R', '23000000-0000-0000-0000-0000000000cc'), ('N', '23000000-0000-0000-0000-0000000000c5'); +insert into auth.users (id, email, email_confirmed_at, raw_user_meta_data) +select id, lower(label) || '@m23.local', now(), jsonb_build_object('display_name', 'M23 ' || label) from pg_temp.who; +insert into auth.mfa_factors (id, user_id, factor_type, status, created_at, updated_at) +select gen_random_uuid(), id, 'totp', 'verified', now(), now() from pg_temp.who; +insert into auth.sessions (id, user_id, aal) +select ('23100000' || substr(id::text, 9))::uuid, id, 'aal2' from pg_temp.who; + +select set_config('request.jwt.claims', '', true); +select private.assign_platform_role(null, '23000000-0000-0000-0000-000000000001', 'owner'); +select private.assign_platform_role(null, '23000000-0000-0000-0000-00000000000a', 'admin'); +select private.assign_platform_role(null, '23000000-0000-0000-0000-00000000000e', 'admin'); +select private.assign_platform_role(null, '23000000-0000-0000-0000-00000000000f', 'admin'); +insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by) values + ('23000000-0000-0000-0000-00000000000e', 'sheet_editor', 'a', 'c', '23000000-0000-0000-0000-000000000001'), + ('23000000-0000-0000-0000-00000000000f', 'sheet_editor', 'a', 'c', '23000000-0000-0000-0000-000000000001'), + ('23000000-0000-0000-0000-0000000000cc', 'catalog_reader', 'a', null, '23000000-0000-0000-0000-000000000001'); + +-- Sheets: SP with a published v1 and a draft v2, SA with a published v1 only, +-- SD deleted, PS personal. +create table pg_temp.k (key text primary key, id uuid not null); +insert into pg_temp.k values + ('SP', '23200000-0000-0000-0000-000000000001'), ('SA', '23200000-0000-0000-0000-000000000002'), + ('SD', '23200000-0000-0000-0000-000000000003'), ('PS', '23200000-0000-0000-0000-000000000004'), + ('SP.v1', '23300000-0000-0000-0000-000000000011'), ('SP.v2', '23300000-0000-0000-0000-000000000012'), + ('SA.v1', '23300000-0000-0000-0000-000000000021'); +create function pg_temp.id(p_key text) returns uuid language sql stable as $$ select id from pg_temp.k where key = p_key $$; +create function pg_temp.doc(p_name text) returns jsonb language sql as $$ + select jsonb_build_object('schema_version', 2, 'species_id', 'equine', 'name', jsonb_build_object('fr', p_name, 'en', p_name || ' EN'), + 'description', jsonb_build_object('fr', 'd', 'en', 'd'), 'scope_disclaimer', jsonb_build_object('fr', 'x', 'en', 'x'), + 'attribution', jsonb_build_object('authors', '[]'::jsonb, 'license', null, 'notes', null), + 'vertebral_config', jsonb_build_object('regions', '[]'::jsonb), 'references', '[]'::jsonb, 'categories', '[]'::jsonb, + 'views', jsonb_build_array(jsonb_build_object('id', 'main', 'kind', 'main', 'name', jsonb_build_object('fr', 'V', 'en', 'V'), + 'svg_asset_id', null, 'view_box', null)), + 'annotations', '[]'::jsonb, 'finding_zones', '[]'::jsonb); +$$; +select set_config('request.jwt.claims', '{"sub":"23000000-0000-0000-0000-00000000000e"}', true); +insert into public.anatomical_sheets (id, species_id, name, slug, is_official, visibility, status, archived_at, deleted_at, + creator_user_id, working_copy_source_version_id) values + (pg_temp.id('SP'), 'equine', 'SP', 'm23-sp', true, 'private', 'active', null, null, null, null), + (pg_temp.id('SA'), 'equine', 'SA', 'm23-sa', true, 'private', 'active', null, null, null, null), + (pg_temp.id('SD'), 'equine', 'SD', 'm23-sd', true, 'private', 'deleted', null, now(), null, null), + (pg_temp.id('PS'), 'equine', 'PS', 'm23-ps', false, 'private', 'active', null, null, '23000000-0000-0000-0000-00000000000e', null); +insert into public.anatomical_sheet_versions (id, sheet_id, version_number, status, document_schema_version, content, + published_at, document_fingerprint) values + (pg_temp.id('SP.v1'), pg_temp.id('SP'), 1, 'published', 2, pg_temp.doc('Publiée'), now(), repeat('0', 64)), + (pg_temp.id('SP.v2'), pg_temp.id('SP'), 2, 'draft', 2, pg_temp.doc('Brouillon'), null, null), + (pg_temp.id('SA.v1'), pg_temp.id('SA'), 1, 'published', 2, pg_temp.doc('Archivée'), now(), repeat('0', 64)); +update public.anatomical_sheets set current_version_id = pg_temp.id('SP.v1'), last_version_number = 2 where id = pg_temp.id('SP'); +update public.anatomical_sheets set current_version_id = pg_temp.id('SA.v1'), last_version_number = 1 where id = pg_temp.id('SA'); + +create function pg_temp.as_user(p text, p_aal text) returns void language plpgsql as $$ +declare v_id uuid := (select id from pg_temp.who where label = p); +begin + perform set_config('role', 'authenticated', true); + perform set_config('request.jwt.claims', jsonb_build_object('sub', v_id, 'role', 'authenticated', 'aal', p_aal, + 'session_id', case when p_aal = 'aal2' then ('23100000' || substr(v_id::text, 9))::uuid else gen_random_uuid() end)::text, true); +end $$; +-- The outcome of a statement: its single value, or its SQLSTATE and message. +create function pg_temp.val(p_sql text) returns text language plpgsql as $$ +declare v text; +begin + execute p_sql into v; + return coalesce(v, 'ok'); +exception when others then + return sqlstate || ' ' || sqlerrm; +end $$; +-- A path of the sheet's admin overview, as text ('null' for a JSON null). +create function pg_temp.last_save(p_key text, p_path text default '{}') returns text language plpgsql as $$ +begin + return coalesce((public.get_official_sheet_admin(pg_temp.id(p_key)) -> 'draft_last_save') #>> p_path::text[], + (public.get_official_sheet_admin(pg_temp.id(p_key)) -> 'draft_last_save')::text); +exception when others then + return sqlstate || ' ' || sqlerrm; +end $$; +-- The saved_at of the overview equals the audit row's time. +create function pg_temp.audit_time(p_key text, p_revision int) returns text language sql as $$ + select max(occurred_at)::text from audit.audit_logs where resource_id = pg_temp.id(p_key) + and action = 'anatomical_sheet_version.saved' and metadata ->> 'revision' = p_revision::text; +$$; +grant all on pg_temp.who, pg_temp.k to authenticated; +grant execute on all functions in schema pg_temp to authenticated; + +-- ============ Who may call: the refusals of M7 ============ +select pg_temp.as_user('O', 'aal2'); +select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) ->> 'slug' $$), '42501 not_found', 'owner without a capability: refused'); +select pg_temp.as_user('A', 'aal2'); +select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) ->> 'slug' $$), '42501 not_found', 'admin without a capability: refused'); +select pg_temp.as_user('R', 'aal2'); +select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) ->> 'slug' $$), '42501 not_found', 'catalogue reader: refused'); +select pg_temp.as_user('N', 'aal2'); +select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) ->> 'slug' $$), '42501 not_found', 'account without a right: refused'); +select pg_temp.as_user('E', 'aal1'); +select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) ->> 'slug' $$), '42501 rights.mfa', 'editor at aal1: refused'); +select pg_temp.as_user('E', 'aal2'); +select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SD')) ->> 'slug' $$), '42501 not_found', 'deleted sheet: not_found'); +select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('PS')) ->> 'slug' $$), '42501 not_found', 'personal sheet: not_found'); +select is(pg_temp.val($$ select public.get_official_sheet_admin('23200000-0000-0000-0000-0000000000ff') ->> 'slug' $$), '42501 not_found', 'unknown id: not_found'); +select is(pg_temp.val($$ select public.get_official_sheet_admin(null) ->> 'slug' $$), '42501 not_found', 'null id: not_found'); + +-- ============ Null cases ============ +select is(pg_temp.last_save('SP'), 'null', 'fresh draft: draft_last_save is null'); +select is(pg_temp.val($$ select (public.get_official_sheet_admin(pg_temp.id('SP')) ? 'draft_last_save')::text $$), 'true', 'the key is present, null'); +select is(pg_temp.last_save('SA'), 'null', 'sheet without a draft: null'); + +-- ============ After a save ============ +select is(pg_temp.val($$ select public.save_sheet_draft(pg_temp.id('SP.v2'), 0, pg_temp.doc('Un'), 'one') ->> 'revision' $$), '1', 'E saves revision 1'); +select is(pg_temp.last_save('SP', '{revision}'), '1', 'after a save: the revision saved'); +select is(pg_temp.last_save('SP', '{saved_by,display_name}'), 'M23 E', 'after a save: the actor named'); +select is(pg_temp.last_save('SP', '{saved_by,user_id}'), '23000000-0000-0000-0000-00000000000e', 'after a save: the actor id'); +select is(pg_temp.val($$ select (public.get_official_sheet_admin(pg_temp.id('SP')) #>> '{draft_last_save,saved_at}')::timestamptz::text $$), + pg_temp.audit_time('SP.v2', 1), 'after a save: the time is the audit row''s'); + +-- ============ A second editor saves ============ +select pg_temp.as_user('F', 'aal2'); +select is(pg_temp.last_save('SP', '{saved_by,display_name}'), 'M23 E', 'F reads the save of E'); +select is(pg_temp.val($$ select public.save_sheet_draft(pg_temp.id('SP.v2'), 1, pg_temp.doc('Deux'), 'two') ->> 'revision' $$), '2', 'F saves revision 2'); +select is(pg_temp.last_save('SP', '{revision}') || '/' || pg_temp.last_save('SP', '{saved_by,display_name}'), '2/M23 F', 'after a second save: the second editor'); +select pg_temp.as_user('E', 'aal2'); +select is(pg_temp.last_save('SP', '{saved_by,display_name}'), 'M23 F', 'E reads the save of F'); + +-- ============ A content write that is not a save ============ +reset role; +select set_config('request.jwt.claims', '{"sub":"23000000-0000-0000-0000-00000000000e"}', true); +update public.anatomical_sheet_versions set content = pg_temp.doc('Directe') where id = pg_temp.id('SP.v2'); +select pg_temp.as_user('E', 'aal2'); +select is(pg_temp.val($$ select public.get_official_sheet_admin(pg_temp.id('SP')) #>> '{draft_version,revision}' $$), '3', 'the draft moved to revision 3'); +select is(pg_temp.last_save('SP'), 'null', 'revision written outside a save: null'); +select is(pg_temp.val($$ select public.save_sheet_draft(pg_temp.id('SP.v2'), 3, pg_temp.doc('Quatre'), 'four') ->> 'revision' $$), '4', 'E saves revision 4'); +select is(pg_temp.last_save('SP', '{revision}') || '/' || pg_temp.last_save('SP', '{saved_by,display_name}'), '4/M23 E', 'a save after it names the saver again'); + +-- ============ Privileges and nature ============ +reset role; +select ok(not has_function_privilege('anon', 'public.get_official_sheet_admin(uuid)', 'execute') + and not has_function_privilege('service_role', 'public.get_official_sheet_admin(uuid)', 'execute') + and has_function_privilege('authenticated', 'public.get_official_sheet_admin(uuid)', 'execute'), 'privileges as M7: authenticated only'); +select is((select p.provolatile::text || '/' || p.prosecdef::text || '/' || (p.proconfig = array['search_path=""'])::text + from pg_proc p join pg_namespace n on n.oid = p.pronamespace + where n.nspname = 'public' and p.proname = 'get_official_sheet_admin'), 's/true/true', + 'stable, security definer, empty search_path'); + +select * from finish(); +rollback;