feat(structures): add Region/Delegation/Structure schema + seed (ADR-0027 PR 1)
Schema (Prisma): three new public-schema models per ADR-0027 - Region (INSEE code PK), Delegation (dept code PK + regionCode FK), Structure (portal-internal code PK + kind discriminator + nullable unique finess/siret/codePaie + nullable delegationCode FK). Migration: hand-written, matches the style of init_audit_schema and users_directory. DDL + CHECK constraint on Structure.kind mirroring STRUCTURE_KINDS + indexes + inline seed (Region 75 Nouvelle-Aquitaine, Delegation 33 Gironde, structures 0330800013 + 0330800021 medico-social Bordeaux/Merignac + siege APF national). Seed is the minimum the 19 test-tenant personas reference; ADR-0029's cascade sync supersedes it when it ships. Catalogue (TypeScript): apps/portal-bff/src/structures/structure-kind.ts exports STRUCTURE_KINDS as const + StructureKind type union + isStructureKind type guard. Jest spec covers catalogue content, type guard, narrowing. Drift gate: extended to scan property-literal "kind: 'X'" expressions in files that import from structure-kind.ts. Three-layer defense in depth for Structure.kind = TS type union + Postgres CHECK + CI gate. 22 spec tests passing. No consumer code yet - PrismaScopeResolver that dereferences Structure.code from UserScope.value lives in ADR-0026 PR 2, which depends on this PR landing first. Reviewer heads-up: schema.prisma already has a User model (ADR-0020 user-directory cache); ADR-0026 PR 1 introduces a different User (UUID PK + Person FK). Naming collision flagged for that PR to resolve - not touched here.
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
-- Organisational hierarchy (per ADR-0027).
|
||||
--
|
||||
-- Region → Delegation → Structure. The portal's ADR-0025 scope-axis
|
||||
-- dereferences these three layers — the BFF guard
|
||||
-- `principalCoversResource` walks etablissement → delegation → region
|
||||
-- to decide whether a scope covers a resource. The schema lives in
|
||||
-- the public schema; ADR-0029 will populate the full APF inventory
|
||||
-- via the cascade sync, additively into the columns defined here.
|
||||
--
|
||||
-- Inline seed at the bottom of this file: just the codes the
|
||||
-- test tenant exercises (Région Nouvelle-Aquitaine, Délégation
|
||||
-- Gironde, two médico-social structures + the APF national siège).
|
||||
-- Superseded — not extended — by ADR-0029's cascade sync once it
|
||||
-- ships.
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "regions" (
|
||||
"code" TEXT NOT NULL,
|
||||
"name" TEXT NOT NULL,
|
||||
|
||||
CONSTRAINT "regions_pkey" PRIMARY KEY ("code")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "delegations" (
|
||||
"code" TEXT NOT NULL,
|
||||
"name" TEXT NOT NULL,
|
||||
"region_code" TEXT NOT NULL,
|
||||
|
||||
CONSTRAINT "delegations_pkey" PRIMARY KEY ("code")
|
||||
);
|
||||
|
||||
-- Closed-set CHECK on Structure.kind. Mirrors STRUCTURE_KINDS in
|
||||
-- apps/portal-bff/src/structures/structure-kind.ts. The CI drift gate
|
||||
-- (scripts/check-catalogue-drift.mjs) asserts the TS constant matches
|
||||
-- the values used in code; this CHECK constraint provides the
|
||||
-- equivalent guarantee at the DB layer for any code path that
|
||||
-- bypasses the type system.
|
||||
CREATE TABLE "structures" (
|
||||
"code" TEXT NOT NULL,
|
||||
"name" TEXT NOT NULL,
|
||||
"kind" TEXT NOT NULL,
|
||||
"finess" TEXT,
|
||||
"siret" TEXT,
|
||||
"code_paie" TEXT,
|
||||
"delegation_code" TEXT,
|
||||
|
||||
CONSTRAINT "structures_pkey" PRIMARY KEY ("code"),
|
||||
CONSTRAINT "structures_kind_check" CHECK (
|
||||
"kind" IN (
|
||||
'medico_social',
|
||||
'antenne',
|
||||
'dispositif',
|
||||
'entreprise_adaptee',
|
||||
'mouvement',
|
||||
'administratif',
|
||||
'siege'
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "delegations_region_code_idx" ON "delegations"("region_code");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "structures_finess_key" ON "structures"("finess");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "structures_siret_key" ON "structures"("siret");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "structures_code_paie_key" ON "structures"("code_paie");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "structures_kind_idx" ON "structures"("kind");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "structures_delegation_code_idx" ON "structures"("delegation_code");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "delegations" ADD CONSTRAINT "delegations_region_code_fkey"
|
||||
FOREIGN KEY ("region_code") REFERENCES "regions"("code")
|
||||
ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "structures" ADD CONSTRAINT "structures_delegation_code_fkey"
|
||||
FOREIGN KEY ("delegation_code") REFERENCES "delegations"("code")
|
||||
ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- ------------------------------------------------------------------
|
||||
-- Test-tenant reference seed (per ADR-0027 §"Seeding posture").
|
||||
--
|
||||
-- Region Nouvelle-Aquitaine + Délégation Gironde + the structures
|
||||
-- referenced by the 19 personas in notes/test-tenant-role-assignments.md:
|
||||
-- - 0330800013 (APF Bordeaux, FINESS = code)
|
||||
-- - 0330800021 (Complexe Mérignac, FINESS = code)
|
||||
-- - 'siege' (APF national headquarters)
|
||||
--
|
||||
-- Cascade-sync (ADR-0029) supersedes this seed entirely when it
|
||||
-- ships — the cleanup migration responsible for that truncates and
|
||||
-- repopulates from cascade's authoritative inventory.
|
||||
-- ------------------------------------------------------------------
|
||||
|
||||
INSERT INTO "regions" ("code", "name") VALUES
|
||||
('75', 'Nouvelle-Aquitaine');
|
||||
|
||||
INSERT INTO "delegations" ("code", "name", "region_code") VALUES
|
||||
('33', 'Gironde', '75');
|
||||
|
||||
INSERT INTO "structures" ("code", "name", "kind", "finess", "delegation_code") VALUES
|
||||
('0330800013', 'APF Bordeaux', 'medico_social', '0330800013', '33'),
|
||||
('0330800021', 'Complexe Mérignac', 'medico_social', '0330800021', '33');
|
||||
|
||||
-- Siège has no delegation parent and no FINESS/SIRET/codePaie (those
|
||||
-- columns stay NULL — the unique indexes tolerate multiple NULLs).
|
||||
INSERT INTO "structures" ("code", "name", "kind") VALUES
|
||||
('siege', 'Siège APF France handicap', 'siege');
|
||||
Reference in New Issue
Block a user