@@ -45,12 +45,13 @@ async function selectJobCandidate (jobId, userId) {
45
45
/**
46
46
* Update the status of the Job to assigned when it positions requirement is fullfilled.
47
47
*
48
- * @param {Object } job the job data
48
+ * @param {String } jobId the job id
49
49
* @returns {undefined }
50
50
*/
51
- async function assignJob ( job ) {
51
+ async function assignJob ( jobId ) {
52
+ const job = await models . Job . findById ( jobId )
52
53
if ( job . status === 'assigned' ) {
53
- logger . info ( {
54
+ logger . debug ( {
54
55
component : 'ResourceBookingEventHandler' ,
55
56
context : 'assignJob' ,
56
57
message : `job with projectId ${ job . projectId } is already assigned`
@@ -59,6 +60,7 @@ async function assignJob (job) {
59
60
}
60
61
const resourceBookings = await models . ResourceBooking . findAll ( {
61
62
where : {
63
+ jobId : job . id ,
62
64
status : 'assigned' ,
63
65
deletedAt : null
64
66
}
@@ -70,7 +72,7 @@ async function assignJob (job) {
70
72
} )
71
73
if ( job . numPositions === resourceBookings . length ) {
72
74
await JobService . partiallyUpdateJob ( helper . getAuditM2Muser ( ) , job . id , { status : 'assigned' } )
73
- logger . info ( { component : 'ResourceBookingEventHandler' , context : 'assignJob' , message : `job with projectId ${ job . projectId } is assigned` } )
75
+ logger . info ( { component : 'ResourceBookingEventHandler' , context : 'assignJob' , message : `job ${ job . id } is assigned` } )
74
76
}
75
77
}
76
78
@@ -90,24 +92,24 @@ async function processUpdate (payload) {
90
92
return
91
93
}
92
94
if ( payload . value . status !== 'assigned' ) {
93
- logger . info ( {
95
+ logger . debug ( {
94
96
component : 'ResourceBookingEventHandler' ,
95
97
context : 'processUpdate' ,
96
98
message : `not interested resource booking - status: ${ payload . value . status } `
97
99
} )
98
100
return
99
101
}
100
102
const resourceBooking = await models . ResourceBooking . findById ( payload . value . id )
101
- const jobs = await models . Job . findAll ( {
102
- where : {
103
- projectId : resourceBooking . projectId ,
104
- deletedAt : null
105
- }
106
- } )
107
- for ( const job of jobs ) {
108
- await selectJobCandidate ( job . id , resourceBooking . userId )
109
- await assignJob ( job )
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
110
}
111
+ await selectJobCandidate ( resourceBooking . jobId , resourceBooking . userId )
112
+ await assignJob ( resourceBooking . jobId )
111
113
}
112
114
113
115
module . exports = {
0 commit comments