-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_receipts.js
74 lines (65 loc) · 2.23 KB
/
read_receipts.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
/* eslint-disable no-console */
/* eslint-disable no-restricted-syntax */
const aws = require('aws-sdk');
const moment = require('moment');
const { Expo } = require('expo-server-sdk');
const ReceiptsTableName = process.env.dynamodb_receipts_table_name;
const dynamoDB = new aws.DynamoDB.DocumentClient();
const expo = new Expo();
async function getReceipts() {
const oneHourAgo = moment().subtract(1, 'hours').format('YYYY-MM-DDTHH:mm:ss');
const fiveHoursAgo = moment().subtract(5, 'hours').format('YYYY-MM-DDTHH:mm:ss');
console.log('oneHourAgo', oneHourAgo);
const params = {
TableName: ReceiptsTableName,
FilterExpression:
'created_at > :fiveHoursAgo and created_at < :oneHourAgo',
ExpressionAttributeValues: {
':oneHourAgo': oneHourAgo,
':fiveHoursAgo': fiveHoursAgo
}
};
const data = await dynamoDB.scan(params).promise();
return data.Items;
}
exports.handler = async () => {
console.log('getting receipts');
const dbReceipts = await getReceipts();
if (!dbReceipts || !dbReceipts.length) {
console.log('no receipts found under conditions');
return {
statusCode: 200,
headers: { 'Access-Control-Allow-Origin': '*' },
body: JSON.stringify({ message: 'No mature unread receipts' })
};
}
// eslint-disable-next-line
const receiptIds = dbReceipts.map(rcp => rcp.receipt_id);
console.log('receiptIds', receiptIds);
const receiptIdChunks = expo.chunkPushNotificationReceiptIds(receiptIds);
console.log('receiptIdChunks', receiptIdChunks);
for (const chunk of receiptIdChunks) {
try {
// eslint-disable-next-line no-await-in-loop
let receipts = await expo.getPushNotificationReceiptsAsync(chunk);
receipts = Object.values(receipts);
console.log('receipts', receipts);
for (const receipt of receipts) {
if (receipt.status === 'error') {
console.error(receipt.message);
if (receipt.details && receipt.details.error) {
console.error(receipt.details.error);
}
}
}
} catch (err) {
console.error(err);
}
}
return {
statusCode: 200,
body: JSON.stringify({
message: 'All mature receipts have been read, details in logs above this message'
})
};
};