-
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.
Add API route to save postal areas in postgres database
- Loading branch information
1 parent
4af8cda
commit 06d2369
Showing
3 changed files
with
61 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}})) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import 'dotenv/config.js' // eslint-disable-line import/no-unassigned-import | ||
import {customAlphabet} from 'nanoid' | ||
import express, {response} from 'express' | ||
import queue from '../../util/queue.cjs' | ||
import auth from '../../middleware/auth.js' | ||
import analyticsMiddleware from '../../middleware/analytics.js' | ||
import {getAllPostalAreas, putPostalAreas, deletePostalAreas} from './models.js' | ||
|
||
const apiQueue = queue('api') | ||
|
||
const BAN_API_URL | ||
= process.env.BAN_API_URL || 'https://plateforme.adresse.data.gouv.fr/api' | ||
|
||
const nanoid = customAlphabet('123456789ABCDEFGHJKMNPQRSTVWXYZ', 9) | ||
|
||
const app = new express.Router() | ||
|
||
app.route('/') | ||
.put(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 |
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