This repository has been archived by the owner on Jul 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.js
87 lines (76 loc) · 1.99 KB
/
http.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
const redis = require('async-redis');
const uuid = require('uuid/v4');
const { runRequestAt } = require('./libs');
const client = redis.createClient({
host: process.env.REDIS_HOST,
port: process.env.REDIS_HOST,
password: process.env.REDIS_PASSWORD,
});
const EXPIRY_SECS = process.env.REDIS_TTL;
// FIXME: actual validation, ideally via openAPI spec
const validatePayload = data => data;
// TODO: add logic to set rule for scheduling reservation
const setReservationSchedule = () => {
};
module.exports.createBonfimFixtureById = async (event) => {
const id = uuid();
let date;
let time;
let court;
try {
({ date, time, court } = validatePayload(
JSON.parse(event.body),
));
} catch (e) {
return {
statusCode: 400,
body: JSON.stringify({ code: 400, message: e }),
};
}
try {
const reserveAt = runRequestAt(date, 'bonfim');
const data = {
date,
time,
court,
reserveAt: reserveAt.toISOString(),
status: 'acknowledged',
};
// into ['id', id, 'date', date, ...]
const zipped = Object.entries(data).reduce(
(arr, keyValue) => [...arr, ...keyValue],
[],
);
await client.hmset(id, zipped);
await client.expire(id, EXPIRY_SECS);
// async send schedule; we don't await the response
setReservationSchedule();
return {
statusCode: 202,
headers: {
link: `/bonfim/${id}`,
},
body: JSON.stringify({ id }),
};
} catch (e) {
return {
statusCode: 500,
body: JSON.stringify({ code: 500, message: e }),
};
}
};
module.exports.showBonfimFixtureById = async (event) => {
const { id } = event.pathParameters;
const data = await client.hgetall(id);
return {
statusCode: data ? 200 : 404,
body: data ? JSON.stringify(data) : undefined,
};
};
module.exports.deleteBonfimFixtureById = async (event) => {
const { id } = event.pathParameters;
const deleted = await client.del(id);
return {
statusCode: deleted ? 204 : 404,
};
};