-
Notifications
You must be signed in to change notification settings - Fork 0
/
days-until-birthday.js
145 lines (124 loc) · 3.07 KB
/
days-until-birthday.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const aws = require("aws-sdk");
const client = new aws.DynamoDB.DocumentClient();
const tableName = "CakeWalkUsers";
const getUser = async (userId) => {
try {
const dbResponse = await client
.get({
TableName: tableName,
Key: {
userId
}
})
.promise();
const item = dbResponse.Item;
if (!item || typeof item !== "object") {
throw new Error("MALFORMED");
}
return {
status: "success",
payload: item
}
} catch (err) {
return {
status: "error",
payload: err
};
}
};
const daysUntilBirthDay = (birthDate) => {
const birthDayThisYear = new Date(
birthDate.split("-").map((chunk, index) => (index === 0 ? "2020" : chunk))
);
const diff =
(birthDayThisYear.getTime() - new Date().getTime()) / 1000 / 60 / 60 / 24;
return Math.floor(diff);
};
exports.handler = async (event, context, callback) => {
console.log(JSON.stringify(event, null, 2));
const body = JSON.parse(event.body);
const response = {
resolvedVariables: [],
unresolvedVariables: [],
context: event.context || {}
};
if (!body.variables) {
return errorResponse({
message: 'bad request',
responseCode: 400,
callback: callback
});
}
if (!body.state) {
return errorResponse({
message: 'bad request',
responseCode: 400,
callback: callback
});
}
for (const variable of body.variables) {
let resolutionResponse = null;
/**
* Iterate over the input variables and try to resolve each of them.
*
* Note: This is the only other section that you'd need to update.
*/
switch (variable.variableId) {
case 'CWBirthdayCountdown':
resolutionResponse = await getUser(body.context.userId);
break;
default:
break;
}
// Variables can have single and multi value responses
if (resolutionResponse && resolutionResponse.status === "success") {
// Single value variable response
response.resolvedVariables.push({
variableId: variable.variableId,
value: daysUntilBirthDay(resolutionResponse.payload.birthday)
});
} else {
// If anything goes wrong, append the variableId to the
// unresolvedVariables array and attach an optional failure reason
response.unresolvedVariables.push({
variableId: variable.variableId,
reason: resolutionResponse ? resolutionResponse.payload : 'unhandled failure'
});
}
}
return successResponse({
body: response,
callback: callback
});
};
/**
* NO NEED TO TOUCH ANYTHING BELOW
*/
const successResponse = ({
body,
callback
}) => {
callback(null, {
statusCode: 200,
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(body)
});
};
const errorResponse = ({
message,
responseCode,
callback
}) => {
callback(null, {
statusCode: responseCode,
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
errorMessage: message,
responseCode: responseCode
})
});
};