generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 201
/
events.js
308 lines (273 loc) · 9.7 KB
/
events.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
const admin = require("firebase-admin");
const Firestore = require("@google-cloud/firestore");
const firestore = require("../utils/firestore");
const logger = require("../utils/logger");
const eventModel = firestore.collection("events");
const peerModel = firestore.collection("peers");
const eventCodeModel = firestore.collection("event-codes");
/**
* Creates a new event document in Firestore and returns the data for the created document.
* @async
* @function
* @param {Object} eventData - The data to be added to the event document in Firestore.
* @returns {Promise<Object>} - The data for the created event document.
* @throws {Error} - Throws an error if there was a problem adding data to Firestore.
*/
const createEvent = async (eventData) => {
try {
const docRef = eventModel.doc(eventData.room_id);
await docRef.set(eventData);
const docSnapshot = await docRef.get();
const data = docSnapshot.data();
return data;
} catch (error) {
logger.error("Error in adding data", error);
throw error;
}
};
/**
* Updates an existing event document in the Firestore database with the given event data.
* @async
* @function
* @param {object} eventData - The data for the event to be updated.
* @throws {Error} If an error occurs while updating the event document.
*/
const updateEvent = async (eventData) => {
try {
const docRef = eventModel.doc(eventData.id);
await docRef.update({ enabled: eventData.enabled });
} catch (error) {
logger.error("Error in enabling event.", error);
throw error;
}
};
/**
* Ends an active event in the Firestore database with the given data.
* @async
* @function
* @param {object} data - The data for the event to be ended.
* @param {string} data.id - The unique identifier for the event to be ended.
* @param {string} data.reason - The reason for ending the event.
* @param {boolean} data.lock - Whether the event should be locked after it's ended.
* @throws {Error} If an error occurs while updating the event document.
*/
const endActiveEvent = async ({ id, reason, lock }) => {
try {
const docRef = eventModel.doc(id);
await docRef.update({
lock,
reason,
enabled: false,
status: "inactive",
});
} catch (error) {
logger.error("Error in ending event.", error);
throw error;
}
};
/**
* Adds a peer to an event in the Firestore database.
* @async
* @function
* @param {Object} peerData - The data of the peer to be added.
* @param {string} peerData.name - The name of the peer.
* @param {string} peerData.eventId - The unique identifier of the event the peer is being added to.
* @param {string} peerData.role - The role of the peer in the event.
* @param {Date} peerData.joinedAt - The timestamp indicating when the peer joined the event.
* @returns {Promise<Object>} The data of the added peer.
* @throws {Error} If an error occurs while adding the peer to the event.
*/
const addPeerToEvent = async (peerData) => {
try {
const batch = firestore.batch();
const peerRef = peerModel.doc(peerData.peerId);
const peerDocSnapshot = await peerRef.get();
if (!peerDocSnapshot.exists) {
// If the peer document doesn't exist, create a new one
const peerDocData = {
peerId: peerData.peerId,
name: peerData.name,
joinedEvents: [
{
event_id: peerData.eventId,
role: peerData.role,
joined_at: peerData.joinedAt,
},
],
};
batch.set(peerRef, peerDocData);
} else {
// If the peer document exists, update the joinedEvents array
batch.update(peerRef, {
joinedEvents: Firestore.FieldValue.arrayUnion({
event_id: peerData.eventId,
role: peerData.role,
joined_at: peerData.joinedAt,
}),
});
}
const eventRef = eventModel.doc(peerData.eventId);
batch.update(eventRef, {
peers: Firestore.FieldValue.arrayUnion(peerRef.id),
});
await batch.commit();
const updatedPeerSnapshot = await peerRef.get();
return updatedPeerSnapshot.data();
} catch (error) {
logger.error("Error in adding peer to the event", error);
throw error;
}
};
/**
* Gets a peer with the help of id from Firestore database
* @async
* @function
* @param {String} params - id of the peer
* @returns {Promise<Object>} The data of the peer
* @throws {Error} If the peer is not found
*/
const getPeerById = async (id) => {
try {
const peerRef = peerModel.doc(id);
const peerSnapshot = await peerRef.get();
const peerData = peerSnapshot.data();
return peerData;
} catch (error) {
logger.error("Error in retrieving the peer", error);
throw error;
}
};
/**
* Removes a peer from an event and marks them as kicked out in the Firestore database.
* @async
* @function
* @param {Object} params - The parameters for kicking out the peer.
* @param {string} params.eventId - The unique identifier of the event from which the peer is being kicked out.
* @param {string} params.peerId - The unique identifier of the peer being kicked out.
* @param {string} params.reason - The reason for kicking out the peer from the event.
* @returns {Promise<Object>} The updated data of the kicked-out peer.
* @throws {Error} If the peer is not found or is not part of the specified event.
*/
const kickoutPeer = async ({ eventId, peerId, reason }) => {
try {
const peerRef = peerModel.doc(peerId);
const peerSnapshot = await peerRef.get();
if (!peerSnapshot.exists) {
throw new Error("Participant not found");
}
const peerData = peerSnapshot.data();
const joinedEvents = peerData.joinedEvents;
const eventIndex = joinedEvents.findIndex((event) => event.event_id === eventId);
if (eventIndex === -1) {
throw new Error("Participant is not part of the specified event");
}
const updatedJoinedEvents = joinedEvents.map((event, index) =>
index === eventIndex ? { ...event, left_at: new Date(), reason: reason, isKickedout: true } : event
);
await peerRef.update({ joinedEvents: updatedJoinedEvents });
const updatedPeerSnapshot = await peerRef.get();
return updatedPeerSnapshot.data();
} catch (error) {
logger.error("Error in removing peer from the event.", error);
throw error;
}
};
/**
* Creates an events code document in the Firestore database with the given event code data.
* @async
* @function
* @param {object} eventCodeData - The data for the event code to be created.
* @throws {Error} If an error occurs while creating the event code document.
*/
const createEventCode = async (eventCodeData) => {
try {
const eventRef = eventModel.doc(eventCodeData.event_id);
const eventSnapshot = await eventRef.get();
const eventSnapshotData = eventSnapshot.data();
const createdAndUpdatedAt = admin.firestore.Timestamp.now();
const docRef = eventCodeModel.doc(eventCodeData.id);
await docRef.set({ ...eventCodeData, created_at: createdAndUpdatedAt, updated_at: createdAndUpdatedAt });
const docSnapshot = await docRef.get();
const data = docSnapshot.data();
const previouslyPresentEventCodes = eventSnapshotData?.event_codes?.by_role?.mavens && [
...eventSnapshotData?.event_codes?.by_role?.mavens,
];
if (!data) throw new Error();
if (previouslyPresentEventCodes?.length) {
await eventRef.update({
event_codes: {
by_role: {
mavens: [...previouslyPresentEventCodes, data?.id],
},
},
});
} else {
await eventRef.update({
event_codes: {
by_role: {
mavens: [data?.id],
},
},
});
}
const updatedEventRef = eventModel.doc(eventCodeData.event_id);
const updatedEventSnapshot = await updatedEventRef.get();
const updatedEventSnapshotData = updatedEventSnapshot.data();
const allEventCodesIdsForMavens = updatedEventSnapshotData?.event_codes?.by_role?.mavens;
const allEventCodesIdsForMavensPromises = allEventCodesIdsForMavens.map(async (eventCodeRefId) => {
const eventCodeRef = eventCodeModel.doc(eventCodeRefId);
const eventCodeSnapshot = await eventCodeRef.get();
const eventCodeData = eventCodeSnapshot.data();
return eventCodeData;
});
const allEventCodesForMavens = await Promise.all(allEventCodesIdsForMavensPromises);
return allEventCodesForMavens;
} catch (error) {
logger.error("Error in creating event code", error);
throw error;
}
};
const getEventById = async ({ id }) => {
try {
const eventRef = eventModel.doc(id);
const eventSnapshot = await eventRef.get();
const eventSnapshotData = eventSnapshot.data();
return eventSnapshotData;
} catch (error) {
logger.error("Error in getting event by id from the database.", error);
throw error;
}
};
const getEventCodes = async ({ id }) => {
try {
const eventData = await getEventById({ id });
if (!eventData) {
throw new Error("Event not found with this id!");
}
const eventCodesArrayPromises = eventData?.event_codes?.by_role?.mavens.map(async (codeId) => {
const eventCodeRef = eventCodeModel.doc(codeId);
const eventCodeSnapshot = await eventCodeRef.get();
const eventCodeSnapshotData = eventCodeSnapshot.data();
return eventCodeSnapshotData;
});
if (!eventCodesArrayPromises) {
throw new Error("Event codes not found with this event id!");
}
const eventCodesArray = await Promise.all(eventCodesArrayPromises);
return eventCodesArray;
} catch (error) {
logger.error("Error in getting event by id from the database.", error);
throw error;
}
};
module.exports = {
createEvent,
updateEvent,
endActiveEvent,
addPeerToEvent,
kickoutPeer,
createEventCode,
getEventById,
getEventCodes,
getPeerById,
};