This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
/
index.js
82 lines (81 loc) · 2.83 KB
/
index.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
let AWS = require('aws-sdk');
let dynamo = new AWS.DynamoDB.DocumentClient();
let table = process.env.TABLE_NAME;
exports.handler = (event, context, callback) => {
let now = Date.now()/1000;
let expiry = new Date('2018.12.31').getTime()/1000;
let getParams = {
TableName: table,
Key:{
"userId": event.arguments.userId
}
};
let putParams = {
TableName:table,
Item:{
"userId": event.arguments.userId,
"username": event.arguments.username,
"points": 1000
}
};
if (expiry < now){
putParams.Item.points = 0;
}
switch(event.field){
case "getUser":
dynamo.get(getParams, function(err,data){
if (err){
console.error("Error JSON: ", JSON.stringify(err,null,2));
callback(err);
} else if (data.Item == undefined) {
let result = {
"userId": "",
"username": "",
"points": 1000
};
callback(null,result);
} else {
console.log("User Exists: ", JSON.stringify(data,null,2));
let result = {
"userId": data.Item.userId,
"username": data.Item.username,
"points": data.Item.points
};
callback(null,result);
}
});
break;
case "registerUser":
dynamo.put(putParams, function(err,data){
if (err){
console.error("Error JSON: ", JSON.stringify(err,null,2));
callback(err);
} else {
console.log("User Added: ", JSON.stringify(data,null,2));
let result = putParams.Item;
callback(null,result);
}
});
break;
case "updateBalance":
putParams.Item.points = event.arguments.points;
dynamo.put(putParams, function(err,data){
if (err){
console.error("Error JSON: ", JSON.stringify(err,null,2));
callback(err);
} else {
console.log("Balance Updated: ", JSON.stringify(data,null,2));
let result = {
"userId": event.arguments.userId,
"username": event.arguments.username,
"points": event.arguments.points
}
callback(null,result);
}
});
break;
default:
callback("Unknown field, unable to resolve" + event.field, null);
break;
}
};