This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Principal": { | |
"Service": "apigateway.amazonaws.com" | |
}, | |
"Action": "sts:AssumeRole" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reminders table response | |
const reminders = { | |
Items: [ | |
{ | |
"activities": { | |
"activity1": { | |
"enabled": true, | |
"dateCompleted": 1608222145658 | |
}, | |
"activity2": { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
await eventBridge.putEvents({ | |
// Limit of ten entries | |
Entries: [{ | |
EventBusName: `main`, | |
Source: 'cron.query', | |
DetailType: 'QUERIED REMINDERS', | |
Detail: JSON.stringify(reminders) | |
}] | |
}).promise().catch(console.error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reminders = { | |
Items: [ | |
... | |
], | |
LastEvaluatedKey = {day: 'monday', userID: '111'} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function * retryQuery () { | |
// calculate current day and pass it to query | |
const daysOfWeek = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'] | |
const currentDay = new Date().getDay() | |
const dayName = daysOfWeek[currentDay] | |
let result = await getRemindersForDay(dayName) | |
// if LastEvaluatedKey is found, query again, and again and again | |
// it means the response is paginated | |
while (result.LastEvaluatedKey) { | |
yield result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function getRemindersForDay (dayName, ExclusiveStartKey) { | |
const params = { | |
TableName: `${process.env.STAGE}-reminders`, | |
KeyConditionExpression: '#dayWeek = :hkey', | |
ExpressionAttributeNames: { | |
'#dayWeek': 'day' | |
}, | |
ExpressionAttributeValues: { | |
':hkey': dayName | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for await (const reminders of retryQuery()) { | |
await eventBridge.putEvents({ | |
// Limit of ten entries | |
Entries: [{ | |
EventBusName: `main`, | |
Source: 'cron.query', | |
DetailType: 'QUERIED REMINDERS', | |
Detail: JSON.stringify(reminders) | |
}] | |
}).promise().catch(console.error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports.lambda = async (event, context) => { | |
// {Items: [{activities, day}, {activities, day}...]} | |
const reminders = event.detail | |
/* | |
* Use your logic to filter, discard the activities, | |
* we will end up with something like: | |
* [{activities, day}, {activities, day}, {activities, day}] | |
*/ | |
const filteredReminders = filterActivitiesIn(reminders) | |
// Send the events to next step |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const admin = require('firebase-admin') | |
const serviceAccount = JSON.parse(process.env.FIREBASE_CRED_KEY) | |
// intialize app just once. Firebase will complain if we do it for each lambda execution | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount), | |
databaseURL: 'https://yourproject.firebaseio.com' | |
}) | |
module.exports.lambda = async (event, context) => { | |
/** | |
* compose push notification message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func addOne(_ num: Int) -> Int { | |
return num + 1 | |
} | |
addOne(2) // 3 | |
addOne(3) // 4 |