Skip to content

Commit 6d8dfff

Browse files
committed
decouple functions assignJob and selectJobCandidate to increase maintainability
1 parent 7f5a621 commit 6d8dfff

File tree

2 files changed

+60
-36
lines changed

2 files changed

+60
-36
lines changed

src/common/helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,4 +667,4 @@ module.exports = {
667667
ensureUserById,
668668
getAuditM2Muser,
669669
checkIsMemberOfProject
670-
}
670+
}

src/eventHandlers/ResourceBookingEventHandler.js

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,39 @@ const JobCandidateService = require('../services/JobCandidateService')
1414
* the corresponding JobCandidate record (with the same userId and jobId)
1515
* should be updated with status `selected`
1616
*
17-
* @param {String} jobId the job id
18-
* @param {String} userId the user id
17+
* @param {Object} payload the event payload
1918
* @returns {undefined}
2019
*/
21-
async function selectJobCandidate (jobId, userId) {
20+
async function selectJobCandidate (payload) {
21+
if (payload.value.status === payload.options.oldValue.status) {
22+
logger.debug({
23+
component: 'ResourceBookingEventHandler',
24+
context: 'selectJobCandidate',
25+
message: 'status not changed'
26+
})
27+
return
28+
}
29+
if (payload.value.status !== 'assigned') {
30+
logger.debug({
31+
component: 'ResourceBookingEventHandler',
32+
context: 'selectJobCandidate',
33+
message: `not interested resource booking - status: ${payload.value.status}`
34+
})
35+
return
36+
}
37+
const resourceBooking = await models.ResourceBooking.findById(payload.value.id)
38+
if (!resourceBooking.jobId) {
39+
logger.debug({
40+
component: 'ResourceBookingEventHandler',
41+
context: 'selectJobCandidate',
42+
message: `id: ${resourceBooking.id} resource booking without jobId - ignored`
43+
})
44+
return
45+
}
2246
const candidates = await models.JobCandidate.findAll({
2347
where: {
24-
jobId,
25-
userId,
48+
jobId: resourceBooking.jobId,
49+
userId: resourceBooking.userId,
2650
status: {
2751
[Op.not]: 'selected'
2852
},
@@ -45,11 +69,36 @@ async function selectJobCandidate (jobId, userId) {
4569
/**
4670
* Update the status of the Job to assigned when it positions requirement is fullfilled.
4771
*
48-
* @param {String} jobId the job id
72+
* @param {Object} payload the event payload
4973
* @returns {undefined}
5074
*/
51-
async function assignJob (jobId) {
52-
const job = await models.Job.findById(jobId)
75+
async function assignJob (payload) {
76+
if (payload.value.status === payload.options.oldValue.status) {
77+
logger.debug({
78+
component: 'ResourceBookingEventHandler',
79+
context: 'assignJob',
80+
message: 'status not changed'
81+
})
82+
return
83+
}
84+
if (payload.value.status !== 'assigned') {
85+
logger.debug({
86+
component: 'ResourceBookingEventHandler',
87+
context: 'assignJob',
88+
message: `not interested resource booking - status: ${payload.value.status}`
89+
})
90+
return
91+
}
92+
const resourceBooking = await models.ResourceBooking.findById(payload.value.id)
93+
if (!resourceBooking.jobId) {
94+
logger.debug({
95+
component: 'ResourceBookingEventHandler',
96+
context: 'assignJob',
97+
message: `id: ${resourceBooking.id} resource booking without jobId - ignored`
98+
})
99+
return
100+
}
101+
const job = await models.Job.findById(resourceBooking.jobId)
53102
if (job.status === 'assigned') {
54103
logger.debug({
55104
component: 'ResourceBookingEventHandler',
@@ -83,33 +132,8 @@ async function assignJob (jobId) {
83132
* @returns {undefined}
84133
*/
85134
async function processUpdate (payload) {
86-
if (payload.value.status === payload.options.oldValue.status) {
87-
logger.debug({
88-
component: 'ResourceBookingEventHandler',
89-
context: 'processUpdate',
90-
message: 'status not changed'
91-
})
92-
return
93-
}
94-
if (payload.value.status !== 'assigned') {
95-
logger.debug({
96-
component: 'ResourceBookingEventHandler',
97-
context: 'processUpdate',
98-
message: `not interested resource booking - status: ${payload.value.status}`
99-
})
100-
return
101-
}
102-
const resourceBooking = await models.ResourceBooking.findById(payload.value.id)
103-
if (!resourceBooking.jobId) {
104-
logger.debug({
105-
component: 'ResourceBookingEventHandler',
106-
context: 'processUpdate',
107-
message: `id: ${resourceBooking.id} resource booking without jobId - ignored`
108-
})
109-
return
110-
}
111-
await selectJobCandidate(resourceBooking.jobId, resourceBooking.userId)
112-
await assignJob(resourceBooking.jobId)
135+
await selectJobCandidate(payload)
136+
await assignJob(payload)
113137
}
114138

115139
module.exports = {

0 commit comments

Comments
 (0)