-
Notifications
You must be signed in to change notification settings - Fork 1
/
netsuite.js
191 lines (179 loc) · 6.16 KB
/
netsuite.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*!
* NetSuite 2017.2 SOAP client for Node.js based apps.
*
* Copyright (C) 20176 by Trujay Group LLC.
* (MIT License)
*/
/* eslint-disable camelcase */
'use strict'
const version = '2017_2'
const path = require('path')
const soap = require('soap')
const _ = require('lodash')
const nsWsdl = path.join(__dirname, 'wsdl', 'netsuite.wsdl')
const privates = new WeakMap()
class NetSuite {
constructor (options) {
// options: { appId: "abc", passport: { account: "x", email: "y", password: "z", roleId: 3 }, searchPrefs: { bodyFieldsOnly: false, pageSize: 10}, debug: true }
let p = {options: options, client: null}
privates.set(this, p)
}
async search (options) {
let client = await soapClient(this)
let type = _.get(options, ['type'])
let searchRecord
switch (type) {
case 'FolderSearch':
// <search xmlns="urn:messages_2017_2.platform.webservices.netsuite.com"><searchRecord xmlns:q1="urn:filecabinet_2017_2.documents.webservices.netsuite.com" xsi:type="q1:FolderSearch"><q1:basic /></searchRecord></search>';
searchRecord = {
attributes: {
'xmlns:q1': 'urn:filecabinet_' + version + '.documents.webservices.netsuite.com',
'xsi:type': 'q1:FolderSearch'
}
}
break
case 'FileSearch':
searchRecord = {
attributes: {
'xmlns:q1': 'urn:filecabinet_' + version + '.documents.webservices.netsuite.com',
'xsi:type': 'q1:FileSearch'
}
}
break
default:
throw new Error('unknown search record type')
}
let request = {
searchRecord: searchRecord
}
return callNs(client.NetSuiteService.NetSuitePort.search, request)
}
async searchMoreWithId (lastSearchResult) {
let client = await soapClient(this)
let searchId = _.get(lastSearchResult, ['searchResult', 'searchId'])
let pageIndex = _.get(lastSearchResult, ['searchResult', 'pageIndex'])
let totalPages = _.get(lastSearchResult, ['searchResult', 'totalPages'])
if (!searchId) throw new Error('lastSearchResult has a blank searchId')
if (!pageIndex) throw new Error('lastSearchResult has a blank pageIndex')
if (pageIndex >= totalPages) return null
return callNs(client.NetSuiteService.NetSuitePort.searchMoreWithId, {searchId: searchId, pageIndex: pageIndex + 1})
}
async get (options) {
let internalId = _.get(options, ['internalId'])
if (!internalId) throw new Error('options has a blank internalId')
let type = _.get(options, ['type'])
if (!type) throw new Error('options has a blank type')
let recordRef
switch (type) {
case 'file':
// <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Header><applicationInfo xmlns="urn:messages_2016_2.platform.webservices.netsuite.com"><applicationId>CA830599-19AE-46C0-83EF-EA667DAD8BE7</applicationId></applicationInfo></soap:Header><soap:Body><get xmlns="urn:messages_2016_2.platform.webservices.netsuite.com"><baseRef xmlns:q1="urn:core_2016_2.platform.webservices.netsuite.com" xsi:type="q1:RecordRef" internalId="8" type="file" /></get></soap:Body></soap:Envelope>
recordRef = {
attributes: {
'xmlns:q1': 'urn:core_' + version + '.platform.webservices.netsuite.com',
'xsi:type': 'q1:RecordRef',
'internalId': internalId,
'type': type
}
}
break
default:
throw new Error('unknown get record type: ' + type)
}
let client = await soapClient(this)
let request = {
recordRef: recordRef
}
return callNs(client.NetSuiteService.NetSuitePort.get, request)
}
}
const soapClient = async (self) => {
let p = privates.get(self)
if (p.client) return p.client
let options = p.options
let promise = new Promise(function (resolve, reject) {
soap.createClient(nsWsdl, (err, client) => {
if (err) {
reject(err)
} else {
/*
client.addSoapHeader(
{
applicationInfo:
{
applicationId: this.appId
},
passport:
{
account: this.accountId,
email: this.username,
password: this.password,
role:
{
attributes:
{
internalId: this.roleId
}
}
}
});
*/
let appId = _.get(options, ['appId'])
let account = _.get(options, ['passport', 'account'])
let email = _.get(options, ['passport', 'email'])
let password = _.get(options, ['passport', 'password'])
let roleId = _.get(options, ['passport', 'roleId'])
let searchPrefs = _.get(options, ['searchPrefs'])
let debug = _.get(options, ['debug'])
let hdr = {
passport:
{
account: account,
email: email,
password: password
}}
if (roleId) {
hdr.passport.role = {
attributes:
{
internalId: roleId
}
}
}
if (appId) {
hdr.applicationInfo = {applicationId: appId}
}
if (searchPrefs) {
// <searchPreferences xmlns="urn:messages_2016_2.platform.webservices.netsuite.com"><bodyFieldsOnly>false</bodyFieldsOnly><pageSize>10</pageSize></searchPreferences></
hdr.searchPreferences = searchPrefs
}
client.addSoapHeader(hdr)
if (debug) {
client.on('request', function (req) {
console.log(req)
})
}
p.client = client
privates.set(self, p)
resolve(client)
}
})
})
return promise
}
const callNs = (func, args) => {
var p = new Promise(function (resolve, reject) {
try {
func(args, function (err, result) {
if (err) {
reject(err)
} else {
resolve(result)
}
})
} catch (err) {
reject(err)
}
})
return p
}
module.exports = NetSuite