@@ -14,15 +14,39 @@ const JobCandidateService = require('../services/JobCandidateService')
14
14
* the corresponding JobCandidate record (with the same userId and jobId)
15
15
* should be updated with status `selected`
16
16
*
17
- * @param {String } jobId the job id
18
- * @param {String } userId the user id
17
+ * @param {Object } payload the event payload
19
18
* @returns {undefined }
20
19
*/
21
- async function selectJobCandidate ( jobId , userId ) {
20
+ async function selectJobCandidate ( payload ) {
21
+ if ( payload . value . status === payload . options . oldValue . status ) {
22
+ logger . debug ( {
23
+ component : 'ResourceBookingEventHandler' ,
24
+ context : 'selectJobCandidate' ,
25
+ message : 'status not changed'
26
+ } )
27
+ return
28
+ }
29
+ if ( payload . value . status !== 'assigned' ) {
30
+ logger . debug ( {
31
+ component : 'ResourceBookingEventHandler' ,
32
+ context : 'selectJobCandidate' ,
33
+ message : `not interested resource booking - status: ${ payload . value . status } `
34
+ } )
35
+ return
36
+ }
37
+ const resourceBooking = await models . ResourceBooking . findById ( payload . value . id )
38
+ if ( ! resourceBooking . jobId ) {
39
+ logger . debug ( {
40
+ component : 'ResourceBookingEventHandler' ,
41
+ context : 'selectJobCandidate' ,
42
+ message : `id: ${ resourceBooking . id } resource booking without jobId - ignored`
43
+ } )
44
+ return
45
+ }
22
46
const candidates = await models . JobCandidate . findAll ( {
23
47
where : {
24
- jobId,
25
- userId,
48
+ jobId : resourceBooking . jobId ,
49
+ userId : resourceBooking . userId ,
26
50
status : {
27
51
[ Op . not ] : 'selected'
28
52
} ,
@@ -45,11 +69,36 @@ async function selectJobCandidate (jobId, userId) {
45
69
/**
46
70
* Update the status of the Job to assigned when it positions requirement is fullfilled.
47
71
*
48
- * @param {String } jobId the job id
72
+ * @param {Object } payload the event payload
49
73
* @returns {undefined }
50
74
*/
51
- async function assignJob ( jobId ) {
52
- const job = await models . Job . findById ( jobId )
75
+ async function assignJob ( payload ) {
76
+ if ( payload . value . status === payload . options . oldValue . status ) {
77
+ logger . debug ( {
78
+ component : 'ResourceBookingEventHandler' ,
79
+ context : 'assignJob' ,
80
+ message : 'status not changed'
81
+ } )
82
+ return
83
+ }
84
+ if ( payload . value . status !== 'assigned' ) {
85
+ logger . debug ( {
86
+ component : 'ResourceBookingEventHandler' ,
87
+ context : 'assignJob' ,
88
+ message : `not interested resource booking - status: ${ payload . value . status } `
89
+ } )
90
+ return
91
+ }
92
+ const resourceBooking = await models . ResourceBooking . findById ( payload . value . id )
93
+ if ( ! resourceBooking . jobId ) {
94
+ logger . debug ( {
95
+ component : 'ResourceBookingEventHandler' ,
96
+ context : 'assignJob' ,
97
+ message : `id: ${ resourceBooking . id } resource booking without jobId - ignored`
98
+ } )
99
+ return
100
+ }
101
+ const job = await models . Job . findById ( resourceBooking . jobId )
53
102
if ( job . status === 'assigned' ) {
54
103
logger . debug ( {
55
104
component : 'ResourceBookingEventHandler' ,
@@ -83,33 +132,8 @@ async function assignJob (jobId) {
83
132
* @returns {undefined }
84
133
*/
85
134
async function processUpdate ( payload ) {
86
- if ( payload . value . status === payload . options . oldValue . status ) {
87
- logger . debug ( {
88
- component : 'ResourceBookingEventHandler' ,
89
- context : 'processUpdate' ,
90
- message : 'status not changed'
91
- } )
92
- return
93
- }
94
- if ( payload . value . status !== 'assigned' ) {
95
- logger . debug ( {
96
- component : 'ResourceBookingEventHandler' ,
97
- context : 'processUpdate' ,
98
- message : `not interested resource booking - status: ${ payload . value . status } `
99
- } )
100
- return
101
- }
102
- const resourceBooking = await models . ResourceBooking . findById ( payload . value . id )
103
- if ( ! resourceBooking . jobId ) {
104
- logger . debug ( {
105
- component : 'ResourceBookingEventHandler' ,
106
- context : 'processUpdate' ,
107
- message : `id: ${ resourceBooking . id } resource booking without jobId - ignored`
108
- } )
109
- return
110
- }
111
- await selectJobCandidate ( resourceBooking . jobId , resourceBooking . userId )
112
- await assignJob ( resourceBooking . jobId )
135
+ await selectJobCandidate ( payload )
136
+ await assignJob ( payload )
113
137
}
114
138
115
139
module . exports = {
0 commit comments