From 8cac8b1a68023eeb4e2a567782aee7e7add108f4 Mon Sep 17 00:00:00 2001 From: bchanot Date: Thu, 8 Oct 2026 23:42:44 +0200 Subject: [PATCH] feat(supabase): regenerated types, row aliases, publishSheetVersion gate, schema test --- packages/supabase/package.json | 1 + packages/supabase/src/anatomical-sheets.ts | 31 ++ packages/supabase/src/database.types.ts | 306 ++++++++++++++++++ packages/supabase/src/index.ts | 16 + .../supabase/tests/anatomical-sheets.test.ts | 74 +++++ .../supabase/tests/db/schema-types.test.ts | 90 +++++- 6 files changed, 510 insertions(+), 8 deletions(-) create mode 100644 packages/supabase/src/anatomical-sheets.ts create mode 100644 packages/supabase/tests/anatomical-sheets.test.ts diff --git a/packages/supabase/package.json b/packages/supabase/package.json index be08c258..5e899890 100644 --- a/packages/supabase/package.json +++ b/packages/supabase/package.json @@ -19,6 +19,7 @@ "build": "tsc -p tsconfig.json --noEmit" }, "dependencies": { + "@faunosteo/domain": "workspace:*", "@supabase/supabase-js": "^2.100.0" }, "devDependencies": { diff --git a/packages/supabase/src/anatomical-sheets.ts b/packages/supabase/src/anatomical-sheets.ts new file mode 100644 index 00000000..373979c6 --- /dev/null +++ b/packages/supabase/src/anatomical-sheets.ts @@ -0,0 +1,31 @@ +import { + AnatomicalSheetDocumentSchema, + assertPublishable, +} from '@faunosteo/domain' +import type { FaunosteoClient } from './client' + +/** + * Publication gate on the client side: the full Zod shape and every + * integrity rule run before the RPC, so the editor gets the complete issue + * list. The database re-checks the referential rules on its own + * (private.sheet_document_issues); this call never replaces that guard. + */ +export async function publishSheetVersion( + client: FaunosteoClient, + versionId: string, +): Promise { + const draft = await client + .from('anatomical_sheet_versions') + .select('content') + .eq('id', versionId) + .single() + if (draft.error) throw draft.error + + assertPublishable(AnatomicalSheetDocumentSchema.parse(draft.data.content)) + + const published = await client.rpc('publish_sheet_version', { + version_id: versionId, + }) + if (published.error) throw published.error + return published.data +} diff --git a/packages/supabase/src/database.types.ts b/packages/supabase/src/database.types.ts index dade293c..23461cc7 100644 --- a/packages/supabase/src/database.types.ts +++ b/packages/supabase/src/database.types.ts @@ -9,6 +9,230 @@ export type Json = export type Database = { public: { Tables: { + anatomical_sheet_assets: { + Row: { + byte_size: number + content_type: string + created_at: string + created_by: string | null + file_name: string + id: string + sha256: string + sheet_id: string + } + ComputedFields: never + Insert: { + byte_size: number + content_type?: string + created_at?: string + created_by?: string | null + file_name: string + id?: string + sha256: string + sheet_id: string + } + Update: { + byte_size?: number + content_type?: string + created_at?: string + created_by?: string | null + file_name?: string + id?: string + sha256?: string + sheet_id?: string + } + Relationships: [ + { + foreignKeyName: 'anatomical_sheet_assets_created_by_fkey' + columns: ['created_by'] + isOneToOne: false + referencedRelation: 'profiles' + referencedColumns: ['id'] + }, + { + foreignKeyName: 'anatomical_sheet_assets_sheet_id_fkey' + columns: ['sheet_id'] + isOneToOne: false + referencedRelation: 'anatomical_sheets' + referencedColumns: ['id'] + }, + ] + } + anatomical_sheet_versions: { + Row: { + change_note: string + content: NonNullable + created_at: string + created_by: string | null + deprecated_at: string | null + deprecated_by: string | null + document_schema_version: number + id: string + published_at: string | null + published_by: string | null + sheet_id: string + status: Database['public']['Enums']['sheet_version_status'] + updated_at: string + version_number: number + } + ComputedFields: never + Insert: { + change_note?: string + content: NonNullable + created_at?: string + created_by?: string | null + deprecated_at?: string | null + deprecated_by?: string | null + document_schema_version: number + id?: string + published_at?: string | null + published_by?: string | null + sheet_id: string + status?: Database['public']['Enums']['sheet_version_status'] + updated_at?: string + version_number: number + } + Update: { + change_note?: string + content?: NonNullable + created_at?: string + created_by?: string | null + deprecated_at?: string | null + deprecated_by?: string | null + document_schema_version?: number + id?: string + published_at?: string | null + published_by?: string | null + sheet_id?: string + status?: Database['public']['Enums']['sheet_version_status'] + updated_at?: string + version_number?: number + } + Relationships: [ + { + foreignKeyName: 'anatomical_sheet_versions_created_by_fkey' + columns: ['created_by'] + isOneToOne: false + referencedRelation: 'profiles' + referencedColumns: ['id'] + }, + { + foreignKeyName: 'anatomical_sheet_versions_deprecated_by_fkey' + columns: ['deprecated_by'] + isOneToOne: false + referencedRelation: 'profiles' + referencedColumns: ['id'] + }, + { + foreignKeyName: 'anatomical_sheet_versions_published_by_fkey' + columns: ['published_by'] + isOneToOne: false + referencedRelation: 'profiles' + referencedColumns: ['id'] + }, + { + foreignKeyName: 'anatomical_sheet_versions_sheet_id_fkey' + columns: ['sheet_id'] + isOneToOne: false + referencedRelation: 'anatomical_sheets' + referencedColumns: ['id'] + }, + ] + } + anatomical_sheets: { + Row: { + archived_at: string | null + based_on_sheet_id: string | null + based_on_version_id: string | null + created_at: string + creator_user_id: string | null + current_version_id: string | null + deleted_at: string | null + description: string + id: string + is_official: boolean + name: string + slug: string + species_id: string + status: Database['public']['Enums']['sheet_status'] + updated_at: string + visibility: Database['public']['Enums']['sheet_visibility'] + } + ComputedFields: never + Insert: { + archived_at?: string | null + based_on_sheet_id?: string | null + based_on_version_id?: string | null + created_at?: string + creator_user_id?: string | null + current_version_id?: string | null + deleted_at?: string | null + description?: string + id?: string + is_official?: boolean + name: string + slug: string + species_id: string + status?: Database['public']['Enums']['sheet_status'] + updated_at?: string + visibility?: Database['public']['Enums']['sheet_visibility'] + } + Update: { + archived_at?: string | null + based_on_sheet_id?: string | null + based_on_version_id?: string | null + created_at?: string + creator_user_id?: string | null + current_version_id?: string | null + deleted_at?: string | null + description?: string + id?: string + is_official?: boolean + name?: string + slug?: string + species_id?: string + status?: Database['public']['Enums']['sheet_status'] + updated_at?: string + visibility?: Database['public']['Enums']['sheet_visibility'] + } + Relationships: [ + { + foreignKeyName: 'anatomical_sheets_based_on_sheet_id_fkey' + columns: ['based_on_sheet_id'] + isOneToOne: false + referencedRelation: 'anatomical_sheets' + referencedColumns: ['id'] + }, + { + foreignKeyName: 'anatomical_sheets_based_on_version_fkey' + columns: ['based_on_version_id'] + isOneToOne: false + referencedRelation: 'anatomical_sheet_versions' + referencedColumns: ['id'] + }, + { + foreignKeyName: 'anatomical_sheets_creator_user_id_fkey' + columns: ['creator_user_id'] + isOneToOne: false + referencedRelation: 'profiles' + referencedColumns: ['id'] + }, + { + foreignKeyName: 'anatomical_sheets_current_version_fkey' + columns: ['current_version_id'] + isOneToOne: false + referencedRelation: 'anatomical_sheet_versions' + referencedColumns: ['id'] + }, + { + foreignKeyName: 'anatomical_sheets_species_id_fkey' + columns: ['species_id'] + isOneToOne: false + referencedRelation: 'species' + referencedColumns: ['id'] + }, + ] + } profile_roles: { Row: { granted_at: string @@ -108,11 +332,76 @@ export type Database = { } Relationships: [] } + species: { + Row: { + active: boolean + created_at: string + id: string + name_en: string + name_fr: string + sort_order: number + } + ComputedFields: never + Insert: { + active?: boolean + created_at?: string + id: string + name_en: string + name_fr: string + sort_order?: number + } + Update: { + active?: boolean + created_at?: string + id?: string + name_en?: string + name_fr?: string + sort_order?: number + } + Relationships: [] + } } Views: { [_ in never]: never } Functions: { + archive_anatomical_sheet: { Args: { sheet_id: string }; Returns: string } + create_anatomical_sheet: { + Args: { + description?: string + document: Json + name: string + official?: boolean + slug: string + species_id: string + visibility?: Database['public']['Enums']['sheet_visibility'] + } + Returns: string + } + create_sheet_draft: { + Args: { + change_note?: string + from_version_id?: string + sheet_id: string + } + Returns: string + } + delete_anatomical_sheet: { Args: { sheet_id: string }; Returns: string } + delete_sheet_asset: { Args: { asset_id: string }; Returns: string } + delete_sheet_version: { Args: { version_id: string }; Returns: string } + deprecate_sheet_version: { + Args: { reason?: string; version_id: string } + Returns: string + } + fork_anatomical_sheet: { + Args: { + name: string + slug: string + source_version_id: string + visibility?: Database['public']['Enums']['sheet_visibility'] + } + Returns: string + } grant_platform_role: { Args: { role: Database['public']['Enums']['platform_role'] @@ -120,6 +409,17 @@ export type Database = { } Returns: string } + publish_sheet_version: { Args: { version_id: string }; Returns: string } + register_sheet_asset: { + Args: { + byte_size: number + file_name: string + sha256: string + sheet_id: string + } + Returns: string + } + restore_anatomical_sheet: { Args: { sheet_id: string }; Returns: string } revoke_platform_role: { Args: { reason?: string @@ -131,6 +431,9 @@ export type Database = { } Enums: { platform_role: 'owner' | 'admin' | 'moderator' | 'support' + sheet_status: 'active' | 'archived' | 'deleted' + sheet_version_status: 'draft' | 'published' | 'deprecated' + sheet_visibility: 'private' | 'public' } CompositeTypes: { [_ in never]: never @@ -259,6 +562,9 @@ export const Constants = { public: { Enums: { platform_role: ['owner', 'admin', 'moderator', 'support'], + sheet_status: ['active', 'archived', 'deleted'], + sheet_version_status: ['draft', 'published', 'deprecated'], + sheet_visibility: ['private', 'public'], }, }, } as const diff --git a/packages/supabase/src/index.ts b/packages/supabase/src/index.ts index b3ddf824..7738d67e 100644 --- a/packages/supabase/src/index.ts +++ b/packages/supabase/src/index.ts @@ -21,3 +21,19 @@ export type Profile = Tables<'profiles'> export type ProfileRole = Tables<'profile_roles'> /** owner | admin | moderator | support. Not a professional status. */ export type PlatformRole = Enums<'platform_role'> +export { publishSheetVersion } from './anatomical-sheets' + +/** Row of public.species. */ +export type Species = Tables<'species'> +/** Row of public.anatomical_sheets. */ +export type AnatomicalSheet = Tables<'anatomical_sheets'> +/** Row of public.anatomical_sheet_versions (content = canonical document). */ +export type AnatomicalSheetVersion = Tables<'anatomical_sheet_versions'> +/** Row of public.anatomical_sheet_assets. */ +export type AnatomicalSheetAsset = Tables<'anatomical_sheet_assets'> +/** private | public. */ +export type SheetVisibility = Enums<'sheet_visibility'> +/** active | archived | deleted. */ +export type SheetStatus = Enums<'sheet_status'> +/** draft | published | deprecated. */ +export type SheetVersionStatus = Enums<'sheet_version_status'> diff --git a/packages/supabase/tests/anatomical-sheets.test.ts b/packages/supabase/tests/anatomical-sheets.test.ts new file mode 100644 index 00000000..aec1e889 --- /dev/null +++ b/packages/supabase/tests/anatomical-sheets.test.ts @@ -0,0 +1,74 @@ +import { publishSheetVersion } from '../src' +import type { FaunosteoClient } from '../src' + +/** + * Stub of the two client calls publishSheetVersion makes. The real RPC is + * covered by supabase/tests/005_anatomical_sheets.sql; here the point is the + * order of the gate: no RPC call unless the document is publishable. + */ +function stubClient(content: unknown) { + const rpcCalls: unknown[] = [] + const client = { + from: () => ({ + select: () => ({ + eq: () => ({ + single: async () => ({ data: { content }, error: null }), + }), + }), + }), + rpc: async (name: string, args: unknown) => { + rpcCalls.push([name, args]) + return { data: 'published-id', error: null } + }, + } + return { client: client as unknown as FaunosteoClient, rpcCalls } +} + +const publishable = { + schema_version: 1, + species_id: 'equine', + name: { fr: 'Test', en: 'Test' }, + description: {}, + scope_disclaimer: {}, + attribution: { authors: [], license: null, notes: null }, + vertebral_config: { regions: [] }, + categories: [], + views: [ + { + id: 'view-main', + kind: 'main', + name: { fr: 'Vue', en: 'View' }, + svg_asset_id: '11111111-1111-4111-8111-111111111111', + view_box: null, + }, + ], + annotations: [], + finding_zones: [], +} + +describe('publishSheetVersion', () => { + it('calls the RPC once the document passes shape and integrity checks', async () => { + const { client, rpcCalls } = stubClient(publishable) + await expect(publishSheetVersion(client, 'v1')).resolves.toBe( + 'published-id', + ) + expect(rpcCalls).toEqual([['publish_sheet_version', { version_id: 'v1' }]]) + }) + + it('refuses a document with integrity errors before any RPC', async () => { + const { client, rpcCalls } = stubClient({ + ...publishable, + views: [{ ...publishable.views[0], svg_asset_id: null }], + }) + await expect(publishSheetVersion(client, 'v1')).rejects.toThrow( + /view\.asset\.missing/, + ) + expect(rpcCalls).toEqual([]) + }) + + it('refuses a document that does not match the schema', async () => { + const { client, rpcCalls } = stubClient({ schema_version: 2 }) + await expect(publishSheetVersion(client, 'v1')).rejects.toThrow() + expect(rpcCalls).toEqual([]) + }) +}) diff --git a/packages/supabase/tests/db/schema-types.test.ts b/packages/supabase/tests/db/schema-types.test.ts index 75f48b3a..aff66993 100644 --- a/packages/supabase/tests/db/schema-types.test.ts +++ b/packages/supabase/tests/db/schema-types.test.ts @@ -20,6 +20,50 @@ type ColumnMap = { } const expectedColumns = { + anatomical_sheet_assets: [ + 'byte_size', + 'content_type', + 'created_at', + 'created_by', + 'file_name', + 'id', + 'sha256', + 'sheet_id', + ], + anatomical_sheet_versions: [ + 'change_note', + 'content', + 'created_at', + 'created_by', + 'deprecated_at', + 'deprecated_by', + 'document_schema_version', + 'id', + 'published_at', + 'published_by', + 'sheet_id', + 'status', + 'updated_at', + 'version_number', + ], + anatomical_sheets: [ + 'archived_at', + 'based_on_sheet_id', + 'based_on_version_id', + 'created_at', + 'creator_user_id', + 'current_version_id', + 'deleted_at', + 'description', + 'id', + 'is_official', + 'name', + 'slug', + 'species_id', + 'status', + 'updated_at', + 'visibility', + ], profile_roles: [ 'granted_at', 'granted_by', @@ -43,6 +87,7 @@ const expectedColumns = { 'timezone', 'updated_at', ], + species: ['active', 'created_at', 'id', 'name_en', 'name_fr', 'sort_order'], } as const satisfies ColumnMap // Compile-time: a column present in the type but absent from the list is an error. @@ -59,7 +104,26 @@ type FunctionArgs = { string)[] } const expectedFunctionArgs = { + archive_anatomical_sheet: ['sheet_id'], + create_anatomical_sheet: [ + 'description', + 'document', + 'name', + 'official', + 'slug', + 'species_id', + 'visibility', + ], + create_sheet_draft: ['change_note', 'from_version_id', 'sheet_id'], + delete_anatomical_sheet: ['sheet_id'], + delete_sheet_asset: ['asset_id'], + delete_sheet_version: ['version_id'], + deprecate_sheet_version: ['reason', 'version_id'], + fork_anatomical_sheet: ['name', 'slug', 'source_version_id', 'visibility'], grant_platform_role: ['role', 'target_user_id'], + publish_sheet_version: ['version_id'], + register_sheet_asset: ['byte_size', 'file_name', 'sha256', 'sheet_id'], + restore_anatomical_sheet: ['sheet_id'], revoke_platform_role: ['reason', 'role', 'target_user_id'], } as const satisfies FunctionArgs @@ -78,6 +142,10 @@ interface ColumnRow { interface NameRow { name: string } +interface EnumRow { + name: string + labels: string[] +} interface FunctionRow { name: string args: string[] @@ -122,17 +190,23 @@ describe('database.types.ts matches the migrated schema', () => { expect(actual).toEqual(expected) }) - it('lists the enum labels in order', async () => { - const { rows } = await db.query( - `select e.enumlabel as name + it('lists every public enum with its labels in order', async () => { + const { rows } = await db.query( + `select t.typname as name, + array_agg(e.enumlabel order by e.enumsortorder) as labels from pg_enum e join pg_type t on t.oid = e.enumtypid join pg_namespace n on n.oid = t.typnamespace - where n.nspname = 'public' and t.typname = 'platform_role' - order by e.enumsortorder`, + where n.nspname = 'public' + group by t.typname order by t.typname`, ) - expect(rows.map((r) => r.name)).toEqual([ - ...Constants.public.Enums.platform_role, - ]) + const actual = Object.fromEntries(rows.map((r) => [r.name, r.labels])) + const expected = Object.fromEntries( + Object.entries(Constants.public.Enums).map(([name, labels]) => [ + name, + [...labels], + ]), + ) + expect(actual).toEqual(expected) }) it('lists exactly the public functions and their argument names', async () => {