1
1
/*
2
2
* Handle events for JobCandidate.
3
3
*/
4
-
4
+ const { Op } = require ( 'sequelize' )
5
+ const _ = require ( 'lodash' )
6
+ const config = require ( 'config' )
5
7
const models = require ( '../models' )
6
8
const logger = require ( '../common/logger' )
7
9
const helper = require ( '../common/helper' )
8
10
const JobService = require ( '../services/JobService' )
11
+ const JobCandidateService = require ( '../services/JobCandidateService' )
9
12
10
13
/**
11
14
* Once we create at least one JobCandidate for a Job, the Job status should be changed to in-review.
@@ -44,6 +47,84 @@ async function inReviewJob (payload) {
44
47
}
45
48
}
46
49
50
+ /**
51
+ * Actual Update Job Candidates
52
+ *
53
+ * @param {* } statuses the source status we'll update
54
+ * @param {* } userId the userID
55
+ */
56
+ async function updateJobCandidates ( statuses , userId ) {
57
+ logger . info ( {
58
+ component : 'JobCandidateEventHandler' ,
59
+ context : 'updateJobCandidates' ,
60
+ message : `Update jobCandidates for user ${ userId } `
61
+ } )
62
+ const filter = { [ Op . and ] : [ ] }
63
+ filter [ Op . and ] . push ( { status : statuses } )
64
+ filter [ Op . and ] . push ( { userId : userId } )
65
+ const candidates = await models . JobCandidate . findAll ( {
66
+ where : filter
67
+ } )
68
+ if ( candidates && candidates . length > 0 ) {
69
+ _ . each ( candidates , async ( candidate ) => {
70
+ logger . info ( {
71
+ component : 'JobCandidateEventHandler' ,
72
+ context : 'updateJobCandidates' ,
73
+ message : `Begin update id: ${ candidate . id } ' candidate with ${ candidate . status } status into ${ config . WITHDRAWN_STATUS_CHANGE_MAPPING [ candidate . status ] } for userId: ${ userId } `
74
+ } )
75
+ await JobCandidateService . partiallyUpdateJobCandidate (
76
+ helper . getAuditM2Muser ( ) ,
77
+ candidate . id ,
78
+ { status : config . WITHDRAWN_STATUS_CHANGE_MAPPING [ candidate . status ] }
79
+ ) . then ( result => {
80
+ logger . info ( {
81
+ component : 'JobCandidateEventHandler' ,
82
+ context : 'updateJobCandidates' ,
83
+ message : `Finish update id: ${ result . id } ' candidate into ${ result . status } status for userId: ${ userId } `
84
+ } )
85
+ } )
86
+ } )
87
+ } else {
88
+ logger . info ( {
89
+ component : 'JobCandidateEventHandler' ,
90
+ context : 'updateJobCandidates' ,
91
+ message : `There are not jobCandidates for user ${ userId } that required to be updated.`
92
+ } )
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Update Job Candidates based on business rules
98
+ *
99
+ * @param {* } payload the updated jobCandidate info
100
+ */
101
+ async function withDrawnJobCandidates ( payload ) {
102
+ const jobCandidate = payload . value
103
+ if ( jobCandidate . status === 'placed' ) {
104
+ const job = await models . Job . findById ( payload . value . jobId )
105
+ if ( job . hoursPerWeek > config . JOBS_HOUR_PER_WEEK ) {
106
+ // find all these user's open job Candidate and mark the status as withdrawn or withdrawn-prescreen
107
+ logger . info ( {
108
+ component : 'JobCandidateEventHandler' ,
109
+ context : 'withDrawnJobCandidates' ,
110
+ message : `Begin update jobCandidates as ${ payload . value . id } candidate's new gig is requiring more than 20 hrs per week`
111
+ } )
112
+ await updateJobCandidates ( [ 'applied' , 'skills-test' , 'phone-screen' , 'open' , 'interview' , 'selected' , 'offered' ] , payload . value . userId )
113
+ logger . info ( {
114
+ component : 'JobCandidateEventHandler' ,
115
+ context : 'withDrawnJobCandidates' ,
116
+ message : `Finish update jobCandidates as ${ payload . value . id } candidate`
117
+ } )
118
+ } else {
119
+ logger . debug ( {
120
+ component : 'JobCandidateEventHandler' ,
121
+ context : 'withDrawnJobCandidates' ,
122
+ message : `id: ${ payload . value . id } candidate is not placing on a gig requiring 20 hrs per week`
123
+ } )
124
+ }
125
+ }
126
+ }
127
+
47
128
/**
48
129
* Process job candidate create event.
49
130
*
@@ -52,6 +133,9 @@ async function inReviewJob (payload) {
52
133
*/
53
134
async function processCreate ( payload ) {
54
135
await inReviewJob ( payload )
136
+ if ( payload . value . status === 'placed' ) {
137
+ await withDrawnJobCandidates ( payload )
138
+ }
55
139
}
56
140
57
141
/**
@@ -62,6 +146,9 @@ async function processCreate (payload) {
62
146
*/
63
147
async function processUpdate ( payload ) {
64
148
await inReviewJob ( payload )
149
+ if ( payload . value . status === 'placed' && payload . options . oldValue . status !== 'placed' ) {
150
+ await withDrawnJobCandidates ( payload )
151
+ }
65
152
}
66
153
67
154
module . exports = {
0 commit comments