Skip to content

Instantly share code, notes, and snippets.

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "apigateway.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
// Reminders table response
const reminders = {
Items: [
{
"activities": {
"activity1": {
"enabled": true,
"dateCompleted": 1608222145658
},
"activity2": {
await eventBridge.putEvents({
// Limit of ten entries
Entries: [{
EventBusName: `main`,
Source: 'cron.query',
DetailType: 'QUERIED REMINDERS',
Detail: JSON.stringify(reminders)
}]
}).promise().catch(console.error)
const reminders = {
Items: [
...
],
LastEvaluatedKey = {day: 'monday', userID: '111'}
}
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
async function getRemindersForDay (dayName, ExclusiveStartKey) {
const params = {
TableName: `${process.env.STAGE}-reminders`,
KeyConditionExpression: '#dayWeek = :hkey',
ExpressionAttributeNames: {
'#dayWeek': 'day'
},
ExpressionAttributeValues: {
':hkey': dayName
},
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)
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
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
func addOne(_ num: Int) -> Int {
return num + 1
}
addOne(2) // 3
addOne(3) // 4