|
| 1 | +/** |
| 2 | + * Interview Processor Service |
| 3 | + */ |
| 4 | + |
| 5 | +const Joi = require('@hapi/joi') |
| 6 | +const logger = require('../common/logger') |
| 7 | +const helper = require('../common/helper') |
| 8 | +const constants = require('../common/constants') |
| 9 | +const config = require('config') |
| 10 | + |
| 11 | +const esClient = helper.getESClient() |
| 12 | + |
| 13 | +/** |
| 14 | + * Updates jobCandidate via a painless script |
| 15 | + * |
| 16 | + * @param {String} jobCandidateId job candidate id |
| 17 | + * @param {String} script script definition |
| 18 | + * @param {String} transactionId transaction id |
| 19 | + */ |
| 20 | +async function updateJobCandidateViaScript (jobCandidateId, script, transactionId) { |
| 21 | + await esClient.updateExtra({ |
| 22 | + index: config.get('esConfig.ES_INDEX_JOB_CANDIDATE'), |
| 23 | + id: jobCandidateId, |
| 24 | + transactionId, |
| 25 | + body: { script }, |
| 26 | + refresh: constants.esRefreshOption |
| 27 | + }) |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Process request interview entity message. |
| 32 | + * Creates an interview record under jobCandidate. |
| 33 | + * |
| 34 | + * @param {Object} message the kafka message |
| 35 | + * @param {String} transactionId |
| 36 | + */ |
| 37 | +async function processRequestInterview (message, transactionId) { |
| 38 | + const interview = message.payload |
| 39 | + // add interview in collection if there's already an existing collection |
| 40 | + // or initiate a new one with this interview |
| 41 | + const script = { |
| 42 | + source: ` |
| 43 | + ctx._source.containsKey("interviews") |
| 44 | + ? ctx._source.interviews.add(params.interview) |
| 45 | + : ctx._source.interviews = [params.interview] |
| 46 | + `, |
| 47 | + params: { interview } |
| 48 | + } |
| 49 | + await updateJobCandidateViaScript(interview.jobCandidateId, script, transactionId) |
| 50 | +} |
| 51 | + |
| 52 | +processRequestInterview.schema = { |
| 53 | + message: Joi.object().keys({ |
| 54 | + topic: Joi.string().required(), |
| 55 | + originator: Joi.string().required(), |
| 56 | + timestamp: Joi.date().required(), |
| 57 | + 'mime-type': Joi.string().required(), |
| 58 | + payload: Joi.object().keys({ |
| 59 | + id: Joi.string().uuid().required(), |
| 60 | + jobCandidateId: Joi.string().uuid().required(), |
| 61 | + googleCalendarId: Joi.string().allow(null), |
| 62 | + customMessage: Joi.string().allow(null), |
| 63 | + xaiTemplate: Joi.string().required(), |
| 64 | + round: Joi.number().integer().positive().required(), |
| 65 | + status: Joi.interviewStatus().required(), |
| 66 | + createdAt: Joi.date().required(), |
| 67 | + createdBy: Joi.string().uuid().required(), |
| 68 | + updatedAt: Joi.date().allow(null), |
| 69 | + updatedBy: Joi.string().uuid().allow(null) |
| 70 | + }).required() |
| 71 | + }).required(), |
| 72 | + transactionId: Joi.string().required() |
| 73 | +} |
| 74 | + |
| 75 | +module.exports = { |
| 76 | + processRequestInterview |
| 77 | +} |
| 78 | + |
| 79 | +logger.buildService(module.exports, 'InterviewProcessorService') |
0 commit comments