Skip to content

update status of candidate when the candidate is booked #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/services/ResourceBookingService.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const helper = require('../common/helper')
const logger = require('../common/logger')
const errors = require('../common/errors')
const models = require('../models')
const JobCandidateService = require('./JobCandidateService')

const ResourceBooking = models.ResourceBooking
const esClient = helper.getESClient()
Expand Down Expand Up @@ -111,6 +112,7 @@ createResourceBooking.schema = Joi.object().keys({
*/
async function updateResourceBooking (currentUser, id, data) {
const resourceBooking = await ResourceBooking.findById(id)
const isDiffStatus = resourceBooking.status !== data.status
if (!currentUser.isBookingManager && !currentUser.isMachine) {
const connect = await helper.isConnectMember(resourceBooking.dataValues.projectId, currentUser.jwtToken)
if (!connect) {
Expand All @@ -120,8 +122,34 @@ async function updateResourceBooking (currentUser, id, data) {
data.updatedAt = new Date()
data.updatedBy = await helper.getUserId(currentUser.userId)

await resourceBooking.update(data)
const updatedResourceBooking = await resourceBooking.update(data)
await helper.postEvent(config.TAAS_RESOURCE_BOOKING_UPDATE_TOPIC, { id, ...data })
// When we are updating the status of ResourceBooking to `assigned`
// the corresponding JobCandidate record (with the same userId and jobId)
// should be updated with the status `selected`
if (isDiffStatus && data.status === 'assigned') {
const candidates = await models.JobCandidate.findAll({
where: {
jobId: updatedResourceBooking.jobId,
userId: updatedResourceBooking.userId,
status: {
[Op.not]: 'selected'
},
deletedAt: null
}
})
await Promise.all(candidates.map(candidate => JobCandidateService.partiallyUpdateJobCandidate(
currentUser,
candidate.id,
{ status: 'selected' }
).then(result => {
logger.debug({
component: 'ResourceBookingService',
context: 'updatedResourceBooking',
message: `id: ${result.id} candidate got selected.`
})
})))
}
const result = helper.clearObject(_.assign(resourceBooking.dataValues, data))
return result
}
Expand Down