Skip to content

Commit 9ecc498

Browse files
authored
Merge pull request #1 from callinize/feature/tenfold-integration
Allow external API consume netsuiteApi and add customized methods
2 parents 2b3937c + d3ea461 commit 9ecc498

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

lib/endpoint.js

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
this.Endpoint = (function () {
2+
3+
var EXCEPTIONS = {
4+
METHOD_NOT_SUPPORTED: 'Method not supported!',
5+
REQUIRED_PARAMETER: 'Parameter(s) {{PARAMETER}} is required',
6+
PARAMETERS_MUST_BE_AN_ARRAY: 'The parameter \'params\' must be an array'
7+
};
8+
9+
var FIELD_TYPE_WITH_OPTIONS = ['select', 'multiselect', 'radio'];
10+
11+
function Endpoint(netsuiteApi, requestParams) {
12+
this.netsuiteApi = netsuiteApi;
13+
this.requestParams = requestParams || {};
14+
this.result = null;
15+
this.exception = null;
16+
}
17+
18+
Endpoint.prototype.handleRequest = function () {
19+
var method = this.requestParams.method;
20+
var params = this.requestParams.params || [];
21+
22+
if (!Array.isArray(params)) {
23+
this.addException(EXCEPTIONS.PARAMETERS_MUST_BE_AN_ARRAY);
24+
} else if (this._isFunction(this[method])) {
25+
this.result = this[method].apply(this, params);
26+
} else if (this._isFunction(this.netsuiteApi[method])) {
27+
this.result = this.netsuiteApi[method].apply(this.netsuiteApi, params);
28+
} else {
29+
this.addException(EXCEPTIONS.METHOD_NOT_SUPPORTED);
30+
}
31+
32+
return this.generateReply();
33+
};
34+
35+
Endpoint.prototype.getModuleFields = function (module) {
36+
var self = this;
37+
38+
if (module) {
39+
var fields = [];
40+
var records = NetsuiteToolkit.searchRecord(module);
41+
42+
if (records.length) {
43+
var record = NetsuiteToolkit.loadRecord(module, records[0].id);
44+
var fieldNames = record.getAllFields();
45+
46+
fieldNames.forEach(function(fieldName) {
47+
var field = self._getField(record, fieldName);
48+
49+
if (field) {
50+
fields.push(field);
51+
}
52+
});
53+
}
54+
55+
return fields;
56+
} else {
57+
self.addException(EXCEPTIONS.REQUIRED_PARAMETER.replace('{{PARAMETER}}', 'module'));
58+
}
59+
};
60+
61+
Endpoint.prototype._getField = function (record, fieldName) {
62+
var field = record.getField(fieldName);
63+
if (field) {
64+
return this._buildField(field);
65+
}
66+
};
67+
68+
Endpoint.prototype._buildField = function (field) {
69+
var formattedField = {
70+
name : field.name,
71+
label : field.label || field.name,
72+
type : field.type,
73+
readOnly : !!field.readOnly,
74+
mandatory: !!field.mandatory,
75+
disabled : !!field.disabled,
76+
hidden : !!field.hidden,
77+
popup : !!field.popup
78+
};
79+
80+
if (FIELD_TYPE_WITH_OPTIONS.indexOf(formattedField.type) !== -1) {
81+
formattedField.values = this._getSelectFieldValues(field);
82+
}
83+
84+
return formattedField;
85+
};
86+
87+
Endpoint.prototype._getSelectFieldValues = function (field) {
88+
var values = [];
89+
90+
try {
91+
var options = field.getSelectOptions();
92+
93+
values = options.map(function (option) {
94+
return {
95+
label: option.getText(),
96+
value: option.getId()
97+
};
98+
});
99+
} catch(e) {}
100+
101+
return values;
102+
};
103+
104+
105+
Endpoint.prototype.addException = function (message) {
106+
this.exception = new Error(message);
107+
};
108+
109+
Endpoint.prototype.generateReply = function () {
110+
return NetsuiteToolkit.formatReply(this.params, this.result, this.exception);
111+
};
112+
113+
Endpoint.prototype._isFunction = function (object) {
114+
return Object.prototype.toString.call(object) === '[object Function]';
115+
};
116+
117+
return Endpoint;
118+
119+
})();
120+
121+
122+
function endpointPostHandler(params) {
123+
var service = new Endpoint(this, params);
124+
return service.handleRequest();
125+
}

0 commit comments

Comments
 (0)