@@ -7,9 +7,12 @@ import Rejected from "../models/dismissedStageSchema";
7
7
import Admitted from "../models/admittedStageSchema" ;
8
8
import InterviewAssessment from "../models/InterviewAssessmentStageSchema" ;
9
9
import TechnicalAssessment from "../models/technicalAssessmentStage" ;
10
- import mongoose from "mongoose " ;
11
- import { traineEAttributes } from "../models/traineeAttribute " ;
10
+ import { sendEmailTemplate } from "../helpers/bulkyMails " ;
11
+ import { ApplicantNotificationsModel } from "../models/applicantNotifications " ;
12
12
import { LoggedUserModel } from "../models/AuthUser" ;
13
+ import { pusher } from "../helpers/pusher" ;
14
+ import { traineEAttributes } from "../models/traineeAttribute" ;
15
+ import mongoose from "mongoose" ;
13
16
14
17
const validStages = [
15
18
"Shortlisted" ,
@@ -31,7 +34,7 @@ async function getApplicantsByModel(model: any) {
31
34
async function updateApplicantAfterDismissed ( model : any , applicantId : string ) {
32
35
await model . updateOne ( { applicantId } , { $set : { status : "Rejected" } } ) ;
33
36
}
34
- async function updateApplicantAfterAdmitted ( model : any , applicantId : string ) {
37
+ async function updateApplicantAfterAdmitted ( model : any , applicantId :string ) {
35
38
await model . updateOne ( { applicantId } , { $set : { status : "Admitted" } } ) ;
36
39
}
37
40
export const applicationStageResolvers : any = {
@@ -179,6 +182,9 @@ export const applicationStageResolvers: any = {
179
182
context : any
180
183
) => {
181
184
try {
185
+ const applicant = await TraineeApplicant . findById ( applicantId ) ;
186
+ const user = await LoggedUserModel . findOne ( { email : applicant ! . email } ) ;
187
+
182
188
if ( ! context . currentUser ) {
183
189
throw new CustomGraphQLError (
184
190
"You must be logged in to perform this action"
@@ -208,6 +214,8 @@ export const applicationStageResolvers: any = {
208
214
) ;
209
215
}
210
216
217
+ let scoreDetails = "" ;
218
+
211
219
if ( nextStage === "Interview Assessment" ) {
212
220
const technicalScore = await TechnicalAssessment . findOne ( {
213
221
applicantId,
@@ -221,6 +229,7 @@ export const applicationStageResolvers: any = {
221
229
"Technical assessment score is required before moving to the Interview Assessment, Please add score😎."
222
230
) ;
223
231
}
232
+ scoreDetails = `Your Technical Assessment Score: ${ technicalScore . score } ` ;
224
233
}
225
234
226
235
// If moving to Admitted, ensure Interview Assessment has a score
@@ -237,6 +246,7 @@ export const applicationStageResolvers: any = {
237
246
"Interview assessment score is required before moving to Admitted, Please add score😎."
238
247
) ;
239
248
}
249
+ scoreDetails = `Your Interview Assessment Score: ${ interviewScore . interviewScore } ` ;
240
250
}
241
251
242
252
if ( ! stageTracking ) {
@@ -291,8 +301,36 @@ export const applicationStageResolvers: any = {
291
301
await TraineeApplicant . updateOne (
292
302
{ _id : applicantId } ,
293
303
{ $set : { applicationPhase : nextStage , status : "No action" } }
304
+ ) ;
305
+ message = `You have advanced to the ${ nextStage } stage.` ;
306
+ const notification = await ApplicantNotificationsModel . create ( {
307
+ userId : user ! . _id ,
308
+ message,
309
+ eventType : "general" ,
310
+ } ) ;
311
+
312
+ await sendEmailTemplate (
313
+ user ! . email ,
314
+ "Application Update" ,
315
+ `Hello ${ user ! . email . split ( "@" ) [ 0 ] } , ` ,
316
+ `Your application has been moved to ${ nextStage } stage.
317
+ <br />
318
+ You will hear from us very soon.
319
+ <br />
320
+ Thank you for your patience.
321
+ `
294
322
) ;
295
- message = `Applicant advanced to ${ nextStage } stage.` ;
323
+
324
+ await pusher
325
+ . trigger ( `notifications-${ user ! . _id } ` , "new-notification" , {
326
+ message : notification . message ,
327
+ id : notification . _id ,
328
+ createdAt : notification . createdAt ,
329
+ read : notification . read ,
330
+ } )
331
+ . catch ( ( error ) => {
332
+ console . error ( "Error with Pusher trigger:" , error ) ;
333
+ } ) ;
296
334
break ;
297
335
298
336
case "Interview Assessment" :
@@ -311,7 +349,38 @@ export const applicationStageResolvers: any = {
311
349
comments,
312
350
status : "No action" ,
313
351
} ) ;
314
- message = `Applicant advanced to ${ nextStage } stage.` ;
352
+ message = `You have advanced to the ${ nextStage } stage.` ;
353
+ const notification1 = await ApplicantNotificationsModel . create ( {
354
+ userId : user ! . _id ,
355
+ message,
356
+ eventType : "general" ,
357
+ } ) ;
358
+
359
+ await sendEmailTemplate (
360
+ user ! . email ,
361
+ "Application Update" ,
362
+ `Hello ${ user ! . email . split ( "@" ) [ 0 ] } , ` ,
363
+ `Your application has been moved to ${ nextStage } stage.
364
+ <br />
365
+ ${ scoreDetails }
366
+ <br />
367
+ <br />
368
+ You will hear from us very soon.
369
+ <br />
370
+ Thank you for your patience.
371
+ `
372
+ ) ;
373
+
374
+ await pusher
375
+ . trigger ( `notifications-${ user ! . _id } ` , "new-notification" , {
376
+ message : notification1 . message ,
377
+ id : notification1 . _id ,
378
+ createdAt : notification1 . createdAt ,
379
+ read : notification1 . read ,
380
+ } )
381
+ . catch ( ( error ) => {
382
+ console . error ( "Error with Pusher trigger:" , error ) ;
383
+ } ) ;
315
384
break ;
316
385
317
386
case "Admitted" :
@@ -333,8 +402,7 @@ export const applicationStageResolvers: any = {
333
402
comments,
334
403
status : "Passed" ,
335
404
} ) ;
336
- message = `Applicant passed the application stage✅.` ;
337
-
405
+ message = `You have passed the application stage✅.` ;
338
406
await TraineeApplicant . updateOne (
339
407
{ _id : applicantId } ,
340
408
{
@@ -345,29 +413,38 @@ export const applicationStageResolvers: any = {
345
413
} ,
346
414
}
347
415
) ;
416
+
417
+ const notification2 = await ApplicantNotificationsModel . create ( {
418
+ userId : user ! . _id ,
419
+ message,
420
+ eventType : "general" ,
421
+ } ) ;
348
422
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
- }
423
+ await sendEmailTemplate (
424
+ user ! . email ,
425
+ "Application Update" ,
426
+ `Hello ${ user ! . email . split ( "@" ) [ 0 ] } , ` ,
427
+ `Your application has successfully passed the application stage.
428
+ <br />
429
+ ${ scoreDetails }
430
+ <br />
431
+ <br />
432
+ You will hear from us very soon.
433
+ <br />
434
+ Thank you for your patience.
435
+ `
436
+ ) ;
437
+
438
+ await pusher
439
+ . trigger ( `notifications-${ user ! . _id } ` , "new-notification" , {
440
+ message : notification2 . message ,
441
+ id : notification2 . _id ,
442
+ createdAt : notification2 . createdAt ,
443
+ read : notification2 . read ,
444
+ } )
445
+ . catch ( ( error ) => {
446
+ console . error ( "Error with Pusher trigger:" , error ) ;
447
+ } ) ;
371
448
break ;
372
449
373
450
case "Rejected" :
@@ -394,7 +471,36 @@ export const applicationStageResolvers: any = {
394
471
{ _id : applicantId } ,
395
472
{ $set : { applicationPhase : "Rejected" , status : "Rejected" } }
396
473
) ;
397
- message = `Applicant Rejected from the ${ stageDismissedFrom ?. applicationPhase } stage.` ;
474
+ message = `You have been rejected from the ${ stageDismissedFrom ?. applicationPhase } stage.` ;
475
+
476
+ const notification3 = await ApplicantNotificationsModel . create ( {
477
+ userId : user ! . _id ,
478
+ message,
479
+ eventType : "general" ,
480
+ } ) ;
481
+
482
+ await sendEmailTemplate (
483
+ user ! . email ,
484
+ "Application Update" ,
485
+ `Hello ${ user ! . email . split ( "@" ) [ 0 ] } , ` ,
486
+ `We are sorry to inform you that
487
+ your application has been rejected from the ${ stageDismissedFrom ?. applicationPhase } stage.
488
+ <br />
489
+ <br />
490
+ You can always apply again.
491
+ `
492
+ ) ;
493
+
494
+ await pusher
495
+ . trigger ( `notifications-${ user ! . _id } ` , "new-notification" , {
496
+ message : notification3 . message ,
497
+ id : notification3 . _id ,
498
+ createdAt : notification3 . createdAt ,
499
+ read : notification3 . read ,
500
+ } )
501
+ . catch ( ( error ) => {
502
+ console . error ( "Error with Pusher trigger:" , error ) ;
503
+ } ) ;
398
504
break ;
399
505
400
506
case "Shortlisted" :
@@ -414,7 +520,35 @@ export const applicationStageResolvers: any = {
414
520
{ _id : applicantId } ,
415
521
{ $set : { applicationPhase : nextStage , status : "No action" } }
416
522
) ;
417
- message = `Applicant advanced to ${ nextStage } stage.` ;
523
+ message = `You have advanced to the ${ nextStage } stage.` ;
524
+ const notification4 = await ApplicantNotificationsModel . create ( {
525
+ userId : user ! . _id ,
526
+ message,
527
+ eventType : "general" ,
528
+ } ) ;
529
+
530
+ await sendEmailTemplate (
531
+ user ! . email ,
532
+ "Application Update" ,
533
+ `Hello ${ user ! . email . split ( "@" ) [ 0 ] } , ` ,
534
+ `Your application has been moved to ${ nextStage } stage.
535
+ <br />
536
+ You will hear from us very soon.
537
+ <br />
538
+ Thank you for your patience.
539
+ `
540
+ ) ;
541
+
542
+ await pusher
543
+ . trigger ( `notifications-${ user ! . _id } ` , "new-notification" , {
544
+ message : notification4 . message ,
545
+ id : notification4 . _id ,
546
+ createdAt : notification4 . createdAt ,
547
+ read : notification4 . read ,
548
+ } )
549
+ . catch ( ( error ) => {
550
+ console . error ( "Error with Pusher trigger:" , error ) ;
551
+ } ) ;
418
552
break ;
419
553
}
420
554
0 commit comments