@resolveio/server-lib 3.2.11 → 3.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/collections/active-subscription.collection.d.ts +7 -0
  2. package/collections/active-subscription.collection.js +68 -0
  3. package/collections/app-status.collection.d.ts +7 -0
  4. package/collections/app-status.collection.js +43 -0
  5. package/collections/app-version.collection.d.ts +7 -0
  6. package/collections/app-version.collection.js +56 -0
  7. package/collections/billing-logged-in-users.collection.d.ts +7 -0
  8. package/collections/billing-logged-in-users.collection.js +73 -0
  9. package/collections/counter.collection.d.ts +9 -0
  10. package/collections/counter.collection.js +73 -0
  11. package/collections/cron-job-history.collection.d.ts +7 -0
  12. package/collections/cron-job-history.collection.js +86 -0
  13. package/collections/cron-job.collection.d.ts +7 -0
  14. package/collections/cron-job.collection.js +89 -0
  15. package/collections/file.collection.d.ts +9 -0
  16. package/collections/file.collection.js +73 -0
  17. package/collections/flag.collection.d.ts +7 -0
  18. package/collections/flag.collection.js +47 -0
  19. package/collections/log.collection.d.ts +7 -0
  20. package/collections/log.collection.js +65 -0
  21. package/collections/logged-in-users.collection.d.ts +7 -0
  22. package/collections/logged-in-users.collection.js +70 -0
  23. package/collections/method-call.collection.d.ts +6 -0
  24. package/collections/method-call.collection.js +44 -0
  25. package/collections/method-response.collection.d.ts +6 -0
  26. package/collections/method-response.collection.js +48 -0
  27. package/collections/notification.collection.d.ts +7 -0
  28. package/collections/notification.collection.js +43 -0
  29. package/collections/queue-flag.collection.d.ts +7 -0
  30. package/collections/queue-flag.collection.js +41 -0
  31. package/collections/queue-method.collection.d.ts +7 -0
  32. package/collections/queue-method.collection.js +119 -0
  33. package/collections/queue-response.collection.d.ts +7 -0
  34. package/collections/queue-response.collection.js +74 -0
  35. package/collections/support-ticket.collection.d.ts +7 -0
  36. package/collections/support-ticket.collection.js +200 -0
  37. package/collections/user.collection.d.ts +9 -0
  38. package/collections/user.collection.js +153 -0
  39. package/cron/cron.d.ts +5 -0
  40. package/cron/cron.js +160 -0
  41. package/fixtures/cron-jobs.d.ts +1 -0
  42. package/fixtures/cron-jobs.js +90 -0
  43. package/fixtures/init.d.ts +1 -0
  44. package/fixtures/init.js +67 -0
  45. package/http/auth.d.ts +2 -0
  46. package/http/auth.js +468 -0
  47. package/http/health.d.ts +1 -0
  48. package/http/health.js +8 -0
  49. package/index.d.ts +16 -0
  50. package/index.js +125 -0
  51. package/managers/cron.manager.d.ts +16 -0
  52. package/managers/cron.manager.js +202 -0
  53. package/managers/database.manager.d.ts +6 -0
  54. package/managers/database.manager.js +12 -0
  55. package/managers/method.manager.d.ts +28 -0
  56. package/managers/method.manager.js +584 -0
  57. package/managers/queue-method.manager.d.ts +13 -0
  58. package/managers/queue-method.manager.js +240 -0
  59. package/managers/subscription.manager.d.ts +28 -0
  60. package/managers/subscription.manager.js +808 -0
  61. package/managers/support.manager.d.ts +22 -0
  62. package/managers/support.manager.js +320 -0
  63. package/methods/accounts.d.ts +2 -0
  64. package/methods/accounts.js +253 -0
  65. package/methods/aws.d.ts +2 -0
  66. package/methods/aws.js +387 -0
  67. package/methods/cloudconvert.d.ts +2 -0
  68. package/methods/cloudconvert.js +37 -0
  69. package/methods/collections.d.ts +2 -0
  70. package/methods/collections.js +750 -0
  71. package/methods/counters.d.ts +2 -0
  72. package/methods/counters.js +107 -0
  73. package/methods/cron-jobs.d.ts +2 -0
  74. package/methods/cron-jobs.js +132 -0
  75. package/methods/flags.d.ts +2 -0
  76. package/methods/flags.js +6 -0
  77. package/methods/logs.d.ts +2 -0
  78. package/methods/logs.js +29 -0
  79. package/methods/pdf.d.ts +2 -0
  80. package/methods/pdf.js +303 -0
  81. package/models/active-subscription.model.d.ts +13 -0
  82. package/models/active-subscription.model.js +2 -0
  83. package/models/app-status.model.d.ts +4 -0
  84. package/models/app-status.model.js +2 -0
  85. package/models/app-version.model.d.ts +4 -0
  86. package/models/app-version.model.js +2 -0
  87. package/models/billing-logged-in-users.model.d.ts +7 -0
  88. package/models/billing-logged-in-users.model.js +2 -0
  89. package/models/collection-document.model.d.ts +6 -0
  90. package/models/collection-document.model.js +2 -0
  91. package/models/counter.model.d.ts +5 -0
  92. package/models/counter.model.js +2 -0
  93. package/models/cron-job-history.model.d.ts +12 -0
  94. package/models/cron-job-history.model.js +2 -0
  95. package/models/cron-job.model.d.ts +13 -0
  96. package/models/cron-job.model.js +2 -0
  97. package/models/dialog.model.d.ts +23 -0
  98. package/models/dialog.model.js +2 -0
  99. package/models/file.model.d.ts +9 -0
  100. package/models/file.model.js +2 -0
  101. package/models/flag.model.d.ts +5 -0
  102. package/models/flag.model.js +2 -0
  103. package/models/log.model.d.ts +11 -0
  104. package/models/log.model.js +2 -0
  105. package/models/logged-in-users.model.d.ts +9 -0
  106. package/models/logged-in-users.model.js +2 -0
  107. package/models/method-call.model.d.ts +7 -0
  108. package/models/method-call.model.js +2 -0
  109. package/models/method-response.model.d.ts +8 -0
  110. package/models/method-response.model.js +2 -0
  111. package/models/method.model.d.ts +10 -0
  112. package/models/method.model.js +2 -0
  113. package/models/notification.model.d.ts +4 -0
  114. package/models/notification.model.js +2 -0
  115. package/models/pagination.model.d.ts +11 -0
  116. package/models/pagination.model.js +20 -0
  117. package/models/permission.model.d.ts +12 -0
  118. package/models/permission.model.js +2 -0
  119. package/models/queue-method.model.d.ts +10 -0
  120. package/models/queue-method.model.js +2 -0
  121. package/models/queue-response.model.d.ts +10 -0
  122. package/models/queue-response.model.js +2 -0
  123. package/models/select-data-label.model.d.ts +9 -0
  124. package/models/select-data-label.model.js +2 -0
  125. package/models/server-response.model.d.ts +5 -0
  126. package/models/server-response.model.js +2 -0
  127. package/models/subscription.model.d.ts +30 -0
  128. package/models/subscription.model.js +2 -0
  129. package/models/support-method.model.d.ts +9 -0
  130. package/models/support-method.model.js +2 -0
  131. package/models/support-ticket.model.d.ts +61 -0
  132. package/models/support-ticket.model.js +2 -0
  133. package/models/user.model.d.ts +14 -0
  134. package/models/user.model.js +2 -0
  135. package/package.json +1 -1
  136. package/private/email-templates/enrollment.html +12 -0
  137. package/private/email-templates/forgot-password.html +12 -0
  138. package/private/email-templates/support-ticket-deleted.html +40 -0
  139. package/private/email-templates/support-ticket-modified.html +71 -0
  140. package/private/email-templates/support-ticket.html +71 -0
  141. package/private/images/ResolveIO.png +0 -0
  142. package/publications/app-status.d.ts +2 -0
  143. package/publications/app-status.js +14 -0
  144. package/publications/app-version.d.ts +2 -0
  145. package/publications/app-version.js +14 -0
  146. package/publications/billing-logged-in-users.d.ts +1 -0
  147. package/publications/billing-logged-in-users.js +63 -0
  148. package/publications/cron-jobs.d.ts +2 -0
  149. package/publications/cron-jobs.js +14 -0
  150. package/publications/files.d.ts +2 -0
  151. package/publications/files.js +71 -0
  152. package/publications/flags.d.ts +2 -0
  153. package/publications/flags.js +23 -0
  154. package/publications/logs.d.ts +2 -0
  155. package/publications/logs.js +26 -0
  156. package/publications/method-calls.d.ts +2 -0
  157. package/publications/method-calls.js +14 -0
  158. package/publications/method-responses.d.ts +2 -0
  159. package/publications/method-responses.js +14 -0
  160. package/publications/notifications.d.ts +2 -0
  161. package/publications/notifications.js +14 -0
  162. package/publications/super-admin.d.ts +2 -0
  163. package/publications/super-admin.js +21 -0
  164. package/publications/support-tickets.d.ts +1 -0
  165. package/publications/support-tickets.js +200 -0
  166. package/server-app.d.ts +35 -0
  167. package/server-app.js +638 -0
  168. package/support-methods/aws.d.ts +2 -0
  169. package/support-methods/aws.js +276 -0
  170. package/support-methods/collections.d.ts +2 -0
  171. package/support-methods/collections.js +471 -0
  172. package/support-methods/counters.d.ts +2 -0
  173. package/support-methods/counters.js +189 -0
  174. package/support-methods/support.d.ts +2 -0
  175. package/support-methods/support.js +236 -0
  176. package/util/common.d.ts +14 -0
  177. package/util/common.js +341 -0
@@ -0,0 +1,584 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var collections_1 = require("../methods/collections");
4
+ var logs_1 = require("../methods/logs");
5
+ var counters_1 = require("../methods/counters");
6
+ var pdf_1 = require("../methods/pdf");
7
+ var aws_1 = require("../methods/aws");
8
+ var cloudconvert_1 = require("../methods/cloudconvert");
9
+ var path = require("path");
10
+ var fs = require("fs");
11
+ var nodemailer = require("nodemailer");
12
+ var aws = require("aws-sdk");
13
+ var accounts_1 = require("../methods/accounts");
14
+ var init_1 = require("../fixtures/init");
15
+ var cron_jobs_1 = require("../fixtures/cron-jobs");
16
+ var cron_jobs_2 = require("../methods/cron-jobs");
17
+ var flags_1 = require("../methods/flags");
18
+ var method_response_collection_1 = require("../collections/method-response.collection");
19
+ var mongoose_1 = require("mongoose");
20
+ var common_1 = require("../util/common");
21
+ var log_collection_1 = require("../collections/log.collection");
22
+ var support_manager_1 = require("./support.manager");
23
+ var MethodManager = /** @class */ (function () {
24
+ function MethodManager(mainServer, serverConfig, clientDir) {
25
+ var _this = this;
26
+ this._methods = {};
27
+ this._sendQueue = [];
28
+ this._runningQueue = false;
29
+ this._mainServer = mainServer;
30
+ this.serverConfig = serverConfig;
31
+ this.clientDir = clientDir;
32
+ // Fixtures
33
+ init_1.loadServerInit();
34
+ cron_jobs_1.loadServerCronJobs();
35
+ // Methods
36
+ accounts_1.loadAccountMethods(this);
37
+ aws_1.loadAWSMethods(this);
38
+ cloudconvert_1.loadCloudConvertMethods(this);
39
+ collections_1.loadCollectionMethods(this);
40
+ counters_1.loadCounterMethods(this);
41
+ logs_1.loadLogMethods(this);
42
+ pdf_1.loadPDFMethods(this);
43
+ cron_jobs_2.loadCronJobMethods(this);
44
+ flags_1.loadFlagMethods(this);
45
+ this._mailer = nodemailer.createTransport({
46
+ SES: new aws.SES({
47
+ apiVersion: '2010-12-01',
48
+ accessKeyId: serverConfig.AWSACCESSKEYID,
49
+ secretAccessKey: serverConfig.AWSSECRETACCESSKEY,
50
+ region: serverConfig.AWSSES_REGION
51
+ })
52
+ });
53
+ // this._imap = new imap({
54
+ // user: this.serverConfig['ORDERS_EMAIL'],
55
+ // password: this.serverConfig['ORDERS_PASSWORD'],
56
+ // host: this.serverConfig['ORDERS_HOST'],
57
+ // port: this.serverConfig['ORDERS_PORT'],
58
+ // tls: this.serverConfig['ORDERS_TLS']
59
+ // });
60
+ // let that = this;
61
+ // let chemicalNames = await Chemicals.find({}).select('description').exec();
62
+ // let jobNames = await Jobs.find({status: 'Active'}).select('well').exec();
63
+ // console.log(jobNames.map(a => a.well));
64
+ // this._imap.once('ready', () => {
65
+ // this._imap.openBox('INBOX', true, function(err, box) {
66
+ // if (err) {
67
+ // throw err;
68
+ // }
69
+ // that._imap.search([ 'NEW' ], function(err, results) {
70
+ // console.log(err, results);
71
+ // if (err || !results.length) {
72
+ // return that._imap.end();
73
+ // }
74
+ // let f = that._imap.fetch(results[0], { bodies: ['HEADER.FIELDS (FROM)', 'TEXT'] });
75
+ // f.on('message', function(msg, seqno) {
76
+ // // console.log('Message #%d', seqno);
77
+ // let prefix = '(#' + seqno + ') ';
78
+ // msg.on('body', function(stream, info) {
79
+ // let buffer = '', count = 0;
80
+ // stream.on('data', function(chunk) {
81
+ // count += chunk.length;
82
+ // buffer += chunk.toString('utf8');
83
+ // });
84
+ // stream.once('end', function() {
85
+ // if (info.which === 'TEXT') {
86
+ // // console.log(buffer);
87
+ // // console.log(typeof(buffer));
88
+ // let lines = buffer.split('\r\n');
89
+ // // console.log(lines);
90
+ // // let i = 0;
91
+ // // lines.forEach(line => {
92
+ // // let date: Date;
93
+ // // let dateString;
94
+ // // if (line.includes('From')) {
95
+ // // console.log(line);
96
+ // // }
97
+ // // if (!line.includes('class=3D') && !line.includes('style=3D')) {
98
+ // // if (line.includes('Well') || line.includes('Well Name') || line.includes('Job') || line.includes('Pad')) {
99
+ // // let best = stringSimilarity.findBestMatch(line, jobNames.map(a => a.well)).bestMatch;
100
+ // // if (best && best.rating > 0.5) {
101
+ // // console.log('Well', line);
102
+ // // }
103
+ // // }
104
+ // // if (line.includes('Product') || line.includes('Chemical')) {
105
+ // // let best = stringSimilarity.findBestMatch(line, chemicalNames.map(a => a.description)).bestMatch;
106
+ // // if (best && best.rating > 0.5) {
107
+ // // console.log('Chemical', line);
108
+ // // }
109
+ // // }
110
+ // // if (line.includes('Quantity') || line.includes('Qty') || line.includes('Amount')) {
111
+ // // console.log('Qty', line);
112
+ // // }
113
+ // // if (line.includes('Date')) {
114
+ // // console.log('Date', line);
115
+ // // console.log(moment(Date.parse(line)));
116
+ // // date = moment(Date.parse(line)).toDate();
117
+ // // dateString = line;
118
+ // // }
119
+ // // if (line.includes('Time')) {
120
+ // // console.log('Time', line);
121
+ // // console.log(moment(Date.parse(line)));
122
+ // // console.log(moment(date.setTime(Date.parse(line))));
123
+ // // }
124
+ // // }
125
+ // // });
126
+ // }
127
+ // });
128
+ // });
129
+ // msg.once('attributes', function(attrs) {
130
+ // console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
131
+ // });
132
+ // msg.once('end', function() {
133
+ // console.log(prefix + 'Finished');
134
+ // });
135
+ // });
136
+ // f.once('error', function(error) {
137
+ // console.log('Fetch error: ' + error);
138
+ // });
139
+ // f.once('end', function() {
140
+ // console.log('Done fetching all messages!');
141
+ // that._imap.end();
142
+ // });
143
+ // });
144
+ // });
145
+ // });
146
+ // this._imap.connect();
147
+ this._aws = new aws.S3({
148
+ accessKeyId: serverConfig.AWSACCESSKEYID,
149
+ secretAccessKey: serverConfig.AWSSECRETACCESSKEY,
150
+ region: serverConfig.AWSREGION
151
+ });
152
+ setInterval(function () {
153
+ if (!_this._runningQueue) {
154
+ if (_this._sendQueue.length) {
155
+ _this._runningQueue = true;
156
+ var _loop_1 = function (i) {
157
+ var ws = _this._mainServer.getWS(_this._sendQueue[i].id_ws);
158
+ if (ws && ws.readyState === ws.OPEN) {
159
+ if (ws.bufferedAmount < 20480) {
160
+ var sendItem = _this._sendQueue.pop();
161
+ ws.send(JSON.stringify(sendItem.data), function (error) {
162
+ if (error) {
163
+ console.log('Error on WS: ', error);
164
+ _this._mainServer.unsubscribeWS(ws);
165
+ }
166
+ });
167
+ if (sendItem.method !== 'reportBuilderGetResults' && sendItem.method !== 'reportBuilderGetDistinctValue' && sendItem.method !== 'reportBuilderBuildTree' && sendItem.method !== 'generatePDF' && sendItem.method !== 'getWOOfflineData' && sendItem.method !== 'countQuery' && sendItem.method !== 'countWithQuery' && sendItem.method !== 'countCollectionWithQuery' && sendItem.method !== 'find' && sendItem.method !== 'findOne' && sendItem.method !== 'findWithOptions' && sendItem.method !== 'setWSAppVersion' && sendItem.method !== 'uploadFileAndSave' && sendItem.method !== 'getDrivers') {
168
+ if (common_1.getBinarySize(JSON.stringify(sendItem.methodData)) < 200000 && common_1.getBinarySize(JSON.stringify(sendItem.data)) < 200000) {
169
+ log_collection_1.Logs.create({
170
+ _id: mongoose_1.Types.ObjectId().toHexString(),
171
+ date: new Date(),
172
+ type: 'client-response',
173
+ title: 'Client Response',
174
+ message: 'User: ' + ws['user'] + ', MessageId: ' + sendItem.data.messageId + ', Method: ' + sendItem.method,
175
+ payload: JSON.stringify([sendItem.methodData, sendItem.data]),
176
+ method: sendItem.method,
177
+ user: ws['user'],
178
+ messageId: sendItem.data.messageId
179
+ });
180
+ method_response_collection_1.MethodResponses.create({
181
+ _id: mongoose_1.Types.ObjectId().toHexString(),
182
+ __v: 0,
183
+ id_user: ws['id_user'],
184
+ message_id: sendItem.data.messageId,
185
+ response: sendItem.data,
186
+ method: sendItem.method,
187
+ date: sendItem.date
188
+ });
189
+ }
190
+ else {
191
+ log_collection_1.Logs.create({
192
+ _id: mongoose_1.Types.ObjectId().toHexString(),
193
+ date: new Date(),
194
+ type: 'client-response',
195
+ title: 'Client Response',
196
+ message: 'User: ' + ws['user'] + ', MessageId: ' + sendItem.data.messageId + ', Method: ' + sendItem.method,
197
+ payload: 'Too Big',
198
+ method: sendItem.method,
199
+ user: ws['user'],
200
+ messageId: sendItem.data.messageId
201
+ });
202
+ }
203
+ }
204
+ }
205
+ }
206
+ else {
207
+ _this._sendQueue.splice(i, 1);
208
+ }
209
+ };
210
+ for (var i = _this._sendQueue.length - 1; i >= 0; i--) {
211
+ _loop_1(i);
212
+ }
213
+ _this._runningQueue = false;
214
+ }
215
+ }
216
+ }, 25);
217
+ }
218
+ MethodManager.prototype.getMainServer = function () {
219
+ return this._mainServer;
220
+ };
221
+ // Add methods to private methods object
222
+ MethodManager.prototype.methods = function (method) {
223
+ this._methods = Object.assign(this._methods, method);
224
+ };
225
+ MethodManager.prototype.callMethodCron = function (method) {
226
+ var _a;
227
+ var methodData = [];
228
+ for (var _i = 1; _i < arguments.length; _i++) {
229
+ methodData[_i - 1] = arguments[_i];
230
+ }
231
+ var that = this;
232
+ if (!that._methods[method]) {
233
+ console.log('No Method: ' + method);
234
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Method: ' + method);
235
+ return;
236
+ }
237
+ if ((methodData.length > 1 || methodData[0]) && !that._methods[method].skipValidation) {
238
+ if (!that._methods[method].check) {
239
+ console.error('No Check Function For Method ' + method);
240
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Check Function For Method ' + method);
241
+ return;
242
+ }
243
+ else if (!that._methods[method].check._schema) {
244
+ console.error('No Check Schema For Method ' + method);
245
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Check Function For Method ' + method);
246
+ return;
247
+ }
248
+ else {
249
+ var valObj = {};
250
+ var valKeys = Object.keys(that._methods[method].check._schema);
251
+ var rootKeys = valKeys.filter(function (a) { return !a.includes('.'); });
252
+ for (var i = 0; i < methodData.length; i++) {
253
+ valObj[rootKeys[i]] = methodData[i];
254
+ }
255
+ try {
256
+ that._methods[method].check.validate(valObj);
257
+ }
258
+ catch (errors) {
259
+ if (errors) {
260
+ console.error('Error in Method Check (' + method + ')', errors);
261
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'Match Error On Method ' + method + '\n\nData Being Checked\n' + JSON.stringify(valObj, null, 2) + '\n\nErrors\n' + JSON.stringify(errors, null, 2));
262
+ return;
263
+ }
264
+ }
265
+ }
266
+ }
267
+ (_a = that._methods[method].function).call.apply(_a, [Object.assign({}, that, MethodManager.prototype, { id_user: '', user: '', id_ws: '' })].concat(methodData));
268
+ };
269
+ MethodManager.prototype.callMethodQueue = function (update) {
270
+ var _this = this;
271
+ // console.log('Call Method Queue', update);
272
+ return new Promise(function (resolve, reject) {
273
+ var _a;
274
+ var updateCopy = common_1.deepCopy(update);
275
+ var that = _this;
276
+ if (!that._methods[update.method]) {
277
+ console.log('No Method: ' + update.method);
278
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Method: ' + update.method);
279
+ var serverRes = {
280
+ messageId: update.messageId,
281
+ hasError: true,
282
+ data: 'Internal Error'
283
+ };
284
+ _this._mainServer.getQueueManager().addMethodResponse(updateCopy, serverRes);
285
+ return resolve('No Method: ' + update.method);
286
+ }
287
+ if ((update.method_data.length > 1 || update.method_data[0]) && !that._methods[update.method].skipValidation) {
288
+ if (!that._methods[update.method].check) {
289
+ console.error('No Check Function For Method ' + update.method);
290
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Check Function For Method ' + update.method);
291
+ var serverRes = {
292
+ messageId: update.messageId,
293
+ hasError: true,
294
+ data: 'Internal Error'
295
+ };
296
+ _this._mainServer.getQueueManager().addMethodResponse(updateCopy, serverRes);
297
+ return resolve('No Check Function For Method ' + update.method);
298
+ }
299
+ else if (!that._methods[update.method].check._schema) {
300
+ console.error('No Check Schema For Method ' + update.method);
301
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Check Function For Method ' + update.method);
302
+ var serverRes = {
303
+ messageId: update.messageId,
304
+ hasError: true,
305
+ data: 'Internal Error'
306
+ };
307
+ _this._mainServer.getQueueManager().addMethodResponse(updateCopy, serverRes);
308
+ return resolve('No Check Schema For Method ' + update.method);
309
+ }
310
+ else {
311
+ var valObj = {};
312
+ var valKeys = Object.keys(that._methods[update.method].check._schema);
313
+ var rootKeys = valKeys.filter(function (a) { return !a.includes('.'); });
314
+ for (var i = 0; i < update.method_data.length; i++) {
315
+ valObj[rootKeys[i]] = update.method_data[i];
316
+ }
317
+ try {
318
+ that._methods[update.method].check.validate(valObj);
319
+ }
320
+ catch (errors) {
321
+ if (errors) {
322
+ console.error('Error in Method Check (' + update.method + ')', errors);
323
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'Match Error On Method ' + update.method + '\n\nData Being Checked\n' + JSON.stringify(valObj, null, 2) + '\n\nErrors\n' + JSON.stringify(errors, null, 2));
324
+ var serverRes = {
325
+ messageId: update.messageId,
326
+ hasError: true,
327
+ data: 'Internal Error'
328
+ };
329
+ _this._mainServer.getQueueManager().addMethodResponse(updateCopy, serverRes);
330
+ return resolve('Error in Method Check (' + update.method + ')');
331
+ }
332
+ }
333
+ }
334
+ }
335
+ var timeout = setTimeout(function () {
336
+ console.error('Timeout on method', update);
337
+ _this.callMethodInternal.call(_this, 'insertErrorLog', 'Timeout on method: ', [update]);
338
+ resolve('Timeout');
339
+ }, 5000);
340
+ // console.log('Run Method', update.method);
341
+ (_a = that._methods[update.method].function).call.apply(_a, [Object.assign({}, that, MethodManager.prototype, { id_user: update.id_user, user: update.user, id_ws: update.id_ws })].concat(update.method_data)).then(function (res) {
342
+ var serverRes = {
343
+ messageId: update.messageId,
344
+ hasError: false,
345
+ data: res
346
+ };
347
+ _this._mainServer.getQueueManager().addMethodResponse(updateCopy, serverRes);
348
+ clearTimeout(timeout);
349
+ resolve(serverRes);
350
+ }, function (err) {
351
+ var serverRes = {
352
+ messageId: update.messageId,
353
+ hasError: true,
354
+ data: err
355
+ };
356
+ _this._mainServer.getQueueManager().addMethodResponse(updateCopy, serverRes);
357
+ clearTimeout(timeout);
358
+ resolve(serverRes);
359
+ });
360
+ });
361
+ };
362
+ // Call/run method (Emit on Socket)
363
+ MethodManager.prototype.callMethod = function (ws, messageDate, messageId, method) {
364
+ var _a;
365
+ var _this = this;
366
+ var methodData = [];
367
+ for (var _i = 4; _i < arguments.length; _i++) {
368
+ methodData[_i - 4] = arguments[_i];
369
+ }
370
+ var that = this;
371
+ if (!that._methods[method]) {
372
+ console.log('No Method: ' + method);
373
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Method: ' + method);
374
+ var serverRes = {
375
+ messageId: messageId,
376
+ hasError: true,
377
+ data: 'Internal Error'
378
+ };
379
+ this.sendWS(ws, messageDate, method, methodData, serverRes);
380
+ return;
381
+ }
382
+ if ((methodData.length > 1 || methodData[0]) && !that._methods[method].skipValidation) {
383
+ if (!that._methods[method].check) {
384
+ console.error('No Check Function For Method ' + method);
385
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Check Function For Method ' + method);
386
+ var serverRes = {
387
+ messageId: messageId,
388
+ hasError: true,
389
+ data: 'Internal Error'
390
+ };
391
+ this.sendWS(ws, messageDate, method, methodData, serverRes);
392
+ return;
393
+ }
394
+ else if (!that._methods[method].check._schema) {
395
+ console.error('No Check Schema For Method ' + method);
396
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Check Function For Method ' + method);
397
+ var serverRes = {
398
+ messageId: messageId,
399
+ hasError: true,
400
+ data: 'Internal Error'
401
+ };
402
+ this.sendWS(ws, messageDate, method, methodData, serverRes);
403
+ return;
404
+ }
405
+ else {
406
+ var valObj = {};
407
+ var valKeys = Object.keys(that._methods[method].check._schema);
408
+ var rootKeys = valKeys.filter(function (a) { return !a.includes('.'); });
409
+ for (var i = 0; i < methodData.length; i++) {
410
+ valObj[rootKeys[i]] = methodData[i];
411
+ }
412
+ try {
413
+ that._methods[method].check.validate(valObj);
414
+ }
415
+ catch (errors) {
416
+ if (errors) {
417
+ console.error('Error in Method Check (' + method + ')', errors);
418
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'Match Error On Method ' + method + '\n\nData Being Checked\n' + JSON.stringify(valObj, null, 2) + '\n\nErrors\n' + JSON.stringify(errors, null, 2));
419
+ var serverRes = {
420
+ messageId: messageId,
421
+ hasError: true,
422
+ data: 'Internal Error'
423
+ };
424
+ this.sendWS(ws, messageDate, method, methodData, serverRes);
425
+ return;
426
+ }
427
+ }
428
+ }
429
+ }
430
+ (_a = that._methods[method].function).call.apply(_a, [Object.assign({}, that, MethodManager.prototype, { id_user: ws['id_user'], user: ws['user'], id_ws: ws['id_socket'] })].concat(methodData)).then(function (res) {
431
+ var serverRes = {
432
+ messageId: messageId,
433
+ hasError: false,
434
+ data: res
435
+ };
436
+ _this.sendWS(ws, messageDate, method, methodData, serverRes);
437
+ }, function (err) {
438
+ var serverRes = {
439
+ messageId: messageId,
440
+ hasError: true,
441
+ data: err
442
+ };
443
+ _this.sendWS(ws, messageDate, method, methodData, serverRes);
444
+ });
445
+ };
446
+ // Call/run method internal (No Emit on Socket)
447
+ MethodManager.prototype.callMethodInternal = function (method) {
448
+ var _a;
449
+ var methodData = [];
450
+ for (var _i = 1; _i < arguments.length; _i++) {
451
+ methodData[_i - 1] = arguments[_i];
452
+ }
453
+ var that = this;
454
+ if (!that._methods[method]) {
455
+ console.log('No Method: ' + method);
456
+ return;
457
+ }
458
+ if ((methodData.length > 1 || (methodData[0] && typeof (methodData[0]) !== 'function')) && !that._methods[method].skipValidation) {
459
+ if (!that._methods[method].check) {
460
+ console.error('No Check Function For Method ' + method);
461
+ return;
462
+ }
463
+ else if (!that._methods[method].check._schema) {
464
+ console.error('No Check Schema For Method ' + method);
465
+ return;
466
+ }
467
+ }
468
+ var promise = (_a = that._methods[method].function).call.apply(_a, [Object.assign({}, that, MethodManager.prototype)].concat(methodData));
469
+ if (methodData[methodData.length - 1] && typeof (methodData[methodData.length - 1]) === 'function') {
470
+ promise.then(function (res) { return methodData[methodData.length - 1](null, res); }, function (err) { return methodData[methodData.length - 1](err, null); });
471
+ }
472
+ return promise;
473
+ };
474
+ MethodManager.prototype.callSupportMethodInternal = function (supportMethod) {
475
+ var _a;
476
+ var methodData = [];
477
+ for (var _i = 1; _i < arguments.length; _i++) {
478
+ methodData[_i - 1] = arguments[_i];
479
+ }
480
+ var that = this;
481
+ if (!that.getMainServer().getSupportManager()._supportMethods[supportMethod]) {
482
+ console.log('No Support Method: ' + supportMethod);
483
+ return;
484
+ }
485
+ if ((methodData.length > 1 || (methodData[0] && typeof (methodData[0]) !== 'function')) && !that.getMainServer().getSupportManager()._supportMethods[supportMethod].skipValidation) {
486
+ if (!that.getMainServer().getSupportManager()._supportMethods[supportMethod].check) {
487
+ console.error('No Check Function For Method ' + supportMethod);
488
+ return;
489
+ }
490
+ else if (!that.getMainServer().getSupportManager()._supportMethods[supportMethod].check._schema) {
491
+ console.error('No Check Schema For Method ' + supportMethod);
492
+ return;
493
+ }
494
+ }
495
+ var promise = (_a = that.getMainServer().getSupportManager()._supportMethods[supportMethod].function).call.apply(_a, [Object.assign({}, that, support_manager_1.SupportManager.prototype)].concat(methodData));
496
+ if (methodData[methodData.length - 1] && typeof (methodData[methodData.length - 1]) === 'function') {
497
+ promise.then(function (res) { return methodData[methodData.length - 1](null, res); }, function (err) { return methodData[methodData.length - 1](err, null); });
498
+ }
499
+ return promise;
500
+ };
501
+ MethodManager.prototype.sendWS = function (ws, messageDate, method, methodData, data) {
502
+ this._sendQueue.splice(0, 0, {
503
+ id_ws: ws['id_socket'],
504
+ method: method,
505
+ methodData: methodData,
506
+ data: data,
507
+ date: messageDate
508
+ });
509
+ };
510
+ MethodManager.prototype.sendEmail = function (sendTo, subject, text, html, attachments, send_from) {
511
+ this._mailer.sendMail({
512
+ replyTo: undefined,
513
+ from: send_from ? send_from : this.serverConfig.MAIL_FROM,
514
+ to: sendTo,
515
+ subject: subject,
516
+ text: text,
517
+ html: html,
518
+ attachments: attachments
519
+ }, function (err, info) {
520
+ if (err) {
521
+ console.log(err);
522
+ }
523
+ });
524
+ };
525
+ MethodManager.prototype.getAWS = function () {
526
+ return this._aws;
527
+ };
528
+ MethodManager.prototype.readFile = function (fileName) {
529
+ var _this = this;
530
+ return new Promise(function (resolve, reject) {
531
+ if (fs.existsSync(path.join(__dirname, ('../private/' + fileName)))) {
532
+ fs.readFile(path.join(__dirname, ('../private/' + fileName)), 'utf-8', function (err, res) {
533
+ if (err) {
534
+ reject(err);
535
+ }
536
+ else {
537
+ resolve(res);
538
+ }
539
+ });
540
+ }
541
+ else {
542
+ if (fs.existsSync(path.join(_this.clientDir, ('./private/' + fileName)))) {
543
+ fs.readFile(path.join(_this.clientDir, ('./private/' + fileName)), 'utf-8', function (err, res) {
544
+ if (err) {
545
+ reject(err);
546
+ }
547
+ else {
548
+ resolve(res);
549
+ }
550
+ });
551
+ }
552
+ }
553
+ });
554
+ };
555
+ MethodManager.prototype.readImage = function (fileName) {
556
+ var _this = this;
557
+ return new Promise(function (resolve, reject) {
558
+ if (fs.existsSync(path.join(__dirname, ('../private/' + fileName)))) {
559
+ fs.readFile(path.join(__dirname, ('../private/' + fileName)), 'base64', function (err, res) {
560
+ if (err) {
561
+ reject(err);
562
+ }
563
+ else {
564
+ resolve(res);
565
+ }
566
+ });
567
+ }
568
+ else {
569
+ if (fs.existsSync(path.join(_this.clientDir, ('./private/' + fileName)))) {
570
+ fs.readFile(path.join(_this.clientDir, ('./private/' + fileName)), 'base64', function (err, res) {
571
+ if (err) {
572
+ reject(err);
573
+ }
574
+ else {
575
+ resolve(res);
576
+ }
577
+ });
578
+ }
579
+ }
580
+ });
581
+ };
582
+ return MethodManager;
583
+ }());
584
+ exports.MethodManager = MethodManager;
@@ -0,0 +1,13 @@
1
+ import { QueueMethodModel } from '../models/queue-method.model';
2
+ import { ServerResponseModel } from '../models/server-response.model';
3
+ export declare class QueueMethodManager {
4
+ private _mainServer;
5
+ private _sendQueue;
6
+ private _runningQueue;
7
+ private _runningMethodResponse;
8
+ constructor(mainServer: any);
9
+ addMethodToQueue(id_ws: string, id_user: string, user: string, messageDate: Date, messageId: string, method: string, data: any[]): void;
10
+ runMethodQueue(count?: number): Promise<void>;
11
+ addMethodResponse(update: QueueMethodModel, response: ServerResponseModel): void;
12
+ runMethodResponse(): Promise<void>;
13
+ }