|
1 |
| -/* eslint-disable camelcase */ |
2 | 1 | const { GET_ALL_EVENTS_LIMIT_MIN, UNWANTED_PROPERTIES_FROM_100MS } = require("../constants/events");
|
| 2 | +const { INTERNAL_SERVER_ERROR } = require("../constants/errorMessages"); |
| 3 | + |
3 | 4 | const { EventTokenService, EventAPIService } = require("../services");
|
4 |
| -const { removeUnwantedProperties } = require("../utils/events"); |
5 | 5 | const eventQuery = require("../models/events");
|
| 6 | + |
6 | 7 | const logger = require("../utils/logger");
|
| 8 | +const { removeUnwantedProperties } = require("../utils/events"); |
7 | 9 |
|
8 | 10 | const tokenService = new EventTokenService();
|
9 | 11 | const apiService = new EventAPIService(tokenService);
|
@@ -100,7 +102,7 @@ const joinEvent = async (req, res) => {
|
100 | 102 | });
|
101 | 103 | } catch (error) {
|
102 | 104 | logger.error({ error });
|
103 |
| - return res.status(500).send("Internal Server Error"); |
| 105 | + return res.boom.badImplementation(INTERNAL_SERVER_ERROR); |
104 | 106 | }
|
105 | 107 | };
|
106 | 108 |
|
@@ -193,11 +195,77 @@ const endActiveEvent = async (req, res) => {
|
193 | 195 | }
|
194 | 196 | };
|
195 | 197 |
|
| 198 | +/** |
| 199 | + * Adds a peer to an event. |
| 200 | + * |
| 201 | + * @async |
| 202 | + * @function |
| 203 | + * @param {Object} req - The Express request object. |
| 204 | + * @param {Object} res - The Express response object. |
| 205 | + * @returns {Promise<Object>} The JSON response with the added peer data and a success message. |
| 206 | + * @throws {Object} The JSON response with an error message if an error occurred while adding the peer. |
| 207 | + */ |
| 208 | +const addPeerToEvent = async (req, res) => { |
| 209 | + try { |
| 210 | + const data = await eventQuery.addPeerToEvent({ |
| 211 | + peerId: req.body.peerId, |
| 212 | + name: req.body.name, |
| 213 | + role: req.body.role, |
| 214 | + joinedAt: req.body.joinedAt, |
| 215 | + eventId: req.params.id, |
| 216 | + }); |
| 217 | + return res.status(200).json({ |
| 218 | + data, |
| 219 | + message: `Selected Participant is added to the event.`, |
| 220 | + }); |
| 221 | + } catch (error) { |
| 222 | + logger.error({ error }); |
| 223 | + return res.status(500).json({ |
| 224 | + error: error.code, |
| 225 | + message: "You can't add selected Participant. Please ask Admin or Host for help.", |
| 226 | + }); |
| 227 | + } |
| 228 | +}; |
| 229 | + |
| 230 | +/** |
| 231 | + * Kicks out a peer from an event. |
| 232 | + * |
| 233 | + * @async |
| 234 | + * @function |
| 235 | + * @param {Object} req - The Express request object. |
| 236 | + * @param {Object} res - The Express response object. |
| 237 | + * @returns {Promise<Object>} The JSON response with a success message if the peer is successfully kicked out. |
| 238 | + * @throws {Object} The JSON response with an error message if an error occurred while kicking out the peer. |
| 239 | + */ |
| 240 | +const kickoutPeer = async (req, res) => { |
| 241 | + const { id } = req.params; |
| 242 | + const payload = { |
| 243 | + peer_id: req.body.peerId, |
| 244 | + reason: req.body.reason, |
| 245 | + }; |
| 246 | + |
| 247 | + try { |
| 248 | + await apiService.post(`/active-rooms/${id}/remove-peers`, payload); |
| 249 | + await eventQuery.kickoutPeer({ eventId: id, peerId: payload.peer_id, reason: req.body.reason }); |
| 250 | + return res.status(200).json({ |
| 251 | + message: `Selected Participant is removed from event.`, |
| 252 | + }); |
| 253 | + } catch (error) { |
| 254 | + logger.error({ error }); |
| 255 | + return res.status(500).json({ |
| 256 | + error: error.code, |
| 257 | + message: "You can't remove selected Participant from Remove, Please ask Admin or Host for help.", |
| 258 | + }); |
| 259 | + } |
| 260 | +}; |
| 261 | + |
196 | 262 | module.exports = {
|
197 | 263 | createEvent,
|
198 | 264 | getAllEvents,
|
199 | 265 | joinEvent,
|
200 | 266 | getEventById,
|
201 | 267 | updateEvent,
|
202 | 268 | endActiveEvent,
|
| 269 | + addPeerToEvent, |
| 270 | + kickoutPeer, |
203 | 271 | };
|
0 commit comments