Skip to content

Commit b8c503f

Browse files
authored
Merge pull request #45 from imcaizheng/update-booked-candidate-status
update status of candidate when the candidate is booked
2 parents c79d11d + c7069ed commit b8c503f

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/services/ResourceBookingService.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const helper = require('../common/helper')
1111
const logger = require('../common/logger')
1212
const errors = require('../common/errors')
1313
const models = require('../models')
14+
const JobCandidateService = require('./JobCandidateService')
1415

1516
const ResourceBooking = models.ResourceBooking
1617
const esClient = helper.getESClient()
@@ -111,6 +112,7 @@ createResourceBooking.schema = Joi.object().keys({
111112
*/
112113
async function updateResourceBooking (currentUser, id, data) {
113114
const resourceBooking = await ResourceBooking.findById(id)
115+
const isDiffStatus = resourceBooking.status !== data.status
114116
if (!currentUser.isBookingManager && !currentUser.isMachine) {
115117
const connect = await helper.isConnectMember(resourceBooking.dataValues.projectId, currentUser.jwtToken)
116118
if (!connect) {
@@ -120,8 +122,34 @@ async function updateResourceBooking (currentUser, id, data) {
120122
data.updatedAt = new Date()
121123
data.updatedBy = await helper.getUserId(currentUser.userId)
122124

123-
await resourceBooking.update(data)
125+
const updatedResourceBooking = await resourceBooking.update(data)
124126
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+
}
125153
const result = helper.clearObject(_.assign(resourceBooking.dataValues, data))
126154
return result
127155
}

0 commit comments

Comments
 (0)