diff --git a/lib/api/district/routes.js b/lib/api/district/routes.js index 11158d1a..8dbe9445 100644 --- a/lib/api/district/routes.js +++ b/lib/api/district/routes.js @@ -4,7 +4,6 @@ import express from 'express' import queue from '../../util/queue.cjs' import auth from '../../middleware/auth.js' import analyticsMiddleware from '../../middleware/analytics.js' -import fetch from '../../util/fetch.cjs' import {getDistrict, getDistrictsFromCog, deleteDistrict} from './models.js' import {formatDistrict} from './utils.js' @@ -100,6 +99,69 @@ app.route('/') res.send(response) }) +app.route('/postal-codes-from-datanova-url', auth) + .put(async (req, res) => { + let response + try { + // On February 2024 the postal file url is : + // https://datanova.laposte.fr/data-fair/api/v1/datasets/laposte-hexasmal/raw + const {url} = req.query + const statusID = nanoid() + + await apiQueue.add( + {dataType: 'district', jobType: 'updatePostalCodeFromUrl', data: url, statusID}, + {jobId: statusID, removeOnComplete: true} + ) + + response = { + date: new Date(), + status: 'success', + message: `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`, + response: {statusID}, + } + } catch (error) { + const {message} = error + response = { + date: new Date(), + status: 'error', + message, + response: {}, + } + } + + res.send(response) + }) + +app.route('/postal-codes-from-datanova', auth) + .put(express.text(), async (req, res) => { + let response + try { + const postalFile = req.body + const statusID = nanoid() + + await apiQueue.add( + {dataType: 'district', jobType: 'updatePostalCode', data: postalFile, statusID}, + {jobId: statusID, removeOnComplete: true} + ) + response = { + date: new Date(), + status: 'success', + message: `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`, + response: {statusID}, + } + } catch (error) { + const {message} = error + response = { + date: new Date(), + status: 'error', + message, + response: {}, + } + } + + res.send(response) + }) + app.route('/:districtID') .get(analyticsMiddleware, async (req, res) => { let response @@ -220,66 +282,4 @@ app.get('/cog/:cog', analyticsMiddleware, async (req, res) => { res.send(response) }) -app.route('/codePostal', auth) - .get(async (req, res) => { - let response - try { - // On February 2024 the postal file url is : - // https://datanova.laposte.fr/data-fair/api/v1/datasets/laposte-hexasmal/raw - const {url} = req.query - const postalFile = await fetch(url) - const statusID = nanoid() - - await apiQueue.add( - {dataType: 'district', jobType: 'updatePostalCode', data: postalFile, statusID}, - {jobId: statusID, removeOnComplete: true} - ) - - response = { - date: new Date(), - status: 'success', - message: `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`, - response: {statusID}, - } - } catch (error) { - const {message} = error - response = { - date: new Date(), - status: 'error', - message, - response: {}, - } - } - - res.send(response) - }) - .post(express.text(), async (req, res) => { - let response - try { - const postalFile = req.body - const statusID = nanoid() - - await apiQueue.add( - {dataType: 'district', jobType: 'updatePostalCode', data: postalFile, statusID}, - {jobId: statusID, removeOnComplete: true} - ) - response = { - date: new Date(), - status: 'success', - message: `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`, - response: {statusID}, - } - } catch (error) { - const {message} = error - response = { - date: new Date(), - status: 'error', - message, - response: {}, - } - } - - res.send(response) - }) - export default app diff --git a/lib/api/district/utils.js b/lib/api/district/utils.js index c60ea463..94966f2d 100644 --- a/lib/api/district/utils.js +++ b/lib/api/district/utils.js @@ -1,4 +1,5 @@ import Papa from 'papaparse' +import fetch from '../../util/fetch.cjs' import {checkDataFormat, dataValidationReportFrom, checkIdsIsUniq, checkIdsIsVacant, checkIdsIsAvailable, checkDataShema, checkIdsShema} from '../helper.js' import {banID} from '../schema.js' import {getDistricts, getDistrictsFromCogList} from './models.js' @@ -100,7 +101,7 @@ export async function formatDataNova(postalFile) { (acc, district) => ({ ...acc, ...(district?.meta?.insee?.cog - ? {[district.meta.insee.cog]: [...acc[district.meta.insee.cog], district.id]} + ? {[district.meta.insee.cog]: [...(acc[district.meta.insee.cog] || []), district.id]} : {} ), }), {}) @@ -136,3 +137,9 @@ export async function formatDataNova(postalFile) { } })) } + +export async function formatDataNovaFromUrl(url) { + const postalFileResponse = await fetch(url) + const postalFile = await postalFileResponse.text() + return formatDataNova(postalFile) +}