-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparticipant_connections_controller.ts
More file actions
297 lines (252 loc) · 11.1 KB
/
participant_connections_controller.ts
File metadata and controls
297 lines (252 loc) · 11.1 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
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
// controllers/participant_connections_controller.ts
import { Request, Response } from "express";
import { Types } from "mongoose";
import { check_req_fields } from "../utils/requests_utils";
import { User } from "../models/user_model";
import { Participant } from "../models/participant_model";
import { ParticipantConnection } from "../models/participant_connection_model";
/**
* POST /api/participantConnections
* Create a new ParticipantConnection document.
* The server generates the ParticipantConnection `_id` automatically (pre-save hook).
*
* @param req.body._eventId - MongoDB ObjectId of the event (required)
* @param req.body.primaryParticipantId - MongoDB ObjectId of the primary participant (required)
* @param req.body.secondaryParticipantId - MongoDB ObjectId of the secondary participant (required)
* @param req.body.description - Optional description for the connection, can be the bingo question the participants connected with (optional)
*
* Behavior notes:
* - Validates ObjectId format before hitting the DB
* - Ensures BOTH participants exist AND belong to the given event
* - Prevents duplicates with exact same (_eventId, primaryParticipantId, secondaryParticipantId)
*
* @returns 201 - Created ParticipantConnection document
* @returns 400 - Missing required fields or invalid ObjectId format
* @returns 404 - Primary participant or secondary participant not found for this event
* @returns 409 - ParticipantConnection already exists for this event and participants
* @returns 500 - Internal server error
*/
export async function createParticipantConnection(req: Request, res: Response) {
try {
const requiredFields = ["_eventId", "primaryParticipantId", "secondaryParticipantId"];
if (!check_req_fields(req, requiredFields)) {
return res.status(400).json({ error: "Missing required fields" });
}
const { _eventId, primaryParticipantId, secondaryParticipantId, description } = req.body;
// Validate ObjectId format before hitting the DB
const idsToValidate = { _eventId, primaryParticipantId, secondaryParticipantId };
for (const [key, value] of Object.entries(idsToValidate)) {
if (!Types.ObjectId.isValid(value)) {
return res.status(400).json({ error: `Invalid ${key}` });
}
}
// Ensure both participants exist AND belong to the event
const [primaryParticipant, secondaryParticipant] = await Promise.all([
Participant.findOne({ _id: primaryParticipantId, eventId: _eventId }).select("_id"),
Participant.findOne({ _id: secondaryParticipantId, eventId: _eventId }).select("_id"),
]);
if (!primaryParticipant) {
return res.status(404).json({ error: "Primary participant not found for this event" });
}
if (!secondaryParticipant) {
return res.status(404).json({ error: "Secondary participant not found for this event" });
}
// Prevent duplicates with exact same (_eventId, primaryParticipantId, secondaryParticipantId)
const existing = await ParticipantConnection.findOne({
_eventId,
primaryParticipantId,
secondaryParticipantId,
});
if (existing) {
return res.status(409).json({
error: "ParticipantConnection already exists for this event and participants",
existingConnection: existing,
});
}
const newConnection = await ParticipantConnection.create({
_eventId,
primaryParticipantId,
secondaryParticipantId,
description,
});
return res.status(201).json(newConnection);
} catch (_error) {
return res.status(500).json({ error: "Internal server error" });
}
}
/**
* POST /api/participantConnections/by-emails
* Create a new ParticipantConnection by providing USER emails.
*
* Looks up users by email, then finds their Participant records for the given event,
* then saves the connection using the participants' MongoDB ObjectIds.
* The server generates the ParticipantConnection `_id` automatically (pre-save hook).
*
* @param req.body._eventId - MongoDB ObjectId of the event (required)
* @param req.body.primaryUserEmail - Email of the primary user (required)
* @param req.body.secondaryUserEmail - Email of the secondary user (required)
* @param req.body.description - Optional description for the connection (optional)
*
* Behavior notes:
* - Validates _eventId ObjectId format
* - Validates emails + prevents same email provided twice
* - Maps email -> User -> Participant (for that event)
* - Prevents duplicates with exact same (_eventId, primaryParticipantId, secondaryParticipantId)
*
* @returns 201 - Created ParticipantConnection document
* @returns 400 - Missing required fields, invalid ObjectId, invalid emails, or same email provided twice
* @returns 404 - Primary/secondary user not found OR participant not found for this event
* @returns 409 - ParticipantConnection already exists for this event and participants
* @returns 500 - Internal server error
*/
export async function createParticipantConnectionByEmails(req: Request, res: Response) {
try {
const requiredFields = ["_eventId", "primaryUserEmail", "secondaryUserEmail"];
if (!check_req_fields(req, requiredFields)) {
return res.status(400).json({ error: "Missing required fields" });
}
const { _eventId, primaryUserEmail, secondaryUserEmail, description } = req.body;
if (!Types.ObjectId.isValid(_eventId)) {
return res.status(400).json({ error: "Invalid _eventId" });
}
const primaryEmail = String(primaryUserEmail).trim().toLowerCase();
const secondaryEmail = String(secondaryUserEmail).trim().toLowerCase();
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
if (!emailRegex.test(primaryEmail)) {
return res.status(400).json({ error: "Invalid primaryUserEmail" });
}
if (!emailRegex.test(secondaryEmail)) {
return res.status(400).json({ error: "Invalid secondaryUserEmail" });
}
if (primaryEmail === secondaryEmail) {
return res.status(400).json({ error: "primaryUserEmail and secondaryUserEmail must be different" });
}
// Find users by email
const [primaryUser, secondaryUser] = await Promise.all([
User.findOne({ email: primaryEmail }).select("_id"),
User.findOne({ email: secondaryEmail }).select("_id"),
]);
if (!primaryUser) return res.status(404).json({ error: "Primary user not found" });
if (!secondaryUser) return res.status(404).json({ error: "Secondary user not found" });
// Map User -> Participant (for the event)
const [primaryParticipant, secondaryParticipant] = await Promise.all([
Participant.findOne({ eventId: _eventId, userId: primaryUser._id }).select("_id"),
Participant.findOne({ eventId: _eventId, userId: secondaryUser._id }).select("_id"),
]);
if (!primaryParticipant) {
return res.status(404).json({ error: "Primary participant not found for this event (by user email)" });
}
if (!secondaryParticipant) {
return res.status(404).json({ error: "Secondary participant not found for this event (by user email)" });
}
// Prevent duplicates with exact same (_eventId, primaryParticipantId, secondaryParticipantId)
const existing = await ParticipantConnection.findOne({
_eventId,
primaryParticipantId: primaryParticipant._id,
secondaryParticipantId: secondaryParticipant._id,
});
if (existing) {
return res.status(409).json({
error: "ParticipantConnection already exists for this event and participants",
existingConnection: existing,
});
}
const newConnection = await ParticipantConnection.create({
_eventId,
primaryParticipantId: primaryParticipant._id,
secondaryParticipantId: secondaryParticipant._id,
description,
});
return res.status(201).json(newConnection);
} catch (_error) {
return res.status(500).json({ error: "Internal server error" });
}
}
/**
* DELETE /api/participantConnections/delete
*
* Deletes a participant connection only if it belongs to the given event.
*
* @param req.body.eventId - MongoDB ObjectId of the event (required)
* @param req.body.connectionId - ParticipantConnection string _id (required)
*
* @returns 200 - Deleted connection
* @returns 400 - Missing/invalid body params
* @returns 404 - ParticipantConnection not found for this event
* @returns 500 - Internal server error
*/
export async function deleteParticipantConnection(req: Request, res: Response) {
try {
const { eventId, connectionId } = req.body;
if (!eventId || !Types.ObjectId.isValid(eventId)) {
return res.status(400).json({ error: "Invalid eventId" });
}
if (!connectionId || typeof connectionId !== "string") {
return res.status(400).json({ error: "Invalid connectionId" });
}
const deleted = await ParticipantConnection.findOneAndDelete({
_id: connectionId,
_eventId: eventId,
});
if (!deleted) {
return res.status(404).json({ error: "ParticipantConnection not found for this event" });
}
return res.status(200).json({
message: "ParticipantConnection deleted successfully",
deletedConnection: deleted,
});
} catch (_error) {
return res.status(500).json({ error: "Internal server error" });
}
}
export async function getConnectionsByParticipantAndEvent(req: Request, res: Response) {
try {
const { eventId, participantId } = req.query;
if (!eventId || typeof eventId !== "string" || !Types.ObjectId.isValid(eventId)) {
return res.status(400).json({ error: "Invalid eventId" });
}
if (!participantId || typeof participantId !== "string" || !Types.ObjectId.isValid(participantId)) {
return res.status(400).json({ error: "Invalid participantId" });
}
const connections = await ParticipantConnection.find({
_eventId: eventId,
$or: [
{ primaryParticipantId: participantId },
{ secondaryParticipantId: participantId },
],
});
return res.status(200).json(connections);
} catch (_error) {
return res.status(500).json({ error: "Internal server error" });
}
}
export async function getConnectionsByUserEmailAndEvent(req: Request, res: Response) {
try {
const { eventId, userEmail } = req.query;
if (!eventId || typeof eventId !== "string" || !Types.ObjectId.isValid(eventId)) {
return res.status(400).json({ error: "Invalid eventId" });
}
if (!userEmail || typeof userEmail !== "string") {
return res.status(400).json({ error: "Invalid userEmail" });
}
const email = userEmail.trim().toLowerCase();
const user = await User.findOne({ email }).select("_id");
if (!user) {
return res.status(400).json({ error: "Invalid userEmail" });
}
const participant = await Participant.findOne({ eventId, userId: user._id }).select("_id");
if (!participant) {
return res.status(404).json({ error: "Participant not found for this event (by user email)" });
}
const connections = await ParticipantConnection.find({
_eventId: eventId,
$or: [
{ primaryParticipantId: participant._id },
{ secondaryParticipantId: participant._id }
],
});
return res.status(200).json(connections);
} catch (_error) {
return res.status(500).json({ error: "Internal server error" });
}
}