-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendReports.js
68 lines (54 loc) · 1.47 KB
/
sendReports.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
const awsXRay = require('aws-xray-sdk');
const AWS = awsXRay.captureAWS(require('aws-sdk'));
const { v4: uuid } = require('uuid');
const client = new AWS.DynamoDB.DocumentClient({});
const eventBridge = new AWS.EventBridge({});
exports.exec = async (event) => {
const { email, year, monthFrom, monthTo } = event.detail;
const record = await client.query({
TableName: process.env.DATA_TABLE,
KeyConditionExpression: "id = :year AND yearMonth BETWEEN :monthFrom AND :monthTo",
ExpressionAttributeValues: {
":year": year,
":monthFrom": Number(`${year}${monthFrom}`),
":monthTo": Number(`${year}${monthTo}`),
},
}).promise();
const entry = {
Source: "sendReports",
DetailType: "email.send",
Detail: JSON.stringify({
id: uuid(),
email,
subject: `Report from ${year}${monthFrom} to ${year}${monthTo}`,
body: createTable(record.Items),
title: `Report from ${year}${monthFrom} to ${year}${monthTo}`,
}),
};
console.warn('Creating new event', entry);
await eventBridge.putEvents({
Entries: [
entry,
],
}).promise();
}
const createTable = (items) => {
const header = `
<tr>
<th>yearMonth</th>
<th>amount</th>
</tr>
`
const rows = items.map(({ yearMonth, amount }) => createRow(yearMonth, amount));
return `
<table>
${header}
${rows}
</table>
`;
}
const createRow = (yearMonth, amount) => `
<tr>
<td>${yearMonth}</td>
<td>${amount}</td>
</tr>`;