From d80ed3587fa187044849d9d9f8dd6668fef502b0 Mon Sep 17 00:00:00 2001 From: Nicolas KOKLA Date: Thu, 20 Jun 2024 18:38:45 +0200 Subject: [PATCH] Add POST route for extract new format BAN from multi cog --- lib/api/extract/routes.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/api/extract/routes.js b/lib/api/extract/routes.js index 28041afe..50e93ace 100644 --- a/lib/api/extract/routes.js +++ b/lib/api/extract/routes.js @@ -3,12 +3,11 @@ import {streamBanDataFromCog, testIsEnabledCog} from './models.js' const app = new express.Router() -app.get('/cog/:cog?', async (req, res) => { +const banFormatMiddleware = async (req, res, next) => { let response try { - const {cog} = req.params - const cogList = cog && [cog] - const fileName = cog ? `ban_${cog}.json` : 'ban_france.json' + const {cogList} = req.params + const fileName = cogList ? `ban_${cogList.join('_')}.json` : 'ban_france.json' const checkCog = ( await Promise.all( @@ -50,6 +49,26 @@ app.get('/cog/:cog?', async (req, res) => { } res.send(response) -}) + + next() +} + +app.get( + '/cog/:cog?', + (req, res, next) => { + const {cog} = req.params + req.params.cogList = cog ? [cog] : null + next() + }, + banFormatMiddleware +) +app.post( + '/cog', + (req, res, next) => { + req.params.cogList = req.body.cogList.map(cog => cog.toString().padStart(5, '0')) + next() + }, + banFormatMiddleware +) export default app