From 9d7c11a2b706c52ef837d2890d3fe74da4ecc9b9 Mon Sep 17 00:00:00 2001 From: dev4 Date: Fri, 19 Sep 2025 12:24:55 +0000 Subject: [PATCH] ci: docker_tag=ext - Fix: fetch entite/personne data separately v1.1.0 --- package.json | 2 +- src/controllers/idnot.controller.ts | 29 +++++++++++-- src/services/idnot/index.ts | 66 ++++++++++++++++++++++++++--- 3 files changed, 87 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index fd83e2c..009f75f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lecoffre-back-mini", - "version": "1.0.9", + "version": "1.1.0", "description": "Mini serveur avec une route /api/ping", "main": "dist/server.js", "scripts": { diff --git a/src/controllers/idnot.controller.ts b/src/controllers/idnot.controller.ts index 11bae26..4c1001b 100644 --- a/src/controllers/idnot.controller.ts +++ b/src/controllers/idnot.controller.ts @@ -113,12 +113,33 @@ export class IdNotController { Logger.info('IdNot using rattachements API with sub', { sub: payload.sub }); const rattachementsJson = await IdNotService.getUserRattachements(payload.sub); const results: any[] = Array.isArray(rattachementsJson?.result) ? rattachementsJson.result : []; - // pick first office rattachement with a defined entite - const candidate = results.find((r: any) => r?.entite?.typeEntite?.name === 'office') || results[0]; - if (!candidate) { + + if (results.length === 0) { throw new ForbiddenError('User not attached to an office'); } - const userData = candidate; + + // Get the first rattachement + const rattachement = results[0]; + + // Fetch entite and personne data separately if not present + let entiteData = rattachement.entite; + let personneData = rattachement.personne; + + if (!entiteData && rattachement.entiteUrl) { + Logger.info('IdNot fetching entite data', { entiteUrl: rattachement.entiteUrl }); + entiteData = await IdNotService.getEntiteData(rattachement.entiteUrl); + } + + if (!personneData && rattachement.personneUrl) { + Logger.info('IdNot fetching personne data', { personneUrl: rattachement.personneUrl }); + personneData = await IdNotService.getPersonneData(rattachement.personneUrl); + } + + const userData = { + ...rattachement, + entite: entiteData, + personne: personneData + }; // Log d'analyse (non sensible) pour diagnostiquer les cas de rattachement Logger.info('IdNot userData summary', { diff --git a/src/services/idnot/index.ts b/src/services/idnot/index.ts index 2a12792..460aede 100644 --- a/src/services/idnot/index.ts +++ b/src/services/idnot/index.ts @@ -231,18 +231,74 @@ export class IdNotService { throw new ExternalServiceError('IdNot', 'Failed to fetch user data after all attempts'); } - static async getOfficeLocationData(locationsUrl: string) { - const { IDNOT_API_KEY, IDNOT_API_BASE_URL } = process.env; + static async getEntiteData(entiteUrl: string) { + const { IDNOT_API_KEY, IDNOT_ANNUARY_BASE_URL } = process.env; - if (!IDNOT_API_KEY || !IDNOT_API_BASE_URL) { - throw new Error('Missing IDnot API key or base URL'); + if (!IDNOT_API_KEY || !IDNOT_ANNUARY_BASE_URL) { + throw new Error('Missing IDnot API key or annuary base URL'); } const searchParams = new URLSearchParams({ key: IDNOT_API_KEY }); - const locUrl = `${IDNOT_API_BASE_URL}${locationsUrl}?` + searchParams; + const url = `${IDNOT_ANNUARY_BASE_URL}${entiteUrl}?${searchParams}`; + const response = await fetch(url, { method: 'GET' }); + + if (!response.ok) { + const text = await response.text().catch(() => ''); + Logger.error('IdNot getEntiteData failed', { + url, + status: response.status, + statusText: response.statusText, + bodySnippet: text?.substring(0, 500) + }); + throw new Error(`Failed to fetch entite data: ${response.status} ${response.statusText}`); + } + + return response.json(); + } + + static async getPersonneData(personneUrl: string) { + const { IDNOT_API_KEY, IDNOT_ANNUARY_BASE_URL } = process.env; + + if (!IDNOT_API_KEY || !IDNOT_ANNUARY_BASE_URL) { + throw new Error('Missing IDnot API key or annuary base URL'); + } + + const searchParams = new URLSearchParams({ + key: IDNOT_API_KEY + }); + + const url = `${IDNOT_ANNUARY_BASE_URL}${personneUrl}?${searchParams}`; + const response = await fetch(url, { method: 'GET' }); + + if (!response.ok) { + const text = await response.text().catch(() => ''); + Logger.error('IdNot getPersonneData failed', { + url, + status: response.status, + statusText: response.statusText, + bodySnippet: text?.substring(0, 500) + }); + throw new Error(`Failed to fetch personne data: ${response.status} ${response.statusText}`); + } + + return response.json(); + } + + static async getOfficeLocationData(locationsUrl: string) { + const { IDNOT_API_KEY, IDNOT_ANNUARY_BASE_URL } = process.env; + + if (!IDNOT_API_KEY || !IDNOT_ANNUARY_BASE_URL) { + throw new Error('Missing IDnot API key or annuary base URL'); + } + + const searchParams = new URLSearchParams({ + key: IDNOT_API_KEY + }); + + const locUrl = `${IDNOT_ANNUARY_BASE_URL}${locationsUrl}?${searchParams}`; const locResp = await fetch(locUrl, { method: 'GET' }); if (!locResp.ok) { const text = await locResp.text().catch(() => '');