Règle posée (CKPT-AEP-1211-B4b-RECAP §4 point 4) : une posture lue dans NocoDB s'affiche sans source par posture ; la source reste au mapping. Une ligne au type vide retombe sur le mapping pour les deux colonnes. Recoupé sur la base réelle : 91 lignes, 0 écart avec le mapping. Suite taxonomie 41 → 56. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
158 lines
7.4 KiB
TypeScript
158 lines
7.4 KiB
TypeScript
/**
|
|
* taxonomie — B4a-M3, B4b-M3. Le type d'organisation et les postures de chaque fiche de la
|
|
* carte. Une structure des réseaux les porte elle-même (`type_organisation`, `postures`,
|
|
* `taxonomie_source` dans `reseaux-bifurcation.json`, B4b) ; une Org d'entraide les lit dans
|
|
* ses colonnes NocoDB quand elles sont remplies (B4b-3) ; sinon, ils sont lus dans
|
|
* `data/taxonomie/organisations.json` (une entrée par identifiant : `org:<Id NocoDB>`,
|
|
* `structure:<id>`), devenu la trace de la migration. Vocabulaire : la taxonomie N
|
|
* (`Cadrage/N-prompt-session-taxonomie-carte-unifiee.md`).
|
|
*
|
|
* Pur (aucune dépendance à Vue ni au routeur) : `scripts/test-taxonomie.mjs` le teste,
|
|
* `pages/index.vue` le passe à `toMini` (useFicheAdapter), qui lui donne l'identifiant et,
|
|
* pour une structure, la fiche elle-même, et en fait les étiquettes `type` et `postures`.
|
|
*/
|
|
import mapping from '../data/taxonomie/organisations.json'
|
|
|
|
export const TYPES_ORGANISATION = ['institution', 'association', 'syndicat', 'agence', 'formation', 'media', 'autre'] as const
|
|
export type TypeOrganisation = (typeof TYPES_ORGANISATION)[number]
|
|
|
|
export const POSTURES = ['regenerative', 'bas-carbone', 'cooperative', 'patrimoine', 'ecoconstruction', 'low-tech', 'participatif'] as const
|
|
export type Posture = (typeof POSTURES)[number]
|
|
|
|
/** Libellés des chips (valeur d'URL → texte). */
|
|
export const LIBELLES_TYPE: Record<TypeOrganisation, string> = {
|
|
institution: 'Institution',
|
|
association: 'Association',
|
|
syndicat: 'Syndicat',
|
|
agence: 'Agence',
|
|
formation: 'Formation',
|
|
media: 'Média',
|
|
autre: 'Autre',
|
|
}
|
|
|
|
export const LIBELLES_POSTURE: Record<Posture, string> = {
|
|
regenerative: 'Régénératif',
|
|
'bas-carbone': 'Bas carbone',
|
|
cooperative: 'Coopératif',
|
|
patrimoine: 'Patrimoine',
|
|
ecoconstruction: 'Écoconstruction',
|
|
'low-tech': 'Low-tech',
|
|
participatif: 'Participatif',
|
|
}
|
|
|
|
/** Une entrée du fichier. `nom` n'est là que pour qui corrige le fichier à la main. */
|
|
export interface EntreeTaxonomie {
|
|
nom?: string
|
|
type_organisation: string
|
|
postures: string[]
|
|
source: { type_organisation: string; postures: Record<string, string> }
|
|
}
|
|
|
|
export interface MappingTaxonomie {
|
|
version: number
|
|
entrees: Record<string, EntreeTaxonomie>
|
|
}
|
|
|
|
/** Ce qu'une fiche porte, toujours dans le vocabulaire. */
|
|
export interface Classement {
|
|
type: TypeOrganisation
|
|
postures: Posture[]
|
|
}
|
|
|
|
export const MAPPING = mapping as MappingTaxonomie
|
|
|
|
export const idOrg = (id: number | string) => `org:${id}`
|
|
export const idStructure = (id: string) => `structure:${id}`
|
|
|
|
const estType = (v: unknown): v is TypeOrganisation => (TYPES_ORGANISATION as readonly string[]).includes(v as string)
|
|
const estPosture = (v: unknown): v is Posture => (POSTURES as readonly string[]).includes(v as string)
|
|
|
|
/**
|
|
* Ce qu'une fiche porte elle-même (B4b) : les champs `type_organisation`, `postures` et
|
|
* `taxonomie_source` d'une structure de `reseaux-bifurcation.json` (recopiés du mapping par
|
|
* `scripts/porter-taxonomie-reseaux.mjs`). Tout est optionnel : une fiche sans ces champs
|
|
* est lue dans le mapping.
|
|
*/
|
|
export interface PorteTaxonomie {
|
|
type_organisation?: unknown
|
|
postures?: unknown
|
|
taxonomie_source?: { type_organisation?: string; postures?: Record<string, string> } | null
|
|
}
|
|
|
|
/** Une entrée (du mapping, ou portée par la fiche) → classement, selon la même règle. */
|
|
function lireEntree(type: unknown, postures: unknown, sources: Record<string, string> | undefined): Classement {
|
|
const ps = Array.isArray(postures) ? postures : []
|
|
return {
|
|
type: estType(type) ? type : 'autre',
|
|
postures: POSTURES.filter((p) => ps.includes(p) && !!sources?.[p]),
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Postures d'une ligne NocoDB : le choix multiple arrive en texte « a,b » (API v2), parfois
|
|
* en liste. Vide, nul ou d'une autre forme → aucune posture.
|
|
*/
|
|
export function posturesNocodb(v: unknown): string[] {
|
|
const brut = Array.isArray(v) ? v : typeof v === 'string' ? v.split(',') : []
|
|
return brut.map((x) => String(x).trim()).filter(Boolean)
|
|
}
|
|
|
|
/**
|
|
* Une Org d'entraide lue dans NocoDB (B4b-3) : colonnes `type_organisation` (choix unique)
|
|
* et `postures` (choix multiple), remplies le 12/11 depuis le mapping. Règle posée le 14/11
|
|
* (CKPT-AEP-1211-B4b-RECAP §4 point 4) : **une posture lue dans NocoDB s'affiche sans source
|
|
* par posture** ; la source reste écrite dans le mapping, qui garde la trace. Le vocabulaire
|
|
* tient toujours : type hors liste → `autre`, posture hors liste ignorée.
|
|
*/
|
|
function lireNocodb(type: unknown, postures: unknown): Classement {
|
|
const ps = posturesNocodb(postures)
|
|
return { type: estType(type) ? type : 'autre', postures: POSTURES.filter((p) => ps.includes(p)) }
|
|
}
|
|
|
|
/**
|
|
* Classement d'une fiche. Le champ de la fiche d'abord (`porte`, quand elle a un
|
|
* `type_organisation`), le fichier de mapping en repli (`id`). Repli : un identifiant
|
|
* absent (fiche ajoutée après, sans champ) ou un type hors vocabulaire vaut `autre` ; une
|
|
* posture hors vocabulaire ou sans source est ignorée (une posture ne s'affiche que si sa
|
|
* source est écrite), qu'elle vienne de la fiche ou du mapping. Exception : une Org (`org:`)
|
|
* dont la ligne NocoDB a un `type_organisation` est lue dans NocoDB, type et postures
|
|
* ensemble, postures sans source (`lireNocodb`) ; une ligne au type vide retombe sur le
|
|
* mapping pour les deux colonnes, pour qu'une colonne vidée à moitié ne mélange pas deux sources.
|
|
*/
|
|
export function classement(id: string, porte?: PorteTaxonomie | null, m: MappingTaxonomie = MAPPING): Classement {
|
|
if (porte && typeof porte.type_organisation === 'string' && porte.type_organisation) {
|
|
if (id.startsWith('org:')) return lireNocodb(porte.type_organisation, porte.postures)
|
|
return lireEntree(porte.type_organisation, porte.postures, porte.taxonomie_source?.postures)
|
|
}
|
|
const e = m.entrees[id]
|
|
if (!e) return { type: 'autre', postures: [] }
|
|
return lireEntree(e.type_organisation, e.postures, e.source?.postures)
|
|
}
|
|
|
|
/**
|
|
* Écarts d'un mapping à la règle (liste vide = conforme) : type et postures dans le
|
|
* vocabulaire, une source pour le type et pour chaque posture, pas de posture en double.
|
|
*/
|
|
export function ecartsMapping(m: MappingTaxonomie): string[] {
|
|
const out: string[] = []
|
|
for (const [id, e] of Object.entries(m.entrees)) {
|
|
if (!/^(org:\d+|structure:[a-z0-9-]+)$/.test(id)) out.push(`${id} : identifiant hors forme (org:<Id> ou structure:<id>)`)
|
|
if (!estType(e.type_organisation)) out.push(`${id} : type « ${e.type_organisation} » hors vocabulaire`)
|
|
if (!e.source?.type_organisation?.trim()) out.push(`${id} : type sans source`)
|
|
const ps = e.postures ?? []
|
|
if (new Set(ps).size !== ps.length) out.push(`${id} : posture en double`)
|
|
for (const p of ps) {
|
|
if (!estPosture(p)) out.push(`${id} : posture « ${p} » hors vocabulaire`)
|
|
if (!e.source?.postures?.[p]?.trim()) out.push(`${id} : posture « ${p} » sans source`)
|
|
}
|
|
for (const p of Object.keys(e.source?.postures ?? {})) if (!ps.includes(p)) out.push(`${id} : source pour « ${p} », absente de postures`)
|
|
}
|
|
return out
|
|
}
|
|
|
|
/** Valeurs d'une dimension présentes dans des fiches, dans l'ordre du vocabulaire (chips sans zéro). */
|
|
export function valeursPresentes<T extends string>(vocabulaire: readonly T[], libelles: Record<T, string>, portees: Iterable<string>): { valeur: T; libelle: string }[] {
|
|
const vues = new Set(portees)
|
|
return vocabulaire.filter((v) => vues.has(v)).map((v) => ({ valeur: v, libelle: libelles[v] }))
|
|
}
|