|
| 1 | +/** |
| 2 | + * Interview Processor Service |
| 3 | + */ |
| 4 | + |
| 5 | +const Joi = require('@hapi/joi') |
| 6 | +const _ = require('lodash') |
| 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.xaiTemplate().required(), |
| 65 | + round: Joi.number().integer().positive().required(), |
| 66 | + startTimestamp: Joi.date().allow(null), |
| 67 | + attendeesList: Joi.array().items(Joi.string().email()).allow(null), |
| 68 | + status: Joi.interviewStatus().required(), |
| 69 | + createdAt: Joi.date().required(), |
| 70 | + createdBy: Joi.string().uuid().required(), |
| 71 | + updatedAt: Joi.date().allow(null), |
| 72 | + updatedBy: Joi.string().uuid().allow(null) |
| 73 | + }).required() |
| 74 | + }).required(), |
| 75 | + transactionId: Joi.string().required() |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Process update interview entity message |
| 80 | + * Updates the 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 interview = message.payload |
| 87 | + // if there's an interview with this id, |
| 88 | + // update it with the payload |
| 89 | + const script = { |
| 90 | + source: ` |
| 91 | + if (ctx._source.containsKey("interviews")) { |
| 92 | + def target = ctx._source.interviews.find(i -> i.id == params.interview.id); |
| 93 | + if (target != null) { |
| 94 | + for (prop in params.interview.entrySet()) { |
| 95 | + target[prop.getKey()] = prop.getValue() |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + `, |
| 100 | + params: { interview } |
| 101 | + } |
| 102 | + await updateJobCandidateViaScript(interview.jobCandidateId, script, transactionId) |
| 103 | +} |
| 104 | + |
| 105 | +processUpdateInterview.schema = processRequestInterview.schema |
| 106 | + |
| 107 | +/** |
| 108 | + * Process bulk (partially) update interviews entity message. |
| 109 | + * Currently supports status, updatedAt and updatedBy fields. |
| 110 | + * Update Joi schema to allow more fields. |
| 111 | + * (implementation should already handle new fields - just updating Joi schema should be enough) |
| 112 | + * |
| 113 | + * payload format: |
| 114 | + * { |
| 115 | + * "jobCandidateId": { |
| 116 | + * "interviewId": { ...fields }, |
| 117 | + * "interviewId2": { ...fields }, |
| 118 | + * ... |
| 119 | + * }, |
| 120 | + * "jobCandidateId2": { // like above... }, |
| 121 | + * ... |
| 122 | + * } |
| 123 | + * |
| 124 | + * @param {Object} message the kafka message |
| 125 | + * @param {String} transactionId |
| 126 | + */ |
| 127 | +async function processBulkUpdateInterviews (message, transactionId) { |
| 128 | + const jobCandidates = message.payload |
| 129 | + // script to update & params |
| 130 | + const script = { |
| 131 | + source: ` |
| 132 | + def completedInterviews = params.jobCandidates[ctx._id]; |
| 133 | + for (interview in completedInterviews.entrySet()) { |
| 134 | + def interviewId = interview.getKey(); |
| 135 | + def affectedFields = interview.getValue(); |
| 136 | + def target = ctx._source.interviews.find(i -> i.id == interviewId); |
| 137 | + if (target != null) { |
| 138 | + for (field in affectedFields.entrySet()) { |
| 139 | + target[field.getKey()] = field.getValue(); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + `, |
| 144 | + params: { jobCandidates } |
| 145 | + } |
| 146 | + // update interviews |
| 147 | + await esClient.updateByQuery({ |
| 148 | + index: config.get('esConfig.ES_INDEX_JOB_CANDIDATE'), |
| 149 | + transactionId, |
| 150 | + body: { |
| 151 | + script, |
| 152 | + query: { |
| 153 | + ids: { |
| 154 | + values: _.keys(jobCandidates) |
| 155 | + } |
| 156 | + } |
| 157 | + }, |
| 158 | + refresh: true |
| 159 | + }) |
| 160 | +} |
| 161 | + |
| 162 | +processBulkUpdateInterviews.schema = { |
| 163 | + message: Joi.object().keys({ |
| 164 | + topic: Joi.string().required(), |
| 165 | + originator: Joi.string().required(), |
| 166 | + timestamp: Joi.date().required(), |
| 167 | + 'mime-type': Joi.string().required(), |
| 168 | + payload: Joi.object().pattern( |
| 169 | + Joi.string().uuid(), // key - jobCandidateId |
| 170 | + Joi.object().pattern( |
| 171 | + Joi.string().uuid(), // inner key - interviewId |
| 172 | + Joi.object().keys({ |
| 173 | + status: Joi.interviewStatus(), |
| 174 | + updatedAt: Joi.date(), |
| 175 | + updatedBy: Joi.string().uuid() |
| 176 | + }) // inner value - affected fields of interview |
| 177 | + ) // value - object containing interviews |
| 178 | + ).min(1) // at least one key - i.e. don't allow empty object |
| 179 | + }).required(), |
| 180 | + transactionId: Joi.string().required() |
| 181 | +} |
| 182 | + |
| 183 | +module.exports = { |
| 184 | + processRequestInterview, |
| 185 | + processUpdateInterview, |
| 186 | + processBulkUpdateInterviews |
| 187 | +} |
| 188 | + |
| 189 | +logger.buildService(module.exports, 'InterviewProcessorService') |
0 commit comments