-
Notifications
You must be signed in to change notification settings - Fork 5
/
Customer.js
377 lines (337 loc) · 13.3 KB
/
Customer.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
'use strict';
console.log('Loading Customer.js...');
const doc = require('dynamodb-doc');
const dynamo = new doc.DynamoDB();
var aws = require('aws-sdk');
var sns = new aws.SNS();
var topic = "arn:aws:sns:us-east-1:498679776130:slackLambda";
exports.handler = function(event, context, callback) {
console.log('handler event: ' + JSON.stringify(event));
console.log('handler context: ' + JSON.stringify(context));
// use sns to trigger this lambda function instead of API Gateway
if (event.Records !== undefined)
event = JSON.parse(event.Records[0].Sns.Message);
var operation = event.operation;
switch (operation) {
case ('read'):
getCustomer(event, callback);
break;
case ('create'):
createCustomer(event, callback);
break;
case ('update'):
updateCustomer(event, callback);
break;
case ('delete'):
deleteCustomer(event, callback);
break;
case ('getaddress'):
getCustomerAddress(event, callback);
break;
default:
var err = new Error('405 Unrecognized operation');
err.name = "Unrecognized operation '${event.operation}'";
putResult(event.res_key, err, callback);
}
};
function putResult(key, res, cb) {
var params = {
TableName: "Result",
Item: { "key": key, "result": JSON.stringify(res) }
};
console.log('putResult data: ' + JSON.stringify(params));
dynamo.putItem(params, function(err, data) {
if (err) {
console.log('putResult err: ' + JSON.stringify(err));
}
});
cb(null, 'putResult ends');
}
function validateCustomer(item) {
var err = null;
if("email" in item) {
err = validateEmail(item.email);
if (err) return err;
}
if("firstname" in item) {
err = validateFirstName(item.firstname);
if (err) return err;
}
if("lastname" in item) {
err = validateLastName(item.lastname);
if (err) return err;
}
if("phone_num" in item) {
err = validatePhone(item.phone_num);
if (err) return err;
}
return err;
}
function validateEmail(email) {
// regular expression borrowed from a post in stackoverflow
// valid email is [email protected]
var err = null;
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(email === undefined || !re.test(email)) {
err = new Error("400 Invalid parameter");
err.name = "Email is invalid";
}
return err;
}
function validateFirstName(name) {
var err = null;
var re = /^[A-Za-z]+$/;
if (name === undefined || !re.test(name)) {
err = new Error('400 Invalid parameter');
err.name = 'First name is invalid!';
}
return err;
}
function validateLastName(name) {
var err = null;
var re = /^[A-Za-z]+$/;
if (name === undefined|| !re.test(name)) {
err = new Error('400 Invalid parameter');
err.name = 'Last name is invalid!';
}
return err;
}
function validatePhone(phone) {
var err = null;
var re = /^\d+$/;
if (phone === undefined || !re.test(phone) || phone.length != 10) {
err = new Error('400 Invalid parameter');
err.name = 'Phone number is invalid!';
}
return err;
}
function hasAllAttributes(item) {
// check whether the new customer has all necessary attributes.
var obj = {'firstname': false, 'lastname': false, 'email': false, 'phone_num' : false, 'address' : false};
for (var key in item) {
if (key in obj) obj.key = true;
}
return obj.city && obj.number && obj.street && obj.zip && obj.address;
}
// Use this function with care! It modifies the content of data.
function refineCustomerData(data, req) {
data.forEach(function(item) {
if (item.address) {
var addr = item.address;
item.address = {
"href": req.proto + "://" + req.host + "/" + req.stage + "/address/" + addr
};
}
if (item.email) {
item['self'] = {
"href": req.proto + "://" + req.host + "/" + req.stage + "/customer/" + item.email
};
}
});
}
function getCustomer(event, callback) {
var params = {
TableName: event.tableName,
Key: {'email': event.email}
};
console.log('In getCustomer, params is: ' + JSON.stringify(params));
var err = validateEmail(params.Key.email);
if(err) {
console.log('validateEmail() returns err: ' + JSON.stringify(err));
putResult(event.res_key, err, callback);
} else {
dynamo.getItem(params, function (err, data) {
if (err) {
console.log('getCustomer err: ' + JSON.stringify(err));
putResult(event.res_key, err, callback);
} else if (Object.keys(data).length === 0) {
err = new Error('404 Resource not found');
err.name = 'Item is not found in the table, cannot read!';
putResult(event.res_key, err, callback);
} else {
console.log('getCustomer success, data: ' + JSON.stringify(data));
// Only come with request when invoked by API gateway
if (event.request) refineCustomerData([data.Item], event.request);
putResult(event.res_key, data.Item, callback);
}
});
}
}
function createCustomer(event, callback) {
var params = {
TableName: event.tableName,
Item: event.item,
ConditionExpression: "attribute_not_exists(#myid)",
ExpressionAttributeNames: {"#myid": "email"}
};
console.log('In createCustomer, params is: ' + JSON.stringify(params));
var err = hasAllAttributes(params.item);
if (err) {
console.log('400 Invalid input, newCustomer does not have enough attributes');
err = new Error('400 invalid parameter');
err.name = 'newCustomer does not have enough attributes';
putResult(event.res_key, err, callback);
}
else
{
err = validateCustomer(params.Item);
if (err) {
console.log('Invalid Input: ' + JSON.stringify(err));
putResult(event.res_key, err, callback);
} else {
dynamo.putItem(params, function(err, data) {
if (err) {
if (err.code === 'ConditionalCheckFailedException') {
err = new Error('403 permission denied');
err.name = 'Creating Customer already exists in the table';
}
console.log('createCustomer err: ' + JSON.stringify(err));
putResult(event.res_key, err, callback);
} else {
console.log('createCustomer success, data: ' + JSON.stringify(data));
var pub_event = {
"operation": event.operation + " customer",
"email": event.item.email,
"name": event.item.firstname + " " + event.item.lastname,
"phone": event.item.phone_num,
"address": event.item.address
};
sns.publish({
Message: JSON.stringify(pub_event),
TopicArn: topic
}, function(err, data) {
if (err) {
console.log('publish to sns error: ' + err);
putResult(event.res_key, { email: params.Item.email }, callback);
} else {
console.log('publish to sns success');
putResult(event.res_key, { email: params.Item.email }, callback);
}
});
//console.log('go to put result');
//putResult(event.res_key, { email: params.Item.email }, callback);
}
});
}
}
}
function updateExpression(updates, params) {
var expr = " SET";
var exprAttrName = params.ExpressionAttributeNames;
var exprAttrVal = params.ExpressionAttributeValues;
for (var key in updates) {
var attrKey = "#" + key;
var attrVal = ":" + key;
expr += " " + attrKey + " = " + attrVal + ",";
exprAttrName[attrKey] = key;
exprAttrVal[attrVal] = updates[key];
}
expr = expr.slice(0, -1);
console.log("Translated expr in updateExpression: " + expr);
console.log("Translated exprAttrName in updateExpression: " + JSON.stringify(exprAttrName));
console.log("Translated exprAttrVal in updateExpression: " + JSON.stringify(exprAttrVal));
params.UpdateExpression += expr;
return params;
}
function updateCustomer(event, callback) {
var params = {
TableName: event.tableName,
Key: {'email': event.email},
ConditionExpression: "attribute_exists(#myid)",
UpdateExpression: "",
ExpressionAttributeNames: {"#myid": "email"},
ExpressionAttributeValues: {}
};
console.log('In updateCustomer, params is: ' + JSON.stringify(params));
var err = validateCustomer(event.updates);
if (err) {
console.log('validateEmail() returns err: ' + JSON.stringify(err));
putResult(event.res_key, err, callback);
} else {
params = updateExpression(event.updates, params);
dynamo.updateItem(params, function(err, data) {
if (err) {
if (err.code === 'ConditionalCheckFailedException') {
err = new Error('404, Resource not found');
err.name = 'updating Customer not exists in the table.';
}
console.log('updateCustomer err: ' + JSON.stringify(err));
putResult(event.res_key, err, callback);
// TODO check errtype == 'ConditionalCheckFailedException'
} else {
console.log('updateCustomer success, data: ' + JSON.stringify(data));
putResult(event.res_key, params, callback);
}
});
}
}
function deleteCustomer(event, callback) {
var params = {
TableName: event.tableName,
Key: {'email': event.email},
ConditionExpression: "attribute_exists(#myid)",
ExpressionAttributeNames: {"#myid": "email"}
};
console.log('In deleteCustomer, params is: ' + JSON.stringify(params));
var errEmail = validateEmail(params.Key.email);
if(errEmail) {
console.log('validateEmail() returns err: ' + JSON.stringify(errEmail));
putResult(event.res_key, errEmail, callback);
} else {
dynamo.deleteItem(params, function(err, data) {
if (err) {
if (err.code === 'ConditionalCheckFailedException') {
err = new Error('404 Resource not found');
err.name = 'deleting Customer not exists in the table.';
}
console.log('deleteCustomer err: ' + JSON.stringify(err));
putResult(event.res_key, err, callback);
} else {
console.log('deleteCustomer complete, data: ' + JSON.stringify(data));
putResult(event.res_key, {"result ": event.email + " is delete"}, callback);
}
});
}
}
function getCustomerAddress(event, callback) {
var params = {
TableName: event.tableName,
Key: {'email': event.email},
};
console.log('In deleteCustomer, params is: ' + JSON.stringify(params));
var errEmail = validateEmail(params.Key.email);
if(errEmail) {
console.log('validateEmail() returns err: ' + JSON.stringify(errEmail));
putResult(event.res_key, errEmail, callback);
} else {
var addressRef = null;
dynamo.getItem(params, function (err, data) {
if (err) {
console.log('getCustomer err: ' + JSON.stringify(err));
putResult(event.res_key, err, callback);
} else if (Object.keys(data).length === 0) {
err = new Error('404 Resource not found');
err.name = 'Item is not found in the table, cannot read!';
putResult(event.res_key, err, callback);
} else {
console.log('getCustomer success, data: ' + JSON.stringify(data));
addressRef = data.Item.address;
params.TableName = 'Address';
params.Key = {'UUID': addressRef};
dynamo.getItem(params, function(err, data) {
if (err) {
console.log('get customer address err: ' + JSON.stringify(err));
putResult(event.res_key, err, callback);
} else if (Object.keys(data).length === 0) {
err = new Error('404 Resource not found');
err.name = 'Item is not found in the table, cannot read!';
putResult(event.res_key, err, callback);
} else {
console.log('getCustomer address success, data: ' + JSON.stringify(data));
putResult(event.res_key, data.Item, callback);
}
});
}
});
}
}