-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make postal areas API route asynchronous
- Loading branch information
1 parent
4b5c11a
commit 69aba5e
Showing
3 changed files
with
111 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,46 @@ | ||
import 'dotenv/config.js' // eslint-disable-line import/no-unassigned-import | ||
import {customAlphabet} from 'nanoid' | ||
import express from 'express' | ||
import queue from '../../util/queue.cjs' | ||
import auth from '../../middleware/auth.js' | ||
import {getAllPostalAreas, putPostalAreas, deletePostalAreas} from './models.js' | ||
Check failure on line 6 in lib/api/postal-code/routes.js GitHub Actions / build (16.x)
Check failure on line 6 in lib/api/postal-code/routes.js GitHub Actions / build (16.x)
|
||
|
||
const app = new express.Router() | ||
|
||
app.route('/') | ||
.put(auth, async (req, res) => { | ||
const postalCodeDBResponse = await getAllPostalAreas() || [] | ||
const postalCodesFromDB = new Set(postalCodeDBResponse.map(({postalCode}) => postalCode)) | ||
|
||
const {features, crs} = req.body || {} | ||
|
||
const bulkOperations = features.map(({properties, geometry}) => { | ||
const postalCode = properties.cp | ||
postalCodesFromDB.delete(postalCode) | ||
if (geometry.crs === undefined) { | ||
geometry.crs = crs | ||
} | ||
const apiQueue = queue('api') | ||
|
||
return {postalCode, geometry} | ||
}) | ||
const BAN_API_URL | ||
= process.env.BAN_API_URL || 'https://plateforme.adresse.data.gouv.fr/api' | ||
|
||
const bulkPostalCode = putPostalAreas(bulkOperations) | ||
const nanoid = customAlphabet('123456789ABCDEFGHJKMNPQRSTVWXYZ', 9) | ||
|
||
const deletePostalCode = postalCodesFromDB.size > 0 ? deletePostalAreas([...postalCodesFromDB]) : true | ||
|
||
const operations = await Promise.all([bulkPostalCode, deletePostalCode]) | ||
const app = new express.Router() | ||
|
||
res.send( | ||
{ | ||
app.route('/') | ||
.put(auth, async (req, res) => { | ||
let response | ||
try { | ||
const postalAreas = req.body || {} | ||
const statusID = nanoid() | ||
|
||
await apiQueue.add( | ||
{dataType: 'postalArea', jobType: 'update', data: postalAreas, statusID}, | ||
{jobId: statusID, removeOnComplete: true} | ||
) | ||
response = { | ||
date: new Date(), | ||
status: 'success', | ||
response: operations, | ||
message: `Check the status of your request : ${BAN_API_URL}/job-status/${statusID}`, | ||
response: {statusID}, | ||
} | ||
) | ||
} catch (error) { | ||
response = { | ||
date: new Date(), | ||
status: 'error', | ||
message: error, | ||
response: {}, | ||
} | ||
} | ||
|
||
res.send(response) | ||
}) | ||
|
||
export default app |