-
Notifications
You must be signed in to change notification settings - Fork 21
/
exports.js
112 lines (98 loc) · 4.39 KB
/
exports.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
const express = require('express');
const router = express.Router();
const { HeadObjectCommand, GetObjectCommand } = require('@aws-sdk/client-s3');
const _ = require('lodash');
const { SendMessageCommand } = require('@aws-sdk/client-sqs');
const aws = require('../../lib/gost-aws');
const { requireUser, getAdminAuthInfo } = require('../../lib/access-helpers');
const arpa = require('../services/generate-arpa-report');
const { getReportingPeriodID, getReportingPeriod } = require('../db/reporting-periods');
const { useTenantId, useUser } = require('../use-request');
router.get('/:tenantId/:periodId/:filename', async (req, res) => {
let user;
try {
const info = await getAdminAuthInfo(req);
user = info.user;
if (user.tenant_id !== Number(req.params.tenantId)) {
throw new Error('Unauthorized');
}
} catch (error) {
res.redirect(encodeURI(`${process.env.WEBSITE_DOMAIN}/arpa_reporter/login?redirect_to=/api/exports/${req.params.tenantId}/${req.params.periodId}/${req.params.filename}&message=Please login to visit the link.`));
return;
}
const s3 = aws.getS3Client();
const Key = `${user.tenant_id}/${req.params.periodId}/${req.params.filename}`;
const baseParams = { Bucket: process.env.AUDIT_REPORT_BUCKET, Key };
try {
await s3.send(new HeadObjectCommand(baseParams));
} catch (error) {
console.log(error);
res.redirect(encodeURI(`${process.env.WEBSITE_DOMAIN}/arpa_reporter?alert_text=The treasury report you requested has expired. Please try again by clicking the 'Send Treasury Report By Email'.&alert_level=err`));
return;
}
let signedUrl;
try {
signedUrl = await aws.getSignedUrl(s3, new GetObjectCommand(baseParams), { expiresIn: 60 });
} catch (error) {
console.log(error);
res.redirect(`${process.env.WEBSITE_DOMAIN}/arpa_reporter?alert_text=Something went wrong. Please reach out to grants-helpdesk@usdigitalresponse.org.&alert_level=err`);
return;
}
res.redirect(signedUrl);
});
router.get('/', requireUser, async (req, res) => {
const periodId = req.query.period_id ?? await getReportingPeriodID(req.query.period_id);
const period = await getReportingPeriod(periodId);
if (!period) {
res.status(404).json({ error: 'invalid reporting period' });
return;
}
const tenantId = useTenantId();
if (req.query.queue) {
// Special handling for deferring treasury report generation and sending to a task queue
console.log('/api/exports?queue=true GET');
console.log('Generating Async treasury report via task queue');
try {
const user = useUser();
const sqs = aws.getSQSClient();
await sqs.send(new SendMessageCommand({
QueueUrl: process.env.ARPA_TREASURY_REPORT_SQS_QUEUE_URL,
MessageBody: JSON.stringify({ userId: user.id, periodId, tenantId }),
}));
res.json({ success: true });
return;
} catch (error) {
console.log(`Failed to generate and send treasury report ${error}`);
res.status(500).json({ error: 'Unable to generate treasury report and send email.' });
return;
}
}
if (req.query.async) {
// Special handling for async treasury report generation and sending.
console.log('/api/exports?async=true GET');
console.log('Generating Async treasury report');
try {
const user = useUser();
arpa.generateAndSendEmail(user.email, periodId, tenantId);
res.json({ success: true });
return;
} catch (error) {
console.log(`Failed to generate and send treasury report ${error}`);
res.status(500).json({ error: 'Unable to generate treasury report and send email.' });
return;
}
}
const report = await arpa.generateReport(periodId, tenantId);
if (_.isError(report)) {
res.status(500).send(report.message);
return;
}
res.header(
'Content-Disposition',
`attachment; filename="${report.filename}"`,
);
res.header('Content-Type', 'application/octet-stream');
res.send(Buffer.from(report.content, 'binary'));
});
module.exports = router;
// NOTE: This file was copied from src/server/routes/exports.js (git @ ada8bfdc98) in the arpa-reporter repo on 2022-09-23T20:05:47.735Z