@resolveio/server-lib 1.0.0-rc2 → 1.0.0-rc21

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 (159) hide show
  1. package/collections/active-subscription.collection.d.ts +8 -0
  2. package/collections/active-subscription.collection.js +68 -1
  3. package/collections/app-status.collection.d.ts +8 -0
  4. package/collections/app-status.collection.js +43 -1
  5. package/collections/app-version.collection.d.ts +8 -0
  6. package/collections/app-version.collection.js +56 -1
  7. package/collections/counter.collection.d.ts +10 -0
  8. package/collections/counter.collection.js +73 -1
  9. package/collections/cron-job-history.collection.d.ts +8 -0
  10. package/collections/cron-job-history.collection.js +86 -1
  11. package/collections/cron-job.collection.d.ts +8 -0
  12. package/collections/cron-job.collection.js +89 -1
  13. package/collections/file.collection.d.ts +10 -0
  14. package/collections/file.collection.js +73 -1
  15. package/collections/flag.collection.d.ts +8 -0
  16. package/collections/flag.collection.js +47 -1
  17. package/collections/log.collection.d.ts +8 -0
  18. package/collections/log.collection.js +64 -1
  19. package/collections/logged-in-users.collection.d.ts +8 -0
  20. package/collections/logged-in-users.collection.js +70 -1
  21. package/collections/method-call.collection.d.ts +7 -0
  22. package/collections/method-call.collection.js +44 -1
  23. package/collections/method-response.collection.d.ts +7 -0
  24. package/collections/method-response.collection.js +48 -1
  25. package/collections/queue-flag.collection.d.ts +8 -0
  26. package/collections/queue-flag.collection.js +41 -1
  27. package/collections/queue-method.collection.d.ts +8 -0
  28. package/collections/queue-method.collection.js +116 -1
  29. package/collections/queue-response.collection.d.ts +8 -0
  30. package/collections/queue-response.collection.js +78 -1
  31. package/collections/support-ticket.collection.d.ts +8 -0
  32. package/collections/support-ticket.collection.js +146 -1
  33. package/collections/user.collection.d.ts +9 -0
  34. package/collections/user.collection.js +108 -1
  35. package/cron/cron.d.ts +6 -0
  36. package/cron/cron.js +160 -1
  37. package/fixtures/cron-jobs.d.ts +2 -0
  38. package/fixtures/cron-jobs.js +72 -1
  39. package/fixtures/init.d.ts +2 -0
  40. package/fixtures/init.js +73 -1
  41. package/http/auth.d.ts +3 -0
  42. package/http/auth.js +472 -1
  43. package/http/health.d.ts +2 -0
  44. package/http/health.js +8 -1
  45. package/index.d.ts +15 -0
  46. package/index.js +123 -1
  47. package/managers/cron.manager.d.ts +17 -0
  48. package/managers/cron.manager.js +193 -1
  49. package/managers/method.manager.d.ts +27 -0
  50. package/managers/method.manager.js +513 -1
  51. package/managers/queue-method.manager.d.ts +14 -0
  52. package/managers/queue-method.manager.js +239 -1
  53. package/managers/subscription.manager.d.ts +27 -0
  54. package/managers/subscription.manager.js +700 -1
  55. package/managers/support.manager.d.ts +18 -0
  56. package/managers/support.manager.js +208 -1
  57. package/methods/accounts.d.ts +3 -0
  58. package/methods/accounts.js +253 -1
  59. package/methods/aws.d.ts +3 -0
  60. package/methods/aws.js +323 -1
  61. package/methods/cloudconvert.d.ts +3 -0
  62. package/methods/cloudconvert.js +37 -1
  63. package/methods/collections.d.ts +3 -0
  64. package/methods/collections.js +738 -1
  65. package/methods/counters.d.ts +3 -0
  66. package/methods/counters.js +404 -1
  67. package/methods/cron-jobs.d.ts +3 -0
  68. package/methods/cron-jobs.js +63 -1
  69. package/methods/flags.d.ts +3 -0
  70. package/methods/flags.js +6 -1
  71. package/methods/logs.d.ts +3 -0
  72. package/methods/logs.js +29 -1
  73. package/methods/pdf.d.ts +3 -0
  74. package/methods/pdf.js +303 -1
  75. package/models/active-subscription.model.d.ts +14 -0
  76. package/models/active-subscription.model.js +2 -1
  77. package/models/app-status.model.d.ts +5 -0
  78. package/models/app-status.model.js +2 -1
  79. package/models/app-version.model.d.ts +5 -0
  80. package/models/app-version.model.js +2 -1
  81. package/models/collection-document.model.d.ts +7 -0
  82. package/models/collection-document.model.js +2 -1
  83. package/models/counter.model.d.ts +6 -0
  84. package/models/counter.model.js +2 -1
  85. package/models/cron-job-history.model.d.ts +13 -0
  86. package/models/cron-job-history.model.js +2 -1
  87. package/models/cron-job.model.d.ts +14 -0
  88. package/models/cron-job.model.js +2 -1
  89. package/models/dialog.model.d.ts +24 -0
  90. package/models/dialog.model.js +2 -1
  91. package/models/file.model.d.ts +10 -0
  92. package/models/file.model.js +2 -1
  93. package/models/flag.model.d.ts +6 -0
  94. package/models/flag.model.js +2 -1
  95. package/models/log.model.d.ts +12 -0
  96. package/models/log.model.js +2 -1
  97. package/models/logged-in-users.model.d.ts +10 -0
  98. package/models/logged-in-users.model.js +2 -1
  99. package/models/method-call.model.d.ts +8 -0
  100. package/models/method-call.model.js +2 -1
  101. package/models/method-response.model.d.ts +9 -0
  102. package/models/method-response.model.js +2 -1
  103. package/models/method.model.d.ts +11 -0
  104. package/models/method.model.js +2 -1
  105. package/models/notification.model.d.ts +5 -0
  106. package/models/notification.model.js +2 -0
  107. package/models/pagination.model.d.ts +12 -0
  108. package/models/pagination.model.js +20 -1
  109. package/models/permission.model.d.ts +13 -0
  110. package/models/permission.model.js +2 -1
  111. package/models/queue-method.model.d.ts +10 -0
  112. package/models/queue-method.model.js +2 -1
  113. package/models/queue-response.model.d.ts +11 -0
  114. package/models/queue-response.model.js +2 -1
  115. package/models/select-data-label.model.d.ts +10 -0
  116. package/models/select-data-label.model.js +2 -1
  117. package/models/server-response.model.d.ts +6 -0
  118. package/models/server-response.model.js +2 -1
  119. package/models/subscription.model.d.ts +31 -0
  120. package/models/subscription.model.js +2 -1
  121. package/models/support-method.model.d.ts +10 -0
  122. package/models/support-method.model.js +2 -1
  123. package/models/support-ticket.model.d.ts +40 -0
  124. package/models/support-ticket.model.js +2 -1
  125. package/models/user.model.d.ts +15 -0
  126. package/models/user.model.js +2 -1
  127. package/package.json +5 -2
  128. package/publications/app-status.d.ts +3 -0
  129. package/publications/app-status.js +14 -1
  130. package/publications/app-version.d.ts +3 -0
  131. package/publications/app-version.js +14 -1
  132. package/publications/cron-jobs.d.ts +3 -0
  133. package/publications/cron-jobs.js +14 -1
  134. package/publications/files.d.ts +3 -0
  135. package/publications/files.js +71 -1
  136. package/publications/flags.d.ts +3 -0
  137. package/publications/flags.js +23 -1
  138. package/publications/logs.d.ts +3 -0
  139. package/publications/logs.js +26 -1
  140. package/publications/method-calls.d.ts +3 -0
  141. package/publications/method-calls.js +14 -1
  142. package/publications/method-responses.d.ts +3 -0
  143. package/publications/method-responses.js +14 -1
  144. package/publications/super-admin.d.ts +3 -0
  145. package/publications/super-admin.js +21 -1
  146. package/publications/support-tickets.d.ts +2 -0
  147. package/publications/support-tickets.js +118 -1
  148. package/server-app.d.ts +33 -0
  149. package/server-app.js +640 -1
  150. package/support-methods/aws.d.ts +3 -0
  151. package/support-methods/aws.js +276 -1
  152. package/support-methods/collections.d.ts +3 -0
  153. package/support-methods/collections.js +471 -1
  154. package/support-methods/counters.d.ts +3 -0
  155. package/support-methods/counters.js +189 -1
  156. package/support-methods/support.d.ts +3 -0
  157. package/support-methods/support.js +19 -1
  158. package/util/common.d.ts +15 -0
  159. package/util/common.js +334 -1
@@ -1 +1,189 @@
1
- "use strict";var __awaiter=this&&this.__awaiter||function(c,u,i,a){return new(i||(i=Promise))(function(e,t){function n(e){try{r(a.next(e))}catch(e){t(e)}}function o(e){try{r(a.throw(e))}catch(e){t(e)}}function r(t){t.done?e(t.value):new i(function(e){e(t.value)}).then(n,o)}r((a=a.apply(c,u||[])).next())})},__generator=this&&this.__generator||function(n,o){var r,c,u,e,i={label:0,sent:function(){if(1&u[0])throw u[1];return u[1]},trys:[],ops:[]};return e={next:t(0),throw:t(1),return:t(2)},"function"==typeof Symbol&&(e[Symbol.iterator]=function(){return this}),e;function t(t){return function(e){return function(t){if(r)throw new TypeError("Generator is already executing.");for(;i;)try{if(r=1,c&&(u=2&t[0]?c.return:t[0]?c.throw||((u=c.return)&&u.call(c),0):c.next)&&!(u=u.call(c,t[1])).done)return u;switch(c=0,u&&(t=[2&t[0],u.value]),t[0]){case 0:case 1:u=t;break;case 4:return i.label++,{value:t[1],done:!1};case 5:i.label++,c=t[1],t=[0];continue;case 7:t=i.ops.pop(),i.trys.pop();continue;default:if(!(u=0<(u=i.trys).length&&u[u.length-1])&&(6===t[0]||2===t[0])){i=0;continue}if(3===t[0]&&(!u||t[1]>u[0]&&t[1]<u[3])){i.label=t[1];break}if(6===t[0]&&i.label<u[1]){i.label=u[1],u=t;break}if(u&&i.label<u[2]){i.label=u[2],i.ops.push(t);break}u[2]&&i.ops.pop(),i.trys.pop();continue}t=o.call(n,i)}catch(e){t=[6,e],c=0}finally{r=u=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}([t,e])}}};Object.defineProperty(exports,"__esModule",{value:!0});var mongoose_1=require("mongoose"),counter_collection_1=require("../collections/counter.collection"),simpl_schema_1=require("simpl-schema"),__1=require("..");function loadCounterMethods(e){e.supportMethods({incCounter:{check:new simpl_schema_1.default({counterType:{type:String}}),function:function(o){var t=this;return new Promise(function(n,e){return __awaiter(t,void 0,void 0,function(){var t;return __generator(this,function(e){switch(e.label){case 0:return[4,counter_collection_1.SupportCounters.findOne({type:o})];case 1:return e.sent()?[3,2]:(counter_collection_1.SupportCounters.create({_id:mongoose_1.Types.ObjectId().toHexString(),count:2,type:o}),n(1),[3,4]);case 2:return[4,counter_collection_1.SupportCounters.findOneAndUpdate({type:o},{$inc:{count:1}}).exec()];case 3:t=e.sent(),n(t.count),e.label=4;case 4:return[2]}})})})}},countCollectionWithQuery:{check:new simpl_schema_1.default({collectionType:{type:String},query:{type:Object,blackbox:!0}}),function:function(e,t){return __1.default.getSupportDB().model(e).collection.countDocuments(t)}},countQuery:{check:new simpl_schema_1.default({collectionType:{type:String},search:{type:String,optional:!0},filters:simpl_schema_1.default.oneOf({type:Array,optional:!0},{type:Object,blackbox:!0,optional:!0}),"filters.$":{type:Object,optional:!0,blackbox:!0},subType:{type:String,optional:!0}}),function:function(e,t,n){void 0===t&&(t=""),void 0===n&&(n=[]);var o={};if(t||n&&n.length){if(n&&n.length&&(o.$and=[],n.forEach(function(e){var t;o.$and.push(((t={})[e.prop]=e.data,t))})),t&&"support-tickets"===e){var r={$regex:".*"+(t||"")+".*",$options:"i"};o.$and?o.$and.push({$or:[{support_ticket_number_string:r},{date_created_string:r},{status:r},{user_created:r}]}):o={$or:[{support_ticket_number_string:r},{date_created_string:r},{status:r},{user_created:r}]}}}else o={};return __1.default.getSupportDB().model(e).collection.countDocuments(o)}},countCollection:{check:new simpl_schema_1.default({collectionType:{type:String}}),function:function(e){return __1.default.getSupportDB().model(e).collection.countDocuments()}}})}exports.loadCounterMethods=loadCounterMethods;
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 mongoose_1 = require("mongoose");
39
+ var counter_collection_1 = require("../collections/counter.collection");
40
+ var simpl_schema_1 = require("simpl-schema");
41
+ var __1 = require("..");
42
+ function loadCounterMethods(supportManager) {
43
+ supportManager.supportMethods({
44
+ incCounter: {
45
+ check: new simpl_schema_1.default({
46
+ counterType: {
47
+ type: String
48
+ }
49
+ }),
50
+ function: function (counterType) {
51
+ var _this = this;
52
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
53
+ var counter, newCount;
54
+ return __generator(this, function (_a) {
55
+ switch (_a.label) {
56
+ case 0: return [4 /*yield*/, counter_collection_1.SupportCounters.findOne({ type: counterType })];
57
+ case 1:
58
+ counter = _a.sent();
59
+ if (!!counter) return [3 /*break*/, 2];
60
+ counter_collection_1.SupportCounters.create({
61
+ _id: mongoose_1.Types.ObjectId().toHexString(),
62
+ count: 2,
63
+ type: counterType
64
+ });
65
+ resolve(1);
66
+ return [3 /*break*/, 4];
67
+ case 2: return [4 /*yield*/, counter_collection_1.SupportCounters.findOneAndUpdate({ type: counterType }, { $inc: { count: 1 } }).exec()];
68
+ case 3:
69
+ newCount = _a.sent();
70
+ resolve(newCount.count);
71
+ _a.label = 4;
72
+ case 4: return [2 /*return*/];
73
+ }
74
+ });
75
+ }); });
76
+ }
77
+ },
78
+ countCollectionWithQuery: {
79
+ check: new simpl_schema_1.default({
80
+ collectionType: {
81
+ type: String
82
+ },
83
+ query: {
84
+ type: Object,
85
+ blackbox: true
86
+ }
87
+ }),
88
+ function: function (collectionType, query) {
89
+ return __1.ResolveIOServer.getSupportDB().model(collectionType).collection.countDocuments(query);
90
+ }
91
+ },
92
+ countQuery: {
93
+ check: new simpl_schema_1.default({
94
+ collectionType: {
95
+ type: String
96
+ },
97
+ search: {
98
+ type: String,
99
+ optional: true
100
+ },
101
+ filters: simpl_schema_1.default.oneOf({
102
+ type: Array,
103
+ optional: true
104
+ }, {
105
+ type: Object,
106
+ blackbox: true,
107
+ optional: true
108
+ }),
109
+ 'filters.$': {
110
+ type: Object,
111
+ optional: true,
112
+ blackbox: true
113
+ },
114
+ subType: {
115
+ type: String,
116
+ optional: true
117
+ }
118
+ }),
119
+ function: function (collectionType, search, filters) {
120
+ if (search === void 0) { search = ''; }
121
+ if (filters === void 0) { filters = []; }
122
+ var query = {};
123
+ if (!search && (!filters || !filters.length)) {
124
+ query = {};
125
+ }
126
+ else {
127
+ if (filters && filters.length) {
128
+ query['$and'] = [];
129
+ filters.forEach(function (filter) {
130
+ var _a;
131
+ query['$and'].push((_a = {}, _a[filter['prop']] = filter['data'], _a));
132
+ });
133
+ }
134
+ if (search && collectionType === 'support-tickets') {
135
+ var searchRegEx = { '$regex': '.*' + (search || '') + '.*', '$options': 'i' };
136
+ if (query['$and']) {
137
+ query['$and'].push({
138
+ $or: [
139
+ {
140
+ support_ticket_number_string: searchRegEx
141
+ },
142
+ {
143
+ date_created_string: searchRegEx
144
+ },
145
+ {
146
+ status: searchRegEx
147
+ },
148
+ {
149
+ user_created: searchRegEx
150
+ }
151
+ ]
152
+ });
153
+ }
154
+ else {
155
+ query = {
156
+ $or: [
157
+ {
158
+ support_ticket_number_string: searchRegEx
159
+ },
160
+ {
161
+ date_created_string: searchRegEx
162
+ },
163
+ {
164
+ status: searchRegEx
165
+ },
166
+ {
167
+ user_created: searchRegEx
168
+ }
169
+ ]
170
+ };
171
+ }
172
+ }
173
+ }
174
+ return __1.ResolveIOServer.getSupportDB().model(collectionType).collection.countDocuments(query);
175
+ }
176
+ },
177
+ countCollection: {
178
+ check: new simpl_schema_1.default({
179
+ collectionType: {
180
+ type: String
181
+ }
182
+ }),
183
+ function: function (collectionType) {
184
+ return __1.ResolveIOServer.getSupportDB().model(collectionType).collection.countDocuments();
185
+ }
186
+ }
187
+ });
188
+ }
189
+ exports.loadCounterMethods = loadCounterMethods;
@@ -0,0 +1,3 @@
1
+ import { SupportManager } from '../managers/support.manager';
2
+ export declare function loadSupportMethods(supportManager: SupportManager): void;
3
+ //# sourceMappingURL=support.d.ts.map
@@ -1 +1,19 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var simpl_schema_1=require("simpl-schema"),support_ticket_collection_1=require("../collections/support-ticket.collection");function loadSupportMethods(t){t.supportMethods({testSupportMethods:{check:new simpl_schema_1.default({test:{type:String}}),function:function(t){return support_ticket_collection_1.SupportTickets.findOne({}).exec()}}})}exports.loadSupportMethods=loadSupportMethods;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var simpl_schema_1 = require("simpl-schema");
4
+ var support_ticket_collection_1 = require("../collections/support-ticket.collection");
5
+ function loadSupportMethods(supportManager) {
6
+ supportManager.supportMethods({
7
+ testSupportMethods: {
8
+ check: new simpl_schema_1.default({
9
+ test: {
10
+ type: String
11
+ }
12
+ }),
13
+ function: function (test) {
14
+ return support_ticket_collection_1.SupportTickets.findOne({}).exec();
15
+ }
16
+ },
17
+ });
18
+ }
19
+ exports.loadSupportMethods = loadSupportMethods;
@@ -0,0 +1,15 @@
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;
15
+ //# sourceMappingURL=common.d.ts.map
package/util/common.js CHANGED
@@ -1 +1,334 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var rusDiff=require("rus-diff"),deepDiff=require("deep-diff").diff;function deepCopy(e){return JSON.parse(JSON.stringify(e),dateReviver)}function dateReviver(e,t){if("string"==typeof t){var r=/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(t);if(r)return new Date(Date.UTC(+r[1],+r[2]-1,+r[3],+r[4],+r[5],+r[6].split(".")[0],+r[6].split(".")[1]))}return t}function getMongoUpdates(e,t,r){e.createdAt&&(t.createdAt=e.createdAt),e.updatedAt&&(t.updatedAt=e.updatedAt);var n=rusDiff.diff(e,t);return n?(n.$set&&!Object.keys(n.$set).length&&delete n.$set,r&&(n.$inc={__v:1})):r&&(n={$inc:{__v:1}}),n}function getMongoMergeUpdatedDoc(e,t,r){var n=deepCopy(r);n.__v=t.__v;var i=getDeepDiff(r,t);return applyMongoUpdate(n,mergeUpdates(getDeepDiff(r,e),i)),n}function mergeUpdates(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var n={$set:{},$unset:{},$inc:{},$push:{},$pull:{}};return e.filter(function(e){return e}).forEach(function(e){e.forEach(function(t){for(var e=t.path[0],r=1;r<t.path.length;r++)e+="."+t.path[r];"E"===t.kind?"number"==typeof t.rhs&&e.endsWith("quantity")?n.$inc[e]?n.$inc[e]+=t.rhs-t.lhs:n.$inc[e]=t.rhs-t.lhs:n.$set[e]=t.rhs:"A"===t.kind&&("N"===t.item.kind?n.$push[e]&&JSON.stringify(n.$push[e])===JSON.stringify(t.item.rhs)||(n.$push[e]||(n.$push[e]=[]),n.$push[e].filter(function(e){return JSON.stringify(e)===JSON.stringify(t.item.rhs)}).length||n.$push[e].push(t.item.rhs)):"D"===t.item.kind?n.$pull[e]&&JSON.stringify(n.$pull[e])===JSON.stringify(t.item.lhs)||(n.$pull[e]||(n.$pull[e]=[]),n.$pull[e].filter(function(e){return JSON.stringify(e)===JSON.stringify(t.item.lhs)}).length||n.$pull[e].push(t.item.lhs)):"E"===t.item.kind&&console.log("Condition E not satisfied."))})}),n}function getDeepDiff(e,t){return delete e._id,delete e.__v,e.updatedAt&&delete e.updatedAt,e.createdAt&&delete e.createdAt,delete t._id,delete t.__v,t.updatedAt&&delete t.updatedAt,t.createdAt&&delete t.createdAt,deepDiff(e,t)}function applyMongoUpdate(e,t){var r,n,i,o,l,f,u,s,d,p,a,c,g,h,v,$,y,D,m,w,A,S,_,U,M,O,N,E;if(r=deepCopy(e),null!=t){if(null!=t.$rename)for(n in c=t.$rename){if(E=c[n],p=(g=resolve(e,n))[0],f=g[1],a=(h=resolve(e,E))[0],u=h[1],null==p||1!==f.length)throw new Error(p+"/"+f+" - couldn't resolve second for "+e+" "+n);if(null==a||1!==u.length)throw new Error(a+"/"+u+" - couldn't resolve first for "+e+" "+E);a[u[0]]=p[f[0]],delete p[f[0]]}if(null!=t.$set)for(n in v=t.$set){if(E=v[n],d=($=resolve(e,n,{force:!0}))[0],l=$[1],null==d||1!==l.length)throw new Error(d+"/"+l+" - couldn't set for "+e+" "+n);d[l[0]]=E}if(null!=t.$inc)for(n in y=t.$inc){if(E=y[n],d=(D=resolve(e,n,{force:!0}))[0],l=D[1],null==d||1!==l.length)throw new Error(d+"/"+l+" - couldn't set for "+e+" "+n);null==d[s=l[0]]&&(d[s]=0),d[l[0]]+=E}if(null!=t.$unset)for(n in m=t.$unset){if(E=m[n],d=(w=resolve(e,n))[0],l=w[1],null==d||1!==l.length)throw new Error(d+"/"+l+" - couldn't unset for "+e+" "+n);delete d[l[0]]}if(null!=t.$pull)for(i in A=t.$pull)for(var J=(S=A[i]).length-1;0<=J;J--){if(o=S[J],d=(_=resolve(e,i))[0],l=_[1],a=(U=resolve(r,i))[0],u=U[1],null==d||1!==l.length)throw new Error(d+"/"+l+" - couldn't unset for "+e+" "+i);d[l[0]].splice(a[u[0]].findIndex(function(e){return JSON.stringify(e)===JSON.stringify(o)}),1)}if(null!=t.$push)for(i in M=t.$push)for(J=(O=M[i]).length-1;0<=J;J--){if(o=O[J],d=(N=resolve(e,i))[0],l=N[1],null==d||1!==l.length)throw new Error(d+"/"+l+" - couldn't unset for "+e+" "+i);d[l[0]].push(o)}}return e}function resolve(e,t,r){var n,i,o,l;if(null==r&&(r={}),o=[],0<(l=arrize(t)).length&&o.unshift(l.pop()),null!==(n=e))for(;void 0!==(i=l.shift());){if(void 0===n[i]){l.unshift(i);break}n=n[i]}if(r.force)for(;void 0!==(i=l.shift());)"number"==typeof l[0]||0===l.length&&"number"==typeof o[0]?n[i]=[]:n[i]={},n=n[i];else for(;void 0!==(i=l.pop());)o.unshift(i);return[n,o]}function arrize(e,t){return null==t&&(t="."),function(){if(Array.isArray(e))return e.slice(0);switch(e){case void 0:case null:case!1:case"":return[];default:return e.toString().split(t)}}().map(function(e){switch(e){case void 0:case null:case!1:case"":return null;default:return e.toString()}}).filter(function(e){return null!=e})}function getBinarySize(e){return e?Buffer.byteLength(e,"utf8"):0}exports.deepCopy=deepCopy,exports.dateReviver=dateReviver,exports.getMongoUpdates=getMongoUpdates,exports.getMongoMergeUpdatedDoc=getMongoMergeUpdatedDoc,exports.mergeUpdates=mergeUpdates,exports.getDeepDiff=getDeepDiff,exports.applyMongoUpdate=applyMongoUpdate,exports.getBinarySize=getBinarySize;
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
+ }
158
+ }
159
+ else {
160
+ throw new Error(o1 + '/' + n1 + ' - couldn\'t resolve second for ' + uDoc + ' ' + k);
161
+ }
162
+ }
163
+ }
164
+ if (delta.$set != null) {
165
+ ref3 = delta.$set;
166
+ // tslint:disable-next-line:forin
167
+ for (k in ref3) {
168
+ v = ref3[k];
169
+ ref4 = resolve(uDoc, k, {
170
+ force: true
171
+ }), o = ref4[0], n = ref4[1];
172
+ if ((o != null) && n.length === 1) {
173
+ o[n[0]] = v;
174
+ }
175
+ else {
176
+ throw new Error(o + '/' + n + ' - couldn\'t set for ' + uDoc + ' ' + k);
177
+ }
178
+ }
179
+ }
180
+ if (delta.$inc != null) {
181
+ ref5 = delta.$inc;
182
+ // tslint:disable-next-line:forin
183
+ for (k in ref5) {
184
+ v = ref5[k];
185
+ ref6 = resolve(uDoc, k, {
186
+ force: true
187
+ }), o = ref6[0], n = ref6[1];
188
+ if ((o != null) && n.length === 1) {
189
+ if (o[name = n[0]] == null) {
190
+ o[name] = 0;
191
+ }
192
+ o[n[0]] += v;
193
+ }
194
+ else {
195
+ throw new Error(o + '/' + n + ' - couldn\'t set for ' + uDoc + ' ' + k);
196
+ }
197
+ }
198
+ }
199
+ if (delta.$unset != null) {
200
+ ref7 = delta.$unset;
201
+ // tslint:disable-next-line:forin
202
+ for (k in ref7) {
203
+ v = ref7[k];
204
+ ref8 = resolve(uDoc, k), o = ref8[0], n = ref8[1];
205
+ if ((o != null) && n.length === 1) {
206
+ delete o[n[0]];
207
+ }
208
+ else {
209
+ throw new Error(o + '/' + n + ' - couldn\'t unset for ' + uDoc + ' ' + k);
210
+ }
211
+ }
212
+ }
213
+ if (delta.$pull != null) {
214
+ ref9a = delta.$pull;
215
+ // tslint:disable-next-line:forin
216
+ for (pathk in ref9a) { // should only loop 1 time
217
+ ref9b = ref9a[pathk];
218
+ for (var kk = (ref9b.length - 1); kk >= 0; kk--) {
219
+ vk = ref9b[kk];
220
+ ref10 = resolve(uDoc, pathk), o = ref10[0], n = ref10[1];
221
+ ref11 = resolve(orig, pathk), o2 = ref11[0], n2 = ref11[1];
222
+ if ((o != null) && n.length === 1) {
223
+ o[n[0]].splice(o2[n2[0]].findIndex(function (z) { return JSON.stringify(z) === JSON.stringify(vk); }), 1);
224
+ }
225
+ else {
226
+ throw new Error(o + '/' + n + ' - couldn\'t unset for ' + uDoc + ' ' + pathk);
227
+ }
228
+ }
229
+ }
230
+ }
231
+ if (delta.$push != null) {
232
+ ref12a = delta.$push;
233
+ // tslint:disable-next-line:forin
234
+ for (pathk in ref12a) { // should only loop 1 time
235
+ ref12b = ref12a[pathk];
236
+ // for (let kk = 0; kk < ref12b.length; kk++) {
237
+ for (var kk = (ref12b.length - 1); kk >= 0; kk--) {
238
+ vk = ref12b[kk];
239
+ ref13 = resolve(uDoc, pathk), o = ref13[0], n = ref13[1];
240
+ if ((o != null) && n.length === 1) {
241
+ o[n[0]].push(vk);
242
+ }
243
+ else {
244
+ throw new Error(o + '/' + n + ' - couldn\'t unset for ' + uDoc + ' ' + pathk);
245
+ }
246
+ }
247
+ }
248
+ }
249
+ }
250
+ return uDoc;
251
+ }
252
+ exports.applyMongoUpdate = applyMongoUpdate;
253
+ function resolve(a, path, options) {
254
+ var e, k, last, stack;
255
+ if (options == null) {
256
+ options = {};
257
+ }
258
+ stack = arrize(path);
259
+ last = [];
260
+ if (stack.length > 0) {
261
+ last.unshift(stack.pop());
262
+ }
263
+ e = a;
264
+ if (e !== null) {
265
+ while ((k = stack.shift()) !== void 0) {
266
+ if (e[k] !== void 0) {
267
+ e = e[k];
268
+ }
269
+ else {
270
+ stack.unshift(k);
271
+ break;
272
+ }
273
+ }
274
+ }
275
+ if (options.force) {
276
+ while ((k = stack.shift()) !== void 0) {
277
+ if ((typeof stack[0] === 'number') || ((stack.length === 0) && (typeof last[0] === 'number'))) {
278
+ e[k] = [];
279
+ }
280
+ else {
281
+ e[k] = {};
282
+ }
283
+ e = e[k];
284
+ }
285
+ }
286
+ else {
287
+ while ((k = stack.pop()) !== void 0) {
288
+ last.unshift(k);
289
+ }
290
+ }
291
+ return [e, last];
292
+ }
293
+ function arrize(path, glue) {
294
+ if (glue == null) {
295
+ glue = '.';
296
+ }
297
+ return ((function () {
298
+ if (Array.isArray(path)) {
299
+ return path.slice(0);
300
+ }
301
+ else {
302
+ switch (path) {
303
+ case void 0:
304
+ case null:
305
+ case false:
306
+ case '':
307
+ return [];
308
+ default:
309
+ return path.toString().split(glue);
310
+ }
311
+ }
312
+ })()).map(function (e) {
313
+ switch (e) {
314
+ case void 0:
315
+ case null:
316
+ case false:
317
+ case '':
318
+ return null;
319
+ default:
320
+ return e.toString();
321
+ }
322
+ }).filter(function (e) {
323
+ return e != null;
324
+ });
325
+ }
326
+ function getBinarySize(string) {
327
+ if (string) {
328
+ return Buffer.byteLength(string, 'utf8');
329
+ }
330
+ else {
331
+ return 0;
332
+ }
333
+ }
334
+ exports.getBinarySize = getBinarySize;