-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.js
76 lines (68 loc) · 2 KB
/
storage.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
'use strict';
const AWS = require('aws-sdk');
const client = new AWS.DynamoDB.DocumentClient();
const moment = require('moment');
/**
* Creates a new reminder and saves it to the database.
*
* @param chatId User's chatId
* @param time Time at which the reminder should be sent
* @param message Reminder's message
*/
module.exports.createReminder = async (chatId, time, message) => {
const params = {
TableName: process.env.DYNAMODB_TABLE,
Item: {
chat_id: chatId,
reminder_text: message,
reminder_time: time.toISOString()
}
};
await client.put(params).promise();
};
/**
* Retrieves all reminders ofr given chatId from the database.
*
* @param chatId User's chatId
* @returns List of reminders with `reminder_text` and `reminder_time`
*/
module.exports.getReminders = async (chatId) => {
const params = {
TableName: process.env.DYNAMODB_TABLE,
KeyConditionExpression: 'chat_id = :cid',
ExpressionAttributeValues: {
':cid': chatId
},
ProjectionExpression: 'reminder_text, reminder_time'
};
const reminders = await client.query(params).promise();
return reminders.Items;
};
/**
* Retrieves all reminders that are ready to be sent (reminder_time < current time)
* @returns List of reminders with `chat_id`, `reminder_text` and `reminder_time`
*/
module.exports.getDueReminders = async () => {
const params = {
TableName: process.env.DYNAMODB_TABLE,
FilterExpression: 'reminder_time <= :now',
ExpressionAttributeValues: {
':now': moment().toISOString()
},
ProjectionExpression: 'chat_id, reminder_text, reminder_time'
};
const data = await client.scan(params).promise();
return data.Items;
};
/**
* Deletes the reminder for given chatId and time
* @param chatId reminder chatId
* @param time reminder time
*/
module.exports.deleteReminder = async (chatId, time) => {
const params = {
TableName: process.env.DYNAMODB_TABLE,
Key: { chat_id : chatId, reminder_time: time }
};
await client.delete(params).promise();
};