Skip to content

Commit

Permalink
update handler to store event
Browse files Browse the repository at this point in the history
  • Loading branch information
HDGoldi committed Mar 28, 2020
1 parent 9275105 commit 9e45331
Showing 1 changed file with 55 additions and 8 deletions.
63 changes: 55 additions & 8 deletions serverless-backend/particle-backend/handler.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,61 @@
'use strict';

const uuid = require('uuid');
const AWS = require('aws-sdk');

AWS.config.setPromisesDependency(require('bluebird'));

const dynamoDb = new AWS.DynamoDB.DocumentClient();

module.exports.webook = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
}),
};
const requestBody = JSON.parse(event.body);
const name = requestBody.name;
const data = requestBody.data;

if (typeof name !== 'string' || typeof data !== 'string') {
console.error('Validation Failed');
callback(new Error('Couldn\'t submit candidate because of validation errors.'));
return;
}

callback(null, response);
submitEvent(events(name, data))
.then(res => {
callback(null, {
statusCode: 200,
body: JSON.stringify({
message: `Sucessfully stored event`,
candidateId: res.id
})
});
})
.catch(err => {
console.log(err);
callback(null, {
statusCode: 500,
body: JSON.stringify({
message: `Unable to store event`
})
})
});

};

const submitEvent = event => {
console.log('Submitting event');
const events = {
TableName: process.env.EVENT_TABLE,
Item: event,
};
return dynamoDb.put(events).promise()
.then(res => event);
};

const events = (name, data) => {
const timestamp = new Date().getTime();
return {
id: uuid.v1(),
name: name,
data: data,
receivedAt: timestamp,
};
};

0 comments on commit 9e45331

Please sign in to comment.