-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlobPlus.js
108 lines (84 loc) · 2.83 KB
/
lobPlus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict'
const batchRequest = require('./utils/batchRequest')
const merge = require('lodash.merge')
const cloneDeep = require('lodash.clonedeep')
function lobPlus (lob) {
lobCheck(lob)
// batching from the params value if its an array
attachBatchCreate(lob.addresses)
attachBatchCreate(lob.bankAccounts)
// batching from the params.to value if its an array
attachBatchSend(lob.postcards)
attachBatchSend(lob.letters)
attachBatchSend(lob.checks)
return lob
};
lobPlus.safe = function (lob) {
lobCheck(lob)
// batching from the params value if its an array
attachBatchCreate(lob.addresses, 'batch')
attachBatchCreate(lob.bankAccounts, 'batch')
// batching from the params.to value if its an array
attachBatchSend(lob.postcards, 'batch')
attachBatchSend(lob.letters, 'batch')
attachBatchSend(lob.checks, 'batch')
}
function lobCheck (lob) {
if (!(lob.constructor.name === 'Lob')) {
throw new Error('lob must be an instance of Lob Node library')
}
}
/**
* Overrides the create function to allow for batching from params given if its an array
* @param {Object} resource The lob resource to modify (postcards, letters, etc.)
*/
function attachBatchCreate (resource, key = 'create') {
resource.pureCreate = resource.create // save the original create for later
resource[key] = function (params, config, callback) {
if (typeof config === 'function') {
callback = config
config = {}
}
if (!(params instanceof Array)) {
return this.pureCreate.apply(this, arguments)
}
config.queue = params
config.action = function (params, callback) {
return resource.pureCreate(params, callback)
}
return batchRequest(config).asCallback(callback)
}
}
/**
* Overrides the create function to allow for batching from the "to" parameter in the params
* @param {Object} resource The lob resource to modify (postcards, letters, etc.)
*/
function attachBatchSend (resource, key = 'create') {
resource.pureCreate = resource.create // save the original create for later
resource[key] = function (params, config, callback) {
if (typeof config === 'function') {
callback = config
config = {}
}
if (!(params.to instanceof Array)) {
return this.pureCreate.apply(this, arguments)
}
let recipients = params.to
let queue = []
for (let i = 0; i < recipients.length; i++) {
let newParams = cloneDeep(params)
newParams.to = recipients[i]
if (typeof newParams.to !== 'string') {
merge(newParams, newParams.to.overrides || {})
delete newParams.to.overrides
}
queue.push(newParams)
}
config.queue = queue
config.action = function (params, callback) {
return resource.pureCreate(params, callback)
}
return batchRequest(config).asCallback(callback)
}
}
module.exports = lobPlus