-
Notifications
You must be signed in to change notification settings - Fork 21
/
agencies.js
60 lines (51 loc) · 1.75 KB
/
agencies.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const express = require('express');
const { requireAdminUser, requireUser } = require('../../lib/access-helpers');
const router = express.Router();
const {
agencies: getAgencies,
createAgency,
updateAgency,
agencyById,
} = require('../../db/arpa_reporter_db_shims/agencies');
router.get('/', requireUser, (req, res) => {
getAgencies().then((agencies) => res.json({ agencies }));
});
async function validateAgency(agency) {
if (!agency.name) {
throw new Error('Agency requires a name');
}
if (!agency.code) {
throw new Error('Agency requires a code');
}
}
router.post('/', requireAdminUser, async (req, res) => {
const agencyInfo = req.body.agency;
try {
await validateAgency(agencyInfo);
} catch (e) {
res.status(400).json({ error: e.message });
return;
}
try {
if (agencyInfo.id) {
const existingAgency = await agencyById(agencyInfo.id);
if (!existingAgency || existingAgency.tenant_id !== req.session.user.tenant_id) {
res.status(404).json({ error: 'invalid agency' });
return;
}
const agency = await updateAgency(agencyInfo);
res.json({ agency });
} else {
const agency = await createAgency(agencyInfo);
res.json({ agency });
}
} catch (e) {
if (e.message.match(/violates unique constraint/)) {
res.status(400).json({ error: 'Agency with that code already exists' });
} else {
res.status(500).json({ error: e.message });
}
}
});
module.exports = router;
// NOTE: This file was copied from src/server/routes/agencies.js (git @ ada8bfdc98) in the arpa-reporter repo on 2022-09-23T20:05:47.735Z