@resolveio/server-lib 1.0.9 → 1.0.10

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 (2) hide show
  1. package/methods/collections.js +671 -666
  2. package/package.json +1 -1
@@ -7,732 +7,737 @@ var pagination_model_1 = require("../models/pagination.model");
7
7
  var log_collection_1 = require("../collections/log.collection");
8
8
  var index_1 = require("../index");
9
9
  function loadCollectionMethods(methodManager) {
10
- index_1.ResolveIOServer.getMainServer().getMethodManager().methods({
11
- // Query database for any collection and query
12
- findOne: {
13
- skipQueue: true,
14
- check: new simpl_schema_1.default({
15
- collection: {
16
- type: String
17
- },
18
- query: {
19
- type: Object,
20
- blackbox: true
21
- }
22
- }),
23
- function: function (collection, query) {
24
- return index_1.ResolveIOServer.getMainDB().model(collection).findOne(query).exec();
25
- }
26
- },
27
- find: {
28
- skipQueue: true,
29
- check: new simpl_schema_1.default({
30
- collection: {
31
- type: String
32
- },
33
- query: {
34
- type: Object,
35
- blackbox: true
36
- },
37
- sort: {
38
- type: Object,
39
- blackbox: true,
40
- optional: true
41
- }
42
- }),
43
- function: function (collection, query, sortQuery) {
44
- if (sortQuery === void 0) { sortQuery = {}; }
45
- return index_1.ResolveIOServer.getMainDB().model(collection).find(query).sort(sortQuery).exec();
46
- }
47
- },
48
- findWithOptions: {
49
- skipQueue: true,
50
- check: new simpl_schema_1.default({
51
- collection: {
52
- type: String
53
- },
54
- query: {
55
- type: Object,
56
- blackbox: true
57
- },
58
- options: {
59
- type: pagination_model_1.PaginationOptionsSchema
60
- }
61
- }),
62
- function: function (collection, query, options) {
63
- return index_1.ResolveIOServer.getMainDB().model(collection).find(query).sort(options.sort).limit(options.limit).skip(options.skip).select(options.fields).exec();
64
- }
65
- },
66
- // Insert 1 document
67
- // @Inputs: collection, document
68
- // @Outputs: res = _id of new document, err = not inserted
69
- insertDocument: {
70
- check: new simpl_schema_1.default({
71
- collection: {
72
- type: String
73
- },
74
- document: {
75
- type: Object,
76
- blackbox: true
77
- }
78
- }),
79
- function: function (collection, document) {
80
- var _this = this;
81
- return new Promise(function (resolve, reject) {
82
- var schemaErrors = null;
83
- try {
84
- index_1.ResolveIOServer.getMainDB().model(collection)['simplschema'].validate(document);
85
- }
86
- catch (errors) {
87
- schemaErrors = errors;
88
- _this.callMethodInternal.call(_this, 'insertErrorLog', 'Schema Failed on insert - ' + collection, [schemaErrors, document]);
89
- }
90
- if (schemaErrors) {
91
- console.log(schemaErrors);
92
- reject('Schema Errors');
10
+ var int = setInterval(function () {
11
+ if (index_1.ResolveIOServer && index_1.ResolveIOServer.getMainServer() && index_1.ResolveIOServer.getMainServer().getMethodManager()) {
12
+ clearInterval(int);
13
+ index_1.ResolveIOServer.getMainServer().getMethodManager().methods({
14
+ // Query database for any collection and query
15
+ findOne: {
16
+ skipQueue: true,
17
+ check: new simpl_schema_1.default({
18
+ collection: {
19
+ type: String
20
+ },
21
+ query: {
22
+ type: Object,
23
+ blackbox: true
24
+ }
25
+ }),
26
+ function: function (collection, query) {
27
+ return index_1.ResolveIOServer.getMainDB().model(collection).findOne(query).exec();
93
28
  }
94
- else {
95
- if (common_1.getBinarySize(JSON.stringify(document)) < 200000) {
96
- log_collection_1.Logs.create({
97
- _id: mongoose_1.Types.ObjectId().toHexString(),
98
- date: new Date(),
99
- type: 'info',
100
- title: 'Insert Document',
101
- message: 'User: ' + _this.user + ', Collection Type: ' + collection,
102
- payload: document,
103
- method: 'insertDocument',
104
- user: _this.user,
105
- messageId: ''
106
- });
29
+ },
30
+ find: {
31
+ skipQueue: true,
32
+ check: new simpl_schema_1.default({
33
+ collection: {
34
+ type: String
35
+ },
36
+ query: {
37
+ type: Object,
38
+ blackbox: true
39
+ },
40
+ sort: {
41
+ type: Object,
42
+ blackbox: true,
43
+ optional: true
107
44
  }
108
- else {
109
- log_collection_1.Logs.create({
110
- _id: mongoose_1.Types.ObjectId().toHexString(),
111
- date: new Date(),
112
- type: 'info',
113
- title: 'Insert Document',
114
- message: 'User: ' + _this.user + ', Collection Type: ' + collection,
115
- payload: 'Too Big',
116
- method: 'insertDocument',
117
- user: _this.user,
118
- messageId: ''
119
- });
45
+ }),
46
+ function: function (collection, query, sortQuery) {
47
+ if (sortQuery === void 0) { sortQuery = {}; }
48
+ return index_1.ResolveIOServer.getMainDB().model(collection).find(query).sort(sortQuery).exec();
49
+ }
50
+ },
51
+ findWithOptions: {
52
+ skipQueue: true,
53
+ check: new simpl_schema_1.default({
54
+ collection: {
55
+ type: String
56
+ },
57
+ query: {
58
+ type: Object,
59
+ blackbox: true
60
+ },
61
+ options: {
62
+ type: pagination_model_1.PaginationOptionsSchema
120
63
  }
121
- var id_1 = mongoose_1.Types.ObjectId().toHexString();
122
- document['_id'] = id_1;
123
- document['__v'] = 0;
124
- index_1.ResolveIOServer.getMainDB().model(collection).create(document).then(function () { return resolve(id_1); }, function (err) { return reject(err); });
64
+ }),
65
+ function: function (collection, query, options) {
66
+ return index_1.ResolveIOServer.getMainDB().model(collection).find(query).sort(options.sort).limit(options.limit).skip(options.skip).select(options.fields).exec();
125
67
  }
126
- });
127
- }
128
- },
129
- // Replaces 1 document with new document (from _id)
130
- // @Inputs: collection, document id
131
- // @Outputs: res = 1 (updated 1), res = 0 (not updated), err = not updated/wrong version
132
- // Search for doc, check version, if found then try and update
133
- updateDocument: {
134
- check: new simpl_schema_1.default({
135
- collection: {
136
- type: String
137
68
  },
138
- f_document: {
139
- type: Object,
140
- blackbox: true
141
- }
142
- }),
143
- function: function (collection, f_document) {
144
- var _this = this;
145
- return new Promise(function (resolve, reject) {
146
- index_1.ResolveIOServer.getMainDB().model(collection).findOne({ _id: f_document['_id'] }).lean().exec(function (errDoc, currDoc) {
147
- if (currDoc) {
148
- if (currDoc.__v === f_document['__v']) {
149
- var versionDoc = common_1.deepCopy(currDoc);
150
- versionDoc._id = {
151
- _id: currDoc._id,
152
- __v: currDoc.__v
153
- };
154
- var schemaErrors = null;
155
- try {
156
- index_1.ResolveIOServer.getMainDB().model(collection)['simplschema'].validate(f_document);
157
- }
158
- catch (errors) {
159
- schemaErrors = errors;
160
- _this.callMethodInternal.call(_this, 'insertErrorLog', 'Schema Failed on updateDocument - ' + collection, [schemaErrors, f_document]);
161
- }
162
- if (schemaErrors) {
163
- console.log(schemaErrors);
164
- reject('Schema Errors');
69
+ // Insert 1 document
70
+ // @Inputs: collection, document
71
+ // @Outputs: res = _id of new document, err = not inserted
72
+ insertDocument: {
73
+ check: new simpl_schema_1.default({
74
+ collection: {
75
+ type: String
76
+ },
77
+ document: {
78
+ type: Object,
79
+ blackbox: true
80
+ }
81
+ }),
82
+ function: function (collection, document) {
83
+ var _this = this;
84
+ return new Promise(function (resolve, reject) {
85
+ var schemaErrors = null;
86
+ try {
87
+ index_1.ResolveIOServer.getMainDB().model(collection)['simplschema'].validate(document);
88
+ }
89
+ catch (errors) {
90
+ schemaErrors = errors;
91
+ _this.callMethodInternal.call(_this, 'insertErrorLog', 'Schema Failed on insert - ' + collection, [schemaErrors, document]);
92
+ }
93
+ if (schemaErrors) {
94
+ console.log(schemaErrors);
95
+ reject('Schema Errors');
96
+ }
97
+ else {
98
+ if (common_1.getBinarySize(JSON.stringify(document)) < 200000) {
99
+ log_collection_1.Logs.create({
100
+ _id: mongoose_1.Types.ObjectId().toHexString(),
101
+ date: new Date(),
102
+ type: 'info',
103
+ title: 'Insert Document',
104
+ message: 'User: ' + _this.user + ', Collection Type: ' + collection,
105
+ payload: document,
106
+ method: 'insertDocument',
107
+ user: _this.user,
108
+ messageId: ''
109
+ });
165
110
  }
166
111
  else {
167
- index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).create(versionDoc).then(function (doc) {
168
- index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).deleteMany({
169
- $and: [
170
- { '_id._id': currDoc._id },
171
- { '_id.__v': { $lte: currDoc.__v - 4 } }
172
- ]
173
- }).exec();
174
- }, function (err) {
175
- console.log(err);
112
+ log_collection_1.Logs.create({
113
+ _id: mongoose_1.Types.ObjectId().toHexString(),
114
+ date: new Date(),
115
+ type: 'info',
116
+ title: 'Insert Document',
117
+ message: 'User: ' + _this.user + ', Collection Type: ' + collection,
118
+ payload: 'Too Big',
119
+ method: 'insertDocument',
120
+ user: _this.user,
121
+ messageId: ''
176
122
  });
177
- var updates = common_1.getMongoUpdates(currDoc, f_document, true);
178
- if (updates) {
179
- index_1.ResolveIOServer.getMainDB().model(collection).updateOne({ _id: f_document['_id'] }, updates, { strict: false }).exec().then(function (res) {
180
- resolve(res.nModified);
181
- }, function (err) { return reject(err); });
182
- }
183
- else {
184
- resolve(1);
185
- }
186
- if (common_1.getBinarySize(JSON.stringify(f_document)) < 200000) {
187
- log_collection_1.Logs.create({
188
- _id: mongoose_1.Types.ObjectId().toHexString(),
189
- date: new Date(),
190
- type: 'info',
191
- title: 'Update Document',
192
- message: 'User: ' + _this.user + ', Collection Type: ' + collection,
193
- payload: f_document,
194
- method: 'updateDocument',
195
- user: _this.user,
196
- messageId: ''
197
- });
198
- }
199
- else {
200
- log_collection_1.Logs.create({
201
- _id: mongoose_1.Types.ObjectId().toHexString(),
202
- date: new Date(),
203
- type: 'info',
204
- title: 'Update Document',
205
- message: 'User: ' + _this.user + ', Collection Type: ' + collection,
206
- payload: 'Too Big',
207
- method: 'updateDocument',
208
- user: _this.user,
209
- messageId: ''
210
- });
211
- }
212
123
  }
124
+ var id_1 = mongoose_1.Types.ObjectId().toHexString();
125
+ document['_id'] = id_1;
126
+ document['__v'] = 0;
127
+ index_1.ResolveIOServer.getMainDB().model(collection).create(document).then(function () { return resolve(id_1); }, function (err) { return reject(err); });
213
128
  }
214
- else {
215
- console.log('invalid version - ' + collection, currDoc.__v, f_document['__v']);
216
- index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).findOne({
217
- $and: [
218
- { '_id._id': currDoc._id },
219
- { '_id.__v': f_document['__v'] }
220
- ]
221
- }).lean().exec(function (errOldDoc, oldDoc) {
222
- var newCurrDocId = currDoc._id;
223
- if (oldDoc) {
224
- var versionDoc_1 = common_1.deepCopy(currDoc);
225
- versionDoc_1._id = {
129
+ });
130
+ }
131
+ },
132
+ // Replaces 1 document with new document (from _id)
133
+ // @Inputs: collection, document id
134
+ // @Outputs: res = 1 (updated 1), res = 0 (not updated), err = not updated/wrong version
135
+ // Search for doc, check version, if found then try and update
136
+ updateDocument: {
137
+ check: new simpl_schema_1.default({
138
+ collection: {
139
+ type: String
140
+ },
141
+ f_document: {
142
+ type: Object,
143
+ blackbox: true
144
+ }
145
+ }),
146
+ function: function (collection, f_document) {
147
+ var _this = this;
148
+ return new Promise(function (resolve, reject) {
149
+ index_1.ResolveIOServer.getMainDB().model(collection).findOne({ _id: f_document['_id'] }).lean().exec(function (errDoc, currDoc) {
150
+ if (currDoc) {
151
+ if (currDoc.__v === f_document['__v']) {
152
+ var versionDoc = common_1.deepCopy(currDoc);
153
+ versionDoc._id = {
226
154
  _id: currDoc._id,
227
155
  __v: currDoc.__v
228
156
  };
229
- var updatedDoc = common_1.getMongoMergeUpdatedDoc(f_document, currDoc, oldDoc);
230
- updatedDoc._id = newCurrDocId;
231
- updatedDoc.__v++;
232
- index_1.ResolveIOServer.getMainDB().model(collection).replaceOne({ _id: newCurrDocId }, updatedDoc).exec().then(function (res) {
233
- index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).create(versionDoc_1).then(function (doc) {
157
+ var schemaErrors = null;
158
+ try {
159
+ index_1.ResolveIOServer.getMainDB().model(collection)['simplschema'].validate(f_document);
160
+ }
161
+ catch (errors) {
162
+ schemaErrors = errors;
163
+ _this.callMethodInternal.call(_this, 'insertErrorLog', 'Schema Failed on updateDocument - ' + collection, [schemaErrors, f_document]);
164
+ }
165
+ if (schemaErrors) {
166
+ console.log(schemaErrors);
167
+ reject('Schema Errors');
168
+ }
169
+ else {
170
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).create(versionDoc).then(function (doc) {
234
171
  index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).deleteMany({
235
172
  $and: [
236
- { '_id._id': versionDoc_1._id._id },
237
- { '_id.__v': { $lte: versionDoc_1._id.__v - 4 } }
173
+ { '_id._id': currDoc._id },
174
+ { '_id.__v': { $lte: currDoc.__v - 4 } }
238
175
  ]
239
176
  }).exec();
240
- resolve(res.nModified);
177
+ }, function (err) {
178
+ console.log(err);
241
179
  });
242
- }, function (err) { return reject(err); });
180
+ var updates = common_1.getMongoUpdates(currDoc, f_document, true);
181
+ if (updates) {
182
+ index_1.ResolveIOServer.getMainDB().model(collection).updateOne({ _id: f_document['_id'] }, updates, { strict: false }).exec().then(function (res) {
183
+ resolve(res.nModified);
184
+ }, function (err) { return reject(err); });
185
+ }
186
+ else {
187
+ resolve(1);
188
+ }
189
+ if (common_1.getBinarySize(JSON.stringify(f_document)) < 200000) {
190
+ log_collection_1.Logs.create({
191
+ _id: mongoose_1.Types.ObjectId().toHexString(),
192
+ date: new Date(),
193
+ type: 'info',
194
+ title: 'Update Document',
195
+ message: 'User: ' + _this.user + ', Collection Type: ' + collection,
196
+ payload: f_document,
197
+ method: 'updateDocument',
198
+ user: _this.user,
199
+ messageId: ''
200
+ });
201
+ }
202
+ else {
203
+ log_collection_1.Logs.create({
204
+ _id: mongoose_1.Types.ObjectId().toHexString(),
205
+ date: new Date(),
206
+ type: 'info',
207
+ title: 'Update Document',
208
+ message: 'User: ' + _this.user + ', Collection Type: ' + collection,
209
+ payload: 'Too Big',
210
+ method: 'updateDocument',
211
+ user: _this.user,
212
+ messageId: ''
213
+ });
214
+ }
215
+ }
243
216
  }
244
217
  else {
245
- reject('Invalid Version And Could Not Find History - DB: ' + currDoc.__v + ', Trying to update with :' + f_document['__v']);
246
- }
247
- });
248
- }
249
- }
250
- else {
251
- reject(errDoc);
252
- }
253
- });
254
- });
255
- }
256
- },
257
- // Updates 1 document's props (from _id)
258
- // @Inputs: collection, document id, document version, update paramters ({prop: 'xxx', data: 'yyy})
259
- // @Outputs: res = 1 (updated 1), res = 0 (not updated), err = not updated/wrong version
260
- // Search for doc, check version, if found then try and update props
261
- updateDocumentProps: {
262
- check: new simpl_schema_1.default({
263
- collection: {
264
- type: String
265
- },
266
- doc_id: {
267
- type: String
268
- },
269
- updateParams: {
270
- type: Array
271
- },
272
- 'updateParams.$': {
273
- type: Object,
274
- blackbox: true
275
- },
276
- doc__v: {
277
- type: Number
278
- }
279
- }),
280
- function: function (collection, doc_id, updateParams, doc__v) {
281
- var _this = this;
282
- return new Promise(function (resolve, reject) {
283
- index_1.ResolveIOServer.getMainDB().model(collection).findOne({ _id: doc_id }).lean().exec(function (errDoc, currDoc) {
284
- if (currDoc) {
285
- var modifiedDoc_1 = common_1.deepCopy(currDoc);
286
- updateParams.forEach(function (data) {
287
- modifiedDoc_1[data.prop] = data.data;
288
- });
289
- if (modifiedDoc_1.__v === doc__v) {
290
- var versionDoc = common_1.deepCopy(currDoc);
291
- versionDoc._id = {
292
- _id: currDoc._id,
293
- __v: currDoc.__v
294
- };
295
- var schemaErrors = null;
296
- try {
297
- index_1.ResolveIOServer.getMainDB().model(collection)['simplschema'].validate(modifiedDoc_1);
298
- }
299
- catch (errors) {
300
- schemaErrors = errors;
301
- _this.callMethodInternal.call(_this, 'insertErrorLog', 'Schema Failed on updateDocumentProps - ' + collection, [schemaErrors, modifiedDoc_1]);
302
- }
303
- if (schemaErrors) {
304
- console.log(schemaErrors);
305
- reject('Schema Errors');
306
- }
307
- else {
308
- index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).create(versionDoc).then(function (doc) {
309
- index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).deleteMany({
218
+ console.log('invalid version - ' + collection, currDoc.__v, f_document['__v']);
219
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).findOne({
310
220
  $and: [
311
221
  { '_id._id': currDoc._id },
312
- { '_id.__v': { $lte: currDoc.__v - 4 } }
222
+ { '_id.__v': f_document['__v'] }
313
223
  ]
314
- }).exec();
315
- }, function (err) {
316
- console.log(err);
317
- });
318
- var updates = common_1.getMongoUpdates(currDoc, modifiedDoc_1, true);
319
- if (updates) {
320
- index_1.ResolveIOServer.getMainDB().model(collection).updateOne({ _id: modifiedDoc_1._id }, updates, { strict: false }).exec().then(function (res) {
321
- resolve(res.ok);
322
- }, function (err) { return reject(err); });
323
- }
324
- else {
325
- resolve(1);
326
- }
327
- if (common_1.getBinarySize(JSON.stringify(updateParams)) < 200000) {
328
- log_collection_1.Logs.create({
329
- _id: mongoose_1.Types.ObjectId().toHexString(),
330
- date: new Date(),
331
- type: 'info',
332
- title: 'Update Document Props',
333
- message: 'User: ' + _this.user + ', Collection Type: ' + collection + ', Id: ' + doc_id + ', ' + 'Version: ' + doc__v,
334
- payload: updateParams,
335
- method: 'updateDocumentProps',
336
- user: _this.user,
337
- messageId: ''
338
- });
339
- }
340
- else {
341
- log_collection_1.Logs.create({
342
- _id: mongoose_1.Types.ObjectId().toHexString(),
343
- date: new Date(),
344
- type: 'info',
345
- title: 'Update Document Props',
346
- message: 'User: ' + _this.user + ', Collection Type: ' + collection + ', Id: ' + doc_id + ', ' + 'Version: ' + doc__v,
347
- payload: 'Too Big',
348
- method: 'updateDocumentProps',
349
- user: _this.user,
350
- messageId: ''
224
+ }).lean().exec(function (errOldDoc, oldDoc) {
225
+ var newCurrDocId = currDoc._id;
226
+ if (oldDoc) {
227
+ var versionDoc_1 = common_1.deepCopy(currDoc);
228
+ versionDoc_1._id = {
229
+ _id: currDoc._id,
230
+ __v: currDoc.__v
231
+ };
232
+ var updatedDoc = common_1.getMongoMergeUpdatedDoc(f_document, currDoc, oldDoc);
233
+ updatedDoc._id = newCurrDocId;
234
+ updatedDoc.__v++;
235
+ index_1.ResolveIOServer.getMainDB().model(collection).replaceOne({ _id: newCurrDocId }, updatedDoc).exec().then(function (res) {
236
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).create(versionDoc_1).then(function (doc) {
237
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).deleteMany({
238
+ $and: [
239
+ { '_id._id': versionDoc_1._id._id },
240
+ { '_id.__v': { $lte: versionDoc_1._id.__v - 4 } }
241
+ ]
242
+ }).exec();
243
+ resolve(res.nModified);
244
+ });
245
+ }, function (err) { return reject(err); });
246
+ }
247
+ else {
248
+ reject('Invalid Version And Could Not Find History - DB: ' + currDoc.__v + ', Trying to update with :' + f_document['__v']);
249
+ }
351
250
  });
352
251
  }
353
252
  }
354
- }
355
- else {
356
- console.log('invalid version - ' + collection, currDoc.__v, doc__v);
357
- index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).findOne({
358
- $and: [
359
- { '_id._id': currDoc._id },
360
- { '_id.__v': doc__v }
361
- ]
362
- }).lean().exec(function (errOldDoc, oldDoc) {
363
- var newCurrDocId = currDoc._id;
364
- if (oldDoc) {
365
- var versionDoc_2 = common_1.deepCopy(currDoc);
366
- versionDoc_2._id = {
253
+ else {
254
+ reject(errDoc);
255
+ }
256
+ });
257
+ });
258
+ }
259
+ },
260
+ // Updates 1 document's props (from _id)
261
+ // @Inputs: collection, document id, document version, update paramters ({prop: 'xxx', data: 'yyy})
262
+ // @Outputs: res = 1 (updated 1), res = 0 (not updated), err = not updated/wrong version
263
+ // Search for doc, check version, if found then try and update props
264
+ updateDocumentProps: {
265
+ check: new simpl_schema_1.default({
266
+ collection: {
267
+ type: String
268
+ },
269
+ doc_id: {
270
+ type: String
271
+ },
272
+ updateParams: {
273
+ type: Array
274
+ },
275
+ 'updateParams.$': {
276
+ type: Object,
277
+ blackbox: true
278
+ },
279
+ doc__v: {
280
+ type: Number
281
+ }
282
+ }),
283
+ function: function (collection, doc_id, updateParams, doc__v) {
284
+ var _this = this;
285
+ return new Promise(function (resolve, reject) {
286
+ index_1.ResolveIOServer.getMainDB().model(collection).findOne({ _id: doc_id }).lean().exec(function (errDoc, currDoc) {
287
+ if (currDoc) {
288
+ var modifiedDoc_1 = common_1.deepCopy(currDoc);
289
+ updateParams.forEach(function (data) {
290
+ modifiedDoc_1[data.prop] = data.data;
291
+ });
292
+ if (modifiedDoc_1.__v === doc__v) {
293
+ var versionDoc = common_1.deepCopy(currDoc);
294
+ versionDoc._id = {
367
295
  _id: currDoc._id,
368
296
  __v: currDoc.__v
369
297
  };
370
- var updatedDoc = common_1.getMongoMergeUpdatedDoc(modifiedDoc_1, currDoc, oldDoc);
371
- updatedDoc._id = newCurrDocId;
372
- updatedDoc.__v++;
373
- index_1.ResolveIOServer.getMainDB().model(collection).replaceOne({ _id: newCurrDocId }, updatedDoc).exec().then(function (res) {
374
- index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).create(versionDoc_2).then(function (doc) {
298
+ var schemaErrors = null;
299
+ try {
300
+ index_1.ResolveIOServer.getMainDB().model(collection)['simplschema'].validate(modifiedDoc_1);
301
+ }
302
+ catch (errors) {
303
+ schemaErrors = errors;
304
+ _this.callMethodInternal.call(_this, 'insertErrorLog', 'Schema Failed on updateDocumentProps - ' + collection, [schemaErrors, modifiedDoc_1]);
305
+ }
306
+ if (schemaErrors) {
307
+ console.log(schemaErrors);
308
+ reject('Schema Errors');
309
+ }
310
+ else {
311
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).create(versionDoc).then(function (doc) {
375
312
  index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).deleteMany({
376
313
  $and: [
377
- { '_id._id': versionDoc_2._id._id },
378
- { '_id.__v': { $lte: versionDoc_2._id.__v - 4 } }
314
+ { '_id._id': currDoc._id },
315
+ { '_id.__v': { $lte: currDoc.__v - 4 } }
379
316
  ]
380
317
  }).exec();
381
318
  }, function (err) {
382
319
  console.log(err);
383
320
  });
384
- resolve(res.nModified);
385
- }, function (err) { return reject(err); });
321
+ var updates = common_1.getMongoUpdates(currDoc, modifiedDoc_1, true);
322
+ if (updates) {
323
+ index_1.ResolveIOServer.getMainDB().model(collection).updateOne({ _id: modifiedDoc_1._id }, updates, { strict: false }).exec().then(function (res) {
324
+ resolve(res.ok);
325
+ }, function (err) { return reject(err); });
326
+ }
327
+ else {
328
+ resolve(1);
329
+ }
330
+ if (common_1.getBinarySize(JSON.stringify(updateParams)) < 200000) {
331
+ log_collection_1.Logs.create({
332
+ _id: mongoose_1.Types.ObjectId().toHexString(),
333
+ date: new Date(),
334
+ type: 'info',
335
+ title: 'Update Document Props',
336
+ message: 'User: ' + _this.user + ', Collection Type: ' + collection + ', Id: ' + doc_id + ', ' + 'Version: ' + doc__v,
337
+ payload: updateParams,
338
+ method: 'updateDocumentProps',
339
+ user: _this.user,
340
+ messageId: ''
341
+ });
342
+ }
343
+ else {
344
+ log_collection_1.Logs.create({
345
+ _id: mongoose_1.Types.ObjectId().toHexString(),
346
+ date: new Date(),
347
+ type: 'info',
348
+ title: 'Update Document Props',
349
+ message: 'User: ' + _this.user + ', Collection Type: ' + collection + ', Id: ' + doc_id + ', ' + 'Version: ' + doc__v,
350
+ payload: 'Too Big',
351
+ method: 'updateDocumentProps',
352
+ user: _this.user,
353
+ messageId: ''
354
+ });
355
+ }
356
+ }
386
357
  }
387
358
  else {
388
- reject('Invalid Version And Could Not Find History Props - DB: ' + currDoc.__v + ', Trying to update with :' + modifiedDoc_1.__v);
389
- }
390
- });
391
- }
392
- }
393
- else {
394
- reject(errDoc);
395
- }
396
- });
397
- });
398
- }
399
- },
400
- updateDocumentOffline: {
401
- check: new simpl_schema_1.default({
402
- collection: {
403
- type: String
404
- },
405
- f_document: {
406
- type: Object,
407
- blackbox: true
408
- },
409
- f_oldDocument: {
410
- type: Object,
411
- blackbox: true
412
- }
413
- }),
414
- function: function (collection, f_document, f_oldDocument) {
415
- var _this = this;
416
- return new Promise(function (resolve, reject) {
417
- index_1.ResolveIOServer.getMainDB().model(collection).findOne({ _id: f_document['_id'] }).lean().exec(function (errDoc, currDoc) {
418
- if (currDoc) {
419
- if (currDoc.__v === f_document['__v']) {
420
- var versionDoc = common_1.deepCopy(currDoc);
421
- versionDoc._id = {
422
- _id: currDoc._id,
423
- __v: currDoc.__v
424
- };
425
- var schemaErrors = null;
426
- try {
427
- index_1.ResolveIOServer.getMainDB().model(collection)['simplschema'].validate(f_document);
428
- }
429
- catch (errors) {
430
- schemaErrors = errors;
431
- _this.callMethodInternal.call(_this, 'insertErrorLog', 'Schema Failed on updateDocument - ' + collection, [schemaErrors, f_document]);
432
- }
433
- if (schemaErrors) {
434
- console.log(schemaErrors);
435
- reject('Schema Errors');
436
- }
437
- else {
438
- index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).create(versionDoc).then(function (doc) {
439
- index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).deleteMany({
359
+ console.log('invalid version - ' + collection, currDoc.__v, doc__v);
360
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).findOne({
440
361
  $and: [
441
362
  { '_id._id': currDoc._id },
442
- { '_id.__v': { $lte: currDoc.__v - 4 } }
363
+ { '_id.__v': doc__v }
443
364
  ]
444
- }).exec();
445
- }, function (err) {
446
- console.log(err);
447
- });
448
- var updates = common_1.getMongoUpdates(currDoc, f_document, true);
449
- if (updates) {
450
- index_1.ResolveIOServer.getMainDB().model(collection).updateOne({ _id: f_document['_id'] }, updates, { strict: false }).exec().then(function (res) {
451
- resolve(res.nModified);
452
- }, function (err) { return reject(err); });
453
- }
454
- else {
455
- resolve(1);
456
- }
457
- if (common_1.getBinarySize(JSON.stringify(f_document)) < 200000) {
458
- log_collection_1.Logs.create({
459
- _id: mongoose_1.Types.ObjectId().toHexString(),
460
- date: new Date(),
461
- type: 'info',
462
- title: 'Update Document',
463
- message: 'User: ' + _this.user + ', Collection Type: ' + collection,
464
- payload: f_document,
465
- method: 'updateDocumentOffline',
466
- user: _this.user,
467
- messageId: ''
468
- });
469
- }
470
- else {
471
- log_collection_1.Logs.create({
472
- _id: mongoose_1.Types.ObjectId().toHexString(),
473
- date: new Date(),
474
- type: 'info',
475
- title: 'Update Document',
476
- message: 'User: ' + _this.user + ', Collection Type: ' + collection,
477
- payload: 'Too Big',
478
- method: 'updateDocumentOffline',
479
- user: _this.user,
480
- messageId: ''
365
+ }).lean().exec(function (errOldDoc, oldDoc) {
366
+ var newCurrDocId = currDoc._id;
367
+ if (oldDoc) {
368
+ var versionDoc_2 = common_1.deepCopy(currDoc);
369
+ versionDoc_2._id = {
370
+ _id: currDoc._id,
371
+ __v: currDoc.__v
372
+ };
373
+ var updatedDoc = common_1.getMongoMergeUpdatedDoc(modifiedDoc_1, currDoc, oldDoc);
374
+ updatedDoc._id = newCurrDocId;
375
+ updatedDoc.__v++;
376
+ index_1.ResolveIOServer.getMainDB().model(collection).replaceOne({ _id: newCurrDocId }, updatedDoc).exec().then(function (res) {
377
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).create(versionDoc_2).then(function (doc) {
378
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).deleteMany({
379
+ $and: [
380
+ { '_id._id': versionDoc_2._id._id },
381
+ { '_id.__v': { $lte: versionDoc_2._id.__v - 4 } }
382
+ ]
383
+ }).exec();
384
+ }, function (err) {
385
+ console.log(err);
386
+ });
387
+ resolve(res.nModified);
388
+ }, function (err) { return reject(err); });
389
+ }
390
+ else {
391
+ reject('Invalid Version And Could Not Find History Props - DB: ' + currDoc.__v + ', Trying to update with :' + modifiedDoc_1.__v);
392
+ }
481
393
  });
482
394
  }
483
395
  }
484
- }
485
- else {
486
- console.log('invalid version - ' + collection, currDoc.__v, f_document['__v']);
487
- var newCurrDocId = currDoc._id;
488
- if (f_oldDocument) {
489
- var versionDoc_3 = common_1.deepCopy(currDoc);
490
- versionDoc_3._id = {
491
- _id: currDoc._id,
492
- __v: currDoc.__v
493
- };
494
- var updatedDoc = common_1.getMongoMergeUpdatedDoc(f_document, currDoc, f_oldDocument);
495
- updatedDoc._id = newCurrDocId;
496
- updatedDoc.__v++;
497
- index_1.ResolveIOServer.getMainDB().model(collection).replaceOne({ _id: newCurrDocId }, updatedDoc).exec().then(function (res) {
498
- index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).create(versionDoc_3).then(function (doc) {
499
- index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).deleteMany({
500
- $and: [
501
- { '_id._id': versionDoc_3._id._id },
502
- { '_id.__v': { $lte: versionDoc_3._id.__v - 4 } }
503
- ]
504
- }).exec();
505
- }, function (err) {
506
- console.log(err);
507
- });
508
- resolve(res.nModified);
509
- }, function (err) { return reject(err); });
510
- }
511
396
  else {
512
- reject('OFFLINE - Invalid Version And Could Not Find History - DB: ' + currDoc.__v + ', Trying to update with :' + f_document['__v']);
397
+ reject(errDoc);
513
398
  }
514
- }
515
- }
516
- else {
517
- reject(errDoc);
518
- }
519
- });
520
- });
521
- }
522
- },
523
- // Updates 1 document's props (from _id)
524
- // @Inputs: collection, document id, document version, update paramters ({prop: 'xxx', data: 'yyy})
525
- // @Outputs: res = 1 (updated 1), res = 0 (not updated), err = not updated/wrong version
526
- // Search for doc, check version, if found then try and update props
527
- updateDocumentPropsOffline: {
528
- check: new simpl_schema_1.default({
529
- collection: {
530
- type: String
531
- },
532
- doc_id: {
533
- type: String
534
- },
535
- updateParams: {
536
- type: Array
537
- },
538
- 'updateParams.$': {
539
- type: Object,
540
- blackbox: true
541
- },
542
- doc__v: {
543
- type: Number
544
- },
545
- f_oldDocument: {
546
- type: Object,
547
- blackbox: true
548
- }
549
- }),
550
- function: function (collection, doc_id, updateParams, doc__v, f_oldDocument) {
551
- var _this = this;
552
- return new Promise(function (resolve, reject) {
553
- index_1.ResolveIOServer.getMainDB().model(collection).findOne({ _id: doc_id }).lean().exec(function (errDoc, currDoc) {
554
- if (currDoc) {
555
- var modifiedDoc_2 = common_1.deepCopy(currDoc);
556
- updateParams.forEach(function (data) {
557
- modifiedDoc_2[data.prop] = data.data;
558
399
  });
559
- if (modifiedDoc_2.__v === doc__v) {
560
- var versionDoc = common_1.deepCopy(currDoc);
561
- versionDoc._id = {
562
- _id: currDoc._id,
563
- __v: currDoc.__v
564
- };
565
- var schemaErrors = null;
566
- try {
567
- index_1.ResolveIOServer.getMainDB().model(collection)['simplschema'].validate(modifiedDoc_2);
568
- }
569
- catch (errors) {
570
- schemaErrors = errors;
571
- _this.callMethodInternal.call(_this, 'insertErrorLog', 'Schema Failed on updateDocumentProps - ' + collection, [schemaErrors, modifiedDoc_2]);
572
- }
573
- if (schemaErrors) {
574
- console.log(schemaErrors);
575
- reject('Schema Errors');
576
- }
577
- else {
578
- index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).create(versionDoc).then(function (doc) {
579
- index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).deleteMany({
580
- $and: [
581
- { '_id._id': currDoc._id },
582
- { '_id.__v': { $lte: currDoc.__v - 4 } }
583
- ]
584
- }).exec();
585
- }, function (err) {
586
- console.log(err);
587
- });
588
- var updates = common_1.getMongoUpdates(currDoc, modifiedDoc_2, true);
589
- if (updates) {
590
- index_1.ResolveIOServer.getMainDB().model(collection).updateOne({ _id: modifiedDoc_2._id }, updates, { strict: false }).exec().then(function (res) {
591
- resolve(res.ok);
592
- }, function (err) { return reject(err); });
400
+ });
401
+ }
402
+ },
403
+ updateDocumentOffline: {
404
+ check: new simpl_schema_1.default({
405
+ collection: {
406
+ type: String
407
+ },
408
+ f_document: {
409
+ type: Object,
410
+ blackbox: true
411
+ },
412
+ f_oldDocument: {
413
+ type: Object,
414
+ blackbox: true
415
+ }
416
+ }),
417
+ function: function (collection, f_document, f_oldDocument) {
418
+ var _this = this;
419
+ return new Promise(function (resolve, reject) {
420
+ index_1.ResolveIOServer.getMainDB().model(collection).findOne({ _id: f_document['_id'] }).lean().exec(function (errDoc, currDoc) {
421
+ if (currDoc) {
422
+ if (currDoc.__v === f_document['__v']) {
423
+ var versionDoc = common_1.deepCopy(currDoc);
424
+ versionDoc._id = {
425
+ _id: currDoc._id,
426
+ __v: currDoc.__v
427
+ };
428
+ var schemaErrors = null;
429
+ try {
430
+ index_1.ResolveIOServer.getMainDB().model(collection)['simplschema'].validate(f_document);
431
+ }
432
+ catch (errors) {
433
+ schemaErrors = errors;
434
+ _this.callMethodInternal.call(_this, 'insertErrorLog', 'Schema Failed on updateDocument - ' + collection, [schemaErrors, f_document]);
435
+ }
436
+ if (schemaErrors) {
437
+ console.log(schemaErrors);
438
+ reject('Schema Errors');
439
+ }
440
+ else {
441
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).create(versionDoc).then(function (doc) {
442
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).deleteMany({
443
+ $and: [
444
+ { '_id._id': currDoc._id },
445
+ { '_id.__v': { $lte: currDoc.__v - 4 } }
446
+ ]
447
+ }).exec();
448
+ }, function (err) {
449
+ console.log(err);
450
+ });
451
+ var updates = common_1.getMongoUpdates(currDoc, f_document, true);
452
+ if (updates) {
453
+ index_1.ResolveIOServer.getMainDB().model(collection).updateOne({ _id: f_document['_id'] }, updates, { strict: false }).exec().then(function (res) {
454
+ resolve(res.nModified);
455
+ }, function (err) { return reject(err); });
456
+ }
457
+ else {
458
+ resolve(1);
459
+ }
460
+ if (common_1.getBinarySize(JSON.stringify(f_document)) < 200000) {
461
+ log_collection_1.Logs.create({
462
+ _id: mongoose_1.Types.ObjectId().toHexString(),
463
+ date: new Date(),
464
+ type: 'info',
465
+ title: 'Update Document',
466
+ message: 'User: ' + _this.user + ', Collection Type: ' + collection,
467
+ payload: f_document,
468
+ method: 'updateDocumentOffline',
469
+ user: _this.user,
470
+ messageId: ''
471
+ });
472
+ }
473
+ else {
474
+ log_collection_1.Logs.create({
475
+ _id: mongoose_1.Types.ObjectId().toHexString(),
476
+ date: new Date(),
477
+ type: 'info',
478
+ title: 'Update Document',
479
+ message: 'User: ' + _this.user + ', Collection Type: ' + collection,
480
+ payload: 'Too Big',
481
+ method: 'updateDocumentOffline',
482
+ user: _this.user,
483
+ messageId: ''
484
+ });
485
+ }
486
+ }
593
487
  }
594
488
  else {
595
- resolve(1);
489
+ console.log('invalid version - ' + collection, currDoc.__v, f_document['__v']);
490
+ var newCurrDocId = currDoc._id;
491
+ if (f_oldDocument) {
492
+ var versionDoc_3 = common_1.deepCopy(currDoc);
493
+ versionDoc_3._id = {
494
+ _id: currDoc._id,
495
+ __v: currDoc.__v
496
+ };
497
+ var updatedDoc = common_1.getMongoMergeUpdatedDoc(f_document, currDoc, f_oldDocument);
498
+ updatedDoc._id = newCurrDocId;
499
+ updatedDoc.__v++;
500
+ index_1.ResolveIOServer.getMainDB().model(collection).replaceOne({ _id: newCurrDocId }, updatedDoc).exec().then(function (res) {
501
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).create(versionDoc_3).then(function (doc) {
502
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).deleteMany({
503
+ $and: [
504
+ { '_id._id': versionDoc_3._id._id },
505
+ { '_id.__v': { $lte: versionDoc_3._id.__v - 4 } }
506
+ ]
507
+ }).exec();
508
+ }, function (err) {
509
+ console.log(err);
510
+ });
511
+ resolve(res.nModified);
512
+ }, function (err) { return reject(err); });
513
+ }
514
+ else {
515
+ reject('OFFLINE - Invalid Version And Could Not Find History - DB: ' + currDoc.__v + ', Trying to update with :' + f_document['__v']);
516
+ }
596
517
  }
597
- if (common_1.getBinarySize(JSON.stringify(updateParams)) < 200000) {
598
- log_collection_1.Logs.create({
599
- _id: mongoose_1.Types.ObjectId().toHexString(),
600
- date: new Date(),
601
- type: 'info',
602
- title: 'Update Document Props',
603
- message: 'User: ' + _this.user + ', Collection Type: ' + collection + ', Id: ' + doc_id + ', ' + 'Version: ' + doc__v,
604
- payload: updateParams,
605
- method: 'updateDocumentPropsOffline',
606
- user: _this.user,
607
- messageId: ''
608
- });
518
+ }
519
+ else {
520
+ reject(errDoc);
521
+ }
522
+ });
523
+ });
524
+ }
525
+ },
526
+ // Updates 1 document's props (from _id)
527
+ // @Inputs: collection, document id, document version, update paramters ({prop: 'xxx', data: 'yyy})
528
+ // @Outputs: res = 1 (updated 1), res = 0 (not updated), err = not updated/wrong version
529
+ // Search for doc, check version, if found then try and update props
530
+ updateDocumentPropsOffline: {
531
+ check: new simpl_schema_1.default({
532
+ collection: {
533
+ type: String
534
+ },
535
+ doc_id: {
536
+ type: String
537
+ },
538
+ updateParams: {
539
+ type: Array
540
+ },
541
+ 'updateParams.$': {
542
+ type: Object,
543
+ blackbox: true
544
+ },
545
+ doc__v: {
546
+ type: Number
547
+ },
548
+ f_oldDocument: {
549
+ type: Object,
550
+ blackbox: true
551
+ }
552
+ }),
553
+ function: function (collection, doc_id, updateParams, doc__v, f_oldDocument) {
554
+ var _this = this;
555
+ return new Promise(function (resolve, reject) {
556
+ index_1.ResolveIOServer.getMainDB().model(collection).findOne({ _id: doc_id }).lean().exec(function (errDoc, currDoc) {
557
+ if (currDoc) {
558
+ var modifiedDoc_2 = common_1.deepCopy(currDoc);
559
+ updateParams.forEach(function (data) {
560
+ modifiedDoc_2[data.prop] = data.data;
561
+ });
562
+ if (modifiedDoc_2.__v === doc__v) {
563
+ var versionDoc = common_1.deepCopy(currDoc);
564
+ versionDoc._id = {
565
+ _id: currDoc._id,
566
+ __v: currDoc.__v
567
+ };
568
+ var schemaErrors = null;
569
+ try {
570
+ index_1.ResolveIOServer.getMainDB().model(collection)['simplschema'].validate(modifiedDoc_2);
571
+ }
572
+ catch (errors) {
573
+ schemaErrors = errors;
574
+ _this.callMethodInternal.call(_this, 'insertErrorLog', 'Schema Failed on updateDocumentProps - ' + collection, [schemaErrors, modifiedDoc_2]);
575
+ }
576
+ if (schemaErrors) {
577
+ console.log(schemaErrors);
578
+ reject('Schema Errors');
579
+ }
580
+ else {
581
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).create(versionDoc).then(function (doc) {
582
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).deleteMany({
583
+ $and: [
584
+ { '_id._id': currDoc._id },
585
+ { '_id.__v': { $lte: currDoc.__v - 4 } }
586
+ ]
587
+ }).exec();
588
+ }, function (err) {
589
+ console.log(err);
590
+ });
591
+ var updates = common_1.getMongoUpdates(currDoc, modifiedDoc_2, true);
592
+ if (updates) {
593
+ index_1.ResolveIOServer.getMainDB().model(collection).updateOne({ _id: modifiedDoc_2._id }, updates, { strict: false }).exec().then(function (res) {
594
+ resolve(res.ok);
595
+ }, function (err) { return reject(err); });
596
+ }
597
+ else {
598
+ resolve(1);
599
+ }
600
+ if (common_1.getBinarySize(JSON.stringify(updateParams)) < 200000) {
601
+ log_collection_1.Logs.create({
602
+ _id: mongoose_1.Types.ObjectId().toHexString(),
603
+ date: new Date(),
604
+ type: 'info',
605
+ title: 'Update Document Props',
606
+ message: 'User: ' + _this.user + ', Collection Type: ' + collection + ', Id: ' + doc_id + ', ' + 'Version: ' + doc__v,
607
+ payload: updateParams,
608
+ method: 'updateDocumentPropsOffline',
609
+ user: _this.user,
610
+ messageId: ''
611
+ });
612
+ }
613
+ else {
614
+ log_collection_1.Logs.create({
615
+ _id: mongoose_1.Types.ObjectId().toHexString(),
616
+ date: new Date(),
617
+ type: 'info',
618
+ title: 'Update Document Props',
619
+ message: 'User: ' + _this.user + ', Collection Type: ' + collection + ', Id: ' + doc_id + ', ' + 'Version: ' + doc__v,
620
+ payload: 'Too Big',
621
+ method: 'updateDocumentPropsOffline',
622
+ user: _this.user,
623
+ messageId: ''
624
+ });
625
+ }
626
+ }
609
627
  }
610
628
  else {
611
- log_collection_1.Logs.create({
612
- _id: mongoose_1.Types.ObjectId().toHexString(),
613
- date: new Date(),
614
- type: 'info',
615
- title: 'Update Document Props',
616
- message: 'User: ' + _this.user + ', Collection Type: ' + collection + ', Id: ' + doc_id + ', ' + 'Version: ' + doc__v,
617
- payload: 'Too Big',
618
- method: 'updateDocumentPropsOffline',
619
- user: _this.user,
620
- messageId: ''
621
- });
629
+ console.log('invalid version - ' + collection, currDoc.__v, doc__v);
630
+ var newCurrDocId = currDoc._id;
631
+ if (f_oldDocument) {
632
+ var versionDoc_4 = common_1.deepCopy(currDoc);
633
+ versionDoc_4._id = {
634
+ _id: currDoc._id,
635
+ __v: currDoc.__v
636
+ };
637
+ var updatedDoc = common_1.getMongoMergeUpdatedDoc(modifiedDoc_2, currDoc, f_oldDocument);
638
+ updatedDoc._id = newCurrDocId;
639
+ updatedDoc.__v++;
640
+ index_1.ResolveIOServer.getMainDB().model(collection).replaceOne({ _id: newCurrDocId }, updatedDoc).exec().then(function (res) {
641
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).create(versionDoc_4).then(function (doc) {
642
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).deleteMany({
643
+ $and: [
644
+ { '_id._id': versionDoc_4._id._id },
645
+ { '_id.__v': { $lte: versionDoc_4._id.__v - 4 } }
646
+ ]
647
+ }).exec();
648
+ }, function (err) {
649
+ console.log(err);
650
+ });
651
+ resolve(res.nModified);
652
+ }, function (err) { return reject(err); });
653
+ }
654
+ else {
655
+ reject('OFFLINE - Invalid Version And Could Not Find History Props - DB: ' + currDoc.__v + ', Trying to update with :' + modifiedDoc_2.__v);
656
+ }
622
657
  }
623
658
  }
624
- }
625
- else {
626
- console.log('invalid version - ' + collection, currDoc.__v, doc__v);
627
- var newCurrDocId = currDoc._id;
628
- if (f_oldDocument) {
629
- var versionDoc_4 = common_1.deepCopy(currDoc);
630
- versionDoc_4._id = {
631
- _id: currDoc._id,
632
- __v: currDoc.__v
633
- };
634
- var updatedDoc = common_1.getMongoMergeUpdatedDoc(modifiedDoc_2, currDoc, f_oldDocument);
635
- updatedDoc._id = newCurrDocId;
636
- updatedDoc.__v++;
637
- index_1.ResolveIOServer.getMainDB().model(collection).replaceOne({ _id: newCurrDocId }, updatedDoc).exec().then(function (res) {
638
- index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).create(versionDoc_4).then(function (doc) {
639
- index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).deleteMany({
640
- $and: [
641
- { '_id._id': versionDoc_4._id._id },
642
- { '_id.__v': { $lte: versionDoc_4._id.__v - 4 } }
643
- ]
644
- }).exec();
645
- }, function (err) {
646
- console.log(err);
647
- });
648
- resolve(res.nModified);
649
- }, function (err) { return reject(err); });
650
- }
651
659
  else {
652
- reject('OFFLINE - Invalid Version And Could Not Find History Props - DB: ' + currDoc.__v + ', Trying to update with :' + modifiedDoc_2.__v);
660
+ reject(errDoc);
653
661
  }
654
- }
655
- }
656
- else {
657
- reject(errDoc);
658
- }
659
- });
660
- });
661
- }
662
- },
663
- // Removes 1 document (from _id)
664
- // @Inputs: collection, document id
665
- // @Outputs: res = 1 (deleted), res = 0 (not deleted), err = not deleted
666
- // Search for doc, if found then try and remove
667
- removeDocument: {
668
- check: new simpl_schema_1.default({
669
- collection: {
670
- type: String
662
+ });
663
+ });
664
+ }
671
665
  },
672
- doc_id: {
673
- type: String
674
- }
675
- }),
676
- function: function (collection, doc_id) {
677
- var _this = this;
678
- return new Promise(function (resolve, reject) {
679
- log_collection_1.Logs.create({
680
- _id: mongoose_1.Types.ObjectId().toHexString(),
681
- date: new Date(),
682
- type: 'info',
683
- title: 'Remove Document',
684
- message: 'User: ' + _this.user + ', Collection Type: ' + collection,
685
- payload: { _id: doc_id },
686
- method: 'removeDocument',
687
- user: _this.user,
688
- messageId: ''
689
- });
690
- index_1.ResolveIOServer.getMainDB().model(collection).deleteOne({ _id: doc_id }).exec(function (err, res) {
691
- if (err) {
692
- reject(err);
693
- }
694
- else {
695
- resolve(res.ok);
666
+ // Removes 1 document (from _id)
667
+ // @Inputs: collection, document id
668
+ // @Outputs: res = 1 (deleted), res = 0 (not deleted), err = not deleted
669
+ // Search for doc, if found then try and remove
670
+ removeDocument: {
671
+ check: new simpl_schema_1.default({
672
+ collection: {
673
+ type: String
674
+ },
675
+ doc_id: {
676
+ type: String
696
677
  }
697
- });
698
- });
699
- }
700
- },
701
- removeDocumentWithQuery: {
702
- check: new simpl_schema_1.default({
703
- collection: {
704
- type: String
678
+ }),
679
+ function: function (collection, doc_id) {
680
+ var _this = this;
681
+ return new Promise(function (resolve, reject) {
682
+ log_collection_1.Logs.create({
683
+ _id: mongoose_1.Types.ObjectId().toHexString(),
684
+ date: new Date(),
685
+ type: 'info',
686
+ title: 'Remove Document',
687
+ message: 'User: ' + _this.user + ', Collection Type: ' + collection,
688
+ payload: { _id: doc_id },
689
+ method: 'removeDocument',
690
+ user: _this.user,
691
+ messageId: ''
692
+ });
693
+ index_1.ResolveIOServer.getMainDB().model(collection).deleteOne({ _id: doc_id }).exec(function (err, res) {
694
+ if (err) {
695
+ reject(err);
696
+ }
697
+ else {
698
+ resolve(res.ok);
699
+ }
700
+ });
701
+ });
702
+ }
705
703
  },
706
- query: {
707
- type: Object,
708
- blackbox: true
709
- }
710
- }),
711
- function: function (collection, query) {
712
- var _this = this;
713
- return new Promise(function (resolve, reject) {
714
- log_collection_1.Logs.create({
715
- _id: mongoose_1.Types.ObjectId().toHexString(),
716
- date: new Date(),
717
- type: 'info',
718
- title: 'Remove Document (Query)',
719
- message: 'User: ' + _this.user + ', Collection Type: ' + collection,
720
- payload: { _id: query },
721
- method: 'removeDocumentWithQuery',
722
- user: _this.user,
723
- messageId: ''
724
- });
725
- index_1.ResolveIOServer.getMainDB().model(collection).deleteOne(query).exec(function (err, res) {
726
- if (err) {
727
- reject(err);
704
+ removeDocumentWithQuery: {
705
+ check: new simpl_schema_1.default({
706
+ collection: {
707
+ type: String
708
+ },
709
+ query: {
710
+ type: Object,
711
+ blackbox: true
728
712
  }
729
- else {
730
- resolve(res.ok);
731
- }
732
- });
733
- });
734
- }
713
+ }),
714
+ function: function (collection, query) {
715
+ var _this = this;
716
+ return new Promise(function (resolve, reject) {
717
+ log_collection_1.Logs.create({
718
+ _id: mongoose_1.Types.ObjectId().toHexString(),
719
+ date: new Date(),
720
+ type: 'info',
721
+ title: 'Remove Document (Query)',
722
+ message: 'User: ' + _this.user + ', Collection Type: ' + collection,
723
+ payload: { _id: query },
724
+ method: 'removeDocumentWithQuery',
725
+ user: _this.user,
726
+ messageId: ''
727
+ });
728
+ index_1.ResolveIOServer.getMainDB().model(collection).deleteOne(query).exec(function (err, res) {
729
+ if (err) {
730
+ reject(err);
731
+ }
732
+ else {
733
+ resolve(res.ok);
734
+ }
735
+ });
736
+ });
737
+ }
738
+ }
739
+ });
735
740
  }
736
- });
741
+ }, 1000);
737
742
  }
738
743
  exports.default = loadCollectionMethods;