|
| 1 | +/** |
| 2 | + * WorkPeriodPayment Processor Service |
| 3 | + */ |
| 4 | + |
| 5 | +const Joi = require('@hapi/joi') |
| 6 | +const config = require('config') |
| 7 | +const _ = require('lodash') |
| 8 | +const logger = require('../common/logger') |
| 9 | +const helper = require('../common/helper') |
| 10 | +const constants = require('../common/constants') |
| 11 | + |
| 12 | +const esClient = helper.getESClient() |
| 13 | + |
| 14 | +/** |
| 15 | + * Process create entity message |
| 16 | + * @param {Object} message the kafka message |
| 17 | + * @param {String} transactionId |
| 18 | + */ |
| 19 | +async function processCreate (message, transactionId) { |
| 20 | + const data = message.payload |
| 21 | + const workPeriod = await esClient.getExtra({ |
| 22 | + index: config.get('esConfig.ES_INDEX_WORK_PERIOD'), |
| 23 | + id: data.workPeriodId |
| 24 | + }) |
| 25 | + const payments = _.isArray(workPeriod.body.payments) ? workPeriod.body.payments : [] |
| 26 | + payments.push(data) |
| 27 | + |
| 28 | + return esClient.updateExtra({ |
| 29 | + index: config.get('esConfig.ES_INDEX_WORK_PERIOD'), |
| 30 | + id: data.workPeriodId, |
| 31 | + transactionId, |
| 32 | + body: { |
| 33 | + doc: _.assign(workPeriod.body, { payments }) |
| 34 | + }, |
| 35 | + refresh: constants.esRefreshOption |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +processCreate.schema = { |
| 40 | + message: Joi.object().keys({ |
| 41 | + topic: Joi.string().required(), |
| 42 | + originator: Joi.string().required(), |
| 43 | + timestamp: Joi.date().required(), |
| 44 | + 'mime-type': Joi.string().required(), |
| 45 | + payload: Joi.object().keys({ |
| 46 | + id: Joi.string().uuid().required(), |
| 47 | + workPeriodId: Joi.string().uuid().required(), |
| 48 | + challengeId: Joi.string().uuid().required(), |
| 49 | + amount: Joi.number().greater(0).allow(null), |
| 50 | + status: Joi.workPeriodPaymentStatus().required(), |
| 51 | + createdAt: Joi.date().required(), |
| 52 | + createdBy: Joi.string().uuid().required(), |
| 53 | + updatedAt: Joi.date().allow(null), |
| 54 | + updatedBy: Joi.string().uuid().allow(null) |
| 55 | + }).required() |
| 56 | + }).required(), |
| 57 | + transactionId: Joi.string().required() |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Process update entity message |
| 62 | + * @param {Object} message the kafka message |
| 63 | + * @param {String} transactionId |
| 64 | + */ |
| 65 | +async function processUpdate (message, transactionId) { |
| 66 | + const data = message.payload |
| 67 | + let workPeriod = await esClient.search({ |
| 68 | + index: config.get('esConfig.ES_INDEX_WORK_PERIOD'), |
| 69 | + body: { |
| 70 | + query: { |
| 71 | + nested: { |
| 72 | + path: 'payments', |
| 73 | + query: { |
| 74 | + match: { 'payments.id': data.id } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + }) |
| 80 | + if (!workPeriod.body.hits.total.value) { |
| 81 | + throw new Error(`id: ${data.id} "WorkPeriodPayments" not found`) |
| 82 | + } |
| 83 | + let payments |
| 84 | + // if WorkPeriodPayment's workPeriodId changed then it must be deleted from the old WorkPeriod |
| 85 | + // and added to the new WorkPeriod |
| 86 | + if (workPeriod.body.hits.hits[0]._source.id !== data.workPeriodId) { |
| 87 | + payments = _.filter(workPeriod.body.hits.hits[0]._source.payments, (payment) => payment.id !== data.id) |
| 88 | + await esClient.updateExtra({ |
| 89 | + index: config.get('esConfig.ES_INDEX_WORK_PERIOD'), |
| 90 | + id: workPeriod.body.hits.hits[0]._source.id, |
| 91 | + transactionId, |
| 92 | + body: { |
| 93 | + doc: _.assign(workPeriod.body.hits.hits[0]._source, { payments }) |
| 94 | + } |
| 95 | + }) |
| 96 | + workPeriod = await esClient.getExtra({ |
| 97 | + index: config.get('esConfig.ES_INDEX_WORK_PERIOD'), |
| 98 | + id: data.workPeriodId |
| 99 | + }) |
| 100 | + payments = _.isArray(workPeriod.body.payments) ? workPeriod.body.payments : [] |
| 101 | + payments.push(data) |
| 102 | + return esClient.updateExtra({ |
| 103 | + index: config.get('esConfig.ES_INDEX_WORK_PERIOD'), |
| 104 | + id: data.workPeriodId, |
| 105 | + transactionId, |
| 106 | + body: { |
| 107 | + doc: _.assign(workPeriod.body, { payments }) |
| 108 | + } |
| 109 | + }) |
| 110 | + } |
| 111 | + |
| 112 | + payments = _.map(workPeriod.body.hits.hits[0]._source.payments, (payment) => { |
| 113 | + if (payment.id === data.id) { |
| 114 | + return _.assign(payment, data) |
| 115 | + } |
| 116 | + return payment |
| 117 | + }) |
| 118 | + |
| 119 | + return esClient.updateExtra({ |
| 120 | + index: config.get('esConfig.ES_INDEX_WORK_PERIOD'), |
| 121 | + id: data.workPeriodId, |
| 122 | + transactionId, |
| 123 | + body: { |
| 124 | + doc: _.assign(workPeriod.body.hits.hits[0]._source, { payments }) |
| 125 | + }, |
| 126 | + refresh: constants.esRefreshOption |
| 127 | + }) |
| 128 | +} |
| 129 | + |
| 130 | +processUpdate.schema = processCreate.schema |
| 131 | + |
| 132 | +module.exports = { |
| 133 | + processCreate, |
| 134 | + processUpdate |
| 135 | +} |
| 136 | + |
| 137 | +logger.buildService(module.exports, 'WorkPeriodPaymentProcessorService') |
0 commit comments