-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
8 deletions.
There are no files selected for viewing
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
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, | ||
}; | ||
}; |