|
| 1 | +'use strict' |
| 2 | + |
| 3 | +var async = require('async') |
| 4 | +, _ = require('lodash') |
| 5 | +, utils = require('ief-utils') |
| 6 | +, request = require('request') |
| 7 | +, CONSTANTS = require('../constants') |
| 8 | + |
| 9 | +//constructor |
| 10 | +function AutoBillingHooks(config) { |
| 11 | + |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | +* @param fieldName is "name" of setting for which json object will be returned |
| 16 | +* @param options is options coming from IO |
| 17 | +* @param fieldObj is setting object being returned |
| 18 | +*/ |
| 19 | +AutoBillingHooks.prototype.getSettingField = function(fieldName, settings) { |
| 20 | + var fieldObj = null |
| 21 | + , settingSection = _.find(settings.sections, function(section) { |
| 22 | + fieldObj = _.find(section.fields, function(field) { |
| 23 | + // here fieldName is billingRecordType |
| 24 | + if (field.name.search(fieldName) >= 0) { |
| 25 | + return true |
| 26 | + } |
| 27 | + }) |
| 28 | + if (!!fieldObj) { |
| 29 | + return true |
| 30 | + } |
| 31 | + }) |
| 32 | + return fieldObj |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | +* @param optsObject contains options for request module and json data to be appended to options |
| 37 | +* @param hookResponse is the response to be returned form preSavePage hookResponse |
| 38 | +* @param callback is the callback function |
| 39 | +*/ |
| 40 | +AutoBillingHooks.prototype.invokeInvoiceFlow = function (optsObject, hookResponse, callback){ |
| 41 | + utils.logInSplunk('inside invokeInvoiceFlow| optsObject : ' + JSON.stringify(optsObject) ) |
| 42 | + //no need to invoke invoice flow if there is no data |
| 43 | + if(optsObject.optsArray && !optsObject.optsArray.length) |
| 44 | + return callback(null, hookResponse) |
| 45 | + |
| 46 | + optsObject.opts.json = optsObject.optsArray |
| 47 | + request(optsObject.opts, function(err, response, body) { |
| 48 | + if(err) { |
| 49 | + //TODO write a better way to handle the error here |
| 50 | + utils.logInSplunk('invokeInvoiceFlow | error while triggering Auto-Billing Invoice Flow:' + JSON.stringify(err)) |
| 51 | + utils.logInSplunk('invokeInvoiceFlow | statusCode while triggering Auto-Billing Invoice Flow:' + JSON.stringify(err.code)) |
| 52 | + } |
| 53 | + callback(null, hookResponse) |
| 54 | + }) |
| 55 | + |
| 56 | + |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | +* @param options is the options coming from IO |
| 61 | +* @param cb is the callback function |
| 62 | +*/ |
| 63 | +AutoBillingHooks.prototype.executeAutoBilling= function(options, cb) { |
| 64 | + utils.logInSplunk('invokeAutoBillingFlow | options' + JSON.stringify(options)) |
| 65 | + var optsArray = [] |
| 66 | + , optsObject = {} |
| 67 | + , hookResponseData = [] |
| 68 | + , hookResponse = {} |
| 69 | + , self = this |
| 70 | + , invoiceAutoBillingExportId = null |
| 71 | + , cashsaleAndInvoiceSettingFieldValue = null |
| 72 | + , cashsaleAndInvoiceSettingField = self.getSettingField('billingRecordType', options.settings) |
| 73 | + , opts = null |
| 74 | + |
| 75 | + if(!options.settings.commonresources.invoiceAutoBillingExportId){ |
| 76 | + return cb(new Error('Could not find invoiceAutoBillingExportId inside commonresources of settings.')) |
| 77 | + } |
| 78 | + |
| 79 | + invoiceAutoBillingExportId = options.settings.commonresources.invoiceAutoBillingExportId |
| 80 | + |
| 81 | + opts = { |
| 82 | + uri: CONSTANTS.HERCULES_BASE_URL + '/v1/exports/' + invoiceAutoBillingExportId + '/data' |
| 83 | + , method: 'POST' |
| 84 | + , headers: { |
| 85 | + Authorization: 'Bearer ' + options.bearerToken |
| 86 | + , 'Content-Type': 'application/json' |
| 87 | + } |
| 88 | + , json: [] |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + if(!cashsaleAndInvoiceSettingField) |
| 93 | + return cb(new Error('Could not find billing record type setting value')) |
| 94 | + else |
| 95 | + cashsaleAndInvoiceSettingFieldValue = cashsaleAndInvoiceSettingField.value |
| 96 | + |
| 97 | + if(cashsaleAndInvoiceSettingFieldValue === 'Cashsale') { |
| 98 | + _.each(options.data, function(preMapDataJson, index, cb) { |
| 99 | + hookResponseData.push(preMapDataJson[0]) |
| 100 | + }) |
| 101 | + hookResponse['data'] = hookResponseData |
| 102 | + return cb(null, hookResponse) |
| 103 | + } |
| 104 | + |
| 105 | + if(cashsaleAndInvoiceSettingFieldValue === 'Invoice') { |
| 106 | + optsObject['optsArray'] = options.data |
| 107 | + optsObject.opts = opts |
| 108 | + // invoking invoice flow |
| 109 | + hookResponse['data'] = hookResponseData |
| 110 | + return self.invokeInvoiceFlow(optsObject, hookResponse, cb) |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + if(cashsaleAndInvoiceSettingFieldValue === 'Automatic') { |
| 115 | + _.each(options.data, function(preMapDataJson, index, cb) { |
| 116 | + if(!preMapDataJson[0]['Payment Method']) { |
| 117 | + optsArray.push(preMapDataJson) |
| 118 | + } else { |
| 119 | + hookResponseData.push(preMapDataJson[0]) |
| 120 | + } |
| 121 | + }) |
| 122 | + |
| 123 | + //unifying data into optsObject |
| 124 | + optsObject.optsArray = optsArray |
| 125 | + optsObject.opts = opts |
| 126 | + hookResponse['data'] = hookResponseData |
| 127 | + // invoking invoice flow |
| 128 | + return self.invokeInvoiceFlow(optsObject, hookResponse, cb) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +module.exports = AutoBillingHooks |
0 commit comments