generated from RealDevSquad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathevents.js
More file actions
271 lines (257 loc) · 8.87 KB
/
events.js
File metadata and controls
271 lines (257 loc) · 8.87 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
const { GET_ALL_EVENTS_LIMIT_MIN, UNWANTED_PROPERTIES_FROM_100MS } = require("../constants/events");
const { INTERNAL_SERVER_ERROR } = require("../constants/errorMessages");
const { EventTokenService, EventAPIService } = require("../services");
const eventQuery = require("../models/events");
const logger = require("../utils/logger");
const { removeUnwantedProperties } = require("../utils/events");
const tokenService = new EventTokenService();
const apiService = new EventAPIService(tokenService);
/**
* Creates a new event document in the Firestore database with the data provided in the HTTP request body.
* @async
* @function
* @param {Object} req - The Express request object.
* @param {Object} res - The Express response object.
* @returns {Object} The saved event data in JSON format.
* @throws {Error} If an error occurs while creating the event document.
*/
const createEvent = async (req, res) => {
const { name, description, region, userId } = req.body;
const payload = { name, description, region };
try {
const eventData = await apiService.post("/rooms", payload);
const event = removeUnwantedProperties(UNWANTED_PROPERTIES_FROM_100MS, eventData);
const eventDataFromDB = await eventQuery.createEvent({ room_id: eventData.id, created_by: userId, ...event });
return res.status(201).json(eventDataFromDB);
} catch (error) {
logger.error({ error });
return res.status(500).json({
error: error.code,
message: "Couldn't create event. Please try again later",
});
}
};
/**
* Retrieves all events that match the query parameters provided in the HTTP request query string.
* @async
* @function
* @param {Object} req - The Express request object.
* @param {Object} res - The Express response object.
* @returns {Object} The events data in JSON format.
* @throws {Error} If an error occurs while retrieving the events data.
*/
const getAllEvents = async (req, res) => {
/**
* @type {boolean} - enabled: determines whether the rooms should be enabled or disabled.
* @type {number} - limit: The maximum number of rooms to retrieve.
* @type {string} - offset: The starting point for retrieving rooms.
*/
const { enabled, limit, offset } = req.query;
try {
const start = offset || "";
const limitOfRooms = limit || GET_ALL_EVENTS_LIMIT_MIN;
const isEnabled = enabled || false;
const eventsData = await apiService.get(`/rooms?limit=${limitOfRooms}&enabled=${isEnabled}&start=${start}`);
if (eventsData?.data) {
const events = removeUnwantedProperties(UNWANTED_PROPERTIES_FROM_100MS, eventsData.data);
const filteredEventsData = {
limit: eventsData.limit,
last: eventsData.last,
data: events.map(({ id, ...event }) => ({
id,
room_id: id,
...event,
})),
};
return res.status(200).json(filteredEventsData);
}
return res.status(200).json(eventsData);
} catch (error) {
logger.error({ error });
return res.status(500).json({
error: error.code,
message: "Couldn't get events. Please try again later",
});
}
};
/**
* Generates a token for the specified event and user information.
* @async
* @function
* @param {Object} req - The Express request object.
* @param {Object} res - The Express response object.
* @returns {Object} An object containing the generated token and a success message in JSON format.
* @throws {Error} If an error occurs while generating the token.
*/
const joinEvent = async (req, res) => {
const { roomId, userId, role } = req.body;
const payload = { roomId, userId, role };
try {
const token = tokenService.getAuthToken(payload);
return res.status(201).json({
token: token,
message: "Token generated successfully!",
success: true,
});
} catch (error) {
logger.error({ error });
return res.boom.badImplementation(INTERNAL_SERVER_ERROR);
}
};
/**
* Retrieves event details by ID and returns JSON response
*
* @async
* @function
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @returns {Object} - JSON response containing event details or error message
* @throws {Object} - The JSON response with an error message if an error occurred if event retrieval fails.
*/
const getEventById = async (req, res) => {
const roomId = req.params.id;
const isActiveRoom = req.body.isActiveRoom;
try {
const url = `/${isActiveRoom ? "active-" : ""}rooms/${roomId}`;
const eventData = await apiService.get(url);
if (!isActiveRoom) {
const event = removeUnwantedProperties(UNWANTED_PROPERTIES_FROM_100MS, eventData);
return res.status(200).json({ room_id: event.id, ...event });
}
return res.status(200).json(eventData);
} catch (error) {
logger.error({ error });
return res.status(500).json({
error: error.code,
message: "Unable to retrieve event details",
});
}
};
/**
* Updates a event with the given ID and enables/disables it
*
* @async
* @function
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @returns {Object} - JSON response containing updated event data and success message or error message
* @throws {Object} - If an error occurs while updating the event
*/
const updateEvent = async (req, res) => {
const payload = {
enabled: req.body.enabled,
};
try {
const eventData = await apiService.post(`/rooms/${req.body.id}`, payload);
await eventQuery.updateEvent(eventData);
const event = removeUnwantedProperties(UNWANTED_PROPERTIES_FROM_100MS, eventData);
return res.status(200).json({
data: { room_id: event.id, ...event },
message: `Event is ${req.body.enabled ? "enabled" : "disabled"}`,
});
} catch (error) {
logger.error({ error });
return res.status(500).json({
error: error,
message: "Couldn't update event. Please try again later.",
});
}
};
/**
* Ends an active event.
*
* @async
* @function
* @param {Object} req - The Express request object.
* @param {Object} res - The Express response object.
* @returns {Promise<Object>} The JSON response with a message indicating the session has ended.
* @throws {Object} The JSON response with an error message if an error occurred while ending the event.
*/
const endActiveEvent = async (req, res) => {
const payload = {
reason: req.body.reason,
lock: req.body.lock,
};
try {
await apiService.post(`/active-rooms/${req.body.id}/end-room`, payload);
await eventQuery.endActiveEvent({ id: req.body.id, ...payload });
return res.status(200).json({ message: `Event ended successfully.` });
} catch (error) {
logger.error({ error });
return res.status(500).json({
error: error.code,
message: "Couldn't end the event. Please try again later",
});
}
};
/**
* Adds a peer to an event.
*
* @async
* @function
* @param {Object} req - The Express request object.
* @param {Object} res - The Express response object.
* @returns {Promise<Object>} The JSON response with the added peer data and a success message.
* @throws {Object} The JSON response with an error message if an error occurred while adding the peer.
*/
const addPeerToEvent = async (req, res) => {
try {
const data = await eventQuery.addPeerToEvent({
peerId: req.body.peerId,
name: req.body.name,
role: req.body.role,
joinedAt: req.body.joinedAt,
eventId: req.params.id,
});
return res.status(200).json({
data,
message: `Selected Participant is added to the event.`,
});
} catch (error) {
logger.error({ error });
return res.status(500).json({
error: error.code,
message: "You can't add selected Participant. Please ask Admin or Host for help.",
});
}
};
/**
* Kicks out a peer from an event.
*
* @async
* @function
* @param {Object} req - The Express request object.
* @param {Object} res - The Express response object.
* @returns {Promise<Object>} The JSON response with a success message if the peer is successfully kicked out.
* @throws {Object} The JSON response with an error message if an error occurred while kicking out the peer.
*/
const kickoutPeer = async (req, res) => {
const { id } = req.params;
const payload = {
peer_id: req.body.peerId,
reason: req.body.reason,
};
try {
await apiService.post(`/active-rooms/${id}/remove-peers`, payload);
await eventQuery.kickoutPeer({ eventId: id, peerId: payload.peer_id, reason: req.body.reason });
return res.status(200).json({
message: `Selected Participant is removed from event.`,
});
} catch (error) {
logger.error({ error });
return res.status(500).json({
error: error.code,
message: "You can't remove selected Participant from Remove, Please ask Admin or Host for help.",
});
}
};
module.exports = {
createEvent,
getAllEvents,
joinEvent,
getEventById,
updateEvent,
endActiveEvent,
addPeerToEvent,
kickoutPeer,
};