@@ -299,6 +299,7 @@ async function requestInterview (currentUser, jobCandidateId, interview) {
299
299
}
300
300
// create scheduling page on nylas
301
301
const schedulingPage = await createSchedulingPage ( interview , calendar , pageOptions )
302
+ logger . debug ( `requestInterview -> createSchedulingPage created: ${ JSON . stringify ( schedulingPage ) } , using accessToken: "${ calendar . accessToken } ""` )
302
303
303
304
// Link nylasPage to interview
304
305
interview . nylasPageId = schedulingPage . id
@@ -386,6 +387,7 @@ requestInterview.schema = Joi.object().keys({
386
387
* @returns {Object } updated interview
387
388
*/
388
389
async function partiallyUpdateInterview ( currentUser , interview , data ) {
390
+ const oldInterviewValue = interview . toJSON ( )
389
391
// only status can be updated for Completed interviews
390
392
if ( interview . status === InterviewConstants . Status . Completed ) {
391
393
const updatedFields = _ . keys ( data )
@@ -434,7 +436,7 @@ async function partiallyUpdateInterview (currentUser, interview, data) {
434
436
// if reaches here, it's not one of the common errors handled in `handleSequelizeError`
435
437
throw err
436
438
}
437
- await helper . postEvent ( config . TAAS_INTERVIEW_UPDATE_TOPIC , entity , { oldValue : interview . toJSON ( ) } )
439
+ await helper . postEvent ( config . TAAS_INTERVIEW_UPDATE_TOPIC , entity , { oldValue : oldInterviewValue } )
438
440
return entity
439
441
}
440
442
@@ -467,8 +469,8 @@ partiallyUpdateInterviewByRound.schema = Joi.object().keys({
467
469
jobCandidateId : Joi . string ( ) . uuid ( ) . required ( ) ,
468
470
round : Joi . number ( ) . integer ( ) . positive ( ) . required ( ) ,
469
471
data : Joi . object ( ) . keys ( {
470
- duration : Joi . number ( ) . integer ( ) . positive ( ) . required ( ) ,
471
- timezone : Joi . string ( ) . required ( ) ,
472
+ duration : Joi . number ( ) . integer ( ) . positive ( ) ,
473
+ timezone : Joi . string ( ) ,
472
474
hostUserId : Joi . string ( ) . uuid ( ) ,
473
475
expireTimestamp : Joi . date ( ) ,
474
476
availableTime : Joi . array ( ) . min ( 1 ) . items (
@@ -484,15 +486,15 @@ partiallyUpdateInterviewByRound.schema = Joi.object().keys({
484
486
end : Joi . string ( ) . regex ( InterviewConstants . Nylas . StartEndRegex ) . required ( ) ,
485
487
start : Joi . string ( ) . regex ( InterviewConstants . Nylas . StartEndRegex ) . required ( )
486
488
} )
487
- ) . required ( ) ,
489
+ ) ,
488
490
startTimestamp : Joi . date ( ) . greater ( 'now' ) . when ( 'status' , {
489
491
is : [ InterviewConstants . Status . Scheduled , InterviewConstants . Status . Rescheduled ] ,
490
- then : Joi . required ( ) ,
492
+ then : Joi . invalid ( null ) ,
491
493
otherwise : Joi . allow ( null )
492
494
} ) ,
493
495
endTimestamp : Joi . date ( ) . greater ( Joi . ref ( 'startTimestamp' ) ) . when ( 'status' , {
494
496
is : [ InterviewConstants . Status . Scheduled , InterviewConstants . Status . Rescheduled ] ,
495
- then : Joi . required ( ) ,
497
+ then : Joi . invalid ( null ) ,
496
498
otherwise : Joi . allow ( null )
497
499
} ) ,
498
500
status : Joi . interviewStatus ( ) ,
@@ -540,8 +542,8 @@ partiallyUpdateInterviewById.schema = Joi.object().keys({
540
542
currentUser : Joi . object ( ) . required ( ) ,
541
543
id : Joi . string ( ) . required ( ) ,
542
544
data : Joi . object ( ) . keys ( {
543
- duration : Joi . number ( ) . integer ( ) . positive ( ) . required ( ) ,
544
- timezone : Joi . string ( ) . required ( ) ,
545
+ duration : Joi . number ( ) . integer ( ) . positive ( ) ,
546
+ timezone : Joi . string ( ) ,
545
547
hostUserId : Joi . string ( ) . uuid ( ) ,
546
548
expireTimestamp : Joi . date ( ) ,
547
549
availableTime : Joi . array ( ) . min ( 1 ) . items (
@@ -557,15 +559,15 @@ partiallyUpdateInterviewById.schema = Joi.object().keys({
557
559
end : Joi . string ( ) . regex ( InterviewConstants . Nylas . StartEndRegex ) . required ( ) ,
558
560
start : Joi . string ( ) . regex ( InterviewConstants . Nylas . StartEndRegex ) . required ( )
559
561
} )
560
- ) . required ( ) ,
562
+ ) ,
561
563
startTimestamp : Joi . date ( ) . greater ( 'now' ) . when ( 'status' , {
562
564
is : [ InterviewConstants . Status . Scheduled , InterviewConstants . Status . Rescheduled ] ,
563
- then : Joi . required ( ) ,
565
+ then : Joi . invalid ( null ) ,
564
566
otherwise : Joi . allow ( null )
565
567
} ) ,
566
568
endTimestamp : Joi . date ( ) . greater ( Joi . ref ( 'startTimestamp' ) ) . when ( 'status' , {
567
569
is : [ InterviewConstants . Status . Scheduled , InterviewConstants . Status . Rescheduled ] ,
568
- then : Joi . required ( ) ,
570
+ then : Joi . invalid ( null ) ,
569
571
otherwise : Joi . allow ( null )
570
572
} ) ,
571
573
status : Joi . interviewStatus ( ) ,
@@ -767,6 +769,47 @@ async function updateCompletedInterviews () {
767
769
*/
768
770
async function partiallyUpdateInterviewByWebhook ( interviewId , webhookBody ) {
769
771
logger . info ( { component : 'InterviewService' , context : 'partiallyUpdateInterviewByWebhook' , message : `Received webhook for interview id "${ interviewId } ": ${ JSON . stringify ( webhookBody ) } ` } )
772
+
773
+ // this method is used by the Nylas webhooks, so use M2M user
774
+ const m2mUser = helper . getAuditM2Muser ( )
775
+ const bookingDetails = webhookBody . booking
776
+ const interviewStartTimeMoment = moment . unix ( bookingDetails . start_time )
777
+ const interviewEndTimeMoment = moment . unix ( bookingDetails . end_time )
778
+ let updatedInterview
779
+
780
+ if ( bookingDetails . is_confirmed ) {
781
+ try {
782
+ // CREATED + confirmed ==> inteview updated to scheduled
783
+ // UPDATED + cancelled ==> inteview expired
784
+ updatedInterview = await partiallyUpdateInterviewById (
785
+ m2mUser ,
786
+ interviewId ,
787
+ {
788
+ status : InterviewConstants . Status . Scheduled ,
789
+ startTimestamp : interviewStartTimeMoment . toDate ( ) ,
790
+ endTimestamp : interviewEndTimeMoment . toDate ( )
791
+ }
792
+ )
793
+
794
+ logger . debug ( {
795
+ component : 'InterviewService' ,
796
+ context : 'partiallyUpdateInterviewByWebhook' ,
797
+ message :
798
+ `~~~~~~~~~~~NEW EVENT~~~~~~~~~~~\nInterview Scheduled under account id ${
799
+ bookingDetails . account_id
800
+ } (email is ${ bookingDetails . recipient_email } ) in calendar id ${
801
+ bookingDetails . calendar_id
802
+ } . Event status is ${ InterviewConstants . Status . Scheduled } and starts from ${ interviewStartTimeMoment
803
+ . format ( 'MMM DD YYYY HH:mm' ) } and ends at ${ interviewEndTimeMoment
804
+ . format ( 'MMM DD YYYY HH:mm' ) } `
805
+ } )
806
+ } catch ( err ) {
807
+ logger . logFullError ( err , { component : 'InterviewService' , context : 'partiallyUpdateInterviewByWebhook' } )
808
+ throw new errors . BadRequestError ( `Could not update interview: ${ err . message } ` )
809
+ }
810
+
811
+ return updatedInterview
812
+ }
770
813
}
771
814
772
815
module . exports = {
0 commit comments