Skip to content

Commit

Permalink
Add API route to save postal areas in postgres database
Browse files Browse the repository at this point in the history
  • Loading branch information
FLoreauIGN committed Apr 18, 2024
1 parent 4af8cda commit 06d2369
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/api/postal-code/models.js
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}}))
}
50 changes: 50 additions & 0 deletions lib/api/postal-code/routes.js
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'

Check failure on line 3 in lib/api/postal-code/routes.js

View workflow job for this annotation

GitHub Actions / build (16.x)

'response' is defined but never used.
import queue from '../../util/queue.cjs'
import auth from '../../middleware/auth.js'

Check failure on line 5 in lib/api/postal-code/routes.js

View workflow job for this annotation

GitHub Actions / build (16.x)

'auth' is defined but never used.
import analyticsMiddleware from '../../middleware/analytics.js'

Check failure on line 6 in lib/api/postal-code/routes.js

View workflow job for this annotation

GitHub Actions / build (16.x)

'analyticsMiddleware' is defined but never used.
import {getAllPostalAreas, putPostalAreas, deletePostalAreas} from './models.js'

const apiQueue = queue('api')

Check failure on line 9 in lib/api/postal-code/routes.js

View workflow job for this annotation

GitHub Actions / build (16.x)

'apiQueue' is assigned a value but never used.

const BAN_API_URL

Check failure on line 11 in lib/api/postal-code/routes.js

View workflow job for this annotation

GitHub Actions / build (16.x)

'BAN_API_URL' is assigned a value but never used.
= process.env.BAN_API_URL || 'https://plateforme.adresse.data.gouv.fr/api'

const nanoid = customAlphabet('123456789ABCDEFGHJKMNPQRSTVWXYZ', 9)

Check failure on line 14 in lib/api/postal-code/routes.js

View workflow job for this annotation

GitHub Actions / build (16.x)

'nanoid' is assigned a value but never used.

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
2 changes: 2 additions & 0 deletions lib/api/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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

0 comments on commit 06d2369

Please sign in to comment.