|
| 1 | +/** |
| 2 | + * WorkPeriod 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 | + * Process create entity message |
| 15 | + * @param {Object} message the kafka message |
| 16 | + * @param {String} transactionId |
| 17 | + */ |
| 18 | +async function processCreate (message, transactionId) { |
| 19 | + const workPeriod = message.payload |
| 20 | + await esClient.createExtra({ |
| 21 | + index: config.get('esConfig.ES_INDEX_WORK_PERIOD'), |
| 22 | + id: workPeriod.id, |
| 23 | + transactionId, |
| 24 | + body: workPeriod, |
| 25 | + refresh: constants.esRefreshOption |
| 26 | + }) |
| 27 | +} |
| 28 | + |
| 29 | +processCreate.schema = { |
| 30 | + message: Joi.object().keys({ |
| 31 | + topic: Joi.string().required(), |
| 32 | + originator: Joi.string().required(), |
| 33 | + timestamp: Joi.date().required(), |
| 34 | + 'mime-type': Joi.string().required(), |
| 35 | + payload: Joi.object().keys({ |
| 36 | + id: Joi.string().uuid().required(), |
| 37 | + resourceBookingId: Joi.string().uuid().required(), |
| 38 | + userHandle: Joi.string().required(), |
| 39 | + projectId: Joi.number().integer().required(), |
| 40 | + startDate: Joi.string().required(), |
| 41 | + endDate: Joi.string().required(), |
| 42 | + daysWorked: Joi.number().integer().min(0).allow(null), |
| 43 | + memberRate: Joi.number().allow(null), |
| 44 | + customerRate: Joi.number().allow(null), |
| 45 | + paymentStatus: Joi.paymentStatus().required(), |
| 46 | + createdAt: Joi.date().required(), |
| 47 | + createdBy: Joi.string().uuid().required(), |
| 48 | + updatedAt: Joi.date().allow(null), |
| 49 | + updatedBy: Joi.string().uuid().allow(null) |
| 50 | + }).required() |
| 51 | + }).required(), |
| 52 | + transactionId: Joi.string().required() |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Process update entity message |
| 57 | + * @param {Object} message the kafka message |
| 58 | + * @param {String} transactionId |
| 59 | + */ |
| 60 | +async function processUpdate (message, transactionId) { |
| 61 | + const data = message.payload |
| 62 | + await esClient.updateExtra({ |
| 63 | + index: config.get('esConfig.ES_INDEX_WORK_PERIOD'), |
| 64 | + id: data.id, |
| 65 | + transactionId, |
| 66 | + body: { |
| 67 | + doc: data |
| 68 | + }, |
| 69 | + refresh: constants.esRefreshOption |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +processUpdate.schema = processCreate.schema |
| 74 | + |
| 75 | +/** |
| 76 | + * Process delete entity message |
| 77 | + * @param {Object} message the kafka message |
| 78 | + * @param {String} transactionId |
| 79 | + */ |
| 80 | +async function processDelete (message, transactionId) { |
| 81 | + const id = message.payload.id |
| 82 | + await esClient.deleteExtra({ |
| 83 | + index: config.get('esConfig.ES_INDEX_WORK_PERIOD'), |
| 84 | + id, |
| 85 | + transactionId, |
| 86 | + refresh: constants.esRefreshOption |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +processDelete.schema = { |
| 91 | + message: Joi.object().keys({ |
| 92 | + topic: Joi.string().required(), |
| 93 | + originator: Joi.string().required(), |
| 94 | + timestamp: Joi.date().required(), |
| 95 | + 'mime-type': Joi.string().required(), |
| 96 | + payload: Joi.object().keys({ |
| 97 | + id: Joi.string().uuid().required() |
| 98 | + }).required() |
| 99 | + }).required(), |
| 100 | + transactionId: Joi.string().required() |
| 101 | +} |
| 102 | + |
| 103 | +module.exports = { |
| 104 | + processCreate, |
| 105 | + processUpdate, |
| 106 | + processDelete |
| 107 | +} |
| 108 | + |
| 109 | +logger.buildService(module.exports, 'WorkPeriodProcessorService') |
0 commit comments