Skip to content

Commit 6c384ae

Browse files
committed
fix: update JC in Zapier only when status changed
1 parent 387c397 commit 6c384ae

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/common/helper.js

+16
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ function getESClient () {
109109
}
110110
}
111111

112+
// get document or catch not found error
113+
esClient.getExtra = async function (data) {
114+
let doc
115+
116+
try {
117+
doc = await esClient.getSource(data)
118+
} catch (err) {
119+
if (err.statusCode === 404) {
120+
throw new Error(`id: ${data.id} "${data.index}" not found`)
121+
}
122+
throw err
123+
}
124+
125+
return doc
126+
}
127+
112128
// delete document or catch not found error
113129
esClient.deleteExtra = async function (data) {
114130
try {

src/services/JobCandidateProcessorService.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ const localLogger = {
2020
* @param {Object} message the message object
2121
* @returns {undefined}
2222
*/
23-
async function updateCandidateStatus ({ type, payload }) {
24-
if (!payload.status) {
25-
localLogger.debug({ context: 'updateCandidateStatus', message: 'status not updated' })
23+
async function updateCandidateStatus ({ type, payload, previousData }) {
24+
if (previousData.status === payload.status) {
25+
localLogger.debug({ context: 'updateCandidateStatus', message: `jobCandidate is already in status: ${payload.status}` })
2626
return
2727
}
2828
if (!['rejected', 'shortlist'].includes(payload.status)) {
@@ -56,13 +56,13 @@ async function updateCandidateStatus ({ type, payload }) {
5656
* @param {Object} message the message object
5757
* @returns {undefined}
5858
*/
59-
async function postMessageToZapier ({ type, payload }) {
59+
async function postMessageToZapier ({ type, payload, previousData }) {
6060
if (config.zapier.ZAPIER_JOB_CANDIDATE_SWITCH === constants.Zapier.Switch.OFF) {
6161
localLogger.debug({ context: 'postMessageToZapier', message: 'Zapier Switch off via config, no messages sent' })
6262
return
6363
}
6464
if (type === constants.Zapier.MessageType.JobCandidateUpdate) {
65-
await updateCandidateStatus({ type, payload })
65+
await updateCandidateStatus({ type, payload, previousData })
6666
return
6767
}
6868
throw new Error(`unrecognized message type: ${type}`)
@@ -113,6 +113,12 @@ processCreate.schema = {
113113
*/
114114
async function processUpdate (message, transactionId) {
115115
const data = message.payload
116+
// save previous data for Zapier logic
117+
// NOTE: ideally if we update Kafka event message to have both: pervious and updated value so we don't have to request it again
118+
const { body: previousData } = await esClient.getExtra({
119+
index: config.get('esConfig.ES_INDEX_JOB_CANDIDATE'),
120+
id: data.id
121+
})
116122
await esClient.updateExtra({
117123
index: config.get('esConfig.ES_INDEX_JOB_CANDIDATE'),
118124
id: data.id,
@@ -124,7 +130,8 @@ async function processUpdate (message, transactionId) {
124130
})
125131
await postMessageToZapier({
126132
type: constants.Zapier.MessageType.JobCandidateUpdate,
127-
payload: data
133+
payload: data,
134+
previousData
128135
})
129136
}
130137

0 commit comments

Comments
 (0)