1
1
import TraineeApplicant from "../models/traineeApplicant" ;
2
2
import { traineEAttributes } from "../models/traineeAttribute" ;
3
- import { applicationCycle } from "../models/applicationCycle" ;
3
+ import { applicationCycle } from "../models/applicationCycle" ;
4
4
import mongoose , { ObjectId } from "mongoose" ;
5
5
import { sendEmailTemplate } from "../helpers/bulkyMails" ;
6
6
import { Types } from 'mongoose' ;
7
7
import { AuthenticationError } from 'apollo-server' ;
8
8
import { LoggedUserModel } from "../models/AuthUser" ;
9
9
import { RoleModel } from "../models/roleModel" ;
10
10
11
- const FrontendUrl = process . env . FRONTEND_URL || ""
11
+ const FrontendUrl = process . env . FRONTEND_URL || "" ;
12
12
13
13
import { CustomGraphQLError } from "../utils/customErrorHandler" ;
14
14
import { cohortModels } from "../models/cohortModel" ;
15
15
import { publishNotification } from "./adminNotificationsResolver" ;
16
16
import { any } from "joi" ;
17
17
18
+
18
19
interface Context {
19
20
currentUser : { _id : string } ;
20
21
}
@@ -41,24 +42,24 @@ export const traineeApplicantResolver: any = {
41
42
items = 3 ;
42
43
}
43
44
}
44
-
45
+
45
46
const itemsToSkip = ( pages - 1 ) * items ;
46
47
const allTrainee = await TraineeApplicant . find ( { delete_at : false } )
47
48
. populate ( "cycle_id" )
48
-
49
+
49
50
. skip ( itemsToSkip )
50
51
. limit ( items ) ;
51
52
52
- const formattedTrainees = allTrainee . map ( ( trainee ) => ( {
53
- ...trainee . toObject ( ) ,
54
- createdAt : trainee . createdAt . toLocaleString ( ) , // Format createdAt as ISO string
55
- } ) ) ;
56
- return {
57
- data : formattedTrainees ,
58
- totalItems,
59
- page : pages ,
60
- itemsPerPage : items ,
61
- } ;
53
+ const formattedTrainees = allTrainee . map ( ( trainee ) => ( {
54
+ ...trainee . toObject ( ) ,
55
+ createdAt : trainee . createdAt . toLocaleString ( ) , // Format createdAt as ISO string
56
+ } ) ) ;
57
+ return {
58
+ data : formattedTrainees ,
59
+ totalItems,
60
+ page : pages ,
61
+ itemsPerPage : items ,
62
+ } ;
62
63
} ,
63
64
64
65
async getOneTrainee ( _ : any , { ID } : any ) {
@@ -68,15 +69,15 @@ export const traineeApplicantResolver: any = {
68
69
return trainee ;
69
70
} ,
70
71
71
- async getTraineeByUserId ( _ : any , { userId } : any ) {
72
- const trainee = await TraineeApplicant . findOne ( { user : userId } )
73
-
74
- if ( ! trainee ) {
75
- throw new Error ( ' Trainee not found' ) ;
76
- }
72
+ async getTraineeByUserId ( _ : any , { userId } : any ) {
73
+ const trainee = await TraineeApplicant . findOne ( { user : userId } ) ;
74
+
75
+ if ( ! trainee ) {
76
+ throw new Error ( " Trainee not found" ) ;
77
+ }
77
78
78
- return trainee . _id ;
79
- }
79
+ return trainee . _id ;
80
+ } ,
80
81
} ,
81
82
82
83
Mutation : {
@@ -126,9 +127,12 @@ export const traineeApplicantResolver: any = {
126
127
return false ;
127
128
}
128
129
} ,
129
- async createNewTraineeApplicant ( _ :any , { input } :any ) {
130
+ async createNewTraineeApplicant ( _ : any , { input } : any , context : any ) {
130
131
const { lastName, firstName, email, cycle_id, attributes } = input ;
131
-
132
+ const userWithRole = await LoggedUserModel . findById (
133
+ context . currentUser ?. _id
134
+ ) . populate ( "role" ) ;
135
+
132
136
// Validate email
133
137
const validateEmail = ( email : string ) => {
134
138
return String ( email )
@@ -137,27 +141,35 @@ export const traineeApplicantResolver: any = {
137
141
/ ^ ( ( [ ^ < > ( ) [ \] \\ . , ; : \s @ " ] + ( \. [ ^ < > ( ) [ \] \\ . , ; : \s @ " ] + ) * ) | ( " .+ " ) ) @ ( ( \[ [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \] ) | ( ( [ a - z A - Z \- 0 - 9 ] + \. ) + [ a - z A - Z ] { 2 , } ) ) $ /
138
142
) ;
139
143
} ;
140
-
144
+
141
145
if ( ! validateEmail ( email ) ) {
142
- throw new Error ( "This email is not valid. Please provide a valid email." ) ;
146
+ throw new Error (
147
+ "This email is not valid. Please provide a valid email."
148
+ ) ;
143
149
}
144
-
150
+
145
151
const session = await mongoose . startSession ( ) ;
146
152
session . startTransaction ( ) ;
147
153
148
154
try {
149
- const cycle = await applicationCycle . findById ( cycle_id ) . session ( session ) ;
155
+ const cycle = await applicationCycle
156
+ . findById ( cycle_id )
157
+ . session ( session ) ;
150
158
if ( ! cycle ) {
151
159
throw new Error ( "Application cycle not found" ) ;
152
160
}
153
161
154
- const existingTrainee = await TraineeApplicant . findOne ( { email } ) . session ( session ) ;
162
+ const existingTrainee = await TraineeApplicant . findOne ( {
163
+ email,
164
+ } ) . session ( session ) ;
155
165
if ( existingTrainee ) {
156
166
const existingApplication = existingTrainee . cycleApplied . find (
157
- ( app : any ) => app . cycle . toString ( ) === cycle_id
167
+ ( app : any ) => app . cycle . toString ( ) === cycle_id
158
168
) ;
159
169
if ( existingApplication ) {
160
- throw new Error ( "You have already applied to this application cycle" ) ;
170
+ throw new Error (
171
+ "You have already applied to this application cycle"
172
+ ) ;
161
173
}
162
174
163
175
existingTrainee . cycle_id = cycle_id ;
@@ -166,18 +178,35 @@ export const traineeApplicantResolver: any = {
166
178
} ) ;
167
179
await existingTrainee . save ( { session } ) ;
168
180
await session . commitTransaction ( ) ;
169
- return existingTrainee ;
181
+ //populating traineeApplicant with cycle_id
182
+ return await TraineeApplicant . findById ( existingTrainee . _id ) . populate (
183
+ "cycle_id"
184
+ ) ;
170
185
}
171
186
172
187
const newTrainee = new TraineeApplicant ( {
173
188
lastName,
174
189
firstName,
175
190
email,
176
191
cycle_id,
177
- cycleApplied : [ {
178
- cycle : cycle_id ,
179
- } ]
192
+ cycleApplied : [
193
+ {
194
+ cycle : cycle_id ,
195
+ } ,
196
+ ] ,
180
197
} ) ;
198
+ // Create the corresponding traineEAttributes
199
+ if (
200
+ userWithRole &&
201
+ ( ( userWithRole . role as any ) ?. roleName === "admin" ||
202
+ ( userWithRole . role as any ) ?. roleName === "superAdmin" )
203
+ ) {
204
+ const newTraineeAttributes = new traineEAttributes ( {
205
+ trainee_id : newTrainee . _id ,
206
+ ...attributes ,
207
+ } ) ;
208
+ await newTraineeAttributes . save ( { session } ) ;
209
+ }
181
210
182
211
await newTrainee . save ( { session } ) ;
183
212
await session . commitTransaction ( ) ;
@@ -187,9 +216,11 @@ export const traineeApplicantResolver: any = {
187
216
) ;
188
217
const result = {
189
218
...newTrainee . toObject ( ) ,
190
- createdAt : newTrainee . createdAt . toLocaleString ( )
219
+ createdAt : newTrainee . createdAt . toLocaleString ( ) ,
191
220
} ;
192
- return result ;
221
+
222
+ //return populated traineeApplicant
223
+ return await TraineeApplicant . findById ( result . _id ) . populate ( "cycle_id" ) ;
193
224
} catch ( error ) {
194
225
await session . abortTransaction ( ) ;
195
226
throw error ;
@@ -241,10 +272,9 @@ export const traineeApplicantResolver: any = {
241
272
await cohort . save ( ) ;
242
273
243
274
return { success : true , message : "Trainee accepted successfully" } ;
244
-
245
275
} catch ( error ) {
246
276
throw new CustomGraphQLError ( `Failed to accept trainee: ${ error } ` ) ;
247
277
}
248
- }
278
+ } ,
249
279
} ,
250
- } ;
280
+ } ;
0 commit comments