@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
package/server-app.js ADDED
@@ -0,0 +1,638 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (_) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ var http_1 = require("http");
39
+ var express = require("express");
40
+ var mongoose = require("mongoose");
41
+ var bodyParser = require("body-parser");
42
+ var passport = require("passport");
43
+ var passportlocal = require("passport-local");
44
+ var WebSocket = require("ws");
45
+ var jwt = require("jsonwebtoken");
46
+ var uuid = require("uuid/v4");
47
+ var auth_1 = require("./http/auth");
48
+ var common_1 = require("./util/common");
49
+ var health_1 = require("./http/health");
50
+ var method_call_collection_1 = require("./collections/method-call.collection");
51
+ var mongoose_1 = require("mongoose");
52
+ var method_response_collection_1 = require("./collections/method-response.collection");
53
+ var log_collection_1 = require("./collections/log.collection");
54
+ var moment = require("moment");
55
+ var user_collection_1 = require("./collections/user.collection");
56
+ var cron_manager_1 = require("./managers/cron.manager");
57
+ var method_manager_1 = require("./managers/method.manager");
58
+ var subscription_manager_1 = require("./managers/subscription.manager");
59
+ var support_manager_1 = require("./managers/support.manager");
60
+ var queue_method_manager_1 = require("./managers/queue-method.manager");
61
+ var common_2 = require("./util/common");
62
+ var ResolveIOMainServer = /** @class */ (function () {
63
+ function ResolveIOMainServer(mainServer, serverConfig, clientDir) {
64
+ this._msgQueue = [];
65
+ this._msgQueueRunning = false;
66
+ this._resolveioServer = mainServer;
67
+ this._serverConfig = serverConfig;
68
+ this._clientDir = clientDir;
69
+ // process.on('unhandledException').
70
+ // Start express app
71
+ this._app = express();
72
+ // Use body parser for http call (login)
73
+ this._app.use(bodyParser.json({ limit: '5mb' }));
74
+ this._app.use(bodyParser.urlencoded({ limit: '5mb', extended: true }));
75
+ // Set port
76
+ this._port = process.env.PORT || serverConfig['PORT'];
77
+ // Create http server and websock server
78
+ this.createServer();
79
+ // Set CORS
80
+ this._app.use(function (req, res, next) {
81
+ // Website you wish to allow to connect
82
+ // res.setHeader('Access-Control-Allow-Origin', serverConfig['ROOT_URL']);
83
+ res.setHeader('Access-Control-Allow-Origin', '*');
84
+ // Request methods you wish to allow
85
+ // res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
86
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
87
+ // Request headers you wish to allow
88
+ res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
89
+ // Set to true if you need the website to include cookies in the requests sent
90
+ // to the API (e.g. in case you use sessions)
91
+ res.setHeader('Access-Control-Allow-Credentials', 'false');
92
+ // Pass to next layer of middleware
93
+ next();
94
+ });
95
+ // Set up http login route
96
+ auth_1.setupAuthRoutes(this, this._app, serverConfig);
97
+ health_1.setupHealthRoutes(this._app, serverConfig);
98
+ }
99
+ ResolveIOMainServer.prototype.getWS = function (id_ws) {
100
+ var res = null;
101
+ this._wss.clients.forEach(function (ws) {
102
+ if (ws['id_socket'] === id_ws) {
103
+ res = ws;
104
+ }
105
+ });
106
+ return res;
107
+ };
108
+ ResolveIOMainServer.prototype.getWSList = function () {
109
+ var res = [];
110
+ this._wss.clients.forEach(function (ws) {
111
+ res.push(ws['id_socket']);
112
+ });
113
+ return res;
114
+ };
115
+ ResolveIOMainServer.prototype.getCronManager = function () {
116
+ return this._cronManager;
117
+ };
118
+ ResolveIOMainServer.prototype.getMethodManager = function () {
119
+ return this._methodManager;
120
+ };
121
+ ResolveIOMainServer.prototype.getSubscriptionManager = function () {
122
+ return this._subscriptionManager;
123
+ };
124
+ ResolveIOMainServer.prototype.getQueueManager = function () {
125
+ return this._queueManager;
126
+ };
127
+ ResolveIOMainServer.prototype.getSupportManager = function () {
128
+ return this._supportManager;
129
+ };
130
+ ResolveIOMainServer.prototype.createServer = function () {
131
+ var _this = this;
132
+ // Start express server
133
+ this._server = http_1.createServer(this._app);
134
+ // Start websocket server
135
+ // Verify client with token before opening socket
136
+ this._wss = new WebSocket.Server({
137
+ server: this._server,
138
+ verifyClient: function (info, cb) { return __awaiter(_this, void 0, void 0, function () {
139
+ var infoData, token;
140
+ var _this = this;
141
+ return __generator(this, function (_a) {
142
+ infoData = info.req.headers['sec-websocket-protocol'].split(/,/);
143
+ if (info.origin !== this._serverConfig['ROOT_URL'] && info.origin !== this._serverConfig['SEC_ROOT_URL'] && info.origin !== this._serverConfig['RESOLVEIO_URL'] && info.origin !== this._serverConfig['RESOLVEIO_SECONDARY_URL']) {
144
+ cb(false, 401, 'Unauthorized');
145
+ }
146
+ else {
147
+ if (infoData[1]) { //app version
148
+ token = infoData[0];
149
+ if (!token) {
150
+ cb(false, 401, 'Unauthorized');
151
+ }
152
+ else {
153
+ jwt.verify(token, this._serverConfig['JWT_SECRET'], function (err, decoded) { return __awaiter(_this, void 0, void 0, function () {
154
+ var user, err_1;
155
+ return __generator(this, function (_a) {
156
+ switch (_a.label) {
157
+ case 0:
158
+ if (!err) return [3 /*break*/, 1];
159
+ cb(false, 401, 'Unauthorized');
160
+ return [3 /*break*/, 5];
161
+ case 1:
162
+ info.req['id_user'] = decoded['id_user'];
163
+ _a.label = 2;
164
+ case 2:
165
+ _a.trys.push([2, 4, , 5]);
166
+ return [4 /*yield*/, user_collection_1.Users.findById(decoded['id_user']).exec()];
167
+ case 3:
168
+ user = _a.sent();
169
+ if (user) {
170
+ info.req['user'] = user.fullname;
171
+ cb(true);
172
+ }
173
+ else {
174
+ cb(false);
175
+ }
176
+ return [3 /*break*/, 5];
177
+ case 4:
178
+ err_1 = _a.sent();
179
+ cb(false);
180
+ return [3 /*break*/, 5];
181
+ case 5: return [2 /*return*/];
182
+ }
183
+ });
184
+ }); });
185
+ }
186
+ }
187
+ else {
188
+ cb(false, 401, 'Invalid Version');
189
+ }
190
+ }
191
+ return [2 /*return*/];
192
+ });
193
+ }); }
194
+ });
195
+ // Start Mongoose (MongoDB Connection)
196
+ this.setUpMongoose();
197
+ };
198
+ ResolveIOMainServer.prototype.setUpMongoose = function () {
199
+ this.setUpPassport();
200
+ // Enable lean by default
201
+ var __setOptions = mongoose_1.Query.prototype.setOptions;
202
+ mongoose_1.Query.prototype.setOptions = function (options) {
203
+ if (this._mongooseOptions.lean == null && this.mongooseCollection.collectionName !== 'users') {
204
+ this._mongooseOptions.lean = true;
205
+ }
206
+ __setOptions.apply(this, arguments);
207
+ return this;
208
+ };
209
+ // Start Managers
210
+ this._subscriptionManager = new subscription_manager_1.SubscriptionManager(this, this._wss, this._serverConfig);
211
+ this._methodManager = new method_manager_1.MethodManager(this, this._serverConfig, this._clientDir);
212
+ this._supportManager = new support_manager_1.SupportManager(this, this._serverConfig, this._clientDir);
213
+ this._cronManager = new cron_manager_1.CronManager(this);
214
+ this._queueManager = new queue_method_manager_1.QueueMethodManager(this);
215
+ this.listen();
216
+ mongoose.set('useFindAndModify', false);
217
+ // mongoose.set('useCreateIndexes', true);
218
+ };
219
+ ResolveIOMainServer.prototype.setUpPassport = function () {
220
+ // Set up login passort
221
+ this._app.use(passport.initialize());
222
+ // Start user authentication
223
+ passport.use(new passportlocal.Strategy(user_collection_1.Users.authenticate()));
224
+ passport.serializeUser(user_collection_1.Users.serializeUser());
225
+ passport.deserializeUser(user_collection_1.Users.deserializeUser());
226
+ };
227
+ // Listen to port for http/websocket
228
+ ResolveIOMainServer.prototype.listen = function () {
229
+ var _this = this;
230
+ this._server.listen(this._port, function () {
231
+ console.log('Running server on port %s', _this._port);
232
+ });
233
+ // On websocket connection (already verified)
234
+ this._wss.on('connection', function (ws, req) {
235
+ // Get user from token
236
+ ws['id_user'] = req.id_user;
237
+ ws['user'] = req.user;
238
+ ws['id_socket'] = uuid();
239
+ ws['retryCnt'] = 0;
240
+ // Use for keeping connection alive (ping/pong)
241
+ ws['isAlive'] = true;
242
+ ws.on('pong', function () {
243
+ ws['isAlive'] = true;
244
+ ws['pongTime'] = new Date();
245
+ ws['latency'] = moment.duration(moment(ws['pongTime']).diff(ws['pingTime'])).asMilliseconds();
246
+ _this._subscriptionManager.loggedInLatency(ws);
247
+ });
248
+ ws['versionTimer'] = setTimeout(function () {
249
+ _this.unsubscribeWS(ws);
250
+ ws.terminate();
251
+ }, 10000);
252
+ // On data received (message)
253
+ ws.on('message', function (message) { return __awaiter(_this, void 0, void 0, function () {
254
+ var socketData, data, messageDate, messageId, type, subType, pub;
255
+ var _a, _b;
256
+ var _this = this;
257
+ return __generator(this, function (_c) {
258
+ socketData = JSON.parse(message, common_1.dateReviver);
259
+ data = common_2.deepCopy(socketData);
260
+ if (data[0] === 'ping') {
261
+ if (ws && ws.readyState === ws.OPEN) {
262
+ ws.send('pong', function (error) {
263
+ if (error) {
264
+ console.log('Error on WS: ', error);
265
+ _this.unsubscribeWS(ws);
266
+ }
267
+ });
268
+ }
269
+ return [2 /*return*/];
270
+ }
271
+ messageDate = data.shift();
272
+ messageId = data.shift();
273
+ type = data.shift();
274
+ if (type === 'subscription') {
275
+ subType = data.shift();
276
+ pub = data.shift();
277
+ // Subscribe
278
+ if (subType === 'sub') {
279
+ (_a = this._subscriptionManager).subscribe.apply(_a, [ws, messageId, pub].concat(data));
280
+ }
281
+ // Unsubscribe
282
+ else {
283
+ (_b = this._subscriptionManager).unsubscribe.apply(_b, [ws, messageId, pub].concat(data));
284
+ }
285
+ }
286
+ else {
287
+ this._msgQueue.splice(0, 0, {
288
+ ws: ws,
289
+ data: socketData
290
+ });
291
+ }
292
+ return [2 /*return*/];
293
+ });
294
+ }); });
295
+ ws.on('close', function () {
296
+ _this.unsubscribeWS(ws);
297
+ });
298
+ });
299
+ // Keep alive timer to ping/pong
300
+ setInterval(function () {
301
+ _this._wss.clients.forEach(function (ws) {
302
+ if (ws['isAlive'] === false) {
303
+ ws['retryCnt']++;
304
+ if (ws['retryCnt'] >= 60) {
305
+ _this.unsubscribeWS(ws);
306
+ ws.terminate();
307
+ }
308
+ else {
309
+ ws['pingTime'] = new Date();
310
+ ws.ping(function () { });
311
+ }
312
+ }
313
+ else {
314
+ ws['retryCnt'] = 0;
315
+ ws['isAlive'] = false;
316
+ ws['pingTime'] = new Date();
317
+ ws.ping(function () { });
318
+ }
319
+ });
320
+ }, 5000);
321
+ setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
322
+ var _loop_1, this_1, i;
323
+ var _this = this;
324
+ return __generator(this, function (_a) {
325
+ switch (_a.label) {
326
+ case 0:
327
+ if (!!this._msgQueueRunning) return [3 /*break*/, 5];
328
+ this._msgQueueRunning = true;
329
+ _loop_1 = function (i) {
330
+ var msg, data, ws, messageDate, messageId, type, method, serverRes, query, methodCall, supportMethod, serverRes;
331
+ var _a, _b;
332
+ return __generator(this, function (_c) {
333
+ switch (_c.label) {
334
+ case 0:
335
+ msg = this_1._msgQueue.pop();
336
+ data = msg.data;
337
+ ws = msg.ws;
338
+ messageDate = data.shift();
339
+ messageId = data.shift();
340
+ type = data.shift();
341
+ method = '';
342
+ if (!(type === 'method')) return [3 /*break*/, 7];
343
+ method = data.shift();
344
+ if (method === 'setWSAppVersion') {
345
+ clearTimeout(ws['versionTimer']);
346
+ }
347
+ if (method === 'disconnectWSCommand') {
348
+ setTimeout(function () {
349
+ _this.unsubscribeWS(ws);
350
+ ws.terminate();
351
+ }, 5000);
352
+ }
353
+ if (method !== 'reportBuilderGetResults' && method !== 'reportBuilderGetDistinctValue' && method !== 'reportBuilderBuildTree' && method !== 'generatePDF' && method !== 'getWOOfflineData' && method !== 'countQuery' && method !== 'countWithQuery' && method !== 'countCollectionWithQuery' && method !== 'find' && method !== 'findOne' && method !== 'findWithOptions' && method !== 'setWSAppVersion' && method !== 'getDrivers') {
354
+ if (common_1.getBinarySize(JSON.stringify(data)) < 200000) {
355
+ log_collection_1.Logs.create({
356
+ _id: mongoose_1.Types.ObjectId().toHexString(),
357
+ date: new Date(),
358
+ type: 'client-request',
359
+ title: 'Client Request - Method',
360
+ message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
361
+ payload: JSON.stringify(data),
362
+ method: method,
363
+ user: ws['user'],
364
+ messageId: messageId
365
+ });
366
+ }
367
+ else {
368
+ log_collection_1.Logs.create({
369
+ _id: mongoose_1.Types.ObjectId().toHexString(),
370
+ date: new Date(),
371
+ type: 'client-request',
372
+ title: 'Client Request - Method',
373
+ message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
374
+ payload: 'Too Big',
375
+ method: method,
376
+ user: ws['user'],
377
+ messageId: messageId
378
+ });
379
+ }
380
+ }
381
+ serverRes = {
382
+ messageId: messageId,
383
+ hasError: false,
384
+ data: 'ACK'
385
+ };
386
+ if (ws && ws.readyState === ws.OPEN) {
387
+ ws.send(JSON.stringify(serverRes), function (error) {
388
+ if (error) {
389
+ console.log('Error on WS: ', error);
390
+ _this.unsubscribeWS(ws);
391
+ }
392
+ });
393
+ }
394
+ if (!this_1._methodManager._methods[method]) return [3 /*break*/, 5];
395
+ query = { $and: [
396
+ { id_user: ws['id_user'] },
397
+ { message_id: messageId },
398
+ { date: messageDate }
399
+ ] };
400
+ if (method !== 'updateDocumentOffline' && method !== 'updateDocumentPropsOffline') {
401
+ query.$and.push({ method: method });
402
+ }
403
+ else {
404
+ if (method === 'updateDocumentOffline') {
405
+ query.$and.push({
406
+ $or: [
407
+ { method: method },
408
+ { method: 'updateDocument' }
409
+ ]
410
+ });
411
+ }
412
+ else if (method === 'updateDocumentPropsOffline') {
413
+ query.$and.push({
414
+ $or: [
415
+ { method: method },
416
+ { method: 'updateDocumentProps' }
417
+ ]
418
+ });
419
+ }
420
+ }
421
+ return [4 /*yield*/, method_call_collection_1.MethodCalls.findOne(query).exec()];
422
+ case 1:
423
+ methodCall = _c.sent();
424
+ if (!!methodCall) return [3 /*break*/, 3];
425
+ return [4 /*yield*/, method_call_collection_1.MethodCalls.create({
426
+ _id: mongoose_1.Types.ObjectId().toHexString(),
427
+ __v: 0,
428
+ id_user: ws['id_user'],
429
+ message_id: messageId,
430
+ method: method,
431
+ date: messageDate
432
+ })];
433
+ case 2:
434
+ _c.sent();
435
+ if (this_1._methodManager._methods[method]) {
436
+ if (this_1._methodManager._methods[method].skipQueue) {
437
+ (_a = this_1._methodManager).callMethod.apply(_a, [ws, messageDate, messageId, method].concat(data));
438
+ }
439
+ else {
440
+ this_1._queueManager.addMethodToQueue(ws['id_socket'], ws['id_user'], ws['user'], messageDate, messageId, method, data);
441
+ }
442
+ }
443
+ else {
444
+ console.log('Could not find method: ' + method);
445
+ }
446
+ return [3 /*break*/, 4];
447
+ case 3:
448
+ method_response_collection_1.MethodResponses.findOne({ $and: [
449
+ { id_user: ws['id_user'] },
450
+ { message_id: messageId },
451
+ { method: method },
452
+ { date: messageDate }
453
+ ] }).exec(function (err, res) {
454
+ if (res) {
455
+ if (ws && ws.readyState === ws.OPEN) {
456
+ ws.send(JSON.stringify(res.response), function (error) {
457
+ if (error) {
458
+ console.log('Error on WS: ', error);
459
+ _this.unsubscribeWS(ws);
460
+ }
461
+ });
462
+ }
463
+ }
464
+ });
465
+ _c.label = 4;
466
+ case 4: return [3 /*break*/, 6];
467
+ case 5:
468
+ console.log('Could not find method: ' + method);
469
+ _c.label = 6;
470
+ case 6: return [3 /*break*/, 8];
471
+ case 7:
472
+ if (type === 'methodRetry') {
473
+ method = data.shift();
474
+ if (method !== 'reportBuilderGetResults' && method !== 'reportBuilderGetDistinctValue' && method !== 'reportBuilderBuildTree' && method !== 'generatePDF' && method !== 'getWOOfflineData' && method !== 'countQuery' && method !== 'countWithQuery' && method !== 'countCollectionWithQuery' && method !== 'find' && method !== 'findOne' && method !== 'findWithOptions' && method !== 'setWSAppVersion' && method !== 'getDrivers') {
475
+ if (common_1.getBinarySize(JSON.stringify(data)) < 200000) {
476
+ log_collection_1.Logs.create({
477
+ _id: mongoose_1.Types.ObjectId().toHexString(),
478
+ date: new Date(),
479
+ type: 'client-request',
480
+ title: 'Client Request - Method Retry',
481
+ message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
482
+ payload: JSON.stringify(data),
483
+ method: method,
484
+ user: ws['user'],
485
+ messageId: messageId
486
+ });
487
+ }
488
+ else {
489
+ log_collection_1.Logs.create({
490
+ _id: mongoose_1.Types.ObjectId().toHexString(),
491
+ date: new Date(),
492
+ type: 'client-request',
493
+ title: 'Client Request - Method Retry',
494
+ message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Method: ' + method,
495
+ payload: 'Too Big',
496
+ method: method,
497
+ user: ws['user'],
498
+ messageId: messageId
499
+ });
500
+ }
501
+ }
502
+ method_call_collection_1.MethodCalls.findOne({ $and: [
503
+ { id_user: ws['id_user'] },
504
+ { message_id: messageId },
505
+ { method: method },
506
+ { date: messageDate }
507
+ ] }).exec(function (err, res) {
508
+ if (res) {
509
+ var serverRes = {
510
+ messageId: messageId,
511
+ hasError: false,
512
+ data: 'ACK'
513
+ };
514
+ if (ws && ws.readyState === ws.OPEN) {
515
+ ws.send(JSON.stringify(serverRes), function (error) {
516
+ if (error) {
517
+ console.log('Error on WS: ', error);
518
+ _this.unsubscribeWS(ws);
519
+ }
520
+ });
521
+ }
522
+ }
523
+ else {
524
+ var serverRes = {
525
+ messageId: messageId,
526
+ hasError: false,
527
+ data: 'NACK'
528
+ };
529
+ if (ws && ws.readyState === ws.OPEN) {
530
+ ws.send(JSON.stringify(serverRes), function (error) {
531
+ if (error) {
532
+ console.log('Error on WS: ', error);
533
+ _this.unsubscribeWS(ws);
534
+ }
535
+ });
536
+ }
537
+ }
538
+ });
539
+ }
540
+ else if (type === 'methodResponse') {
541
+ method = data.shift();
542
+ method_response_collection_1.MethodResponses.findOne({ $and: [
543
+ { id_user: ws['id_user'] },
544
+ { message_id: messageId },
545
+ { method: method },
546
+ { date: messageDate }
547
+ ] }).exec(function (err, res) {
548
+ if (res) {
549
+ if (ws && ws.readyState === ws.OPEN) {
550
+ ws.send(JSON.stringify(res.response), function (error) {
551
+ if (error) {
552
+ console.log('Error on WS: ', error);
553
+ _this.unsubscribeWS(ws);
554
+ }
555
+ });
556
+ }
557
+ }
558
+ });
559
+ }
560
+ else if (type === 'support') {
561
+ supportMethod = data.shift();
562
+ if (common_1.getBinarySize(JSON.stringify(data)) < 200000) {
563
+ log_collection_1.Logs.create({
564
+ _id: mongoose_1.Types.ObjectId().toHexString(),
565
+ date: new Date(),
566
+ type: 'client-request',
567
+ title: 'Client Request - Support',
568
+ message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Support Method: ' + supportMethod,
569
+ payload: JSON.stringify(data),
570
+ method: supportMethod,
571
+ user: ws['user'],
572
+ messageId: messageId
573
+ });
574
+ }
575
+ else {
576
+ log_collection_1.Logs.create({
577
+ _id: mongoose_1.Types.ObjectId().toHexString(),
578
+ date: new Date(),
579
+ type: 'client-request',
580
+ title: 'Client Request - Support',
581
+ message: 'User: ' + ws['user'] + ', Type: ' + type + ', MessageId: ' + messageId + ', Support Method: ' + supportMethod,
582
+ payload: 'Too Big',
583
+ method: supportMethod,
584
+ user: ws['user'],
585
+ messageId: messageId
586
+ });
587
+ }
588
+ serverRes = {
589
+ messageId: messageId,
590
+ hasError: false,
591
+ data: 'ACK'
592
+ };
593
+ if (ws && ws.readyState === ws.OPEN) {
594
+ ws.send(JSON.stringify(serverRes), function (error) {
595
+ if (error) {
596
+ console.log('Error on WS: ', error);
597
+ _this.unsubscribeWS(ws);
598
+ }
599
+ });
600
+ }
601
+ if (this_1._supportManager._supportMethods[supportMethod]) {
602
+ (_b = this_1._supportManager).callSupportMethod.apply(_b, [ws, messageId, supportMethod].concat(data));
603
+ }
604
+ else {
605
+ console.log('Could not find support method: ' + supportMethod);
606
+ }
607
+ }
608
+ _c.label = 8;
609
+ case 8: return [2 /*return*/];
610
+ }
611
+ });
612
+ };
613
+ this_1 = this;
614
+ i = this._msgQueue.length - 1;
615
+ _a.label = 1;
616
+ case 1:
617
+ if (!(i >= 0)) return [3 /*break*/, 4];
618
+ return [5 /*yield**/, _loop_1(i)];
619
+ case 2:
620
+ _a.sent();
621
+ _a.label = 3;
622
+ case 3:
623
+ i--;
624
+ return [3 /*break*/, 1];
625
+ case 4:
626
+ this._msgQueueRunning = false;
627
+ _a.label = 5;
628
+ case 5: return [2 /*return*/];
629
+ }
630
+ });
631
+ }); }, 25);
632
+ };
633
+ ResolveIOMainServer.prototype.unsubscribeWS = function (ws) {
634
+ this._subscriptionManager.unsubscribeAll(ws);
635
+ };
636
+ return ResolveIOMainServer;
637
+ }());
638
+ exports.default = ResolveIOMainServer;
@@ -0,0 +1,2 @@
1
+ import { SupportManager } from '../managers/support.manager';
2
+ export declare function loadAWSMethods(supportManager: SupportManager): void;