From 4b5c11adcd2b86595b390c47ee9b399f288892dc Mon Sep 17 00:00:00 2001 From: Florian Loreau Date: Thu, 18 Apr 2024 14:27:51 +0200 Subject: [PATCH] Add API route to save postal areas in postgres database --- lib/api/postal-code/models.js | 9 ++++++++ lib/api/postal-code/routes.js | 40 +++++++++++++++++++++++++++++++++++ lib/api/routes.js | 2 ++ 3 files changed, 51 insertions(+) create mode 100644 lib/api/postal-code/models.js create mode 100644 lib/api/postal-code/routes.js diff --git a/lib/api/postal-code/models.js b/lib/api/postal-code/models.js new file mode 100644 index 00000000..57f32798 --- /dev/null +++ b/lib/api/postal-code/models.js @@ -0,0 +1,9 @@ +import {PostalArea} from '../../util/sequelize.js' + +export const getAllPostalAreas = () => PostalArea.findAll({attributes: ['postalCode']}) + +export const putPostalAreas = postalAreas => PostalArea.bulkCreate(postalAreas, {updateOnDuplicate: ['geometry'], subQuery: false}) + +export const deletePostalAreas = postalCodes => { + postalCodes.map(postalCode => PostalArea.destroy({where: {postalCode}})) +} diff --git a/lib/api/postal-code/routes.js b/lib/api/postal-code/routes.js new file mode 100644 index 00000000..148728ec --- /dev/null +++ b/lib/api/postal-code/routes.js @@ -0,0 +1,40 @@ +import 'dotenv/config.js' // eslint-disable-line import/no-unassigned-import +import express from 'express' +import auth from '../../middleware/auth.js' +import {getAllPostalAreas, putPostalAreas, deletePostalAreas} from './models.js' + +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 + } + + return {postalCode, geometry} + }) + + const bulkPostalCode = putPostalAreas(bulkOperations) + + const deletePostalCode = postalCodesFromDB.size > 0 ? deletePostalAreas([...postalCodesFromDB]) : true + + const operations = await Promise.all([bulkPostalCode, deletePostalCode]) + + res.send( + { + date: new Date(), + status: 'success', + response: operations, + } + ) + }) + +export default app diff --git a/lib/api/routes.js b/lib/api/routes.js index 2ac47ff5..393509b5 100644 --- a/lib/api/routes.js +++ b/lib/api/routes.js @@ -6,6 +6,7 @@ import commonToponymRoutes from './common-toponym/routes.js' import districtRoutes from './district/routes.js' import statusRoutes from './job-status/routes.js' import banIdRoutes from './ban-id/routes.js' +import postalCodeRoutes from './postal-code/routes.js' const app = new express.Router() @@ -14,5 +15,6 @@ app.use('/common-toponym', commonToponymRoutes) app.use('/district', districtRoutes) app.use('/job-status', statusRoutes) app.use('/ban-id', banIdRoutes) +app.use('/postal-code', postalCodeRoutes) export default app