Skip to content

Instantly share code, notes, and snippets.

@yossale
Created July 28, 2020 09:27
Show Gist options
  • Save yossale/92287590c4164e3b3e0765fe010469e1 to your computer and use it in GitHub Desktop.
Save yossale/92287590c4164e3b3e0765fe010469e1 to your computer and use it in GitHub Desktop.
Scalable Webhook - Sample code for the Processing function
const util = require('util')
const AWS = require('aws-sdk')
const docClient = new AWS.DynamoDB.DocumentClient();
const REQ_TABLE = process.env.TABLE_REQUESTSTABLE01
async function updateUserProductsByEmailId(emailId) {
console.info(`Updating user products for email: ${emailId}`)
//Update all the user-specific products as seen
return
}
async function logMessageToTable(msg) {
console.log(`Logging email event to DynamoDB`)
const params = {
TableName: REQ_TABLE,
Item: {
'id': msg.emailId,
'status': msg.eventType,
'updated_at': `${new Date().toISOString()}`
}
};
try {
const data = await docClient.put(params).promise();
console.log("Added item");
} catch (err) {
console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
}
}
module.exports.handler = async (event, context) => {
console.log(`Received SQS event: ${util.inspect(event)}`)
const msg = JSON.parse(event.Records[0].body)
await updateUserProductsByEmailId(msg.emailId)
await logMessageToTable(msg)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment