@@ -11,6 +11,7 @@ const helper = require('../common/helper')
11
11
const logger = require ( '../common/logger' )
12
12
const errors = require ( '../common/errors' )
13
13
const models = require ( '../models' )
14
+ const JobCandidateService = require ( './JobCandidateService' )
14
15
15
16
const ResourceBooking = models . ResourceBooking
16
17
const esClient = helper . getESClient ( )
@@ -111,6 +112,7 @@ createResourceBooking.schema = Joi.object().keys({
111
112
*/
112
113
async function updateResourceBooking ( currentUser , id , data ) {
113
114
const resourceBooking = await ResourceBooking . findById ( id )
115
+ const isDiffStatus = resourceBooking . status !== data . status
114
116
if ( ! currentUser . isBookingManager && ! currentUser . isMachine ) {
115
117
const connect = await helper . isConnectMember ( resourceBooking . dataValues . projectId , currentUser . jwtToken )
116
118
if ( ! connect ) {
@@ -120,8 +122,34 @@ async function updateResourceBooking (currentUser, id, data) {
120
122
data . updatedAt = new Date ( )
121
123
data . updatedBy = await helper . getUserId ( currentUser . userId )
122
124
123
- await resourceBooking . update ( data )
125
+ const updatedResourceBooking = await resourceBooking . update ( data )
124
126
await helper . postEvent ( config . TAAS_RESOURCE_BOOKING_UPDATE_TOPIC , { id, ...data } )
127
+ // When we are updating the status of ResourceBooking to `assigned`
128
+ // the corresponding JobCandidate record (with the same userId and jobId)
129
+ // should be updated with the status `selected`
130
+ if ( isDiffStatus && data . status === 'assigned' ) {
131
+ const candidates = await models . JobCandidate . findAll ( {
132
+ where : {
133
+ jobId : updatedResourceBooking . jobId ,
134
+ userId : updatedResourceBooking . userId ,
135
+ status : {
136
+ [ Op . not ] : 'selected'
137
+ } ,
138
+ deletedAt : null
139
+ }
140
+ } )
141
+ await Promise . all ( candidates . map ( candidate => JobCandidateService . partiallyUpdateJobCandidate (
142
+ currentUser ,
143
+ candidate . id ,
144
+ { status : 'selected' }
145
+ ) . then ( result => {
146
+ logger . debug ( {
147
+ component : 'ResourceBookingService' ,
148
+ context : 'updatedResourceBooking' ,
149
+ message : `id: ${ result . id } candidate got selected.`
150
+ } )
151
+ } ) ) )
152
+ }
125
153
const result = helper . clearObject ( _ . assign ( resourceBooking . dataValues , data ) )
126
154
return result
127
155
}
0 commit comments