@@ -10,37 +10,82 @@ const getEventsName = async (req, res) => {
10
10
const collegeName = req . body . collegeName ;
11
11
const college = await CollegeModel . findOne ( { name : collegeName } ) ;
12
12
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" } ) ;
17
14
}
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 ;
24
26
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
+ } ) ;
27
75
}
28
- const eventData = {
29
- name : event . name ,
30
- id : event . _id ,
31
- } ;
32
- eventDetails . push ( eventData ) ;
33
76
}
77
+
34
78
return res . status ( 200 ) . json ( {
35
79
status : 200 ,
36
80
message : "Success" ,
37
- eventDetails,
81
+ data : {
82
+ collegeId : college . _id ,
83
+ collegeName : college . name ,
84
+ events : eventDetails
85
+ }
38
86
} ) ;
39
87
} 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 } ) ;
44
89
}
45
90
} ;
46
91
@@ -142,7 +187,7 @@ const slotCollegeById = async (req, res) => {
142
187
if ( finalStatus === 200 ) {
143
188
return res . status ( 200 ) . json ( {
144
189
status : 200 ,
145
- message : "Slotting completed for all teams from college" ,
190
+ message : "Slotting completed for all teams from college ✅ " ,
146
191
} ) ;
147
192
} else if ( finalStatus === 404 ) {
148
193
return res . status ( 404 ) . json ( {
0 commit comments