Skip to content

Commit b36ab21

Browse files
committed
adding files
0 parents  commit b36ab21

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

auto-billing/autoBilling.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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

constants.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
'use strict'
3+
var CONSTANTS = {
4+
HERCULES_BASE_URL = 'https://api.integrator.io'
5+
}
6+
7+
if(process.env.NODE_ENV === 'staging') {
8+
CONSTANTS.HERCULES_BASE_URL = 'https://api.staging.integrator.io'
9+
} else if {
10+
CONSTANTS.HERCULES_BASE_URL = 'http://api.localhost.io:5000'
11+
}
12+
13+
module.exports = CONSTANTS

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "etail-utils",
3+
"version": "1.0.0",
4+
"dependecies": {
5+
"lodash": "^4.6.1",
6+
"async": "~1.4.2",
7+
"ief-utils": "0.2.3",
8+
"request": "^2.61.0"
9+
}
10+
}

0 commit comments

Comments
 (0)