-
Notifications
You must be signed in to change notification settings - Fork 21
/
uploads.js
186 lines (151 loc) · 5.26 KB
/
uploads.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
// uploads.js handles uploading an agency report spreadsheet to the database.
/* eslint camelcase: 0 */
const express = require('express');
const router = express.Router();
const multer = require('multer');
const { requireUser, requireAdminUser } = require('../../lib/access-helpers');
const multerUpload = multer({ storage: multer.memoryStorage() });
const { getReportingPeriodID } = require('../db/reporting-periods');
const {
usedForTreasuryExport, getUpload, uploadsInSeries, uploadsInPeriod,
} = require('../db/uploads');
const { recordsForUpload } = require('../services/records');
const { persistUpload, bufferForUpload } = require('../services/persist-upload');
const { invalidateUpload, validateUpload } = require('../services/validate-upload');
const { ensureAsyncContext } = require('../lib/ensure-async-context');
const ValidationError = require('../lib/validation-error');
router.get('/', requireUser, async (req, res) => {
const periodId = await getReportingPeriodID(req.query.period_id);
const uploads = await uploadsInPeriod(periodId);
return res.json({ uploads });
});
router.post(
'/',
requireUser,
// use preserveAsyncContext to work around issue:
// https://github.com/expressjs/multer/issues/814
ensureAsyncContext(multerUpload.single('spreadsheet')),
async (req, res) => {
console.log('POST /api/uploads');
if (req.file) {
console.log('Filename:', req.file.originalname, 'size:', req.file.size);
}
try {
const upload = await persistUpload({
user: req.session.user,
filename: req.file.originalname,
buffer: req.file.buffer,
body: req.body,
});
res.status(200).json({ upload, error: null });
} catch (e) {
res.status(e instanceof ValidationError ? 400 : 500).json({ error: e.message, upload: null });
}
},
);
router.get('/:id', requireUser, async (req, res) => {
const { id } = req.params;
const upload = await getUpload(id);
if (!upload || upload.tenant_id !== req.session.user.tenant_id) {
res.sendStatus(404);
res.end();
return;
}
res.json({ upload });
});
router.get('/:id/series', requireUser, async (req, res) => {
const { id } = req.params;
const upload = await getUpload(id);
if (!upload || upload.tenant_id !== req.session.user.tenant_id) {
res.sendStatus(404);
res.end();
return;
}
let series;
if (upload.agency_id != null && upload.ec_code) {
series = await uploadsInSeries(upload);
} else {
series = [upload];
}
const allExported = await usedForTreasuryExport(upload.reporting_period_id);
const seriesExported = allExported.find((upl) => (upl.agency_id === upload.agency_id && upl.ec_code === upload.ec_code));
res.json({
upload,
series,
seriesExported,
});
});
router.get('/:id/records', requireUser, async (req, res) => {
const { id } = req.params;
const upload = await getUpload(id);
if (!upload || upload.tenant_id !== req.session.user.tenant_id) {
res.sendStatus(404);
res.end();
return;
}
const records = await recordsForUpload(upload);
res.json({
upload,
records,
});
});
router.get('/:id/download', requireUser, async (req, res) => {
const { id } = req.params;
const upload = await getUpload(id);
if (!upload || upload.tenant_id !== req.session.user.tenant_id) {
res.sendStatus(404);
res.end();
return;
}
const buffer = await bufferForUpload(upload);
res.header(
'Content-Disposition',
`attachment; filename="${upload.filename}"`,
);
res.header('Content-Type', 'application/octet-stream');
res.end(buffer);
});
router.post('/:id/validate', requireUser, async (req, res) => {
const { id } = req.params;
const { user } = req.session;
const upload = await getUpload(id);
if (!upload || upload.tenant_id !== user.tenant_id) {
res.sendStatus(404);
res.end();
return;
}
try {
const errors = await validateUpload(upload, user);
res.json({
errors: errors.map((e) => e.toObject()),
upload,
});
} catch (e) {
let msg = e.message;
if (e.code === 'ENOENT') msg = 'Cannot find upload data; please re-submit this upload';
res.status(500).json({ error: msg });
}
});
router.post('/:id/invalidate', requireAdminUser, async (req, res) => {
const { id } = req.params;
const { user } = req.session;
const upload = await getUpload(id);
if (!upload || upload.tenant_id !== user.tenant_id) {
res.sendStatus(404);
res.end();
return;
}
try {
const errors = await invalidateUpload(upload, user);
res.json({
errors: errors.map((e) => e.toObject()),
upload,
});
} catch (e) {
let msg = e.message;
if (e.code === 'ENOENT') msg = 'Cannot invalidate upload';
res.status(500).json({ error: msg });
}
});
module.exports = router;
// NOTE: This file was copied from src/server/routes/uploads.js (git @ ada8bfdc98) in the arpa-reporter repo on 2022-09-23T20:05:47.735Z