-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathagencies.js
executable file
·183 lines (163 loc) · 5.61 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const express = require('express');
const router = express.Router({ mergeParams: true });
const multer = require('multer');
const multerUpload = multer({ storage: multer.memoryStorage() });
const XLSX = require('xlsx');
const { ensureAsyncContext } = require('../arpa_reporter/lib/ensure-async-context');
const {
requireAdminUser,
requireUser,
isUserAuthorized,
requireUSDRSuperAdminUser,
} = require('../lib/access-helpers');
const {
getAgency,
getTenantAgencies,
setAgencyThresholds,
createAgency,
setAgencyName,
setAgencyAbbr,
setAgencyParent,
setAgencyCode,
deleteAgency,
getUsersByAgency,
getAgencyTree,
} = require('../db');
const AgencyImporter = require('../lib/agencyImporter');
const email = require('../lib/email');
router.get('/', requireUser, async (req, res) => {
const { user } = req.session;
const response = await getTenantAgencies(user.tenant_id);
res.json(response);
});
router.get('/sendDigestEmail', requireUSDRSuperAdminUser, async (req, res) => {
const { user } = req.session;
const agency = await getAgency(parseInt(req.params.organizationId, 10));
try {
await email.sendGrantDigest({
name: agency[0].name,
matchedGrants: agency[0].matched_grants,
recipients: agency[0].recipients,
});
} catch (e) {
console.error(`Unable to kick-off digest email for ${req.params.organizationId} by user ${user.id} due to error ${e}}`);
res.sendStatus(500).json({ message: 'Something went wrong while kicking off the digest email. Please investigate the server logs.' });
}
res.sendStatus(200);
});
router.put('/:agency', requireAdminUser, async (req, res) => {
// Currently, agencies are seeded into db; only thresholds are mutable.
const { agency } = req.params;
const { user } = req.session;
const allowed = await isUserAuthorized(user, agency);
if (!allowed) {
res.sendStatus(403);
return;
}
const { warningThreshold, dangerThreshold } = req.body;
const result = await setAgencyThresholds(agency, warningThreshold, dangerThreshold);
res.json(result);
});
router.delete('/del/:agency', requireAdminUser, async (req, res) => {
const { agency } = req.params;
const { user } = req.session;
if (!isUserAuthorized(user, agency)) {
return res.sendStatus(403);
}
if ((await getUsersByAgency(agency)).length > 0) {
return res.status(400).send('agency has assigned users');
}
if ((await getAgencyTree(agency)).length > 1) {
return res.status(400).send('agency has sub-agencies');
}
const {
parent, name, abbreviation, warningThreshold, dangerThreshold,
} = req.body;
const result = await deleteAgency(agency, parent, name, abbreviation, warningThreshold, dangerThreshold);
return res.json(result);
});
router.put('/name/:agency', requireAdminUser, async (req, res) => {
const { agency } = req.params;
const { user } = req.session;
const allowed = await isUserAuthorized(user, agency);
if (!allowed) {
res.sendStatus(403);
return;
}
const { name } = req.body;
const result = await setAgencyName(agency, name);
res.json(result);
});
router.put('/abbr/:agency', requireAdminUser, async (req, res) => {
const { user } = req.session;
const { agency } = req.params;
const { abbreviation } = req.body;
const allowed = await isUserAuthorized(user, agency);
if (!allowed) {
res.sendStatus(403);
return;
}
const result = await setAgencyAbbr(agency, abbreviation);
res.json(result);
});
router.put('/code/:agency', requireAdminUser, async (req, res) => {
const { user } = req.session;
const { agency } = req.params;
const { code } = req.body;
const allowed = await isUserAuthorized(user, agency);
if (!allowed) {
res.sendStatus(403);
return;
}
const result = await setAgencyCode(agency, code);
res.json(result);
});
router.put('/parent/:agency', requireAdminUser, async (req, res) => {
const { user } = req.session;
const { agency } = req.params;
const allowed = await isUserAuthorized(user, agency);
if (!allowed) {
res.sendStatus(403);
return;
}
const result = await setAgencyParent(agency, Number(req.body.parentId));
res.json(result);
});
router.post('/', requireAdminUser, async (req, res) => {
const { user } = req.session;
const allowed = await isUserAuthorized(user, req.body.parentId);
if (!allowed) {
res.sendStatus(403);
return;
}
const agency = {
name: req.body.name,
abbreviation: req.body.abbreviation,
code: req.body.code,
parent: Number(req.body.parentId),
warning_threshold: Number(req.body.warningThreshold),
danger_threshold: Number(req.body.dangerThreshold),
tenant_id: user.tenant_id,
};
const parentAgency = await getAgency(agency.parent);
if (!parentAgency) {
throw new Error(`Agency ${agency.parent} not found`);
}
const result = await createAgency(agency, user.id);
res.json(result);
});
router.post(
'/import',
requireAdminUser,
ensureAsyncContext(multerUpload.single('spreadsheet')),
async (req, res) => {
const workbook = XLSX.read(req.file.buffer, { type: 'buffer' });
const rowsList = XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]]);
const ret = await (new AgencyImporter()).import(
req.session.user,
rowsList,
);
res.status(200).json({ ret, error: null });
},
);
module.exports = router;