-
Notifications
You must be signed in to change notification settings - Fork 33
[re-created]implement local event handling #85
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
maxceem
merged 8 commits into
topcoder-platform:dev
from
imcaizheng:implement-event-handlers-repost
Jan 4, 2021
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c208671
implement event handling
imcaizheng 51128fc
Merge branch 'dev' into implement-event-handlers-repost
imcaizheng b64ac86
fix inconsistence by interating all jobs found filtering by projectId
imcaizheng 1041679
enhancement: encapsulate old value to the event payload as well
imcaizheng 0c093c4
fix: use jobId instead of projectId to find job
imcaizheng 7f5a621
Merge branch 'dev' into implement-event-handlers-repost
imcaizheng 6d8dfff
decouple functions assignJob and selectJobCandidate to increase maint…
imcaizheng 2d14088
find resourceBooking by jobId when cancelJob
imcaizheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Implement an event dispatcher that handles events synchronously. | ||
*/ | ||
|
||
const handlers = [] | ||
|
||
/** | ||
* Handle event. | ||
* | ||
* @param {String} topic the topic name | ||
* @param {Object} payload the message payload | ||
* @returns {undefined} | ||
*/ | ||
async function handleEvent (topic, payload) { | ||
for (const handler of handlers) { | ||
await handler.handleEvent(topic, payload) | ||
} | ||
} | ||
|
||
/** | ||
* Register to the dispatcher. | ||
* | ||
* @param {Object} handler the handler containing the `handleEvent` function | ||
* @returns {undefined} | ||
*/ | ||
function register (handler) { | ||
handlers.push(handler) | ||
} | ||
|
||
module.exports = { | ||
handleEvent, | ||
register | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Handle events for Job. | ||
*/ | ||
|
||
const { Op } = require('sequelize') | ||
const models = require('../models') | ||
const logger = require('../common/logger') | ||
const helper = require('../common/helper') | ||
const JobCandidateService = require('../services/JobCandidateService') | ||
const ResourceBookingService = require('../services/ResourceBookingService') | ||
|
||
/** | ||
* Cancel all related resource bookings and all related candidates when a job is cancelled. | ||
* | ||
* @param {Object} payload the event payload | ||
* @returns {undefined} | ||
*/ | ||
async function cancelJob (payload) { | ||
if (payload.value.status === payload.options.oldValue.status) { | ||
logger.debug({ | ||
component: 'JobEventHandler', | ||
context: 'cancelJob', | ||
message: 'status not changed' | ||
}) | ||
return | ||
} | ||
if (payload.value.status !== 'cancelled') { | ||
logger.info({ | ||
component: 'JobEventHandler', | ||
context: 'cancelJob', | ||
message: `not interested job - status: ${payload.value.status}` | ||
}) | ||
return | ||
} | ||
// pull data from db instead of directly extract data from the payload | ||
// since the payload may not contain all fields when it is from partically update operation. | ||
const job = await models.Job.findById(payload.value.id) | ||
const candidates = await models.JobCandidate.findAll({ | ||
where: { | ||
jobId: job.id, | ||
status: { | ||
[Op.not]: 'cancelled' | ||
}, | ||
deletedAt: null | ||
} | ||
}) | ||
const resourceBookings = await models.ResourceBooking.findAll({ | ||
where: { | ||
projectId: job.projectId, | ||
status: { | ||
[Op.not]: 'cancelled' | ||
}, | ||
deletedAt: null | ||
} | ||
}) | ||
await Promise.all([ | ||
...candidates.map(candidate => JobCandidateService.partiallyUpdateJobCandidate( | ||
helper.getAuditM2Muser(), | ||
candidate.id, | ||
{ status: 'cancelled' } | ||
).then(result => { | ||
logger.info({ | ||
component: 'JobEventHandler', | ||
context: 'cancelJob', | ||
message: `id: ${result.id} candidate got cancelled.` | ||
}) | ||
})), | ||
...resourceBookings.map(resource => ResourceBookingService.partiallyUpdateResourceBooking( | ||
helper.getAuditM2Muser(), | ||
resource.id, | ||
{ status: 'cancelled' } | ||
).then(result => { | ||
logger.info({ | ||
component: 'JobEventHandler', | ||
context: 'cancelJob', | ||
message: `id: ${result.id} resource booking got cancelled.` | ||
}) | ||
})) | ||
]) | ||
} | ||
|
||
/** | ||
* Process job update event. | ||
* | ||
* @param {Object} payload the event payload | ||
* @returns {undefined} | ||
*/ | ||
async function processUpdate (payload) { | ||
await cancelJob(payload) | ||
} | ||
|
||
module.exports = { | ||
processUpdate | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Handle events for ResourceBooking. | ||
*/ | ||
|
||
const { Op } = require('sequelize') | ||
const models = require('../models') | ||
const logger = require('../common/logger') | ||
const helper = require('../common/helper') | ||
const JobService = require('../services/JobService') | ||
const JobCandidateService = require('../services/JobCandidateService') | ||
|
||
/** | ||
* When ResourceBooking's status is changed to `assigned` | ||
* the corresponding JobCandidate record (with the same userId and jobId) | ||
* should be updated with status `selected` | ||
* | ||
* @param {String} jobId the job id | ||
* @param {String} userId the user id | ||
* @returns {undefined} | ||
*/ | ||
async function selectJobCandidate (jobId, userId) { | ||
const candidates = await models.JobCandidate.findAll({ | ||
where: { | ||
jobId, | ||
userId, | ||
status: { | ||
[Op.not]: 'selected' | ||
}, | ||
deletedAt: null | ||
} | ||
}) | ||
await Promise.all(candidates.map(candidate => JobCandidateService.partiallyUpdateJobCandidate( | ||
helper.getAuditM2Muser(), | ||
candidate.id, | ||
{ status: 'selected' } | ||
).then(result => { | ||
logger.info({ | ||
component: 'ResourceBookingEventHandler', | ||
context: 'selectJobCandidate', | ||
message: `id: ${result.id} candidate got selected.` | ||
}) | ||
}))) | ||
} | ||
|
||
/** | ||
* Update the status of the Job to assigned when it positions requirement is fullfilled. | ||
* | ||
* @param {Object} job the job data | ||
* @returns {undefined} | ||
*/ | ||
async function assignJob (job) { | ||
if (job.status === 'assigned') { | ||
logger.info({ | ||
component: 'ResourceBookingEventHandler', | ||
context: 'assignJob', | ||
message: `job with projectId ${job.projectId} is already assigned` | ||
}) | ||
return | ||
} | ||
const resourceBookings = await models.ResourceBooking.findAll({ | ||
where: { | ||
status: 'assigned', | ||
deletedAt: null | ||
} | ||
}) | ||
maxceem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logger.debug({ | ||
component: 'ResourceBookingEventHandler', | ||
context: 'assignJob', | ||
message: `the number of assigned resource bookings is ${resourceBookings.length} - the numPositions of the job is ${job.numPositions}` | ||
}) | ||
if (job.numPositions === resourceBookings.length) { | ||
await JobService.partiallyUpdateJob(helper.getAuditM2Muser(), job.id, { status: 'assigned' }) | ||
logger.info({ component: 'ResourceBookingEventHandler', context: 'assignJob', message: `job with projectId ${job.projectId} is assigned` }) | ||
} | ||
} | ||
|
||
/** | ||
* Process resource booking update event. | ||
* | ||
* @param {Object} payload the event payload | ||
* @returns {undefined} | ||
*/ | ||
async function processUpdate (payload) { | ||
if (payload.value.status === payload.options.oldValue.status) { | ||
logger.debug({ | ||
component: 'ResourceBookingEventHandler', | ||
context: 'processUpdate', | ||
message: 'status not changed' | ||
}) | ||
return | ||
} | ||
if (payload.value.status !== 'assigned') { | ||
logger.info({ | ||
component: 'ResourceBookingEventHandler', | ||
context: 'processUpdate', | ||
message: `not interested resource booking - status: ${payload.value.status}` | ||
}) | ||
return | ||
} | ||
const resourceBooking = await models.ResourceBooking.findById(payload.value.id) | ||
const jobs = await models.Job.findAll({ | ||
maxceem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
where: { | ||
projectId: resourceBooking.projectId, | ||
deletedAt: null | ||
} | ||
}) | ||
for (const job of jobs) { | ||
await selectJobCandidate(job.id, resourceBooking.userId) | ||
await assignJob(job) | ||
} | ||
} | ||
|
||
module.exports = { | ||
processUpdate | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.