This repository was archived by the owner on Jun 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOdooCoreApi.js
423 lines (368 loc) · 14.3 KB
/
OdooCoreApi.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
const Logger = require('xvi-logger');
const Helper = require('xvi-helper');
const OdooXmlrpc = require('odoo-xmlrpc');
class OdooCoreApi {
constructor({
name = 'OdooCoreApi',
loggerInstance = false,
url = false,
port = false,
protocol = false,
db = false,
username = false,
password = false,
dateFormat = 'YYYYMMDD HH:mm:ss',
queueTimer = 250,
maxRecordsPerCall = 200
}) {
var self = this;
self._arguments = arguments[0]; //store arguments
self._name = name; //name of instance
self._logger = new Logger({
name: self._name,
instance: loggerInstance
});
self._helper = new Helper();
self._callQueueTimer = queueTimer;
self._maxRecordsPerCall = maxRecordsPerCall;
self._protocol = protocol;
self._url = url;
self._port = port;
self._db = db;
self._username = username;
self._password = password;
self._dateFormat = dateFormat;
self._callQueue = [];
self._isConnected = false;
}
gDateFormat() {
var self = this;
return self._dateFormat;
}
gTimeZone() {
var self = this;
return self._timezone;
}
format_many2one(ids = []) {
return (ids.length > 0) ? [4, ids] : false;
}
async initialize() {
var self = this;
self._logger.func('initialize')
if (!self._url || !self._port || !self._db || !self._username || !self._password) {
throw new Error('OdooCoreApi: configuration incomplete');
}
self._odoo = new OdooXmlrpc({
url: self.protocol + '://' + self._url,
port: self._port,
db: self._db,
username: self._username,
password: self._password
});
self.setCallQueueProcessor();
return true;
}
async error(messageString = false, p1 = false, p2 = false, p3 = false, locale = false) {
var self = this;
// var localizedMessage = self._e(messageString, p1, p2, p3, locale);
self._logger.error(messageString);
throw new Error(messageString);
}
_e(messageString = false, p1 = false, p2 = false, p3 = false, locale = false) {
var self = this;
//return self._localizer._e(messageString, p1, p2, p3, locale);
}
setCallQueueProcessor() {
var self = this;
//self._logger.func(`setCallQueueProcessor`);
setInterval(function () {
if (self._callQueue.length > 0) {
self.makeCall();
}
}, self._callQueueTimer);
}
makeCall() {
var self = this;
//self._logger.func(`makeCall`);
if (self._callQueue[0]) {
if (self[self._callQueue[0].method]) {
self[self._callQueue[0].method](self._callQueue[0].query, self._callQueue[0].resolve, self._callQueue[0].reject);
}
self._callQueue.shift();
}
}
//QUERY BUILDER
buildOdooQuery(model, fields, filters, method, offset, limit, order, context) {
var self = this;
// //self._logger.func('buildOdooQuery');
var inParams = [];
inParams.push(filters); //"FP.MI.SLME_0062"
inParams.push(fields); //fields 'product_qty', 'routing_id', 'fal_is_bom_template',
inParams.push(offset); //offset
inParams.push(limit); //limit
if (order) {
inParams.push(order); //order
}
var params = [];
params.push(inParams);
return {
model: model,
method: method,
params: params
};
}
buildOdooCreateQuery(record, model) {
var self = this;
// //self._logger.func('buildOdooCreateQuery');
var inParams = [];
inParams.push(record);
var params = [];
params.push(inParams);
//self._logger.debugData(params);
return {
model: model,
method: 'create',
params: params
};
}
buildOdooUpdateQuery(id, record, model) {
var self = this;
//self._logger.func('buildOdooUpdateQuery');
var inParams = [];
inParams.push([id]); //id to update
inParams.push(record);
var params = [];
params.push(inParams);
return {
model: model,
method: 'write',
params: params
};
}
buildOdooWorkflowQuery(id, model, action, additional = []) {
var self = this;
//self._logger.func('buildOdooUpdateQuery');
var inParams = [];
inParams.push(id)
if (additional) {
for (var i = 0; i < additional.length; i++) {
var element = additional[i];
inParams.push(element);
}
}
var params = [];
params.push(inParams);
return {
model: model,
method: action,
params: params
};
}
async queueQuery(query) {
var self = this;
return new Promise(function (resolve, reject) {
self._callQueue.push({
query: query,
method: 'executeQuery',
resolve: resolve,
reject: reject
});
});
}
async executeQuery(query, resolve, reject) {
var self = this;
self._odoo.execute_kw(query.model, query.method, query.params, function (err, value) {
if (err) {
reject(err);
} else {
resolve(value);
}
});
}
//CALLS TO WEB API
async connectOdoo() {
var self = this;
return new Promise(function (resolve, reject) {
if (!self._connected) {
//self._logger.journalEntry('CONNECT-DB', `About to connect to odoo.`, {});
self._odoo.connect(function (err) {
if (err) {
self._logger.warn('!! Error when connecting to Odoo server');
reject(err);
} else {
self._logger.info('Connected to Odoo server.');
self._connected = true;
setTimeout(function () {
self._connected = false;
self._logger.debug('Odoo connection timeout, will reconnect on next attempt.');
}, 1000 * 60 * 1);
resolve();
}
});
} else {
//self._logger.debug('Already connected to odoo - skip reconnect');
resolve();
}
});
}
async updateModel(model, item) {
var self = this;
//self._logger.func('updateModel: ' + model);
try {
await self.connectOdoo();
var dataToUpdate = self._helper.deepcopy(item);
delete dataToUpdate.id;
var updateQuery = self.buildOdooUpdateQuery(item.id, dataToUpdate, model);
//execute
let success = await self.queueQuery(updateQuery);
self._logger.info('updateModel: updated ' + model + ' ' + item.id);
return item.id;
} catch (err) {
self._logger.warn(`Failed to update model ${model}`);
throw new Error(`OdooCoreApi: ${err.message} (updateModel)`);
}
}
async insertModel(model, item) {
var self = this;
//self._logger.func('insertModel: ' + model);
try {
await self.connectOdoo();
var myCreateQuery = self.buildOdooCreateQuery(item, model);
//console.log(myCreateQuery);
//execute
let newItemId = await self.queueQuery(myCreateQuery);
self._logger.info('insertModel: Inserted ' + model + ' ' + newItemId);
return newItemId;
} catch (err) {
self._logger.warn(`Failed to insert model ${model}`);
throw new Error(`OdooCoreApi: ${err.message} (insertModel)`);
}
}
async executeWorkflow(id, model, action, additional = []) {
var self = this;
//self._logger.func('executeWorkflow: ' + model);
try {
await self.connectOdoo();
var myCreateQuery = self.buildOdooWorkflowQuery(id, model, action, additional);
//console.log(myCreateQuery);
//execute
let results = await self.queueQuery(myCreateQuery);
self._logger.info('executeWorkflow: Executed ' + model + ' ' + id);
return id;
} catch (err) {
self._logger.warn(`Failed to execute workflow on model ${model}`);
throw new Error(`OdooCoreApi: ${err.message} (executeWorkflow)`);
}
}
async actionModelByFilters(action = false, model = false, filters = false, comparefields = false, validateCb = false, compareCb = false) {
var self = this;
self._logger.func(`actionModelByFilters`, `${action} ${model} matching ${JSON.stringify(filters)}`);
if (!filters || !model || !action || !comparefields || !validateCb || !compareCb) {
throw new Error('OdooCoreApi: incomplete parameters (actionModelByFilters)');
}
//get elements
var elements = await self.getModelByFilters(model, filters, comparefields, 0, 1, false);
if (elements.length === 0) {
throw new Error(`OdooCoreApi: cannot find the ${model} (actionModelByFilters)`);
}
var element = elements[0];
//validate if this element should be updated
var validation = await validateCb(element);
if (!validation.success) {
throw new Error(validation.message)
}
//try to execute the workflow
var workflowMessage = '';
try {
await self.executeWorkflow(element.id, model, action);
} catch (err) {
self._logger.error(err);
workflowMessage = err.message;
}
//recheck, Just in case, check if it has been done
var elementsUpdated = await self.getModelByFilters(model, [
['id', '=', element.id]
], comparefields, 0, 1, false);
if (elementsUpdated.length === 0) {
throw new Error(`cannot find the updated ${model} (actionModelByFilters)`);
}
var elementUpdated = elementsUpdated[0];
//final check. The state field value should have changed. If not, then an error occured
var comparison = await compareCb(element, elementUpdated);
if (comparison.success) {
return {
success: true,
message: {
validation: validation.message,
workflow: workflowMessage,
comparison: comparison.message,
},
original: element,
updated: elementUpdated
};
} else {
throw new Error(comparison.message);
}
}
async getModelByFilters(model = false, filters = [], fields = ['name'], offset = 0, limit = 100, sorting = false) {
var self = this;
self._logger.func(`getModelByFilters`, `${model}`); //${JSON.stringify(fields)} //${JSON.stringify(filters)}
var countLoops = Math.ceil(limit / self._maxRecordsPerCall);
try {
let results = await self._getModelsByFiltersRecursive(model, filters, fields, offset, limit, sorting, 0, countLoops);
return results;
} catch (err) {
self._logger.warn(`Failed to getModelByFilters ${model}`);
throw new Error(err);
}
}
async _getModelsByFiltersRecursive(model, filters, fields = ['name'], offset = 0, limit = 10, sorting = false, index = 0, countLoops = 1) {
var self = this;
// self._logger.verbose('_getModelsByFiltersRecursive: ' + `model ${model} / filters ${filters} / fields ${fields} / offset ${offset} / limit ${limit} / index ${index} / countLoops ${countLoops}`);
try {
if (index < countLoops) {
var _offset = offset + self._maxRecordsPerCall * index;
//check if we still have enough records to pull
var recordsToPullAfterThis = limit - self._maxRecordsPerCall * (index + 1);
var recordsAlreadyPulled = self._maxRecordsPerCall * (index);
if (recordsToPullAfterThis < 0) {
var _limit = limit - recordsAlreadyPulled;
} else {
var _limit = self._maxRecordsPerCall;
}
// self._logger.verbose(`recordsAlreadyPulled ${recordsAlreadyPulled}`);
// self._logger.verbose(`recordsToPullAfterThis ${recordsToPullAfterThis}`);
// self._logger.verbose('Will pull : ' + `model ${model} / _offset ${_offset} / _limit ${_limit}`);
//connect to odoo
await self.connectOdoo();
var myQuery = self.buildOdooQuery(model, fields, filters, 'search_read', _offset, _limit, sorting);
//execute
var data = await self.queueQuery(myQuery);
// self._logger.verbose('getModelsByFilters: OK (model: ' + model + ')'); //JSON.stringify(filters)
if (limit > 1) {
// self._logger.verbose('Pulled : ' + data.length + 2);
if (data.length === 0) {
//we already pulled all
return data;
} else {
//there is more to pull
var subData = await self._getModelsByFiltersRecursive(model, filters, fields, offset, limit, sorting, index + 1, countLoops);
if (subData) {
data = data.concat(subData);
}
return data;
}
} else {
return data;
}
} else {
// self._logger.verbose('Finished all loops');
return [];
}
} catch (err) {
//self._logger.warn(`Failed to _getModelsByFiltersRecursive ${model}`);
throw new Error(`OdooCoreApi: ${err.message} (_getModelsByFiltersRecursive)`);
}
}
}
module.exports = OdooCoreApi;