|
| 1 | +/** |
| 2 | + * Interview Processor |
| 3 | + */ |
| 4 | + |
| 5 | +const _ = require('lodash') |
| 6 | +const helper = require('../common/helper') |
| 7 | +const config = require('config') |
| 8 | + |
| 9 | +const esClient = helper.getESClient() |
| 10 | + |
| 11 | +/** |
| 12 | + * Updates jobCandidate via a painless script |
| 13 | + * |
| 14 | + * @param {String} jobCandidateId job candidate id |
| 15 | + * @param {String} script script definition |
| 16 | + */ |
| 17 | +async function updateJobCandidateViaScript (jobCandidateId, script) { |
| 18 | + await esClient.update({ |
| 19 | + index: config.get('esConfig.ES_INDEX_JOB_CANDIDATE'), |
| 20 | + id: jobCandidateId, |
| 21 | + body: { script }, |
| 22 | + refresh: 'wait_for' |
| 23 | + }) |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Process request interview entity. |
| 28 | + * Creates an interview record under jobCandidate. |
| 29 | + * |
| 30 | + * @param {Object} interview interview object |
| 31 | + */ |
| 32 | +async function processRequestInterview (interview) { |
| 33 | + // add interview in collection if there's already an existing collection |
| 34 | + // or initiate a new one with this interview |
| 35 | + const script = { |
| 36 | + source: ` |
| 37 | + ctx._source.containsKey("interviews") |
| 38 | + ? ctx._source.interviews.add(params.interview) |
| 39 | + : ctx._source.interviews = [params.interview] |
| 40 | + `, |
| 41 | + params: { interview } |
| 42 | + } |
| 43 | + await updateJobCandidateViaScript(interview.jobCandidateId, script) |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Process update interview entity |
| 48 | + * Updates the interview record under jobCandidate. |
| 49 | + * |
| 50 | + * @param {Object} interview interview object |
| 51 | + */ |
| 52 | +async function processUpdateInterview (interview) { |
| 53 | + // if there's an interview with this id, |
| 54 | + // update it |
| 55 | + const script = { |
| 56 | + source: ` |
| 57 | + if (ctx._source.containsKey("interviews")) { |
| 58 | + def target = ctx._source.interviews.find(i -> i.id == params.interview.id); |
| 59 | + if (target != null) { |
| 60 | + for (prop in params.interview.entrySet()) { |
| 61 | + target[prop.getKey()] = prop.getValue() |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + `, |
| 66 | + params: { interview } |
| 67 | + } |
| 68 | + await updateJobCandidateViaScript(interview.jobCandidateId, script) |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Process bulk (partially) update interviews entity. |
| 73 | + * Currently supports status, updatedAt and updatedBy fields. |
| 74 | + * Update Joi schema to allow more fields. |
| 75 | + * (implementation should already handle new fields - just updating Joi schema should be enough) |
| 76 | + * |
| 77 | + * payload format: |
| 78 | + * { |
| 79 | + * "jobCandidateId": { |
| 80 | + * "interviewId": { ...fields }, |
| 81 | + * "interviewId2": { ...fields }, |
| 82 | + * ... |
| 83 | + * }, |
| 84 | + * "jobCandidateId2": { // like above... }, |
| 85 | + * ... |
| 86 | + * } |
| 87 | + * |
| 88 | + * @param {Object} jobCandidates job candidates |
| 89 | + */ |
| 90 | +async function processBulkUpdateInterviews (jobCandidates) { |
| 91 | + // script to update & params |
| 92 | + const script = { |
| 93 | + source: ` |
| 94 | + def completedInterviews = params.jobCandidates[ctx._id]; |
| 95 | + for (interview in completedInterviews.entrySet()) { |
| 96 | + def interviewId = interview.getKey(); |
| 97 | + def affectedFields = interview.getValue(); |
| 98 | + def target = ctx._source.interviews.find(i -> i.id == interviewId); |
| 99 | + if (target != null) { |
| 100 | + for (field in affectedFields.entrySet()) { |
| 101 | + target[field.getKey()] = field.getValue(); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + `, |
| 106 | + params: { jobCandidates } |
| 107 | + } |
| 108 | + // update interviews |
| 109 | + await esClient.updateByQuery({ |
| 110 | + index: config.get('esConfig.ES_INDEX_JOB_CANDIDATE'), |
| 111 | + body: { |
| 112 | + script, |
| 113 | + query: { |
| 114 | + ids: { |
| 115 | + values: _.keys(jobCandidates) |
| 116 | + } |
| 117 | + } |
| 118 | + }, |
| 119 | + refresh: true |
| 120 | + }) |
| 121 | +} |
| 122 | + |
| 123 | +module.exports = { |
| 124 | + processRequestInterview, |
| 125 | + processUpdateInterview, |
| 126 | + processBulkUpdateInterviews |
| 127 | +} |
0 commit comments