Skip to content

Commit

Permalink
Added export-to-exploitation-db route
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineludeau committed May 3, 2024
1 parent db7854a commit c2c7efc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lib/api/export-to-exploitation-db/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'dotenv/config.js' // eslint-disable-line import/no-unassigned-import
import express from 'express'
import queue from '../../util/queue.cjs'
import auth from '../../middleware/auth.js'
import {getDistricts} from '../district/models.js'

const exportToExploitationDBQueue = queue('export-to-exploitation-db')

const app = new express.Router()

app.route('/districts')
.post(auth, async (req, res) => {
let response
try {
const {districtIDs} = req.body

if (!districtIDs || districtIDs.length === 0) {
res.status(400).send('districtIDs should not be empty')
}

const districts = await getDistricts(districtIDs)
if (districts.length !== districtIDs.length) {
res.status(404).send('Some districts not found')
}

await Promise.all(districtIDs.map(async districtID => exportToExploitationDBQueue.add({districtID}, {removeOnComplete: true, removeOnFail: true})))

response = {
date: new Date(),
status: 'success',
response: {},
}
} catch (error) {
const {message} = error
response = {
date: new Date(),
status: 'error',
message,
response: {},
}
}

res.send(response)
})

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 exportToExploitationDBRoutes from './export-to-exploitation-db/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('/export-to-exploitation-db', exportToExploitationDBRoutes)

export default app

0 comments on commit c2c7efc

Please sign in to comment.