Skip to content

Commit 1041679

Browse files
committed
enhancement: encapsulate old value to the event payload as well
1 parent b64ac86 commit 1041679

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

src/common/helper.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,9 @@ async function getUserId (userId) {
282282
* Send Kafka event message
283283
* @params {String} topic the topic name
284284
* @params {Object} payload the payload
285+
* @params {Object} options the extra options to control the function
285286
*/
286-
async function postEvent (topic, payload) {
287+
async function postEvent (topic, payload, options = {}) {
287288
logger.debug({ component: 'helper', context: 'postEvent', message: `Posting event to Kafka topic ${topic}, ${JSON.stringify(payload)}` })
288289
const client = getBusApiClient()
289290
const message = {
@@ -294,7 +295,7 @@ async function postEvent (topic, payload) {
294295
payload
295296
}
296297
await client.postEvent(message)
297-
await eventDispatcher.handleEvent(topic, payload)
298+
await eventDispatcher.handleEvent(topic, { value: payload, options })
298299
}
299300

300301
/**

src/eventHandlers/JobEventHandler.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,25 @@ const ResourceBookingService = require('../services/ResourceBookingService')
1616
* @returns {undefined}
1717
*/
1818
async function cancelJob (payload) {
19-
if (payload.status !== 'cancelled') {
19+
if (payload.value.status === payload.options.oldValue.status) {
20+
logger.debug({
21+
component: 'JobEventHandler',
22+
context: 'cancelJob',
23+
message: 'status not changed'
24+
})
25+
return
26+
}
27+
if (payload.value.status !== 'cancelled') {
2028
logger.info({
2129
component: 'JobEventHandler',
2230
context: 'cancelJob',
23-
message: `not interested job - status: ${payload.status}`
31+
message: `not interested job - status: ${payload.value.status}`
2432
})
2533
return
2634
}
2735
// pull data from db instead of directly extract data from the payload
2836
// since the payload may not contain all fields when it is from partically update operation.
29-
const job = await models.Job.findById(payload.id)
37+
const job = await models.Job.findById(payload.value.id)
3038
const candidates = await models.JobCandidate.findAll({
3139
where: {
3240
jobId: job.id,

src/eventHandlers/ResourceBookingEventHandler.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,23 @@ async function assignJob (job) {
8181
* @returns {undefined}
8282
*/
8383
async function processUpdate (payload) {
84-
if (payload.status !== 'assigned') {
84+
if (payload.value.status === payload.options.oldValue.status) {
85+
logger.debug({
86+
component: 'ResourceBookingEventHandler',
87+
context: 'processUpdate',
88+
message: 'status not changed'
89+
})
90+
return
91+
}
92+
if (payload.value.status !== 'assigned') {
8593
logger.info({
8694
component: 'ResourceBookingEventHandler',
8795
context: 'processUpdate',
88-
message: `not interested resource booking - status: ${payload.status}`
96+
message: `not interested resource booking - status: ${payload.value.status}`
8997
})
9098
return
9199
}
92-
const resourceBooking = await models.ResourceBooking.findById(payload.id)
100+
const resourceBooking = await models.ResourceBooking.findById(payload.value.id)
93101
const jobs = await models.Job.findAll({
94102
where: {
95103
projectId: resourceBooking.projectId,

src/services/JobService.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ async function updateJob (currentUser, id, data) {
192192
await _validateSkills(data.skills)
193193
}
194194
let job = await Job.findById(id)
195+
const oldValue = job.toJSON()
195196
const ubhanUserId = await helper.getUserId(currentUser.userId)
196197
if (!currentUser.hasManagePermission && !currentUser.isMachine) {
197198
if (currentUser.isConnectManager) {
@@ -209,7 +210,7 @@ async function updateJob (currentUser, id, data) {
209210
data.updatedBy = ubhanUserId
210211

211212
await job.update(data)
212-
await helper.postEvent(config.TAAS_JOB_UPDATE_TOPIC, { id, ...data })
213+
await helper.postEvent(config.TAAS_JOB_UPDATE_TOPIC, { id, ...data }, { oldValue: oldValue })
213214
job = await Job.findById(id, true)
214215
job.dataValues.candidates = _.map(job.dataValues.candidates, (c) => helper.clearObject(c.dataValues))
215216
return helper.clearObject(job.dataValues)

src/services/ResourceBookingService.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,13 @@ async function updateResourceBooking (currentUser, id, data) {
142142
}
143143

144144
const resourceBooking = await ResourceBooking.findById(id)
145-
const isDiffStatus = resourceBooking.status !== data.status
145+
const oldValue = resourceBooking.toJSON()
146146

147147
data.updatedAt = new Date()
148148
data.updatedBy = await helper.getUserId(currentUser.userId)
149149

150150
await resourceBooking.update(data)
151-
const eventPayload = { id, ...(isDiffStatus ? data : _.omit(data, ['status'])) } // send data with status only if status is changed
152-
await helper.postEvent(config.TAAS_RESOURCE_BOOKING_UPDATE_TOPIC, eventPayload)
151+
await helper.postEvent(config.TAAS_RESOURCE_BOOKING_UPDATE_TOPIC, { id, ...data }, { oldValue: oldValue })
153152
const result = helper.clearObject(_.assign(resourceBooking.dataValues, data))
154153
return result
155154
}

0 commit comments

Comments
 (0)