Skip to content

Commit adde7c0

Browse files
authored
Merge pull request #131 from manipalutsav/feature/get-unslotted-events
2 parents 916c737 + d71fca5 commit adde7c0

File tree

1 file changed

+68
-23
lines changed

1 file changed

+68
-23
lines changed

src/controllers/slotting.js

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,82 @@ const getEventsName = async (req, res) => {
1010
const collegeName = req.body.collegeName;
1111
const college = await CollegeModel.findOne({ name: collegeName });
1212
if (!college) {
13-
return res.status(404).json({
14-
status: 404,
15-
message: "Not Found. No college was found for the specified name.",
16-
});
13+
return res.status(404).json({ status: 404, message: "College not found" });
1714
}
18-
const events = await TeamModel.find({ college: college._id });
19-
const eventIds = events.map((event) => event.event);
20-
let eventDetails = [];
21-
let eventData = {};
22-
for (let eventId of eventIds) {
23-
// console.log(eventId);
15+
16+
// Get unique event IDs using aggregation
17+
const events = await TeamModel.aggregate([
18+
{ $match: { college: college._id } },
19+
{ $group: { _id: "$event" } }
20+
]);
21+
22+
const eventDetails = [];
23+
24+
for (const eventObj of events) {
25+
const eventId = eventObj._id;
2426
const event = await EventModel.findById(eventId);
25-
if(event.endDate < Date.now()){
26-
continue;
27+
28+
if (!event || event.endDate < Date.now()) continue;
29+
30+
const rounds = await RoundModel.find({ event: eventId });
31+
const teams = await TeamModel.find({
32+
college: college._id,
33+
event: eventId
34+
});
35+
36+
const unslottedTeams = [];
37+
38+
for (const team of teams) {
39+
const missingRounds = [];
40+
41+
const slotChecks = await Promise.all(rounds.map(async (round) => {
42+
const exists = await Slot2Model.exists({
43+
round: round._id,
44+
college: college._id,
45+
teamIndex: team.index
46+
});
47+
48+
if (!exists) missingRounds.push({
49+
roundId: round._id,
50+
roundName: round.name
51+
});
52+
53+
return exists;
54+
}));
55+
56+
if (missingRounds.length > 0) {
57+
unslottedTeams.push({
58+
teamId: team._id,
59+
teamIndex: team.index,
60+
missingRounds,
61+
fullySlotted: false
62+
});
63+
}
64+
}
65+
66+
// Only add event if it has unslotted teams
67+
if (unslottedTeams.length > 0) {
68+
eventDetails.push({
69+
id: event._id,
70+
name: event.name,
71+
unslottedTeams,
72+
totalTeams: teams.length,
73+
slottedTeams: teams.length - unslottedTeams.length
74+
});
2775
}
28-
const eventData = {
29-
name: event.name,
30-
id: event._id,
31-
};
32-
eventDetails.push(eventData);
3376
}
77+
3478
return res.status(200).json({
3579
status: 200,
3680
message: "Success",
37-
eventDetails,
81+
data: {
82+
collegeId: college._id,
83+
collegeName: college.name,
84+
events: eventDetails
85+
}
3886
});
3987
} catch (err) {
40-
return res.status(500).json({
41-
status: 500,
42-
message: err.message,
43-
});
88+
return res.status(500).json({ status: 500, message: err.message });
4489
}
4590
};
4691

@@ -142,7 +187,7 @@ const slotCollegeById = async (req, res) => {
142187
if (finalStatus === 200) {
143188
return res.status(200).json({
144189
status: 200,
145-
message: "Slotting completed for all teams from college",
190+
message: "Slotting completed for all teams from college",
146191
});
147192
} else if (finalStatus === 404) {
148193
return res.status(404).json({

0 commit comments

Comments
 (0)