-
Notifications
You must be signed in to change notification settings - Fork 21
/
reporting-periods.js
188 lines (155 loc) · 5.46 KB
/
reporting-periods.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
184
185
186
187
188
/*
--------------------------------------------------------------------------------
- routes/reporting-periods.js
--------------------------------------------------------------------------------
*/
/* eslint camelcase: 0 */
const express = require('express');
const router = express.Router();
const multer = require('multer');
const multerUpload = multer({ storage: multer.memoryStorage() });
const knex = require('../../db/connection');
const {
closeReportingPeriod,
createReportingPeriod,
getAllReportingPeriods,
getReportingPeriod,
updateReportingPeriod,
} = require('../db/reporting-periods');
const { requireUser, requireAdminUser } = require('../../lib/access-helpers');
const {
savePeriodTemplate,
templateForPeriod,
} = require('../services/get-template');
const { usedForTreasuryExport } = require('../db/uploads');
const { ensureAsyncContext } = require('../lib/ensure-async-context');
const { revalidateUploads } = require('../services/revalidate-uploads');
router.get('/', requireUser, async (req, res) => {
const periods = await getAllReportingPeriods();
return res.json({ reportingPeriods: periods });
});
router.post('/close', requireAdminUser, async (req, res) => {
const { user } = req.session;
console.log(`Received request to close the current reporting period for User ${user.id}'s tenant.`);
const period = await getReportingPeriod();
const trns = await knex.transaction();
try {
await closeReportingPeriod(period, trns);
trns.commit();
} catch (err) {
if (!trns.isCompleted()) trns.rollback();
res.status(500).json({ error: err.message });
return;
}
res.json({
status: 'OK',
});
});
router.post('/', requireAdminUser, async (req, res) => {
const updatedPeriod = req.body.reportingPeriod;
try {
if (updatedPeriod.id) {
const existing = await getReportingPeriod(updatedPeriod.id);
if (!existing) {
res.status(404).json({ error: 'invalid reporting period id' });
return;
}
const period = await updateReportingPeriod(updatedPeriod);
res.json({ reportingPeriod: period });
} else {
const period = await createReportingPeriod(updatedPeriod);
res.json({ reportingPeriod: period });
}
} catch (e) {
if (e.message.match(/violates unique constraint/)) {
res.status(400).json({ error: 'Period conflicts with an existing one' });
} else {
res.status(500).json({ error: e.message });
}
}
});
router.post(
'/:id/template',
requireAdminUser,
ensureAsyncContext(multerUpload.single('template')),
async (req, res) => {
if (!req.file) {
res.status(400).json({ error: 'File missing' });
return;
}
const periodId = req.params.id;
const reportingPeriod = await getReportingPeriod(periodId);
if (!reportingPeriod) {
res.status(404).json({ error: 'Reporting period not found' });
return;
}
const { originalname, size, buffer } = req.file;
console.log(
`Uploading filename ${originalname} size ${size} for period ${periodId}`,
);
try {
await savePeriodTemplate(periodId, originalname, buffer);
} catch (e) {
res.status(500).json({
success: false,
errorMessage: e.message,
});
return;
}
res.json({ success: true });
},
);
router.get('/:id/template', requireUser, async (req, res) => {
const periodId = req.params.id;
try {
const { filename, data } = await templateForPeriod(periodId);
res.header('Content-Disposition', `attachment; filename="${filename}"`);
res.header('Content-Type', 'application/octet-stream');
res.end(data);
} catch (err) {
if (err.code === 'ENOENT') {
res.status(404).json({ error: 'Could not find template file' });
} else {
res.status(500).json({ error: err.message });
}
}
});
router.get('/:id/exported_uploads', requireUser, async (req, res) => {
const periodId = req.params.id;
try {
const exportedUploads = await usedForTreasuryExport(periodId);
res.json({ exportedUploads });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.post('/:id/revalidate', requireAdminUser, async (req, res) => {
const periodId = req.params.id;
const commit = req.query.commit || false;
const { user } = req.session;
const reportingPeriod = await getReportingPeriod(periodId);
if (!reportingPeriod) {
res.sendStatus(404);
res.end();
return;
}
const trns = await knex.transaction();
try {
const updates = await revalidateUploads(reportingPeriod, user, trns);
if (commit) {
trns.commit();
} else {
trns.rollback();
}
res.json({
updates,
});
} catch (e) {
if (!trns.isCompleted()) trns.rollback();
res.status(500).json({ error: e.message });
throw e;
}
});
module.exports = router;
/* * * * */
// NOTE: This file was copied from src/server/routes/reporting-periods.js (git @ ada8bfdc98) in the arpa-reporter repo on 2022-09-23T20:05:47.735Z