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

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 (157) 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/pagination.model.d.ts +12 -0
  106. package/models/pagination.model.js +20 -1
  107. package/models/permission.model.d.ts +13 -0
  108. package/models/permission.model.js +2 -1
  109. package/models/queue-method.model.d.ts +10 -0
  110. package/models/queue-method.model.js +2 -1
  111. package/models/queue-response.model.d.ts +11 -0
  112. package/models/queue-response.model.js +2 -1
  113. package/models/select-data-label.model.d.ts +10 -0
  114. package/models/select-data-label.model.js +2 -1
  115. package/models/server-response.model.d.ts +6 -0
  116. package/models/server-response.model.js +2 -1
  117. package/models/subscription.model.d.ts +31 -0
  118. package/models/subscription.model.js +2 -1
  119. package/models/support-method.model.d.ts +10 -0
  120. package/models/support-method.model.js +2 -1
  121. package/models/support-ticket.model.d.ts +40 -0
  122. package/models/support-ticket.model.js +2 -1
  123. package/models/user.model.d.ts +15 -0
  124. package/models/user.model.js +2 -1
  125. package/package.json +5 -2
  126. package/publications/app-status.d.ts +3 -0
  127. package/publications/app-status.js +14 -1
  128. package/publications/app-version.d.ts +3 -0
  129. package/publications/app-version.js +14 -1
  130. package/publications/cron-jobs.d.ts +3 -0
  131. package/publications/cron-jobs.js +14 -1
  132. package/publications/files.d.ts +3 -0
  133. package/publications/files.js +71 -1
  134. package/publications/flags.d.ts +3 -0
  135. package/publications/flags.js +23 -1
  136. package/publications/logs.d.ts +3 -0
  137. package/publications/logs.js +26 -1
  138. package/publications/method-calls.d.ts +3 -0
  139. package/publications/method-calls.js +14 -1
  140. package/publications/method-responses.d.ts +3 -0
  141. package/publications/method-responses.js +14 -1
  142. package/publications/super-admin.d.ts +3 -0
  143. package/publications/super-admin.js +21 -1
  144. package/publications/support-tickets.d.ts +2 -0
  145. package/publications/support-tickets.js +118 -1
  146. package/server-app.d.ts +33 -0
  147. package/server-app.js +640 -1
  148. package/support-methods/aws.d.ts +3 -0
  149. package/support-methods/aws.js +276 -1
  150. package/support-methods/collections.d.ts +3 -0
  151. package/support-methods/collections.js +471 -1
  152. package/support-methods/counters.d.ts +3 -0
  153. package/support-methods/counters.js +189 -1
  154. package/support-methods/support.d.ts +3 -0
  155. package/support-methods/support.js +19 -1
  156. package/util/common.d.ts +15 -0
  157. package/util/common.js +334 -1
@@ -1 +1,738 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var mongoose_1=require("mongoose"),common_1=require("../util/common"),simpl_schema_1=require("simpl-schema"),pagination_model_1=require("../models/pagination.model"),log_collection_1=require("../collections/log.collection"),index_1=require("../index");function loadCollectionMethods(e){e.methods({findOne:{skipQueue:!0,check:new simpl_schema_1.default({collection:{type:String},query:{type:Object,blackbox:!0}}),function:function(e,o){return index_1.default.getMainDB().model(e).findOne(o).exec()}},find:{skipQueue:!0,check:new simpl_schema_1.default({collection:{type:String},query:{type:Object,blackbox:!0},sort:{type:Object,blackbox:!0,optional:!0}}),function:function(e,o,t){return void 0===t&&(t={}),index_1.default.getMainDB().model(e).find(o).sort(t).exec()}},findWithOptions:{skipQueue:!0,check:new simpl_schema_1.default({collection:{type:String},query:{type:Object,blackbox:!0},options:{type:pagination_model_1.PaginationOptionsSchema}}),function:function(e,o,t){return index_1.default.getMainDB().model(e).find(o).sort(t.sort).limit(t.limit).skip(t.skip).select(t.fields).exec()}},insertDocument:{check:new simpl_schema_1.default({collection:{type:String},document:{type:Object,blackbox:!0}}),function:function(i,d){var l=this;return new Promise(function(e,o){var t=null;try{index_1.default.getMainDB().model(i).simplschema.validate(d)}catch(e){t=e,l.callMethodInternal.call(l,"insertErrorLog","Schema Failed on insert - "+i,[t,d])}if(t)console.log(t),o("Schema Errors");else{common_1.getBinarySize(JSON.stringify(d))<2e5?log_collection_1.Logs.create({_id:mongoose_1.Types.ObjectId().toHexString(),date:new Date,type:"info",title:"Insert Document",message:"User: "+l.user+", Collection Type: "+i,payload:d,method:"insertDocument",user:l.user,messageId:""}):log_collection_1.Logs.create({_id:mongoose_1.Types.ObjectId().toHexString(),date:new Date,type:"info",title:"Insert Document",message:"User: "+l.user+", Collection Type: "+i,payload:"Too Big",method:"insertDocument",user:l.user,messageId:""});var n=mongoose_1.Types.ObjectId().toHexString();d._id=n,d.__v=0,index_1.default.getMainDB().model(i).create(d).then(function(){return e(n)},function(e){return o(e)})}})}},updateDocument:{check:new simpl_schema_1.default({collection:{type:String},f_document:{type:Object,blackbox:!0}}),function:function(c,_){var i=this;return new Promise(function(l,a){index_1.default.getMainDB().model(c).findOne({_id:_._id}).lean().exec(function(e,d){if(d)if(d.__v===_.__v){var o=common_1.deepCopy(d);o._id={_id:d._id,__v:d.__v};var t=null;try{index_1.default.getMainDB().model(c).simplschema.validate(_)}catch(e){t=e,i.callMethodInternal.call(i,"insertErrorLog","Schema Failed on updateDocument - "+c,[t,_])}if(t)console.log(t),a("Schema Errors");else{index_1.default.getMainDB().model(index_1.default.getMainDB().model(c).versionCollection).create(o).then(function(e){index_1.default.getMainDB().model(index_1.default.getMainDB().model(c).versionCollection).deleteMany({$and:[{"_id._id":d._id},{"_id.__v":{$lte:d.__v-4}}]}).exec()},function(e){console.log(e)});var n=common_1.getMongoUpdates(d,_,!0);n?index_1.default.getMainDB().model(c).updateOne({_id:_._id},n,{strict:!1}).exec().then(function(e){l(e.nModified)},function(e){return a(e)}):l(1),common_1.getBinarySize(JSON.stringify(_))<2e5?log_collection_1.Logs.create({_id:mongoose_1.Types.ObjectId().toHexString(),date:new Date,type:"info",title:"Update Document",message:"User: "+i.user+", Collection Type: "+c,payload:_,method:"updateDocument",user:i.user,messageId:""}):log_collection_1.Logs.create({_id:mongoose_1.Types.ObjectId().toHexString(),date:new Date,type:"info",title:"Update Document",message:"User: "+i.user+", Collection Type: "+c,payload:"Too Big",method:"updateDocument",user:i.user,messageId:""})}}else console.log("invalid version - "+c,d.__v,_.__v),index_1.default.getMainDB().model(index_1.default.getMainDB().model(c).versionCollection).findOne({$and:[{"_id._id":d._id},{"_id.__v":_.__v}]}).lean().exec(function(e,o){var t=d._id;if(o){var n=common_1.deepCopy(d);n._id={_id:d._id,__v:d.__v};var i=common_1.getMongoMergeUpdatedDoc(_,d,o);i._id=t,i.__v++,index_1.default.getMainDB().model(c).replaceOne({_id:t},i).exec().then(function(o){index_1.default.getMainDB().model(index_1.default.getMainDB().model(c).versionCollection).create(n).then(function(e){index_1.default.getMainDB().model(index_1.default.getMainDB().model(c).versionCollection).deleteMany({$and:[{"_id._id":n._id._id},{"_id.__v":{$lte:n._id.__v-4}}]}).exec(),l(o.nModified)})},function(e){return a(e)})}else a("Invalid Version And Could Not Find History - DB: "+d.__v+", Trying to update with :"+_.__v)});else a(e)})})}},updateDocumentProps:{check:new simpl_schema_1.default({collection:{type:String},doc_id:{type:String},updateParams:{type:Array},"updateParams.$":{type:Object,blackbox:!0},doc__v:{type:Number}}),function:function(_,i,r,s){var u=this;return new Promise(function(a,c){index_1.default.getMainDB().model(_).findOne({_id:i}).lean().exec(function(e,d){if(d){var l=common_1.deepCopy(d);if(r.forEach(function(e){l[e.prop]=e.data}),l.__v===s){var o=common_1.deepCopy(d);o._id={_id:d._id,__v:d.__v};var t=null;try{index_1.default.getMainDB().model(_).simplschema.validate(l)}catch(e){t=e,u.callMethodInternal.call(u,"insertErrorLog","Schema Failed on updateDocumentProps - "+_,[t,l])}if(t)console.log(t),c("Schema Errors");else{index_1.default.getMainDB().model(index_1.default.getMainDB().model(_).versionCollection).create(o).then(function(e){index_1.default.getMainDB().model(index_1.default.getMainDB().model(_).versionCollection).deleteMany({$and:[{"_id._id":d._id},{"_id.__v":{$lte:d.__v-4}}]}).exec()},function(e){console.log(e)});var n=common_1.getMongoUpdates(d,l,!0);n?index_1.default.getMainDB().model(_).updateOne({_id:l._id},n,{strict:!1}).exec().then(function(e){a(e.ok)},function(e){return c(e)}):a(1),common_1.getBinarySize(JSON.stringify(r))<2e5?log_collection_1.Logs.create({_id:mongoose_1.Types.ObjectId().toHexString(),date:new Date,type:"info",title:"Update Document Props",message:"User: "+u.user+", Collection Type: "+_+", Id: "+i+", Version: "+s,payload:r,method:"updateDocumentProps",user:u.user,messageId:""}):log_collection_1.Logs.create({_id:mongoose_1.Types.ObjectId().toHexString(),date:new Date,type:"info",title:"Update Document Props",message:"User: "+u.user+", Collection Type: "+_+", Id: "+i+", Version: "+s,payload:"Too Big",method:"updateDocumentProps",user:u.user,messageId:""})}}else console.log("invalid version - "+_,d.__v,s),index_1.default.getMainDB().model(index_1.default.getMainDB().model(_).versionCollection).findOne({$and:[{"_id._id":d._id},{"_id.__v":s}]}).lean().exec(function(e,o){var t=d._id;if(o){var n=common_1.deepCopy(d);n._id={_id:d._id,__v:d.__v};var i=common_1.getMongoMergeUpdatedDoc(l,d,o);i._id=t,i.__v++,index_1.default.getMainDB().model(_).replaceOne({_id:t},i).exec().then(function(e){index_1.default.getMainDB().model(index_1.default.getMainDB().model(_).versionCollection).create(n).then(function(e){index_1.default.getMainDB().model(index_1.default.getMainDB().model(_).versionCollection).deleteMany({$and:[{"_id._id":n._id._id},{"_id.__v":{$lte:n._id.__v-4}}]}).exec()},function(e){console.log(e)}),a(e.nModified)},function(e){return c(e)})}else c("Invalid Version And Could Not Find History Props - DB: "+d.__v+", Trying to update with :"+l.__v)})}else c(e)})})}},updateDocumentOffline:{check:new simpl_schema_1.default({collection:{type:String},f_document:{type:Object,blackbox:!0},f_oldDocument:{type:Object,blackbox:!0}}),function:function(r,s,u){var m=this;return new Promise(function(c,_){index_1.default.getMainDB().model(r).findOne({_id:s._id}).lean().exec(function(e,o){if(o)if(o.__v===s.__v){var t=common_1.deepCopy(o);t._id={_id:o._id,__v:o.__v};var n=null;try{index_1.default.getMainDB().model(r).simplschema.validate(s)}catch(e){n=e,m.callMethodInternal.call(m,"insertErrorLog","Schema Failed on updateDocument - "+r,[n,s])}if(n)console.log(n),_("Schema Errors");else{index_1.default.getMainDB().model(index_1.default.getMainDB().model(r).versionCollection).create(t).then(function(e){index_1.default.getMainDB().model(index_1.default.getMainDB().model(r).versionCollection).deleteMany({$and:[{"_id._id":o._id},{"_id.__v":{$lte:o.__v-4}}]}).exec()},function(e){console.log(e)});var i=common_1.getMongoUpdates(o,s,!0);i?index_1.default.getMainDB().model(r).updateOne({_id:s._id},i,{strict:!1}).exec().then(function(e){c(e.nModified)},function(e){return _(e)}):c(1),common_1.getBinarySize(JSON.stringify(s))<2e5?log_collection_1.Logs.create({_id:mongoose_1.Types.ObjectId().toHexString(),date:new Date,type:"info",title:"Update Document",message:"User: "+m.user+", Collection Type: "+r,payload:s,method:"updateDocumentOffline",user:m.user,messageId:""}):log_collection_1.Logs.create({_id:mongoose_1.Types.ObjectId().toHexString(),date:new Date,type:"info",title:"Update Document",message:"User: "+m.user+", Collection Type: "+r,payload:"Too Big",method:"updateDocumentOffline",user:m.user,messageId:""})}}else{console.log("invalid version - "+r,o.__v,s.__v);var d=o._id;if(u){var l=common_1.deepCopy(o);l._id={_id:o._id,__v:o.__v};var a=common_1.getMongoMergeUpdatedDoc(s,o,u);a._id=d,a.__v++,index_1.default.getMainDB().model(r).replaceOne({_id:d},a).exec().then(function(e){index_1.default.getMainDB().model(index_1.default.getMainDB().model(r).versionCollection).create(l).then(function(e){index_1.default.getMainDB().model(index_1.default.getMainDB().model(r).versionCollection).deleteMany({$and:[{"_id._id":l._id._id},{"_id.__v":{$lte:l._id.__v-4}}]}).exec()},function(e){console.log(e)}),c(e.nModified)},function(e){return _(e)})}else _("OFFLINE - Invalid Version And Could Not Find History - DB: "+o.__v+", Trying to update with :"+s.__v)}else _(e)})})}},updateDocumentPropsOffline:{check:new simpl_schema_1.default({collection:{type:String},doc_id:{type:String},updateParams:{type:Array},"updateParams.$":{type:Object,blackbox:!0},doc__v:{type:Number},f_oldDocument:{type:Object,blackbox:!0}}),function:function(s,u,m,g,f){var p=this;return new Promise(function(_,r){index_1.default.getMainDB().model(s).findOne({_id:u}).lean().exec(function(e,o){if(o){var t=common_1.deepCopy(o);if(m.forEach(function(e){t[e.prop]=e.data}),t.__v===g){var n=common_1.deepCopy(o);n._id={_id:o._id,__v:o.__v};var i=null;try{index_1.default.getMainDB().model(s).simplschema.validate(t)}catch(e){i=e,p.callMethodInternal.call(p,"insertErrorLog","Schema Failed on updateDocumentProps - "+s,[i,t])}if(i)console.log(i),r("Schema Errors");else{index_1.default.getMainDB().model(index_1.default.getMainDB().model(s).versionCollection).create(n).then(function(e){index_1.default.getMainDB().model(index_1.default.getMainDB().model(s).versionCollection).deleteMany({$and:[{"_id._id":o._id},{"_id.__v":{$lte:o.__v-4}}]}).exec()},function(e){console.log(e)});var d=common_1.getMongoUpdates(o,t,!0);d?index_1.default.getMainDB().model(s).updateOne({_id:t._id},d,{strict:!1}).exec().then(function(e){_(e.ok)},function(e){return r(e)}):_(1),common_1.getBinarySize(JSON.stringify(m))<2e5?log_collection_1.Logs.create({_id:mongoose_1.Types.ObjectId().toHexString(),date:new Date,type:"info",title:"Update Document Props",message:"User: "+p.user+", Collection Type: "+s+", Id: "+u+", Version: "+g,payload:m,method:"updateDocumentPropsOffline",user:p.user,messageId:""}):log_collection_1.Logs.create({_id:mongoose_1.Types.ObjectId().toHexString(),date:new Date,type:"info",title:"Update Document Props",message:"User: "+p.user+", Collection Type: "+s+", Id: "+u+", Version: "+g,payload:"Too Big",method:"updateDocumentPropsOffline",user:p.user,messageId:""})}}else{console.log("invalid version - "+s,o.__v,g);var l=o._id;if(f){var a=common_1.deepCopy(o);a._id={_id:o._id,__v:o.__v};var c=common_1.getMongoMergeUpdatedDoc(t,o,f);c._id=l,c.__v++,index_1.default.getMainDB().model(s).replaceOne({_id:l},c).exec().then(function(e){index_1.default.getMainDB().model(index_1.default.getMainDB().model(s).versionCollection).create(a).then(function(e){index_1.default.getMainDB().model(index_1.default.getMainDB().model(s).versionCollection).deleteMany({$and:[{"_id._id":a._id._id},{"_id.__v":{$lte:a._id.__v-4}}]}).exec()},function(e){console.log(e)}),_(e.nModified)},function(e){return r(e)})}else r("OFFLINE - Invalid Version And Could Not Find History Props - DB: "+o.__v+", Trying to update with :"+t.__v)}}else r(e)})})}},removeDocument:{check:new simpl_schema_1.default({collection:{type:String},doc_id:{type:String}}),function:function(e,o){var i=this;return new Promise(function(t,n){log_collection_1.Logs.create({_id:mongoose_1.Types.ObjectId().toHexString(),date:new Date,type:"info",title:"Remove Document",message:"User: "+i.user+", Collection Type: "+e,payload:{_id:o},method:"removeDocument",user:i.user,messageId:""}),index_1.default.getMainDB().model(e).deleteOne({_id:o}).exec(function(e,o){e?n(e):t(o.ok)})})}},removeDocumentWithQuery:{check:new simpl_schema_1.default({collection:{type:String},query:{type:Object,blackbox:!0}}),function:function(e,o){var i=this;return new Promise(function(t,n){log_collection_1.Logs.create({_id:mongoose_1.Types.ObjectId().toHexString(),date:new Date,type:"info",title:"Remove Document (Query)",message:"User: "+i.user+", Collection Type: "+e,payload:{_id:o},method:"removeDocumentWithQuery",user:i.user,messageId:""}),index_1.default.getMainDB().model(e).deleteOne(o).exec(function(e,o){e?n(e):t(o.ok)})})}}})}exports.loadCollectionMethods=loadCollectionMethods;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var mongoose_1 = require("mongoose");
4
+ var common_1 = require("../util/common");
5
+ var simpl_schema_1 = require("simpl-schema");
6
+ var pagination_model_1 = require("../models/pagination.model");
7
+ var log_collection_1 = require("../collections/log.collection");
8
+ var index_1 = require("../index");
9
+ function loadCollectionMethods(methodManager) {
10
+ methodManager.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');
93
+ }
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
+ });
107
+ }
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
+ });
120
+ }
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); });
125
+ }
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
+ },
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');
165
+ }
166
+ 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);
176
+ });
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
+ }
213
+ }
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 = {
226
+ _id: currDoc._id,
227
+ __v: currDoc.__v
228
+ };
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) {
234
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).deleteMany({
235
+ $and: [
236
+ { '_id._id': versionDoc_1._id._id },
237
+ { '_id.__v': { $lte: versionDoc_1._id.__v - 4 } }
238
+ ]
239
+ }).exec();
240
+ resolve(res.nModified);
241
+ });
242
+ }, function (err) { return reject(err); });
243
+ }
244
+ 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({
310
+ $and: [
311
+ { '_id._id': currDoc._id },
312
+ { '_id.__v': { $lte: currDoc.__v - 4 } }
313
+ ]
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: ''
351
+ });
352
+ }
353
+ }
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 = {
367
+ _id: currDoc._id,
368
+ __v: currDoc.__v
369
+ };
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) {
375
+ index_1.ResolveIOServer.getMainDB().model(index_1.ResolveIOServer.getMainDB().model(collection)['versionCollection']).deleteMany({
376
+ $and: [
377
+ { '_id._id': versionDoc_2._id._id },
378
+ { '_id.__v': { $lte: versionDoc_2._id.__v - 4 } }
379
+ ]
380
+ }).exec();
381
+ }, function (err) {
382
+ console.log(err);
383
+ });
384
+ resolve(res.nModified);
385
+ }, function (err) { return reject(err); });
386
+ }
387
+ 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({
440
+ $and: [
441
+ { '_id._id': currDoc._id },
442
+ { '_id.__v': { $lte: currDoc.__v - 4 } }
443
+ ]
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: ''
481
+ });
482
+ }
483
+ }
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
+ else {
512
+ reject('OFFLINE - Invalid Version And Could Not Find History - DB: ' + currDoc.__v + ', Trying to update with :' + f_document['__v']);
513
+ }
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
+ });
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); });
593
+ }
594
+ else {
595
+ resolve(1);
596
+ }
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
+ });
609
+ }
610
+ 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
+ });
622
+ }
623
+ }
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
+ else {
652
+ reject('OFFLINE - Invalid Version And Could Not Find History Props - DB: ' + currDoc.__v + ', Trying to update with :' + modifiedDoc_2.__v);
653
+ }
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
671
+ },
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);
696
+ }
697
+ });
698
+ });
699
+ }
700
+ },
701
+ removeDocumentWithQuery: {
702
+ check: new simpl_schema_1.default({
703
+ collection: {
704
+ type: String
705
+ },
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);
728
+ }
729
+ else {
730
+ resolve(res.ok);
731
+ }
732
+ });
733
+ });
734
+ }
735
+ }
736
+ });
737
+ }
738
+ exports.loadCollectionMethods = loadCollectionMethods;