-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
175 lines (163 loc) · 4.52 KB
/
handler.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
'use strict'
const DynamoDB = require('aws-sdk/clients/dynamodb')
exports.handler = async function user (event) {
const {
detail: {
key: { sk, pk },
payload: { id }
}
} = event
console.log('core handler', JSON.stringify({ pk, sk, event }, null, 2))
const dynamodb = new DynamoDB.DocumentClient({
convertEmptyValues: true,
...(process.env.IS_OFFLINE && {
endpoint: 'http://127.0.0.1:8000',
region: 'us-east-1',
accessKeyId: 'x',
secretAccessKey: 'x'
})
})
const userPk = `users#${id}`
const { Item: { version: currentVersion = 0 } = {} } = await dynamodb
.get({
TableName: process.env.DYNAMODB_CORE_TABLE,
Key: {
pk: userPk,
sk: userPk
},
ConsistentRead: true,
ExpressionAttributeNames: {
'#version': 'version'
},
ProjectionExpression: '#version'
})
.promise()
const { Items: logItems } = await dynamodb
.query({
TableName: process.env.DYNAMODB_LOGS_TABLE,
KeyConditionExpression: '#pk = :pk and #sk > :sk',
ExpressionAttributeNames: {
'#pk': 'pk',
'#sk': 'sk'
},
ExpressionAttributeValues: {
':pk': pk,
':sk': currentVersion
}
})
.promise()
if (logItems.length === 0) {
console.log(`no event found ${pk}, sk > ${currentVersion}`)
return
}
let previousVersion = currentVersion
for (const event of logItems) {
if (event.sk !== previousVersion + 1) {
console.log(
`missing events expected ${previousVersion + 1} got ${event.sk}`
)
return
}
const handler = getEventHandler(dynamodb, userPk, event)
if (!handler) {
console.error(`no event handler found for type ${event.type}`)
} else {
await handler(event)
}
previousVersion = event.sk
}
}
function getEventHandler (dynamodb, pk, { type, payload: { id } }) {
const sk = pk
return {
async signup ({ sk: version, payload: { name, email } }) {
await dynamodb
.put({
TableName: process.env.DYNAMODB_CORE_TABLE,
Item: {
pk,
sk,
type: 'user',
id,
name,
email,
version
},
ConditionExpression: 'attribute_not_exists(pk)',
ReturnValues: 'NONE'
})
.promise()
},
async addPhoneNumber ({ sk: version, payload: { phoneNumber } }) {
await dynamodb
.update({
TableName: process.env.DYNAMODB_CORE_TABLE,
ExpressionAttributeNames: {
'#phoneNumber': 'phoneNumber',
'#version': 'version'
},
ExpressionAttributeValues: {
':phoneNumber': phoneNumber,
':version': version,
':currentVersion': version - 1
},
Key: {
pk,
sk
},
UpdateExpression:
'SET #phoneNumber = :phoneNumber, #version = :version',
ConditionExpression: '#version = :currentVersion',
ReturnValues: 'NONE'
})
.promise()
},
async verifyPhoneNumber ({ sk: version }) {
await dynamodb
.update({
TableName: process.env.DYNAMODB_CORE_TABLE,
ExpressionAttributeNames: {
'#verifiedPhoneNumber': 'verifiedPhoneNumber',
'#version': 'version'
},
ExpressionAttributeValues: {
':verifiedPhoneNumber': true,
':version': version,
':currentVersion': version - 1
},
Key: {
pk,
sk
},
UpdateExpression:
'SET #verifiedPhoneNumber = :verifiedPhoneNumber, #version = :version',
ConditionExpression: '#version = :currentVersion',
ReturnValues: 'NONE'
})
.promise()
},
async addLocation ({ sk: version, payload: { long, lat } }) {
await dynamodb
.update({
TableName: process.env.DYNAMODB_CORE_TABLE,
ExpressionAttributeNames: {
'#location': 'location',
'#version': 'version'
},
ExpressionAttributeValues: {
':location': { long, lat },
':version': version,
':currentVersion': version - 1
},
Key: {
pk,
sk
},
UpdateExpression: 'SET #location = :location, #version = :version',
ConditionExpression: '#version = :currentVersion',
ReturnValues: 'NONE'
})
.promise()
}
}[type]
}