|
| 1 | +/** |
| 2 | + * Service for customer payment Elasticsearch processor. |
| 3 | + */ |
| 4 | + |
| 5 | +const Joi = require('joi') |
| 6 | +const config = require('config') |
| 7 | +const _ = require('lodash') |
| 8 | + |
| 9 | +const logger = require('../common/logger') |
| 10 | +const helper = require('../common/helper') |
| 11 | +const { CUSTOMER_PAYMENT_STATUS } = require('../constants') |
| 12 | + |
| 13 | +const client = helper.getESClient() |
| 14 | + |
| 15 | +/** |
| 16 | + * create schema |
| 17 | + * @return {Object} the schema |
| 18 | + */ |
| 19 | +function createIdSchema () { |
| 20 | + return Joi.object().keys({ |
| 21 | + id: Joi.number().integer().positive().required() |
| 22 | + }).unknown(true).required() |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * create schema |
| 27 | + * @return {Object} the schema |
| 28 | + */ |
| 29 | +function createSchema () { |
| 30 | + return createIdSchema().keys({ |
| 31 | + amount: Joi.number().integer().min(1).required(), |
| 32 | + currency: Joi.string().required(), |
| 33 | + paymentIntentId: Joi.string().required(), |
| 34 | + status: Joi.string().valid(_.values(CUSTOMER_PAYMENT_STATUS)).required(), |
| 35 | + reference: Joi.string().optional(), |
| 36 | + referenceId: Joi.string().optional(), |
| 37 | + createdAt: Joi.any(), |
| 38 | + updatedAt: Joi.any(), |
| 39 | + deletedAt: Joi.any(), |
| 40 | + createdBy: Joi.any(), |
| 41 | + updatedBy: Joi.any(), |
| 42 | + deletedBy: Joi.any() |
| 43 | + }).unknown(true).required() |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Create message in Elasticsearch. |
| 48 | + * @param {Object} message the customer payment created message |
| 49 | + * @return {Promise} promise result |
| 50 | + */ |
| 51 | +async function create (message) { |
| 52 | + await client.create({ |
| 53 | + index: config.get('esConfig.ES_CUSTOMER_PAYMENT_INDEX'), |
| 54 | + type: config.get('esConfig.ES_TYPE'), |
| 55 | + id: message.id, |
| 56 | + body: message |
| 57 | + }) |
| 58 | + logger.debug(`CustomerPayment created successfully in elasticsearch index, (customerPayment: ${JSON.stringify(message)})`) |
| 59 | +} |
| 60 | + |
| 61 | +create.schema = { |
| 62 | + message: createSchema() |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Update message in Elasticsearch. |
| 67 | + * @param {Object} message the customer payment updated message |
| 68 | + * @return {Promise} promise result |
| 69 | + */ |
| 70 | +async function update (message) { |
| 71 | + await client.update({ |
| 72 | + index: config.get('esConfig.ES_CUSTOMER_PAYMENT_INDEX'), |
| 73 | + type: config.get('esConfig.ES_TYPE'), |
| 74 | + id: message.id, |
| 75 | + body: { |
| 76 | + doc: message |
| 77 | + } |
| 78 | + }) |
| 79 | + logger.debug(`CustomerPayment updated successfully in elasticsearch index, (customerPayment: ${message.id})`) |
| 80 | +} |
| 81 | + |
| 82 | +update.schema = { |
| 83 | + message: createSchema() |
| 84 | +} |
| 85 | + |
| 86 | +// Exports |
| 87 | +module.exports = { |
| 88 | + create, |
| 89 | + update |
| 90 | +} |
| 91 | + |
| 92 | +logger.buildService(module.exports) |
0 commit comments