@@ -9,6 +9,7 @@ import InterviewAssessment from "../models/InterviewAssessmentStageSchema";
9
9
import TechnicalAssessment from "../models/technicalAssessmentStage" ;
10
10
import mongoose from "mongoose" ;
11
11
import { traineEAttributes } from "../models/traineeAttribute" ;
12
+ import { LoggedUserModel } from "../models/AuthUser" ;
12
13
13
14
const validStages = [
14
15
"Shortlisted" ,
@@ -93,28 +94,37 @@ export const applicationStageResolvers: any = {
93
94
getTraineeCyclesApplications : async ( _ : any , __ : any , context : any ) => {
94
95
try {
95
96
if ( ! context . currentUser ) {
96
- throw new CustomGraphQLError ( "You must be logged in to view your applications" ) ;
97
+ throw new CustomGraphQLError (
98
+ "You must be logged in to view your applications"
99
+ ) ;
97
100
}
98
101
99
- const applications = await TraineeApplicant . findOne ( { email : context . currentUser . email } )
102
+ const applications = await TraineeApplicant . findOne ( {
103
+ email : context . currentUser . email ,
104
+ } )
100
105
. populate ( "cycle_id" )
101
106
. lean ( ) ;
102
- return applications
103
- }
104
- catch ( error : any ) {
107
+ return applications ;
108
+ } catch ( error : any ) {
105
109
console . error ( "Error retrieving applications with attributes:" , error ) ;
106
110
throw new CustomGraphQLError ( error ) ;
107
111
}
108
112
} ,
109
- getApplicationsAttributes : async ( _ : any , { trainee_id } : any , context : any ) => {
113
+ getApplicationsAttributes : async (
114
+ _ : any ,
115
+ { trainee_id } : any ,
116
+ context : any
117
+ ) => {
110
118
try {
111
119
if ( ! context . currentUser ) {
112
- throw new CustomGraphQLError ( "You must be logged in to view your applications" ) ;
120
+ throw new CustomGraphQLError (
121
+ "You must be logged in to view your applications"
122
+ ) ;
113
123
}
114
124
115
125
const attributes = await traineEAttributes . findOne ( { trainee_id } ) ;
116
126
117
- return attributes
127
+ return attributes ;
118
128
} catch ( error ) {
119
129
console . error ( "Error getting attributes:" , error ) ;
120
130
throw new CustomGraphQLError ( error ) ;
@@ -123,12 +133,21 @@ export const applicationStageResolvers: any = {
123
133
getApplicationStages : async ( _ : any , { trainee_id } : any , context : any ) => {
124
134
try {
125
135
if ( ! context . currentUser ) {
126
- throw new CustomGraphQLError ( "You must be logged in to view your applications" ) ;
136
+ throw new CustomGraphQLError (
137
+ "You must be logged in to view your applications"
138
+ ) ;
127
139
}
128
140
129
141
const trainee = new mongoose . Types . ObjectId ( trainee_id ) ;
130
142
131
- const [ shortlistStage , technicalStage , interviewStage , admittedStage , dismissedStage , AllStages ] = await Promise . all ( [
143
+ const [
144
+ shortlistStage ,
145
+ technicalStage ,
146
+ interviewStage ,
147
+ admittedStage ,
148
+ dismissedStage ,
149
+ AllStages ,
150
+ ] = await Promise . all ( [
132
151
Shortlisted . findOne ( { applicantId : trainee } ) ,
133
152
TechnicalAssessment . findOne ( { applicantId : trainee } ) ,
134
153
InterviewAssessment . findOne ( { applicantId : trainee } ) ,
@@ -143,15 +162,15 @@ export const applicationStageResolvers: any = {
143
162
interview : interviewStage ,
144
163
admitted : admittedStage ,
145
164
dismissed : dismissedStage ,
146
- allStages : AllStages
147
- }
148
-
149
-
165
+ allStages : AllStages ,
166
+ } ;
150
167
} catch ( error ) {
151
168
console . error ( "Error retrieving application stages:" , error ) ;
152
- throw new CustomGraphQLError ( error || "An error occurred while retrieving applications" ) ;
169
+ throw new CustomGraphQLError (
170
+ error || "An error occurred while retrieving applications"
171
+ ) ;
153
172
}
154
- }
173
+ } ,
155
174
} ,
156
175
Mutation : {
157
176
moveToNextStage : async (
@@ -241,6 +260,15 @@ export const applicationStageResolvers: any = {
241
260
await stageTracking . save ( ) ;
242
261
}
243
262
263
+ let traineeRole = await RoleModel . findOne ( { roleName : "trainee" } ) ;
264
+ if ( ! traineeRole ) {
265
+ traineeRole = await RoleModel . create ( {
266
+ roleName : "trainee" ,
267
+ description : "A user who has been accepted as a trainee" ,
268
+ permissions : [ ] ,
269
+ } ) ;
270
+ }
271
+
244
272
let message = "" ;
245
273
switch ( nextStage ) {
246
274
case "Technical Assessment" :
@@ -294,19 +322,52 @@ export const applicationStageResolvers: any = {
294
322
) ;
295
323
}
296
324
297
- await Promise . all ( models . map ( model => updateApplicantAfterAdmitted ( model , applicantId ) ) ) ;
325
+ await Promise . all (
326
+ models . map ( ( model ) =>
327
+ updateApplicantAfterAdmitted ( model , applicantId )
328
+ )
329
+ ) ;
298
330
299
331
await Admitted . create ( {
300
332
applicantId,
301
333
comments,
302
334
status : "Passed" ,
303
335
} ) ;
304
336
message = `Applicant passed the application stage✅.` ;
337
+
305
338
await TraineeApplicant . updateOne (
306
339
{ _id : applicantId } ,
307
- { $set : { applicationPhase : nextStage , status : "Admitted" } }
340
+ {
341
+ $set : {
342
+ applicationPhase : nextStage ,
343
+ status : "Admitted" ,
344
+ role : traineeRole . _id ,
345
+ } ,
346
+ }
308
347
) ;
309
348
349
+ const updatedApplicant = await TraineeApplicant . findOne ( {
350
+ _id : applicantId ,
351
+ } )
352
+ . populate ( "email" )
353
+ . lean ( ) ;
354
+
355
+ const email = updatedApplicant ?. email ;
356
+
357
+ if ( email ) {
358
+ await LoggedUserModel . updateOne (
359
+ { email } ,
360
+ {
361
+ $set : {
362
+ applicationPhase : nextStage ,
363
+ status : "Admitted" ,
364
+ role : traineeRole . _id ,
365
+ } ,
366
+ }
367
+ ) ;
368
+ } else {
369
+ throw new Error ( "Email not found for the provided applicant ID" ) ;
370
+ }
310
371
break ;
311
372
312
373
case "Rejected" :
@@ -316,7 +377,11 @@ export const applicationStageResolvers: any = {
316
377
{ $set : { status : "Rejected" , exitedAt : new Date ( ) } }
317
378
) ;
318
379
}
319
- await Promise . all ( models . map ( model => updateApplicantAfterDismissed ( model , applicantId ) ) ) ;
380
+ await Promise . all (
381
+ models . map ( ( model ) =>
382
+ updateApplicantAfterDismissed ( model , applicantId )
383
+ )
384
+ ) ;
320
385
const stageDismissedFrom = await TraineeApplicant . findOne ( {
321
386
_id : applicantId ,
322
387
} ) ;
@@ -420,4 +485,4 @@ export const applicationStageResolvers: any = {
420
485
}
421
486
} ,
422
487
} ,
423
- } ;
488
+ } ;
0 commit comments