forked from XiangbingJi/CloudAppProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddress.js
351 lines (320 loc) · 13.2 KB
/
Address.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
'use strict';
console.log('Loading Address.js...');
const https = require('https');
const doc = require('dynamodb-doc');
const dynamo = new doc.DynamoDB();
exports.handler = function(event, context, callback) {
console.log('handler event: ' + JSON.stringify(event));
console.log('handler context: ' + JSON.stringify(context));
if (event.Records !== undefined)
event = JSON.parse(event.Records[0].Sns.Message);
var operation = event.operation;
switch (operation) {
case ('read'):
getAddress(event, callback);
break;
case ('create'):
createAddress(event, callback);
break;
case ('update'):
updateAddress(event, callback);
break;
case ('delete'):
deleteAddress(event, callback);
break;
default:
var err = new Error('405 Invalid request method');
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) }
};
dynamo.putItem(params, function(err, data) {
if (err) {
console.log('putResult err: ' + JSON.stringify(err));
}
});
cb(null, 'putResult ends');
}
function getAddress(event, callback) {
var params = {
TableName: event.tableName,
Key: {'UUID': event.UUID}
};
console.log('In getAddress, params is: ' + JSON.stringify(params));
dynamo.getItem(params, function (err, data) {
if (err) {
console.log('getAddress 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 = 'key not found in the table';
putResult(event.res_key, err, callback);
}
else{
console.log('getAddress success, data: ' + JSON.stringify(data));
putResult(event.res_key, data.Item, callback);
}
});
}
function hasAllAttributes(item) {
// check whether the new Address has all four attributes city, number, street, state, and zipcode
var obj = {'city': false, 'number': false, 'street': false, 'state': false, 'zipcode' : false};
for (var key in item) {
if (key in obj) obj[key] = true;
}
return obj.city && obj.number && obj.street && obj.state && obj.zipcode;
}
function getNormAddr(item, callback) {
// set auth-id and auth-token
var id = '1a121d57-acec-669e-5f64-c413e1cd6fe2';
var token = '0eBxLrzCLDYDZm77ilmL';
var titleString = 'https://us-street.api.smartystreets.com/street-address?';
var tileString = "&'%20-H%20%22Content-Type:%20application/json";
// check if the input item contains every field
if (item.city === undefined || item.number === undefined || item.street === undefined
|| item.state === undefined || item.zipcode === undefined) {
console.log('Lost some fields');
callback(new Error('400 Invalid address: Lost some fields'), null);
} else {
// use SmartyStreet API and get the barcode from the response
// create corresponding street string
var streetArr = item.street.split(' ');
var streetStr = '';
for (var i in streetArr) {
streetStr += streetArr[i] + '%20';
}
streetStr = streetStr.substring(0, streetStr.length-3);
// create corresponding city string
var cityArr = item.city.split(' ');
var cityStr = '';
for (var j in cityArr) {
cityStr += cityArr[j] + '%20';
}
cityStr = cityStr.substring(0, cityStr.length-3);
// create the url string
var urlString = titleString + "auth-id=" + id + "&auth-token=" + token
+ "&street=" + item.number + "%20" + streetStr + "&city="
+ cityStr + "&state=" + item.state + tileString;
https.get(urlString, function(res) {
console.log("Got response: " + res.statusCode);
res.on('data', function(d) {
process.stdout.write(d);
console.log('data: ' + d);
var obj = JSON.parse(d);
if (obj[0] === undefined) {
console.log('get response failed');
callback(new Error('400 Invalid address'), null);
} else {
var resAddress = {};
resAddress.barcode = obj[0]["delivery_point_barcode"];
resAddress.components = obj[0]["components"];
console.log('code: ' + JSON.stringify(resAddress.addressBarcode));
console.log('components: ' + JSON.stringify(resAddress.addressComponents));
callback(null, resAddress);
}
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
callback(new Error('500 Failed to get address, err: ' + e.message), null);
});
}
}
function validateAddress(item, create, callback) {
var err = null;
if (create) {
if (!hasAllAttributes(item)) {
err = new Error('400 Invalid parameter');
err.name = 'newAddress does not have enough attributes';
callback(err, null);
}
}
for (var col in item) {
switch (col) {
case ('city'):
if (typeof item.city != 'string') {
err = new Error('400 Invalid parameter');
err.name = 'wrong type! city has to be a Js string type';
callback(err, null);
}
break;
case ('street'):
if (typeof item.street != 'string') {
err = new Error('400 Invalid parameter');
err.name = 'wrong type! street has to be a Js string type';
callback(err, null);
}
break;
case ('number'):
if (typeof item.number != 'string') {
err = new Error('400 Invalid parameter');
err.name = 'wrong type! number has to be a Js string type';
callback(err, null);
} else {
var isNum = /^\d+$/.test(item.number);
if(!isNum){
err = new Error('400 Invalid parameter');
err.name = 'wrong type! street number has to be a real number';
callback(err, null);
}
}
break;
case ('state'):
if (typeof item.state != 'string') {
err = new Error('400 Invalid parameter');
err.name = 'wrong type! state has to be a Js string type';
callback(err, null);
}
break;
case ('zipcode'):
if (typeof item.zipcode != 'string') {
err = new Error('400 Invalid parameter');
err.name = 'wrong type! zip code has to be a Js string type';
callback(err, null);
}
var re = /\d{5}/;
if (!re.test(item.zipcode)) {
err = new Error('400 Invalid parameter');
err.name = 'zip code has to be a 5-digits number';
callback(err, null);
}
break;
default:
err = new Error('400 Invalid parameter');
err.name = 'add cannot have additional fields';
callback(err, null);
}
}
getNormAddr(item, callback);
}
function ssAddr2DBFields(addr) {
var comp = addr.components;
return {
'UUID': addr.barcode,
'number': comp.primary_number,
'city': comp.city_name,
'street': (comp.street_predirection ? (comp.street_predirection + " ") : "") +
comp.street_name +
(comp.street_postdirection ? (" " + comp.street_postdirection) : "") +
(comp.street_suffix ? (" " + comp.street_suffix) : ""),
'state': comp.state_abbreviation,
'zipcode': comp.zipcode + (comp.plus4_code ? ("-" + comp.plus4_code) : "")
};
}
function createAddress(event, callback) {
var params = {
TableName: event.tableName,
Item: {},
ConditionExpression: "attribute_not_exists(#myid)",
ExpressionAttributeNames: {"#myid": "UUID"}
};
console.log('In createAddress, params is: ' + JSON.stringify(params));
validateAddress(event.item, true, function(err, addr) {
if (err) {
console.log('validateAddress() returns err: ' + JSON.stringify(err));
putResult(event.res_key, err, callback);
} else {
params.Item = ssAddr2DBFields(addr);
dynamo.putItem(params, function(err, data) {
// Return OK and the UUID when address is already in table
if (err && err.code != "ConditionalCheckFailedException") {
console.log('createAddress err: ' + JSON.stringify(err));
putResult(event.res_key, err, callback);
} else {
if (err) {
// err.code == "ConditionalCheckFailedException"
console.log('createAddress: UUID conflict, treat it as successful creation');
}
else {
console.log('createAddress success, data: ' + JSON.stringify(data));
}
// return the UUID as the API response
putResult(event.res_key, { UUID: params.Item.UUID }, callback);
}
});
}
});
}
function updateExpression(item, params) {
var expr = " SET";
var exprAttrName = params.ExpressionAttributeNames;
var exprAttrVal = params.ExpressionAttributeValues;
for (var key in item) {
var attrKey = "#" + key;
var attrVal = ":" + key;
expr += " " + attrKey + " = " + attrVal + ",";
exprAttrName[attrKey] = key;
exprAttrVal[attrVal] = item[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 __doUpdateAddress(event, callback) {
// have to create the updated address,
// for the primary key UUID isn't allowed to modify
createAddress(event, callback);
}
function updateAddress(event, callback) {
if (!hasAllAttributes(event.item)) {
// if event.item is lack of some attributes, patch it for verifying the address
var params = {
TableName: event.tableName,
Key: {'UUID': event.UUID}
};
console.log('Get address in updateAddress, params is: ' + JSON.stringify(params));
dynamo.getItem(params, function (err, data) {
if (err) {
console.log('Get address in updateAddress 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 = 'updateAddress: key not found in the table';
putResult(event.res_key, err, callback);
}
else{
// patch missed fields by its original values
for (var key in data.Item) {
if (!(key in event.item) && key != 'UUID') {
event.item[key] = data.Item[key];
}
}
console.log('updateAddress: patched item- ' + JSON.stringify(event.item));
__doUpdateAddress(event, callback);
}
});
}
else {
__doUpdateAddress(event, callback);
}
}
function deleteAddress(event, callback) {
var params = {
TableName: event.tableName,
Key: {'UUID': event.UUID},
ConditionExpression: "attribute_exists(#myid)",
ExpressionAttributeNames: {"#myid": "UUID"}
};
console.log('In deleteAddress, params is: ' + JSON.stringify(params));
dynamo.deleteItem(params, function(err, data) {
if (err && err.code == "ConditionalCheckFailedException") {
err = new Error('404 Resource not found');
err.name = "Deleting address is not found in the table";
console.log('deleteAddress err: ' + JSON.stringify(err));
putResult(event.res_key, err, callback);
} else if (err) {
console.log('deleteAddress err: ' + JSON.stringify(err));
putResult(event.res_key, err, callback);
} else {
console.log('deleteAddress complete, data: ' + JSON.stringify(data));
putResult(event.res_key, { "result": event.UUID + " is deleted" }, callback);
}
});
}