-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
68 lines (60 loc) · 1.7 KB
/
index.ts
File metadata and controls
68 lines (60 loc) · 1.7 KB
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
import {APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2 } from 'aws-lambda';
// case1: input only uuid, return random response
// case2: input uuid with timestamp, return random response
// case3: input uuid, timestamp and forceUpdate flag, return response to update
// unit test:
// - should return 200 with content when there update
// - should return 201 no content, if no update required
// - should return 400, if uuid is not found or timestamp is wrong format
export const handler = async (event: APIGatewayProxyEventV2 ): Promise<APIGatewayProxyStructuredResultV2> => {
const UUID = event.pathParameters?.UUID ?? undefined;
const timeStamp = event.pathParameters?.timeStamp ?? undefined;
const forceUpdate = event.pathParameters?.forceUpdate ?? undefined;
if (!UUID) {
return {
statusCode: 400,
body: JSON.stringify({
message: "no uuid provide",
})
}
}
const result = await mockDB.find(UUID);
if(!result) {
return {
statusCode: 500,
body: JSON.stringify({
message: "user not found",
})
}
}
// compare timestamp param with result
const { updatedAt } = result;
// never get config or need update
if (!timeStamp || (timeStamp < updatedAt)) {
return {
statusCode: 200,
body: JSON.stringify(result),
}
} else {
// no need to update
return {
statusCode: 201,
}
}
}
const mockDB = {
find: (UUID: string) => {
const date = new Date().toISOString();
const mockJson = {
CONFIATION_RATE: "2000",
}
if (UUID == "UUID-001") {
return {
updatedAt: date,
configValue: JSON.stringify(mockJson),
}
} else {
return undefined;
}
},
}