generated from hasibhassan/aws-one-touch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (122 loc) · 3.22 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
const AWS = require('aws-sdk')
const db = new AWS.DynamoDB.DocumentClient()
exports.handler = async (event, context) => {
console.log(`EVENT is: ${JSON.stringify(event)}`)
async function getItem(userEmail) {
let params = {
TableName: 'visiontable-prod',
Key: { 'user': userEmail },
}
try {
const data = await db.get(params).promise()
return data
} catch (err) {
return err
}
}
async function updateItem(userEmail, itemData) {
let params = {
TableName: 'visiontable-prod',
Key: { 'user': userEmail },
UpdateExpression: 'set contextstate = :newstate',
ExpressionAttributeValues: { ':newstate': itemData },
}
try {
await db.update(params).promise()
} catch (err) {
return err
}
}
async function deleteItem(userEmail) {
let params = {
TableName: 'visiontable-prod',
Key: { 'user': userEmail },
}
try {
await db.delete(params).promise()
} catch (err) {
return err
}
}
if (event.httpMethod === 'GET') {
try {
const {
pathParameters: { proxy: email },
} = event
const data = await getItem(email)
let res = {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*', // replace with hostname of frontend (CloudFront)
},
body: JSON.stringify(data),
}
return res
} catch (err) {
let res = {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*', // replace with hostname of frontend (CloudFront)
},
body: JSON.stringify(err),
}
return res
}
}
if (event.httpMethod === 'POST') {
const jsonBody = JSON.parse(event.body)
const contextState = jsonBody.contextState
console.log('state is', contextState)
const stateString = JSON.stringify(contextState)
try {
const {
pathParameters: { proxy: email },
} = event
await updateItem(email, stateString)
let res = {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*', // replace with hostname of frontend (CloudFront)
},
}
return res
} catch (err) {
let res = {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*', // replace with hostname of frontend (CloudFront)
},
}
return res
}
}
if (event.httpMethod === 'PUT') {
try {
const {
pathParameters: { user },
} = event
await deleteItem(user)
let res = {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*', // replace with hostname of frontend (CloudFront)
},
}
return res
} catch (err) {
let res = {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*', // replace with hostname of frontend (CloudFront)
},
}
return res
}
}
}