From a1ad5c0a5635c26c1f7f8a903d710cea86b7e65d Mon Sep 17 00:00:00 2001 From: imcaizheng Date: Sat, 5 Dec 2020 05:50:45 +0800 Subject: [PATCH 1/2] update status of candidate when the candidate is booked --- src/services/ResourceBookingService.js | 29 +++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/services/ResourceBookingService.js b/src/services/ResourceBookingService.js index fc234402..007d463d 100644 --- a/src/services/ResourceBookingService.js +++ b/src/services/ResourceBookingService.js @@ -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() @@ -120,8 +121,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 (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 } From bed2699b08e51e9f4ddf0e301be5d3fd180e1115 Mon Sep 17 00:00:00 2001 From: imcaizheng Date: Tue, 8 Dec 2020 19:05:48 +0800 Subject: [PATCH 2/2] check if status being changed before updating candidates --- src/services/ResourceBookingService.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/services/ResourceBookingService.js b/src/services/ResourceBookingService.js index 007d463d..25d62ed6 100644 --- a/src/services/ResourceBookingService.js +++ b/src/services/ResourceBookingService.js @@ -112,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) { const connect = await helper.isConnectMember(resourceBooking.dataValues.projectId, currentUser.jwtToken) if (!connect) { @@ -126,7 +127,7 @@ async function updateResourceBooking (currentUser, 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 (data.status === 'assigned') { + if (isDiffStatus && data.status === 'assigned') { const candidates = await models.JobCandidate.findAll({ where: { jobId: updatedResourceBooking.jobId,