-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessRequest.js
82 lines (69 loc) · 3.06 KB
/
processRequest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict';
console.log('Loading function');
// DynamoDB stuff
var doc = require('dynamodb-doc');
var dynamodb = new doc.DynamoDB();
var tableName = "RequestTable";
// SQS stuff
var AWS = require('aws-sdk');
var QUEUE_URL = 'https://sqs.us-west-2.amazonaws.com/468979822992/MakerspaceRequests.fifo';
var sqs = new AWS.SQS({region : 'us-west-2'});
// AWS needs to know which region to look in (current setup is us-west-2)
AWS.config.update({
region: "us-west-2",
});
var docClient = new AWS.DynamoDB.DocumentClient();
exports.handler = (event, context, callback) => {
console.log('Received event:', JSON.stringify(event, null, 2));
var datetime = new Date().getTime().toString();
event.Records.forEach((record) => {
console.log(record.eventID);
console.log(record.eventName);
// record.eventName indicates what action DynamoDB is performing
// INSERT means new entry is being inserted into table
if(record.eventName == "INSERT") {
var id = record.dynamodb.Keys.RequestID.S;
var tableNum = record.dynamodb.NewImage.TableNum.S;
// Parse data from DynamoDB table into SQS message
var params = {
MessageBody: JSON.stringify(tableNum),
MessageGroupId: JSON.stringify('group1'),
MessageDeduplicationId: JSON.stringify(id+datetime),
QueueUrl: QUEUE_URL
};
// Insert message into SQS
sqs.sendMessage(params, function(err,data){
if(err) {
console.log('error:',"Fail Send Message" + err);
context.done('error', "ERROR Put SQS"); // ERROR with message
}else{
console.log('data:',data);
context.done(null,''); // SUCCESS
}
});
// SPECIAL CASE:
// A hacky way of pushing the "send" button on the mobile
// application was to send a blank DynamoDB table entry with "push" as
// the "TableNum" (Check Python script to see how this is processed).
// After sending this message, immediately delete this DynamoDB entry
// as this data does not need to be stored for later viewing
if(tableNum == "push") {
var deleteParams = {
TableName: tableName,
Key:{
"RequestID": id
}
}
console.log("Attempting to delete push item...");
docClient.delete(deleteParams, function(err, data) {
if (err) {
console.error("Unable to delete item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("DeleteItem succeeded:", JSON.stringify(data, null, 2));
}
});
}
}
});
callback(null, `Successfully processed ${event.Records.length} records.`);
};