-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.ts
141 lines (129 loc) · 3.56 KB
/
control.ts
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
import { doc, getDoc, setDoc } from 'firebase/firestore'
import type { NextApiRequest, NextApiResponse } from 'next'
import { z } from 'zod'
import { db } from '../../utilities/db'
import {
adjustCountdown,
calculateRamainingSeconds,
cleanRemoteRoomStateData,
hideHours,
pause,
setCountdown,
showHours,
start,
togglePaused,
toggleShowHours,
} from '../../utilities/roomState'
import { secondsToTimeComponents } from '../../utilities/secondsToTimeComponents'
import Cors from 'cors'
type Data =
| {
status: 'error'
message: string
}
| {
status: 'ok'
roomState: {
id: string
showHours: boolean
flashOnZero: boolean
end: string
paused: string | null
remainingTime: {
hours: number | null
minutes: number
seconds: number
}
}
}
const RequestParameters = z.object({
id: z.string(),
start: z.string().optional(),
pause: z.string().optional(),
togglePaused: z.string().optional(),
showHours: z.string().optional(),
hideHours: z.string().optional(),
toggleHours: z.string().optional(),
set: z.string().optional(),
adjust: z.string().optional(),
// @TODO: flash on zero
})
// @TODO: get server time - respect firebase time on server
const cors = Cors({
methods: ['POST', 'GET', 'HEAD'],
})
function runMiddleware(
req: NextApiRequest,
res: NextApiResponse,
fn: Function
) {
return new Promise((resolve, reject) => {
fn(req, res, (result: any) => {
if (result instanceof Error) {
return reject(result)
}
return resolve(result)
})
})
}
export default async function handler(
request: NextApiRequest,
response: NextApiResponse<Data>,
) {
await runMiddleware(request, response, cors)
const result = RequestParameters.safeParse(request.query)
if (!result.success) {
response.status(400).json({ status: 'error', message: 'Invalid request.' })
return
}
const parameters = result.data
const roomDocumentReference = doc(db, 'rooms', parameters.id)
const getRoomState = async () => {
const roomDocumentSnapshot = await getDoc(roomDocumentReference)
if (roomDocumentSnapshot.exists() === false) {
await setDoc(roomDocumentReference, {
name: parameters.id,
})
}
return cleanRemoteRoomStateData(roomDocumentSnapshot.data())
}
if (parameters.set !== undefined) {
await setCountdown(roomDocumentReference, parseInt(parameters.set, 10) || 0)
} else if (parameters.adjust !== undefined) {
await adjustCountdown(
await getRoomState(),
roomDocumentReference,
parseInt(parameters.adjust, 10) || 0,
)
}
if (parameters.start !== undefined) {
await start(await getRoomState(), roomDocumentReference)
} else if (parameters.pause !== undefined) {
await pause(await getRoomState(), roomDocumentReference)
} else if (parameters.togglePaused !== undefined) {
await togglePaused(await getRoomState(), roomDocumentReference)
}
if (parameters.showHours !== undefined) {
await showHours(roomDocumentReference)
} else if (parameters.hideHours !== undefined) {
await hideHours(roomDocumentReference)
} else if (parameters.toggleHours !== undefined) {
await toggleShowHours(await getRoomState(), roomDocumentReference)
}
const roomState = await getRoomState()
const remainingTime = secondsToTimeComponents(
calculateRamainingSeconds(roomState.end, roomState.paused),
roomState.showHours,
)
response.status(200).json({
status: 'ok',
roomState: {
id: roomState.name,
showHours: roomState.showHours,
flashOnZero: roomState.flashOnZero,
end: roomState.end.toISOString(),
paused: roomState.paused?.toISOString() ?? null,
remainingTime,
},
})
}