|
| 1 | +/** |
| 2 | + * Interview Processor Service |
| 3 | + */ |
| 4 | + |
| 5 | +const _ = require('lodash') |
| 6 | +const Joi = require('@hapi/joi') |
| 7 | +const logger = require('../common/logger') |
| 8 | +const helper = require('../common/helper') |
| 9 | +const constants = require('../common/constants') |
| 10 | +const config = require('config') |
| 11 | + |
| 12 | +const esClient = helper.getESClient() |
| 13 | + |
| 14 | +/** |
| 15 | + * Updates jobCandidate via a painless script |
| 16 | + * |
| 17 | + * @param {String} jobCandidateId job candidate id |
| 18 | + * @param {String} script script definition |
| 19 | + * @param {String} transactionId transaction id |
| 20 | + */ |
| 21 | +async function updateJobCandidateViaScript (jobCandidateId, script, transactionId) { |
| 22 | + await esClient.updateExtra({ |
| 23 | + index: config.get('esConfig.ES_INDEX_JOB_CANDIDATE'), |
| 24 | + id: jobCandidateId, |
| 25 | + transactionId, |
| 26 | + body: { script }, |
| 27 | + refresh: constants.esRefreshOption |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Process request interview entity message. |
| 33 | + * Creates an interview record under jobCandidate. |
| 34 | + * |
| 35 | + * @param {Object} message the kafka message |
| 36 | + * @param {String} transactionId |
| 37 | + */ |
| 38 | +async function processRequestInterview (message, transactionId) { |
| 39 | + const interview = message.payload |
| 40 | + // add interview in collection if there's already an existing collection |
| 41 | + // or initiate a new one with this interview |
| 42 | + const script = { |
| 43 | + source: ` |
| 44 | + ctx._source.containsKey("interviews") |
| 45 | + ? ctx._source.interviews.add(params.interview) |
| 46 | + : ctx._source.interviews = [params.interview] |
| 47 | + `, |
| 48 | + params: { interview } |
| 49 | + } |
| 50 | + await updateJobCandidateViaScript(interview.jobCandidateId, script, transactionId) |
| 51 | +} |
| 52 | + |
| 53 | +processRequestInterview.schema = { |
| 54 | + message: Joi.object().keys({ |
| 55 | + topic: Joi.string().required(), |
| 56 | + originator: Joi.string().required(), |
| 57 | + timestamp: Joi.date().required(), |
| 58 | + 'mime-type': Joi.string().required(), |
| 59 | + payload: Joi.object().keys({ |
| 60 | + id: Joi.string().uuid().required(), |
| 61 | + jobCandidateId: Joi.string().uuid().required(), |
| 62 | + googleCalendarId: Joi.string().allow(null), |
| 63 | + customMessage: Joi.string().allow(null), |
| 64 | + xaiTemplate: Joi.string().required(), |
| 65 | + round: Joi.number().integer().positive().required(), |
| 66 | + status: Joi.interviewStatus().required(), |
| 67 | + createdAt: Joi.date().required(), |
| 68 | + createdBy: Joi.string().uuid().required(), |
| 69 | + updatedAt: Joi.date().allow(null), |
| 70 | + updatedBy: Joi.string().uuid().allow(null), |
| 71 | + attendeesList: Joi.array().items(Joi.string().email()).allow(null), |
| 72 | + startTimestamp: Joi.date().allow(null) |
| 73 | + }).required() |
| 74 | + }).required(), |
| 75 | + transactionId: Joi.string().required() |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Process update interview entity message. |
| 80 | + * Update an interview record under jobCandidate. |
| 81 | + * |
| 82 | + * @param {Object} message the kafka message |
| 83 | + * @param {String} transactionId |
| 84 | + */ |
| 85 | +async function processUpdateInterview (message, transactionId) { |
| 86 | + const data = message.payload |
| 87 | + const { body: jobCandidate } = await esClient.getExtra({ |
| 88 | + index: config.get('esConfig.ES_INDEX_JOB_CANDIDATE'), |
| 89 | + id: data.jobCandidateId |
| 90 | + }) |
| 91 | + const interviews = jobCandidate.interviews || [] |
| 92 | + const index = _.findIndex(interviews, ['id', data.id]) |
| 93 | + if (index === -1) { |
| 94 | + interviews.push(data) |
| 95 | + } else { |
| 96 | + interviews.splice(index, 1, data) |
| 97 | + } |
| 98 | + jobCandidate.interviews = interviews |
| 99 | + await esClient.updateExtra({ |
| 100 | + index: config.get('esConfig.ES_INDEX_JOB_CANDIDATE'), |
| 101 | + id: data.jobCandidateId, |
| 102 | + transactionId, |
| 103 | + body: { |
| 104 | + doc: jobCandidate |
| 105 | + }, |
| 106 | + refresh: constants.esRefreshOption |
| 107 | + }) |
| 108 | +} |
| 109 | + |
| 110 | +processUpdateInterview.schema = { |
| 111 | + message: Joi.object().keys({ |
| 112 | + topic: Joi.string().required(), |
| 113 | + originator: Joi.string().required(), |
| 114 | + timestamp: Joi.date().required(), |
| 115 | + 'mime-type': Joi.string().required(), |
| 116 | + payload: Joi.object().keys({ |
| 117 | + id: Joi.string().uuid().required(), |
| 118 | + jobCandidateId: Joi.string().uuid().required(), |
| 119 | + googleCalendarId: Joi.string().allow(null), |
| 120 | + customMessage: Joi.string().allow(null), |
| 121 | + xaiTemplate: Joi.string().required(), |
| 122 | + round: Joi.number().integer().positive().required(), |
| 123 | + status: Joi.interviewStatus().required(), |
| 124 | + createdAt: Joi.date().required(), |
| 125 | + createdBy: Joi.string().uuid().required(), |
| 126 | + updatedAt: Joi.date().required(), |
| 127 | + updatedBy: Joi.string().uuid().required(), |
| 128 | + attendeesList: Joi.array().items(Joi.string().email()).allow(null), |
| 129 | + startTimestamp: Joi.date().allow(null) |
| 130 | + }).required() |
| 131 | + }).required(), |
| 132 | + transactionId: Joi.string().required() |
| 133 | +} |
| 134 | + |
| 135 | +module.exports = { |
| 136 | + processRequestInterview, |
| 137 | + processUpdateInterview |
| 138 | +} |
| 139 | + |
| 140 | +logger.buildService(module.exports, 'InterviewProcessorService') |
0 commit comments