-
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.
Added export-to-exploitation-db route
- Loading branch information
1 parent
db7854a
commit c2c7efc
Showing
2 changed files
with
49 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,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 | ||
|
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