From 1465c9dfd227032995896cedaf86f46276487dbb Mon Sep 17 00:00:00 2001 From: Sosthene Date: Wed, 27 Aug 2025 17:51:48 +0200 Subject: [PATCH] Improve idnot api --- src/server.js | 82 ++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 37 deletions(-) diff --git a/src/server.js b/src/server.js index 8bda57b..1f6db64 100644 --- a/src/server.js +++ b/src/server.js @@ -231,54 +231,62 @@ app.get('/api/v1/db/:tableName', async (req, res) => { } }); -app.get('/api/notaries', async (req, res) => { - const page = req.query.page || 1; - const size = req.query.size || 10; - const firstName = req.query.firstName || null; - const lastName = req.query.lastName || null; +// Returns all the office data for each rattachement of the user +app.get('/api/v1/idnot/user/rattachements', async (req, res) => { + const { idNot } = req.query; const searchParams = new URLSearchParams({ - key: 'ba557f84-0bf6-4dbf-844f-df2767555e3e' + key: process.env.IDNOT_API_KEY, + deleted: false }); - try { - let url = `https://qual-api.notaires.fr/annuaire/api/pp/v2/personnes?page=${page}&size=${size}`; - if (firstName) { - url += `&ctmPrenom=${firstName}`; - } - if (lastName) { - url += `&ctmNomUsuel=${lastName}`; - } + const url = `${process.env.IDNOT_ANNUARY_BASE_URL}/api/pp/v2/personnes/${idNot}/rattachements?` + searchParams; - const json = await ( - await fetch(`${url}&` + searchParams, { + const json = await ( + await fetch(url, { + method: 'GET' + }) + ).json(); + + // if json.result.length is 0, return 404 + if (json.result.length === 0) { + return res.status(404).json({ + success: false, + message: 'No rattachements found' + }); + } + + // Iterate over all results and get the office data by calling the entiteUrl endpoint + const officeData = await Promise.all(json.result.map(async (result) => { + const officeData = await ( + await fetch(`${process.env.IDNOT_ANNUARY_BASE_URL}${result.entiteUrl}?` + searchParams, { method: 'GET' }) ).json(); + return officeData; + })); - json.result = json.result.map(r => { - return { - id: r.uid, - activity: r.activite, - civility: getCivility(r.civility), - lastName: r.nomUsuel, - firstName: r.prenom, - crpcen: r.numeroAdherentCrpcen, - lastModified: r.lastModified, - deleted: r.deleted - } - }); + res.json(officeData); +}); - json.totalPages = Math.ceil(json.totalResultCount / json.size); - delete json.totalResultCount; +// Returns the user data for each user rattached to an entity +app.get('/api/v1/idnot/office/rattachements', async (req, res) => { + const { idNot } = req.query; - res.json(json); - } catch (error) { - res.status(500).json({ - error: 'Internal Server Error', - message: error.message - }); - } + const searchParams = new URLSearchParams({ + key: process.env.IDNOT_API_KEY, + deleted: false + }); + + const url = `${process.env.IDNOT_ANNUARY_BASE_URL}/api/pp/v2/entites/${idNot}/personnes?` + searchParams; + + const json = await ( + await fetch(url, { + method: 'GET' + }) + ).json(); + + res.json(json); }); app.post('/api/v1/idnot/user/:code', async (req, res) => {