-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
151 lines (127 loc) · 4.56 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
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
const aws = require('aws-sdk');
const personalizeevents = new aws.PersonalizeEvents();
const datasetArn = process.env.datasetArn;
const userTableName = process.env.userTableName;
const dynamo = new aws.DynamoDB.DocumentClient();
exports.handler = async (event, context) => {
//console.log('## ENVIRONMENT VARIABLES: ' + JSON.stringify(process.env));
//console.log('## EVENT: ' + JSON.stringify(event));
const body = JSON.parse(Buffer.from(event["body"], "base64"));
//console.log('body: ' + JSON.stringify(body));
//const header = event['multiValueHeaders'];
//console.log('header: ' + JSON.stringify(header));
const userId = body['userId'];
console.log('userId: ' + userId);
const gender = body['gender'];
console.log('gender: ' + gender);
const emotion = body['emotion'];
console.log('emotion: ' + emotion);
let response = "";
let isCompleted = false;
let queryParams = {
TableName: userTableName,
KeyConditionExpression: "USER_ID = :userId",
ExpressionAttributeValues: {
":userId": userId
}
};
// check the user's existence. if not, create a user
let dynamoQuery;
try {
dynamoQuery = await dynamo.query(queryParams).promise();
console.log('queryDynamo: '+JSON.stringify(dynamoQuery));
console.log('queryDynamo: '+dynamoQuery.Count);
if(!dynamoQuery.Count) {
// create the user dataset
try {
let params = {
datasetArn: datasetArn,
users: [{
userId: userId,
properties: {
"GENDER": gender,
"EMOTION": emotion
}
}]
};
console.log('user params: ', JSON.stringify(params));
const result = await personalizeevents.putUsers(params).promise();
console.log('putUser result: '+JSON.stringify(result));
} catch (error) {
console.log(error);
response = {
statusCode: 500,
body: error
};
}
try {
// DynamodB for personalize users
let personalzeParams = {
TableName: userTableName,
Item: {
USER_ID: userId,
GENDER: gender,
EMOTION: emotion,
}
};
console.log('personalzeParams: ' + JSON.stringify(personalzeParams));
dynamo.put(personalzeParams, function (err, data) {
if (err) {
console.log('Failure: ' + err);
}
else {
console.log('dynamodb put result: ' + JSON.stringify(data));
isCompleted = true;
console.log('created: ', userId);
response = {
statusCode: 200,
body: JSON.stringify({
userId: userId,
gender: gender,
emotion: emotion,
})
};
}
});
} catch (error) {
console.log(error);
response = {
statusCode: 500,
body: error
};
}
}
else {
isCompleted = true;
console.log('existed: ', userId);
response = {
statusCode: 200,
body: JSON.stringify({
userId: userId,
gender: gender,
emotion: emotion,
})
};
}
} catch (error) {
console.log(error);
return;
}
function wait() {
return new Promise((resolve, reject) => {
if (!isCompleted) {
setTimeout(() => resolve("wait..."), 1000);
}
else {
setTimeout(() => resolve("done..."), 0);
}
});
}
console.log(await wait());
console.log(await wait());
console.log(await wait());
console.log(await wait());
console.log(await wait());
console.debug('response: ' + JSON.stringify(response));
return response;
};