@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,236 @@
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 simpl_schema_1 = require("simpl-schema");
39
+ var file_collection_1 = require("../collections/file.collection");
40
+ var handlebars = require("handlebars");
41
+ function loadSupportMethods(supportManager) {
42
+ supportManager.supportMethods({
43
+ sendSupportTicketEmail: {
44
+ check: new simpl_schema_1.default({
45
+ supportTicket: {
46
+ type: Object,
47
+ blackbox: true
48
+ },
49
+ id_files: {
50
+ type: Array
51
+ },
52
+ 'id_files.$': {
53
+ type: String
54
+ }
55
+ }),
56
+ function: function (supportTicket, id_files) {
57
+ var _this = this;
58
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
59
+ var files;
60
+ var _this = this;
61
+ return __generator(this, function (_a) {
62
+ switch (_a.label) {
63
+ case 0: return [4 /*yield*/, file_collection_1.Files.find({
64
+ _id: { $in: id_files }
65
+ })];
66
+ case 1:
67
+ files = _a.sent();
68
+ this.callMethodInternal.call(this, 'getSignedUrls', files.map(function (a) { return a.key; }), 900, function (err, res) {
69
+ if (res) {
70
+ _this.readFile('email-templates/support-ticket.html').then(function (html) { return __awaiter(_this, void 0, void 0, function () {
71
+ var template, filesAttached, fileCnt;
72
+ var _this = this;
73
+ return __generator(this, function (_a) {
74
+ template = handlebars.compile(html);
75
+ handlebars.registerHelper('greaterThan', function (a, b) {
76
+ return a > b;
77
+ });
78
+ handlebars.registerHelper('equals', function (a, b) {
79
+ return a === b;
80
+ });
81
+ filesAttached = [];
82
+ fileCnt = 0;
83
+ res.forEach(function (url) {
84
+ filesAttached.push({ filename: files[fileCnt].name, path: url });
85
+ fileCnt++;
86
+ });
87
+ this.readImage('images/ResolveIO.png').then(function (logoData) {
88
+ supportTicket['logo'] = 'data:image/png;base64,' + logoData;
89
+ if (supportTicket.client_user && supportTicket.client_user.email) {
90
+ supportTicket['user'] = supportTicket.client_user.user;
91
+ supportTicket['fileAttached'] = filesAttached;
92
+ _this.sendEmail(supportTicket.client_user.email, 'ResolveIO - ' + supportTicket.support_ticket_number_string + ' Priority: ' + supportTicket.priority, '', template(supportTicket), filesAttached);
93
+ }
94
+ });
95
+ resolve(true);
96
+ return [2 /*return*/];
97
+ });
98
+ }); });
99
+ resolve(true);
100
+ }
101
+ else {
102
+ _this.callMethodInternal.call(_this, 'insertErrorLog', 'supportTicketsmethod - getsignedurl', [
103
+ files.map(function (a) { return a.key; }), err
104
+ ]);
105
+ reject(err);
106
+ }
107
+ });
108
+ return [2 /*return*/];
109
+ }
110
+ });
111
+ }); });
112
+ }
113
+ },
114
+ sendSupportTicketModifiedEmail: {
115
+ check: new simpl_schema_1.default({
116
+ supportTicket: {
117
+ type: Object,
118
+ blackbox: true
119
+ },
120
+ id_files: {
121
+ type: Array
122
+ },
123
+ 'id_files.$': {
124
+ type: String
125
+ }
126
+ }),
127
+ function: function (supportTicket, id_files) {
128
+ var _this = this;
129
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
130
+ var files;
131
+ var _this = this;
132
+ return __generator(this, function (_a) {
133
+ switch (_a.label) {
134
+ case 0: return [4 /*yield*/, file_collection_1.Files.find({
135
+ _id: { $in: id_files }
136
+ })];
137
+ case 1:
138
+ files = _a.sent();
139
+ this.callMethodInternal.call(this, 'getSignedUrls', files.map(function (a) { return a.key; }), 900, function (err, res) {
140
+ if (res) {
141
+ _this.readFile('email-templates/support-ticket-modified.html').then(function (html) { return __awaiter(_this, void 0, void 0, function () {
142
+ var template, filesAttached, fileCnt;
143
+ var _this = this;
144
+ return __generator(this, function (_a) {
145
+ template = handlebars.compile(html);
146
+ handlebars.registerHelper('greaterThan', function (a, b) {
147
+ return a > b;
148
+ });
149
+ handlebars.registerHelper('equals', function (a, b) {
150
+ return a === b;
151
+ });
152
+ filesAttached = [];
153
+ fileCnt = 0;
154
+ res.forEach(function (url) {
155
+ filesAttached.push({ filename: files[fileCnt].name, path: url });
156
+ fileCnt++;
157
+ });
158
+ this.readImage('images/ResolveIO.png').then(function (logoData) {
159
+ supportTicket['logo'] = 'data:image/png;base64,' + logoData;
160
+ supportTicket.current_watchers.forEach(function (user) {
161
+ supportTicket['user'] = user.user;
162
+ supportTicket['fileAttached'] = filesAttached;
163
+ _this.sendEmail(user.email, 'ResolveIO - a ticket has been modified: ' + supportTicket.support_ticket_number_string + ' Priority: ' + supportTicket.priority, '', template(supportTicket), filesAttached);
164
+ });
165
+ if (supportTicket.client_user && supportTicket.client_user.email) {
166
+ supportTicket['user'] = supportTicket.client_user.user;
167
+ supportTicket['fileAttached'] = filesAttached;
168
+ _this.sendEmail(supportTicket.client_user.email, 'ResolveIO - ' + supportTicket.support_ticket_number_string + ' Priority: ' + supportTicket.priority, '', template(supportTicket), filesAttached);
169
+ }
170
+ });
171
+ resolve(true);
172
+ return [2 /*return*/];
173
+ });
174
+ }); });
175
+ resolve(true);
176
+ }
177
+ else {
178
+ _this.callMethodInternal.call(_this, 'insertErrorLog', 'supportTicketsmethod - getsignedurl', [
179
+ files.map(function (a) { return a.key; }), err
180
+ ]);
181
+ reject(err);
182
+ }
183
+ });
184
+ return [2 /*return*/];
185
+ }
186
+ });
187
+ }); });
188
+ }
189
+ },
190
+ sendSupportTicketDeletedEmail: {
191
+ check: new simpl_schema_1.default({
192
+ supportTicket: {
193
+ type: Object,
194
+ blackbox: true
195
+ }
196
+ }),
197
+ function: function (supportTicket) {
198
+ var _this = this;
199
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
200
+ var _this = this;
201
+ return __generator(this, function (_a) {
202
+ this.callMethodInternal.call(this, 'getSignedUrls', [], 900, function (err, res) {
203
+ if (res) {
204
+ _this.readFile('email-templates/support-ticket-deleted.html').then(function (html) { return __awaiter(_this, void 0, void 0, function () {
205
+ var template;
206
+ return __generator(this, function (_a) {
207
+ template = handlebars.compile(html);
208
+ handlebars.registerHelper('greaterThan', function (a, b) {
209
+ return a > b;
210
+ });
211
+ handlebars.registerHelper('equals', function (a, b) {
212
+ return a === b;
213
+ });
214
+ if (supportTicket.client_user && supportTicket.client_user.email) {
215
+ this.sendEmail(supportTicket.client_user.email, 'ResolveIO - ' + supportTicket.support_ticket_number_string + ' Priority: ' + supportTicket.priority, '', template(supportTicket), []);
216
+ }
217
+ return [2 /*return*/];
218
+ });
219
+ }); });
220
+ resolve(true);
221
+ }
222
+ else {
223
+ _this.callMethodInternal.call(_this, 'insertErrorLog', 'supportTicketsmethod - getsignedurl', [
224
+ [], err
225
+ ]);
226
+ reject(err);
227
+ }
228
+ });
229
+ return [2 /*return*/];
230
+ });
231
+ }); });
232
+ }
233
+ }
234
+ });
235
+ }
236
+ exports.loadSupportMethods = loadSupportMethods;
@@ -0,0 +1,14 @@
1
+ export declare function deepCopy(obj: any): any;
2
+ export declare function dateReviver(key: any, value: any): any;
3
+ export declare function getMongoUpdates(currDoc: any, modifiedDoc: any, incVersion: any): any;
4
+ export declare function getMongoMergeUpdatedDoc(f_doc: any, currentDoc: any, oldDoc: any): any;
5
+ export declare function mergeUpdates(...updatesToMerge: any[]): {
6
+ $set: {};
7
+ $unset: {};
8
+ $inc: {};
9
+ $push: {};
10
+ $pull: {};
11
+ };
12
+ export declare function getDeepDiff(doc1: any, doc2: any): any;
13
+ export declare function applyMongoUpdate(uDoc: any, delta: any): any;
14
+ export declare function getBinarySize(string: any): number;
package/util/common.js ADDED
@@ -0,0 +1,341 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var rusDiff = require('rus-diff');
4
+ var deepDiff = require('deep-diff').diff;
5
+ function deepCopy(obj) {
6
+ return JSON.parse(JSON.stringify(obj), dateReviver);
7
+ }
8
+ exports.deepCopy = deepCopy;
9
+ function dateReviver(key, value) {
10
+ if (typeof value === 'string') {
11
+ var a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
12
+ if (a) {
13
+ return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6].split('.')[0], +a[6].split('.')[1]));
14
+ }
15
+ }
16
+ return value;
17
+ }
18
+ exports.dateReviver = dateReviver;
19
+ function getMongoUpdates(currDoc, modifiedDoc, incVersion) {
20
+ if (currDoc.createdAt) {
21
+ modifiedDoc.createdAt = currDoc.createdAt;
22
+ }
23
+ if (currDoc.updatedAt) {
24
+ modifiedDoc.updatedAt = currDoc.updatedAt;
25
+ }
26
+ var updates = rusDiff.diff(currDoc, modifiedDoc);
27
+ if (updates) {
28
+ if (updates.$set && !Object.keys(updates.$set).length) {
29
+ delete updates.$set;
30
+ }
31
+ if (incVersion) {
32
+ updates.$inc = { __v: 1 };
33
+ }
34
+ }
35
+ else {
36
+ if (incVersion) {
37
+ updates = {
38
+ $inc: { __v: 1 }
39
+ };
40
+ }
41
+ }
42
+ return updates;
43
+ }
44
+ exports.getMongoUpdates = getMongoUpdates;
45
+ function getMongoMergeUpdatedDoc(f_doc, currentDoc, oldDoc) {
46
+ var upDoc = deepCopy(oldDoc);
47
+ upDoc.__v = currentDoc.__v;
48
+ var diff_1 = getDeepDiff(oldDoc, currentDoc);
49
+ var diff_2 = getDeepDiff(oldDoc, f_doc);
50
+ var updates = mergeUpdates(diff_2, diff_1);
51
+ applyMongoUpdate(upDoc, updates);
52
+ return upDoc;
53
+ }
54
+ exports.getMongoMergeUpdatedDoc = getMongoMergeUpdatedDoc;
55
+ function mergeUpdates() {
56
+ var updatesToMerge = [];
57
+ for (var _i = 0; _i < arguments.length; _i++) {
58
+ updatesToMerge[_i] = arguments[_i];
59
+ }
60
+ var merged = {
61
+ $set: {},
62
+ $unset: {},
63
+ $inc: {},
64
+ $push: {},
65
+ $pull: {}
66
+ };
67
+ updatesToMerge.filter(function (a) { return a; }).forEach(function (anUpdate) {
68
+ anUpdate.forEach(function (change) {
69
+ var path = change.path[0];
70
+ for (var i = 1; i < change.path.length; i++) {
71
+ path += '.' + change.path[i];
72
+ }
73
+ if (change.kind === 'E') {
74
+ if (typeof (change.rhs) === 'number' && path.endsWith('quantity')) {
75
+ if (merged.$inc[path]) {
76
+ merged.$inc[path] += change.rhs - change.lhs;
77
+ }
78
+ else {
79
+ merged.$inc[path] = change.rhs - change.lhs;
80
+ }
81
+ }
82
+ else {
83
+ merged.$set[path] = change.rhs;
84
+ }
85
+ }
86
+ else if (change.kind === 'A') {
87
+ if (change.item.kind === 'N') {
88
+ if (!merged.$push[path] || JSON.stringify(merged.$push[path]) !== JSON.stringify(change.item.rhs)) {
89
+ if (!merged.$push[path]) {
90
+ merged.$push[path] = [];
91
+ }
92
+ if (!merged.$push[path].filter(function (a) { return JSON.stringify(a) === JSON.stringify(change.item.rhs); }).length) {
93
+ merged.$push[path].push(change.item.rhs);
94
+ }
95
+ }
96
+ }
97
+ else if (change.item.kind === 'D') {
98
+ if (!merged.$pull[path] || JSON.stringify(merged.$pull[path]) !== JSON.stringify(change.item.lhs)) {
99
+ if (!merged.$pull[path]) {
100
+ merged.$pull[path] = [];
101
+ }
102
+ if (!merged.$pull[path].filter(function (a) { return JSON.stringify(a) === JSON.stringify(change.item.lhs); }).length) {
103
+ merged.$pull[path].push(change.item.lhs);
104
+ }
105
+ }
106
+ }
107
+ else if (change.item.kind === 'E') {
108
+ console.log('Condition E not satisfied.');
109
+ // if (!merged.$pull[path] || JSON.stringify(merged.$pull[path]) !== JSON.stringify(change.item.lhs)) {
110
+ // merged.$pull[path] = change.item.lhs;
111
+ // }
112
+ }
113
+ }
114
+ });
115
+ });
116
+ return merged;
117
+ }
118
+ exports.mergeUpdates = mergeUpdates;
119
+ function getDeepDiff(doc1, doc2) {
120
+ delete doc1._id;
121
+ delete doc1.__v;
122
+ if (doc1.updatedAt) {
123
+ delete doc1.updatedAt;
124
+ }
125
+ if (doc1.createdAt) {
126
+ delete doc1.createdAt;
127
+ }
128
+ delete doc2._id;
129
+ delete doc2.__v;
130
+ if (doc2.updatedAt) {
131
+ delete doc2.updatedAt;
132
+ }
133
+ if (doc2.createdAt) {
134
+ delete doc2.createdAt;
135
+ }
136
+ return deepDiff(doc1, doc2);
137
+ }
138
+ exports.getDeepDiff = getDeepDiff;
139
+ function applyMongoUpdate(uDoc, delta) {
140
+ var orig, k, pathk, vk, n, n1, n2, name, o, o1, o2, ref, ref1, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9a, ref9b, ref10, ref11, ref12a, ref12b, ref13, v;
141
+ orig = deepCopy(uDoc);
142
+ if (delta != null) {
143
+ if (delta.$rename != null) {
144
+ ref = delta.$rename;
145
+ // tslint:disable-next-line:forin
146
+ for (k in ref) {
147
+ v = ref[k];
148
+ ref1 = resolve(uDoc, k), o1 = ref1[0], n1 = ref1[1];
149
+ ref2 = resolve(uDoc, v), o2 = ref2[0], n2 = ref2[1];
150
+ if ((o1 != null) && n1.length === 1) {
151
+ if ((o2 != null) && n2.length === 1) {
152
+ o2[n2[0]] = o1[n1[0]];
153
+ delete o1[n1[0]];
154
+ }
155
+ else {
156
+ // throw new Error(o2 + '/' + n2 + ' - couldn\'t resolve first for ' + uDoc + ' ' + v);
157
+ console.log('Error', o2 + '/' + n2 + ' - couldn\'t resolve first for ' + uDoc + ' ' + v);
158
+ }
159
+ }
160
+ else {
161
+ // throw new Error(o1 + '/' + n1 + ' - couldn\'t resolve second for ' + uDoc + ' ' + k);
162
+ console.log('Error', o1 + '/' + n1 + ' - couldn\'t resolve second for ' + uDoc + ' ' + k);
163
+ }
164
+ }
165
+ }
166
+ if (delta.$set != null) {
167
+ ref3 = delta.$set;
168
+ // tslint:disable-next-line:forin
169
+ for (k in ref3) {
170
+ v = ref3[k];
171
+ ref4 = resolve(uDoc, k, {
172
+ force: true
173
+ }), o = ref4[0], n = ref4[1];
174
+ if ((o != null) && n.length === 1) {
175
+ o[n[0]] = v;
176
+ }
177
+ else {
178
+ // throw new Error(o + '/' + n + ' - couldn\'t set for ' + uDoc + ' ' + k);
179
+ console.log('Error', o + '/' + n + ' - couldn\'t set for ' + uDoc + ' ' + k);
180
+ }
181
+ }
182
+ }
183
+ if (delta.$inc != null) {
184
+ ref5 = delta.$inc;
185
+ // tslint:disable-next-line:forin
186
+ for (k in ref5) {
187
+ v = ref5[k];
188
+ ref6 = resolve(uDoc, k, {
189
+ force: true
190
+ }), o = ref6[0], n = ref6[1];
191
+ if ((o != null) && n.length === 1) {
192
+ if (o[name = n[0]] == null) {
193
+ o[name] = 0;
194
+ }
195
+ o[n[0]] += v;
196
+ }
197
+ else {
198
+ // throw new Error(o + '/' + n + ' - couldn\'t set for ' + uDoc + ' ' + k);
199
+ console.log('Error', o + '/' + n + ' - couldn\'t set for ' + uDoc + ' ' + k);
200
+ }
201
+ }
202
+ }
203
+ if (delta.$unset != null) {
204
+ ref7 = delta.$unset;
205
+ // tslint:disable-next-line:forin
206
+ for (k in ref7) {
207
+ v = ref7[k];
208
+ ref8 = resolve(uDoc, k), o = ref8[0], n = ref8[1];
209
+ if ((o != null) && n.length === 1) {
210
+ delete o[n[0]];
211
+ }
212
+ else {
213
+ // throw new Error(o + '/' + n + ' - couldn\'t unset for ' + uDoc + ' ' + k);
214
+ console.log('Error', o + '/' + n + ' - couldn\'t unset for ' + uDoc + ' ' + k);
215
+ }
216
+ }
217
+ }
218
+ if (delta.$pull != null) {
219
+ ref9a = delta.$pull;
220
+ // tslint:disable-next-line:forin
221
+ for (pathk in ref9a) { // should only loop 1 time
222
+ ref9b = ref9a[pathk];
223
+ for (var kk = (ref9b.length - 1); kk >= 0; kk--) {
224
+ vk = ref9b[kk];
225
+ ref10 = resolve(uDoc, pathk), o = ref10[0], n = ref10[1];
226
+ ref11 = resolve(orig, pathk), o2 = ref11[0], n2 = ref11[1];
227
+ if ((o != null) && n.length === 1) {
228
+ o[n[0]].splice(o2[n2[0]].findIndex(function (z) { return JSON.stringify(z) === JSON.stringify(vk); }), 1);
229
+ }
230
+ else {
231
+ // throw new Error(o + '/' + n + ' - couldn\'t unset for ' + uDoc + ' ' + pathk);
232
+ console.log('Error', o + '/' + n + ' - couldn\'t unset for ' + uDoc + ' ' + pathk);
233
+ }
234
+ }
235
+ }
236
+ }
237
+ if (delta.$push != null) {
238
+ ref12a = delta.$push;
239
+ // tslint:disable-next-line:forin
240
+ for (pathk in ref12a) { // should only loop 1 time
241
+ ref12b = ref12a[pathk];
242
+ // for (let kk = 0; kk < ref12b.length; kk++) {
243
+ for (var kk = (ref12b.length - 1); kk >= 0; kk--) {
244
+ vk = ref12b[kk];
245
+ ref13 = resolve(uDoc, pathk), o = ref13[0], n = ref13[1];
246
+ if ((o != null) && n.length === 1) {
247
+ o[n[0]].push(vk);
248
+ }
249
+ else {
250
+ // throw new Error(o + '/' + n + ' - couldn\'t unset for ' + uDoc + ' ' + pathk);
251
+ console.log('Error', o + '/' + n + ' - couldn\'t unset for ' + uDoc + ' ' + pathk);
252
+ }
253
+ }
254
+ }
255
+ }
256
+ }
257
+ return uDoc;
258
+ }
259
+ exports.applyMongoUpdate = applyMongoUpdate;
260
+ function resolve(a, path, options) {
261
+ var e, k, last, stack;
262
+ if (options == null) {
263
+ options = {};
264
+ }
265
+ stack = arrize(path);
266
+ last = [];
267
+ if (stack.length > 0) {
268
+ last.unshift(stack.pop());
269
+ }
270
+ e = a;
271
+ if (e !== null) {
272
+ while ((k = stack.shift()) !== void 0) {
273
+ if (e[k] !== void 0) {
274
+ e = e[k];
275
+ }
276
+ else {
277
+ stack.unshift(k);
278
+ break;
279
+ }
280
+ }
281
+ }
282
+ if (options.force) {
283
+ while ((k = stack.shift()) !== void 0) {
284
+ if ((typeof stack[0] === 'number') || ((stack.length === 0) && (typeof last[0] === 'number'))) {
285
+ e[k] = [];
286
+ }
287
+ else {
288
+ e[k] = {};
289
+ }
290
+ e = e[k];
291
+ }
292
+ }
293
+ else {
294
+ while ((k = stack.pop()) !== void 0) {
295
+ last.unshift(k);
296
+ }
297
+ }
298
+ return [e, last];
299
+ }
300
+ function arrize(path, glue) {
301
+ if (glue == null) {
302
+ glue = '.';
303
+ }
304
+ return ((function () {
305
+ if (Array.isArray(path)) {
306
+ return path.slice(0);
307
+ }
308
+ else {
309
+ switch (path) {
310
+ case void 0:
311
+ case null:
312
+ case false:
313
+ case '':
314
+ return [];
315
+ default:
316
+ return path.toString().split(glue);
317
+ }
318
+ }
319
+ })()).map(function (e) {
320
+ switch (e) {
321
+ case void 0:
322
+ case null:
323
+ case false:
324
+ case '':
325
+ return null;
326
+ default:
327
+ return e.toString();
328
+ }
329
+ }).filter(function (e) {
330
+ return e != null;
331
+ });
332
+ }
333
+ function getBinarySize(string) {
334
+ if (string) {
335
+ return Buffer.byteLength(string, 'utf8');
336
+ }
337
+ else {
338
+ return 0;
339
+ }
340
+ }
341
+ exports.getBinarySize = getBinarySize;