Skip to content

[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
2 changes: 1 addition & 1 deletion src/eventHandlers/JobEventHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function cancelJob (payload) {
return
}
if (payload.value.status !== 'cancelled') {
logger.info({
logger.debug({
component: 'JobEventHandler',
context: 'cancelJob',
message: `not interested job - status: ${payload.value.status}`
Expand Down
30 changes: 16 additions & 14 deletions src/eventHandlers/ResourceBookingEventHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ async function selectJobCandidate (jobId, userId) {
/**
* Update the status of the Job to assigned when it positions requirement is fullfilled.
*
* @param {Object} job the job data
* @param {String} jobId the job id
* @returns {undefined}
*/
async function assignJob (job) {
async function assignJob (jobId) {
const job = await models.Job.findById(jobId)
if (job.status === 'assigned') {
logger.info({
logger.debug({
component: 'ResourceBookingEventHandler',
context: 'assignJob',
message: `job with projectId ${job.projectId} is already assigned`
Expand All @@ -59,6 +60,7 @@ async function assignJob (job) {
}
const resourceBookings = await models.ResourceBooking.findAll({
where: {
jobId: job.id,
status: 'assigned',
deletedAt: null
}
Expand All @@ -70,7 +72,7 @@ async function assignJob (job) {
})
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` })
logger.info({ component: 'ResourceBookingEventHandler', context: 'assignJob', message: `job ${job.id} is assigned` })
}
}

Expand All @@ -90,24 +92,24 @@ async function processUpdate (payload) {
return
}
if (payload.value.status !== 'assigned') {
logger.info({
logger.debug({
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({
where: {
projectId: resourceBooking.projectId,
deletedAt: null
}
})
for (const job of jobs) {
await selectJobCandidate(job.id, resourceBooking.userId)
await assignJob(job)
if (!resourceBooking.jobId) {
logger.debug({
component: 'ResourceBookingEventHandler',
context: 'processUpdate',
message: `id: ${resourceBooking.id} resource booking without jobId - ignored`
})
return
}
await selectJobCandidate(resourceBooking.jobId, resourceBooking.userId)
await assignJob(resourceBooking.jobId)
}

module.exports = {
Expand Down
5 changes: 3 additions & 2 deletions src/eventHandlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ const TopicOperationMapping = {
*/
async function handleEvent (topic, payload) {
if (!TopicOperationMapping[topic]) {
logger.info({ component: 'eventHanders', context: 'handleEvent', message: `not interested event - topic: ${topic}` })
logger.debug({ component: 'eventHanders', context: 'handleEvent', message: `not interested event - topic: ${topic}` })
return
}
logger.info({ component: 'eventHanders', context: 'handleEvent', message: `event handling - topic: ${topic}` })
logger.debug({ component: 'eventHanders', context: 'handleEvent', message: `handling event - topic: ${topic} - payload: ${JSON.stringify(payload)}` })
try {
await TopicOperationMapping[topic](payload)
Expand All @@ -33,7 +34,7 @@ async function handleEvent (topic, payload) {
// throw error so that it can be handled by the app
throw err
}
logger.info({ component: 'eventHanders', context: 'handleEvent', message: 'event successfully handled' })
logger.info({ component: 'eventHanders', context: 'handleEvent', message: `event successfully handled - topic: ${topic}` })
}

/**
Expand Down