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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. package/collections/active-subscription.collection.d.ts +8 -0
  2. package/collections/active-subscription.collection.js +68 -1
  3. package/collections/app-status.collection.d.ts +8 -0
  4. package/collections/app-status.collection.js +43 -1
  5. package/collections/app-version.collection.d.ts +8 -0
  6. package/collections/app-version.collection.js +56 -1
  7. package/collections/counter.collection.d.ts +10 -0
  8. package/collections/counter.collection.js +73 -1
  9. package/collections/cron-job-history.collection.d.ts +8 -0
  10. package/collections/cron-job-history.collection.js +86 -1
  11. package/collections/cron-job.collection.d.ts +8 -0
  12. package/collections/cron-job.collection.js +89 -1
  13. package/collections/file.collection.d.ts +10 -0
  14. package/collections/file.collection.js +73 -1
  15. package/collections/flag.collection.d.ts +8 -0
  16. package/collections/flag.collection.js +47 -1
  17. package/collections/log.collection.d.ts +8 -0
  18. package/collections/log.collection.js +64 -1
  19. package/collections/logged-in-users.collection.d.ts +8 -0
  20. package/collections/logged-in-users.collection.js +70 -1
  21. package/collections/method-call.collection.d.ts +7 -0
  22. package/collections/method-call.collection.js +44 -1
  23. package/collections/method-response.collection.d.ts +7 -0
  24. package/collections/method-response.collection.js +48 -1
  25. package/collections/queue-flag.collection.d.ts +8 -0
  26. package/collections/queue-flag.collection.js +41 -1
  27. package/collections/queue-method.collection.d.ts +8 -0
  28. package/collections/queue-method.collection.js +116 -1
  29. package/collections/queue-response.collection.d.ts +8 -0
  30. package/collections/queue-response.collection.js +78 -1
  31. package/collections/support-ticket.collection.d.ts +8 -0
  32. package/collections/support-ticket.collection.js +146 -1
  33. package/collections/user.collection.d.ts +9 -0
  34. package/collections/user.collection.js +108 -1
  35. package/cron/cron.d.ts +6 -0
  36. package/cron/cron.js +160 -1
  37. package/fixtures/cron-jobs.d.ts +2 -0
  38. package/fixtures/cron-jobs.js +72 -1
  39. package/fixtures/init.d.ts +2 -0
  40. package/fixtures/init.js +73 -1
  41. package/http/auth.d.ts +3 -0
  42. package/http/auth.js +472 -1
  43. package/http/health.d.ts +2 -0
  44. package/http/health.js +8 -1
  45. package/index.d.ts +15 -0
  46. package/index.js +123 -1
  47. package/managers/cron.manager.d.ts +17 -0
  48. package/managers/cron.manager.js +193 -1
  49. package/managers/method.manager.d.ts +27 -0
  50. package/managers/method.manager.js +513 -1
  51. package/managers/queue-method.manager.d.ts +14 -0
  52. package/managers/queue-method.manager.js +239 -1
  53. package/managers/subscription.manager.d.ts +27 -0
  54. package/managers/subscription.manager.js +700 -1
  55. package/managers/support.manager.d.ts +18 -0
  56. package/managers/support.manager.js +208 -1
  57. package/methods/accounts.d.ts +3 -0
  58. package/methods/accounts.js +253 -1
  59. package/methods/aws.d.ts +3 -0
  60. package/methods/aws.js +323 -1
  61. package/methods/cloudconvert.d.ts +3 -0
  62. package/methods/cloudconvert.js +37 -1
  63. package/methods/collections.d.ts +3 -0
  64. package/methods/collections.js +738 -1
  65. package/methods/counters.d.ts +3 -0
  66. package/methods/counters.js +404 -1
  67. package/methods/cron-jobs.d.ts +3 -0
  68. package/methods/cron-jobs.js +63 -1
  69. package/methods/flags.d.ts +3 -0
  70. package/methods/flags.js +6 -1
  71. package/methods/logs.d.ts +3 -0
  72. package/methods/logs.js +29 -1
  73. package/methods/pdf.d.ts +3 -0
  74. package/methods/pdf.js +303 -1
  75. package/models/active-subscription.model.d.ts +14 -0
  76. package/models/active-subscription.model.js +2 -1
  77. package/models/app-status.model.d.ts +5 -0
  78. package/models/app-status.model.js +2 -1
  79. package/models/app-version.model.d.ts +5 -0
  80. package/models/app-version.model.js +2 -1
  81. package/models/collection-document.model.d.ts +7 -0
  82. package/models/collection-document.model.js +2 -1
  83. package/models/counter.model.d.ts +6 -0
  84. package/models/counter.model.js +2 -1
  85. package/models/cron-job-history.model.d.ts +13 -0
  86. package/models/cron-job-history.model.js +2 -1
  87. package/models/cron-job.model.d.ts +14 -0
  88. package/models/cron-job.model.js +2 -1
  89. package/models/dialog.model.d.ts +24 -0
  90. package/models/dialog.model.js +2 -1
  91. package/models/file.model.d.ts +10 -0
  92. package/models/file.model.js +2 -1
  93. package/models/flag.model.d.ts +6 -0
  94. package/models/flag.model.js +2 -1
  95. package/models/log.model.d.ts +12 -0
  96. package/models/log.model.js +2 -1
  97. package/models/logged-in-users.model.d.ts +10 -0
  98. package/models/logged-in-users.model.js +2 -1
  99. package/models/method-call.model.d.ts +8 -0
  100. package/models/method-call.model.js +2 -1
  101. package/models/method-response.model.d.ts +9 -0
  102. package/models/method-response.model.js +2 -1
  103. package/models/method.model.d.ts +11 -0
  104. package/models/method.model.js +2 -1
  105. package/models/notification.model.d.ts +5 -0
  106. package/models/notification.model.js +2 -0
  107. package/models/pagination.model.d.ts +12 -0
  108. package/models/pagination.model.js +20 -1
  109. package/models/permission.model.d.ts +13 -0
  110. package/models/permission.model.js +2 -1
  111. package/models/queue-method.model.d.ts +10 -0
  112. package/models/queue-method.model.js +2 -1
  113. package/models/queue-response.model.d.ts +11 -0
  114. package/models/queue-response.model.js +2 -1
  115. package/models/select-data-label.model.d.ts +10 -0
  116. package/models/select-data-label.model.js +2 -1
  117. package/models/server-response.model.d.ts +6 -0
  118. package/models/server-response.model.js +2 -1
  119. package/models/subscription.model.d.ts +31 -0
  120. package/models/subscription.model.js +2 -1
  121. package/models/support-method.model.d.ts +10 -0
  122. package/models/support-method.model.js +2 -1
  123. package/models/support-ticket.model.d.ts +40 -0
  124. package/models/support-ticket.model.js +2 -1
  125. package/models/user.model.d.ts +15 -0
  126. package/models/user.model.js +2 -1
  127. package/package.json +5 -2
  128. package/publications/app-status.d.ts +3 -0
  129. package/publications/app-status.js +14 -1
  130. package/publications/app-version.d.ts +3 -0
  131. package/publications/app-version.js +14 -1
  132. package/publications/cron-jobs.d.ts +3 -0
  133. package/publications/cron-jobs.js +14 -1
  134. package/publications/files.d.ts +3 -0
  135. package/publications/files.js +71 -1
  136. package/publications/flags.d.ts +3 -0
  137. package/publications/flags.js +23 -1
  138. package/publications/logs.d.ts +3 -0
  139. package/publications/logs.js +26 -1
  140. package/publications/method-calls.d.ts +3 -0
  141. package/publications/method-calls.js +14 -1
  142. package/publications/method-responses.d.ts +3 -0
  143. package/publications/method-responses.js +14 -1
  144. package/publications/super-admin.d.ts +3 -0
  145. package/publications/super-admin.js +21 -1
  146. package/publications/support-tickets.d.ts +2 -0
  147. package/publications/support-tickets.js +118 -1
  148. package/server-app.d.ts +33 -0
  149. package/server-app.js +640 -1
  150. package/support-methods/aws.d.ts +3 -0
  151. package/support-methods/aws.js +276 -1
  152. package/support-methods/collections.d.ts +3 -0
  153. package/support-methods/collections.js +471 -1
  154. package/support-methods/counters.d.ts +3 -0
  155. package/support-methods/counters.js +189 -1
  156. package/support-methods/support.d.ts +3 -0
  157. package/support-methods/support.js +19 -1
  158. package/util/common.d.ts +15 -0
  159. package/util/common.js +334 -1
@@ -1 +1,513 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var collections_1=require("../methods/collections"),logs_1=require("../methods/logs"),counters_1=require("../methods/counters"),pdf_1=require("../methods/pdf"),aws_1=require("../methods/aws"),cloudconvert_1=require("../methods/cloudconvert"),path=require("path"),fs=require("fs"),nodemailer=require("nodemailer"),aws=require("aws-sdk"),accounts_1=require("../methods/accounts"),init_1=require("../fixtures/init"),cron_jobs_1=require("../fixtures/cron-jobs"),cron_jobs_2=require("../methods/cron-jobs"),flags_1=require("../methods/flags"),method_response_collection_1=require("../collections/method-response.collection"),mongoose_1=require("mongoose"),common_1=require("../util/common"),log_collection_1=require("../collections/log.collection"),MethodManager=function(){function e(e,o){var r=this;this._methods={},this._sendQueue=[],this._runningQueue=!1,this._mainServer=e,this.serverConfig=o,init_1.loadServerInit(),cron_jobs_1.loadServerCronJobs(),accounts_1.loadAccountMethods(this),aws_1.loadAWSMethods(this),cloudconvert_1.loadCloudConvertMethods(this),collections_1.loadCollectionMethods(this),counters_1.loadCounterMethods(this),logs_1.loadLogMethods(this),pdf_1.loadPDFMethods(this),cron_jobs_2.loadCronJobMethods(this),flags_1.loadFlagMethods(this),this._mailer=nodemailer.createTransport({SES:new aws.SES({apiVersion:"2010-12-01",accessKeyId:o.AWSACCESSKEYID,secretAccessKey:o.AWSSECRETACCESSKEY,region:o.AWSSES_REGION})}),this._aws=new aws.S3({accessKeyId:o.AWSACCESSKEYID,secretAccessKey:o.AWSSECRETACCESSKEY,region:o.AWSREGION}),setInterval(function(){if(!r._runningQueue&&r._sendQueue.length){r._runningQueue=!0;for(var e=function(e){var o=r._mainServer.getWS(r._sendQueue[e].id_ws);if(o&&o.readyState===o.OPEN){if(o.bufferedAmount<20480){var t=r._sendQueue.pop();o.send(JSON.stringify(t.data),function(e){e&&(console.log("Error on WS: ",e),r._mainServer.unsubscribeWS(o))}),"countQuery"!==t.method&&"countCollectionWithQuery"!==t.method&&"find"!==t.method&&"findOne"!==t.method&&"findWithOptions"!==t.method&&"setWSAppVersion"!==t.method&&"uploadFileAndSave"!==t.method&&(common_1.getBinarySize(JSON.stringify(t.methodData))<2e5&&common_1.getBinarySize(JSON.stringify(t.data))<2e5?log_collection_1.Logs.create({_id:mongoose_1.Types.ObjectId().toHexString(),date:new Date,type:"client-response",title:"Client Response",message:"User: "+o.user+", MessageId: "+t.data.messageId+", Method: "+t.method,payload:[JSON.stringify(t.methodData),t.data],method:t.method,user:o.user,messageId:t.data.messageId}):log_collection_1.Logs.create({_id:mongoose_1.Types.ObjectId().toHexString(),date:new Date,type:"client-response",title:"Client Response",message:"User: "+o.user+", MessageId: "+t.data.messageId+", Method: "+t.method,payload:"Too Big",method:t.method,user:o.user,messageId:t.data.messageId})),method_response_collection_1.MethodResponses.create({_id:mongoose_1.Types.ObjectId().toHexString(),__v:0,id_user:o.id_user,message_id:t.data.messageId,data:JSON.stringify(t.methodData),response:t.data,method:t.method})}}else r._sendQueue.splice(e,1)},o=r._sendQueue.length-1;0<=o;o--)e(o);r._runningQueue=!1}},50)}return e.prototype.getMainServer=function(){return this._mainServer},e.prototype.methods=function(e){this._methods=Object.assign(this._methods,e)},e.prototype.callMethodCron=function(o){for(var e,t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];var s=this;if(!s._methods[o])return console.log("No Method: "+o),void s.sendEmail("dev@resolveio.com","SERVER - Error Detected - "+s.serverConfig.CLIENT_NAME,"No Method: "+o);if((1<t.length||t[0])&&!s._methods[o].skipValidation){if(!s._methods[o].check)return console.error("No Check Function For Method "+o),void s.sendEmail("dev@resolveio.com","SERVER - Error Detected - "+s.serverConfig.CLIENT_NAME,"No Check Function For Method "+o);if(!s._methods[o].check._schema)return console.error("No Check Schema For Method "+o),void s.sendEmail("dev@resolveio.com","SERVER - Error Detected - "+s.serverConfig.CLIENT_NAME,"No Check Function For Method "+o);for(var n={},d=Object.keys(s._methods[o].check._schema).filter(function(e){return!e.includes(".")}),i=0;i<t.length;i++)n[d[i]]=t[i];try{s._methods[o].check.validate(n)}catch(e){if(e)return console.error("Error in Method Check ("+o+")",e),void s.sendEmail("dev@resolveio.com","SERVER - Error Detected - "+s.serverConfig.CLIENT_NAME,"Match Error On Method "+o+"\n\nData Being Checked\n"+JSON.stringify(n,null,2)+"\n\nErrors\n"+JSON.stringify(e,null,2))}}(e=s._methods[o].function).call.apply(e,[Object.assign({},s,{id_user:"",user:"",id_ws:""})].concat(t))},e.prototype.callMethodQueue=function(t){var e,r=this,s=common_1.deepCopy(t),o=this;if(o._methods[t.method]){if((1<t.method_data.length||t.method_data[0])&&!o._methods[t.method].skipValidation){if(!o._methods[t.method].check){console.error("No Check Function For Method "+t.method),o.sendEmail("dev@resolveio.com","SERVER - Error Detected - "+o.serverConfig.CLIENT_NAME,"No Check Function For Method "+t.method);a={messageId:t.messageId,hasError:!0,data:"Internal Error"};return void this._mainServer.getQueueManager().addMethodResponse(s,a)}if(!o._methods[t.method].check._schema){console.error("No Check Schema For Method "+t.method),o.sendEmail("dev@resolveio.com","SERVER - Error Detected - "+o.serverConfig.CLIENT_NAME,"No Check Function For Method "+t.method);a={messageId:t.messageId,hasError:!0,data:"Internal Error"};return void this._mainServer.getQueueManager().addMethodResponse(s,a)}for(var n={},d=Object.keys(o._methods[t.method].check._schema).filter(function(e){return!e.includes(".")}),i=0;i<t.method_data.length;i++)n[d[i]]=t.method_data[i];try{o._methods[t.method].check.validate(n)}catch(e){if(e){console.error("Error in Method Check ("+t.method+")",e),o.sendEmail("dev@resolveio.com","SERVER - Error Detected - "+o.serverConfig.CLIENT_NAME,"Match Error On Method "+t.method+"\n\nData Being Checked\n"+JSON.stringify(n,null,2)+"\n\nErrors\n"+JSON.stringify(e,null,2));a={messageId:t.messageId,hasError:!0,data:"Internal Error"};return void this._mainServer.getQueueManager().addMethodResponse(s,a)}}}(e=o._methods[t.method].function).call.apply(e,[Object.assign({},o,{id_user:t.id_user,user:t.user,id_ws:t.id_ws})].concat(t.method_data)).then(function(e){var o={messageId:t.messageId,hasError:!1,data:e};r._mainServer.getQueueManager().addMethodResponse(s,o)},function(e){var o={messageId:t.messageId,hasError:!0,data:e};r._mainServer.getQueueManager().addMethodResponse(s,o)})}else{console.log("No Method: "+t.method),o.sendEmail("dev@resolveio.com","SERVER - Error Detected - "+o.serverConfig.CLIENT_NAME,"No Method: "+t.method);var a={messageId:t.messageId,hasError:!0,data:"Internal Error"};this._mainServer.getQueueManager().addMethodResponse(s,a)}},e.prototype.callMethod=function(t,r,s){for(var e,n=this,d=[],o=3;o<arguments.length;o++)d[o-3]=arguments[o];var i=this;if(i._methods[s]){if((1<d.length||d[0])&&!i._methods[s].skipValidation){if(!i._methods[s].check){console.error("No Check Function For Method "+s),i.sendEmail("dev@resolveio.com","SERVER - Error Detected - "+i.serverConfig.CLIENT_NAME,"No Check Function For Method "+s);l={messageId:r,hasError:!0,data:"Internal Error"};return void this.sendWS(t,s,d,l)}if(!i._methods[s].check._schema){console.error("No Check Schema For Method "+s),i.sendEmail("dev@resolveio.com","SERVER - Error Detected - "+i.serverConfig.CLIENT_NAME,"No Check Function For Method "+s);l={messageId:r,hasError:!0,data:"Internal Error"};return void this.sendWS(t,s,d,l)}for(var a={},c=Object.keys(i._methods[s].check._schema).filter(function(e){return!e.includes(".")}),h=0;h<d.length;h++)a[c[h]]=d[h];try{i._methods[s].check.validate(a)}catch(e){if(e){console.error("Error in Method Check ("+s+")",e),i.sendEmail("dev@resolveio.com","SERVER - Error Detected - "+i.serverConfig.CLIENT_NAME,"Match Error On Method "+s+"\n\nData Being Checked\n"+JSON.stringify(a,null,2)+"\n\nErrors\n"+JSON.stringify(e,null,2));l={messageId:r,hasError:!0,data:"Internal Error"};return void this.sendWS(t,s,d,l)}}}(e=i._methods[s].function).call.apply(e,[Object.assign({},i,{id_user:t.id_user,user:t.user,id_ws:t.id_socket})].concat(d)).then(function(e){var o={messageId:r,hasError:!1,data:e};n.sendWS(t,s,d,o)},function(e){var o={messageId:r,hasError:!0,data:e};n.sendWS(t,s,d,o)})}else{console.log("No Method: "+s),i.sendEmail("dev@resolveio.com","SERVER - Error Detected - "+i.serverConfig.CLIENT_NAME,"No Method: "+s);var l={messageId:r,hasError:!0,data:"Internal Error"};this.sendWS(t,s,d,l)}},e.prototype.callMethodInternal=function(e){for(var o,t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];var s=this;if(s._methods[e]){if((1<t.length||t[0]&&"function"!=typeof t[0])&&!s._methods[e].skipValidation){if(!s._methods[e].check)return void console.error("No Check Function For Method "+e);if(!s._methods[e].check._schema)return void console.error("No Check Schema For Method "+e)}var n=(o=s._methods[e].function).call.apply(o,[s].concat(t));return t[t.length-1]&&"function"==typeof t[t.length-1]&&n.then(function(e){return t[t.length-1](null,e)},function(e){return t[t.length-1](e,null)}),n}console.log("No Method: "+e)},e.prototype.sendWS=function(e,o,t,r){this._sendQueue.splice(0,0,{id_ws:e.id_socket,method:o,methodData:t,data:r})},e.prototype.sendEmail=function(e,o,t,r,s){this._mailer.sendMail({replyTo:void 0,from:this.serverConfig.MAIL_FROM,to:e,subject:o,text:t,html:r,attachments:s},function(e,o){e&&console.log(e)})},e.prototype.getAWS=function(){return this._aws},e.prototype.readFile=function(e){return new Promise(function(t,r){fs.readFile(path.join(__dirname,"../private/"+e),"utf-8",function(e,o){e?r(e):t(o)})})},e.prototype.readImage=function(e){return new Promise(function(t,r){fs.readFile(path.join(__dirname,"../private/"+e),"base64",function(e,o){e?r(e):t(o)})})},e}();exports.MethodManager=MethodManager;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var collections_1 = require("../methods/collections");
4
+ var logs_1 = require("../methods/logs");
5
+ var counters_1 = require("../methods/counters");
6
+ var pdf_1 = require("../methods/pdf");
7
+ var aws_1 = require("../methods/aws");
8
+ var cloudconvert_1 = require("../methods/cloudconvert");
9
+ var path = require("path");
10
+ var fs = require("fs");
11
+ var nodemailer = require("nodemailer");
12
+ var aws = require("aws-sdk");
13
+ var accounts_1 = require("../methods/accounts");
14
+ var init_1 = require("../fixtures/init");
15
+ var cron_jobs_1 = require("../fixtures/cron-jobs");
16
+ var cron_jobs_2 = require("../methods/cron-jobs");
17
+ var flags_1 = require("../methods/flags");
18
+ var method_response_collection_1 = require("../collections/method-response.collection");
19
+ var mongoose_1 = require("mongoose");
20
+ var common_1 = require("../util/common");
21
+ var log_collection_1 = require("../collections/log.collection");
22
+ var MethodManager = /** @class */ (function () {
23
+ function MethodManager(mainServer, serverConfig) {
24
+ var _this = this;
25
+ this._methods = {};
26
+ this._sendQueue = [];
27
+ this._runningQueue = false;
28
+ this._mainServer = mainServer;
29
+ this.serverConfig = serverConfig;
30
+ // Fixtures
31
+ init_1.loadServerInit();
32
+ cron_jobs_1.loadServerCronJobs();
33
+ // Methods
34
+ accounts_1.loadAccountMethods(this);
35
+ aws_1.loadAWSMethods(this);
36
+ cloudconvert_1.loadCloudConvertMethods(this);
37
+ collections_1.loadCollectionMethods(this);
38
+ counters_1.loadCounterMethods(this);
39
+ logs_1.loadLogMethods(this);
40
+ pdf_1.loadPDFMethods(this);
41
+ cron_jobs_2.loadCronJobMethods(this);
42
+ flags_1.loadFlagMethods(this);
43
+ this._mailer = nodemailer.createTransport({
44
+ SES: new aws.SES({
45
+ apiVersion: '2010-12-01',
46
+ accessKeyId: serverConfig.AWSACCESSKEYID,
47
+ secretAccessKey: serverConfig.AWSSECRETACCESSKEY,
48
+ region: serverConfig.AWSSES_REGION
49
+ })
50
+ });
51
+ // this._imap = new imap({
52
+ // user: this.serverConfig['ORDERS_EMAIL'],
53
+ // password: this.serverConfig['ORDERS_PASSWORD'],
54
+ // host: this.serverConfig['ORDERS_HOST'],
55
+ // port: this.serverConfig['ORDERS_PORT'],
56
+ // tls: this.serverConfig['ORDERS_TLS']
57
+ // });
58
+ // let that = this;
59
+ // let chemicalNames = await Chemicals.find({}).select('description').exec();
60
+ // let jobNames = await Jobs.find({status: 'Active'}).select('well').exec();
61
+ // console.log(jobNames.map(a => a.well));
62
+ // this._imap.once('ready', () => {
63
+ // this._imap.openBox('INBOX', true, function(err, box) {
64
+ // if (err) {
65
+ // throw err;
66
+ // }
67
+ // that._imap.search([ 'NEW' ], function(err, results) {
68
+ // console.log(err, results);
69
+ // if (err || !results.length) {
70
+ // return that._imap.end();
71
+ // }
72
+ // let f = that._imap.fetch(results[0], { bodies: ['HEADER.FIELDS (FROM)', 'TEXT'] });
73
+ // f.on('message', function(msg, seqno) {
74
+ // // console.log('Message #%d', seqno);
75
+ // let prefix = '(#' + seqno + ') ';
76
+ // msg.on('body', function(stream, info) {
77
+ // let buffer = '', count = 0;
78
+ // stream.on('data', function(chunk) {
79
+ // count += chunk.length;
80
+ // buffer += chunk.toString('utf8');
81
+ // });
82
+ // stream.once('end', function() {
83
+ // if (info.which === 'TEXT') {
84
+ // // console.log(buffer);
85
+ // // console.log(typeof(buffer));
86
+ // let lines = buffer.split('\r\n');
87
+ // // console.log(lines);
88
+ // // let i = 0;
89
+ // // lines.forEach(line => {
90
+ // // let date: Date;
91
+ // // let dateString;
92
+ // // if (line.includes('From')) {
93
+ // // console.log(line);
94
+ // // }
95
+ // // if (!line.includes('class=3D') && !line.includes('style=3D')) {
96
+ // // if (line.includes('Well') || line.includes('Well Name') || line.includes('Job') || line.includes('Pad')) {
97
+ // // let best = stringSimilarity.findBestMatch(line, jobNames.map(a => a.well)).bestMatch;
98
+ // // if (best && best.rating > 0.5) {
99
+ // // console.log('Well', line);
100
+ // // }
101
+ // // }
102
+ // // if (line.includes('Product') || line.includes('Chemical')) {
103
+ // // let best = stringSimilarity.findBestMatch(line, chemicalNames.map(a => a.description)).bestMatch;
104
+ // // if (best && best.rating > 0.5) {
105
+ // // console.log('Chemical', line);
106
+ // // }
107
+ // // }
108
+ // // if (line.includes('Quantity') || line.includes('Qty') || line.includes('Amount')) {
109
+ // // console.log('Qty', line);
110
+ // // }
111
+ // // if (line.includes('Date')) {
112
+ // // console.log('Date', line);
113
+ // // console.log(moment(Date.parse(line)));
114
+ // // date = moment(Date.parse(line)).toDate();
115
+ // // dateString = line;
116
+ // // }
117
+ // // if (line.includes('Time')) {
118
+ // // console.log('Time', line);
119
+ // // console.log(moment(Date.parse(line)));
120
+ // // console.log(moment(date.setTime(Date.parse(line))));
121
+ // // }
122
+ // // }
123
+ // // });
124
+ // }
125
+ // });
126
+ // });
127
+ // msg.once('attributes', function(attrs) {
128
+ // console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8));
129
+ // });
130
+ // msg.once('end', function() {
131
+ // console.log(prefix + 'Finished');
132
+ // });
133
+ // });
134
+ // f.once('error', function(error) {
135
+ // console.log('Fetch error: ' + error);
136
+ // });
137
+ // f.once('end', function() {
138
+ // console.log('Done fetching all messages!');
139
+ // that._imap.end();
140
+ // });
141
+ // });
142
+ // });
143
+ // });
144
+ // this._imap.connect();
145
+ this._aws = new aws.S3({
146
+ accessKeyId: serverConfig.AWSACCESSKEYID,
147
+ secretAccessKey: serverConfig.AWSSECRETACCESSKEY,
148
+ region: serverConfig.AWSREGION
149
+ });
150
+ setInterval(function () {
151
+ if (!_this._runningQueue) {
152
+ if (_this._sendQueue.length) {
153
+ _this._runningQueue = true;
154
+ var _loop_1 = function (i) {
155
+ var ws = _this._mainServer.getWS(_this._sendQueue[i].id_ws);
156
+ if (ws && ws.readyState === ws.OPEN) {
157
+ if (ws.bufferedAmount < 20480) {
158
+ var sendItem = _this._sendQueue.pop();
159
+ ws.send(JSON.stringify(sendItem.data), function (error) {
160
+ if (error) {
161
+ console.log('Error on WS: ', error);
162
+ _this._mainServer.unsubscribeWS(ws);
163
+ }
164
+ });
165
+ if (sendItem.method !== 'countQuery' && sendItem.method !== 'countCollectionWithQuery' && sendItem.method !== 'find' && sendItem.method !== 'findOne' && sendItem.method !== 'findWithOptions' && sendItem.method !== 'setWSAppVersion' && sendItem.method !== 'uploadFileAndSave') {
166
+ if (common_1.getBinarySize(JSON.stringify(sendItem.methodData)) < 200000 && common_1.getBinarySize(JSON.stringify(sendItem.data)) < 200000) {
167
+ log_collection_1.Logs.create({
168
+ _id: mongoose_1.Types.ObjectId().toHexString(),
169
+ date: new Date(),
170
+ type: 'client-response',
171
+ title: 'Client Response',
172
+ message: 'User: ' + ws['user'] + ', MessageId: ' + sendItem.data.messageId + ', Method: ' + sendItem.method,
173
+ payload: [JSON.stringify(sendItem.methodData), sendItem.data],
174
+ method: sendItem.method,
175
+ user: ws['user'],
176
+ messageId: sendItem.data.messageId
177
+ });
178
+ }
179
+ else {
180
+ log_collection_1.Logs.create({
181
+ _id: mongoose_1.Types.ObjectId().toHexString(),
182
+ date: new Date(),
183
+ type: 'client-response',
184
+ title: 'Client Response',
185
+ message: 'User: ' + ws['user'] + ', MessageId: ' + sendItem.data.messageId + ', Method: ' + sendItem.method,
186
+ payload: 'Too Big',
187
+ method: sendItem.method,
188
+ user: ws['user'],
189
+ messageId: sendItem.data.messageId
190
+ });
191
+ }
192
+ }
193
+ method_response_collection_1.MethodResponses.create({
194
+ _id: mongoose_1.Types.ObjectId().toHexString(),
195
+ __v: 0,
196
+ id_user: ws['id_user'],
197
+ message_id: sendItem.data.messageId,
198
+ data: JSON.stringify(sendItem.methodData),
199
+ response: sendItem.data,
200
+ method: sendItem.method
201
+ });
202
+ }
203
+ }
204
+ else {
205
+ _this._sendQueue.splice(i, 1);
206
+ }
207
+ };
208
+ for (var i = _this._sendQueue.length - 1; i >= 0; i--) {
209
+ _loop_1(i);
210
+ }
211
+ _this._runningQueue = false;
212
+ }
213
+ }
214
+ }, 50);
215
+ }
216
+ MethodManager.prototype.getMainServer = function () {
217
+ return this._mainServer;
218
+ };
219
+ // Add methods to private methods object
220
+ MethodManager.prototype.methods = function (method) {
221
+ this._methods = Object.assign(this._methods, method);
222
+ };
223
+ MethodManager.prototype.callMethodCron = function (method) {
224
+ var methodData = [];
225
+ for (var _i = 1; _i < arguments.length; _i++) {
226
+ methodData[_i - 1] = arguments[_i];
227
+ }
228
+ var _a;
229
+ var that = this;
230
+ if (!that._methods[method]) {
231
+ console.log('No Method: ' + method);
232
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Method: ' + method);
233
+ return;
234
+ }
235
+ if ((methodData.length > 1 || methodData[0]) && !that._methods[method].skipValidation) {
236
+ if (!that._methods[method].check) {
237
+ console.error('No Check Function For Method ' + method);
238
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Check Function For Method ' + method);
239
+ return;
240
+ }
241
+ else if (!that._methods[method].check._schema) {
242
+ console.error('No Check Schema For Method ' + method);
243
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Check Function For Method ' + method);
244
+ return;
245
+ }
246
+ else {
247
+ var valObj = {};
248
+ var valKeys = Object.keys(that._methods[method].check._schema);
249
+ var rootKeys = valKeys.filter(function (a) { return !a.includes('.'); });
250
+ for (var i = 0; i < methodData.length; i++) {
251
+ valObj[rootKeys[i]] = methodData[i];
252
+ }
253
+ try {
254
+ that._methods[method].check.validate(valObj);
255
+ }
256
+ catch (errors) {
257
+ if (errors) {
258
+ console.error('Error in Method Check (' + method + ')', errors);
259
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'Match Error On Method ' + method + '\n\nData Being Checked\n' + JSON.stringify(valObj, null, 2) + '\n\nErrors\n' + JSON.stringify(errors, null, 2));
260
+ return;
261
+ }
262
+ }
263
+ }
264
+ }
265
+ (_a = that._methods[method].function).call.apply(_a, [Object.assign({}, that, { id_user: '', user: '', id_ws: '' })].concat(methodData));
266
+ };
267
+ MethodManager.prototype.callMethodQueue = function (update) {
268
+ // console.log('Call Method Queue', update);
269
+ var _this = this;
270
+ var _a;
271
+ var updateCopy = common_1.deepCopy(update);
272
+ var that = this;
273
+ if (!that._methods[update.method]) {
274
+ console.log('No Method: ' + update.method);
275
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Method: ' + update.method);
276
+ var serverRes = {
277
+ messageId: update.messageId,
278
+ hasError: true,
279
+ data: 'Internal Error'
280
+ };
281
+ this._mainServer.getQueueManager().addMethodResponse(updateCopy, serverRes);
282
+ return;
283
+ }
284
+ if ((update.method_data.length > 1 || update.method_data[0]) && !that._methods[update.method].skipValidation) {
285
+ if (!that._methods[update.method].check) {
286
+ console.error('No Check Function For Method ' + update.method);
287
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Check Function For Method ' + update.method);
288
+ var serverRes = {
289
+ messageId: update.messageId,
290
+ hasError: true,
291
+ data: 'Internal Error'
292
+ };
293
+ this._mainServer.getQueueManager().addMethodResponse(updateCopy, serverRes);
294
+ return;
295
+ }
296
+ else if (!that._methods[update.method].check._schema) {
297
+ console.error('No Check Schema For Method ' + update.method);
298
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Check Function For Method ' + update.method);
299
+ var serverRes = {
300
+ messageId: update.messageId,
301
+ hasError: true,
302
+ data: 'Internal Error'
303
+ };
304
+ this._mainServer.getQueueManager().addMethodResponse(updateCopy, serverRes);
305
+ return;
306
+ }
307
+ else {
308
+ var valObj = {};
309
+ var valKeys = Object.keys(that._methods[update.method].check._schema);
310
+ var rootKeys = valKeys.filter(function (a) { return !a.includes('.'); });
311
+ for (var i = 0; i < update.method_data.length; i++) {
312
+ valObj[rootKeys[i]] = update.method_data[i];
313
+ }
314
+ try {
315
+ that._methods[update.method].check.validate(valObj);
316
+ }
317
+ catch (errors) {
318
+ if (errors) {
319
+ console.error('Error in Method Check (' + update.method + ')', errors);
320
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'Match Error On Method ' + update.method + '\n\nData Being Checked\n' + JSON.stringify(valObj, null, 2) + '\n\nErrors\n' + JSON.stringify(errors, null, 2));
321
+ var serverRes = {
322
+ messageId: update.messageId,
323
+ hasError: true,
324
+ data: 'Internal Error'
325
+ };
326
+ this._mainServer.getQueueManager().addMethodResponse(updateCopy, serverRes);
327
+ return;
328
+ }
329
+ }
330
+ }
331
+ }
332
+ // console.log('Run Method', update.method);
333
+ (_a = that._methods[update.method].function).call.apply(_a, [Object.assign({}, that, { id_user: update.id_user, user: update.user, id_ws: update.id_ws })].concat(update.method_data)).then(function (res) {
334
+ var serverRes = {
335
+ messageId: update.messageId,
336
+ hasError: false,
337
+ data: res
338
+ };
339
+ _this._mainServer.getQueueManager().addMethodResponse(updateCopy, serverRes);
340
+ }, function (err) {
341
+ var serverRes = {
342
+ messageId: update.messageId,
343
+ hasError: true,
344
+ data: err
345
+ };
346
+ _this._mainServer.getQueueManager().addMethodResponse(updateCopy, serverRes);
347
+ });
348
+ };
349
+ // Call/run method (Emit on Socket)
350
+ MethodManager.prototype.callMethod = function (ws, messageId, method) {
351
+ var _this = this;
352
+ var methodData = [];
353
+ for (var _i = 3; _i < arguments.length; _i++) {
354
+ methodData[_i - 3] = arguments[_i];
355
+ }
356
+ var _a;
357
+ var that = this;
358
+ if (!that._methods[method]) {
359
+ console.log('No Method: ' + method);
360
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Method: ' + method);
361
+ var serverRes = {
362
+ messageId: messageId,
363
+ hasError: true,
364
+ data: 'Internal Error'
365
+ };
366
+ this.sendWS(ws, method, methodData, serverRes);
367
+ return;
368
+ }
369
+ if ((methodData.length > 1 || methodData[0]) && !that._methods[method].skipValidation) {
370
+ if (!that._methods[method].check) {
371
+ console.error('No Check Function For Method ' + method);
372
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Check Function For Method ' + method);
373
+ var serverRes = {
374
+ messageId: messageId,
375
+ hasError: true,
376
+ data: 'Internal Error'
377
+ };
378
+ this.sendWS(ws, method, methodData, serverRes);
379
+ return;
380
+ }
381
+ else if (!that._methods[method].check._schema) {
382
+ console.error('No Check Schema For Method ' + method);
383
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'No Check Function For Method ' + method);
384
+ var serverRes = {
385
+ messageId: messageId,
386
+ hasError: true,
387
+ data: 'Internal Error'
388
+ };
389
+ this.sendWS(ws, method, methodData, serverRes);
390
+ return;
391
+ }
392
+ else {
393
+ var valObj = {};
394
+ var valKeys = Object.keys(that._methods[method].check._schema);
395
+ var rootKeys = valKeys.filter(function (a) { return !a.includes('.'); });
396
+ for (var i = 0; i < methodData.length; i++) {
397
+ valObj[rootKeys[i]] = methodData[i];
398
+ }
399
+ try {
400
+ that._methods[method].check.validate(valObj);
401
+ }
402
+ catch (errors) {
403
+ if (errors) {
404
+ console.error('Error in Method Check (' + method + ')', errors);
405
+ that.sendEmail('dev@resolveio.com', 'SERVER - Error Detected - ' + that.serverConfig['CLIENT_NAME'], 'Match Error On Method ' + method + '\n\nData Being Checked\n' + JSON.stringify(valObj, null, 2) + '\n\nErrors\n' + JSON.stringify(errors, null, 2));
406
+ var serverRes = {
407
+ messageId: messageId,
408
+ hasError: true,
409
+ data: 'Internal Error'
410
+ };
411
+ this.sendWS(ws, method, methodData, serverRes);
412
+ return;
413
+ }
414
+ }
415
+ }
416
+ }
417
+ (_a = that._methods[method].function).call.apply(_a, [Object.assign({}, that, { id_user: ws['id_user'], user: ws['user'], id_ws: ws['id_socket'] })].concat(methodData)).then(function (res) {
418
+ var serverRes = {
419
+ messageId: messageId,
420
+ hasError: false,
421
+ data: res
422
+ };
423
+ _this.sendWS(ws, method, methodData, serverRes);
424
+ }, function (err) {
425
+ var serverRes = {
426
+ messageId: messageId,
427
+ hasError: true,
428
+ data: err
429
+ };
430
+ _this.sendWS(ws, method, methodData, serverRes);
431
+ });
432
+ };
433
+ // Call/run method internal (No Emit on Socket)
434
+ MethodManager.prototype.callMethodInternal = function (method) {
435
+ var methodData = [];
436
+ for (var _i = 1; _i < arguments.length; _i++) {
437
+ methodData[_i - 1] = arguments[_i];
438
+ }
439
+ var _a;
440
+ var that = this;
441
+ if (!that._methods[method]) {
442
+ console.log('No Method: ' + method);
443
+ return;
444
+ }
445
+ if ((methodData.length > 1 || (methodData[0] && typeof (methodData[0]) !== 'function')) && !that._methods[method].skipValidation) {
446
+ if (!that._methods[method].check) {
447
+ console.error('No Check Function For Method ' + method);
448
+ return;
449
+ }
450
+ else if (!that._methods[method].check._schema) {
451
+ console.error('No Check Schema For Method ' + method);
452
+ return;
453
+ }
454
+ }
455
+ var promise = (_a = that._methods[method].function).call.apply(_a, [that].concat(methodData));
456
+ if (methodData[methodData.length - 1] && typeof (methodData[methodData.length - 1]) === 'function') {
457
+ promise.then(function (res) { return methodData[methodData.length - 1](null, res); }, function (err) { return methodData[methodData.length - 1](err, null); });
458
+ }
459
+ return promise;
460
+ };
461
+ MethodManager.prototype.sendWS = function (ws, method, methodData, data) {
462
+ this._sendQueue.splice(0, 0, {
463
+ id_ws: ws['id_socket'],
464
+ method: method,
465
+ methodData: methodData,
466
+ data: data
467
+ });
468
+ };
469
+ MethodManager.prototype.sendEmail = function (sendTo, subject, text, html, attachments) {
470
+ this._mailer.sendMail({
471
+ replyTo: undefined,
472
+ from: this.serverConfig.MAIL_FROM,
473
+ to: sendTo,
474
+ subject: subject,
475
+ text: text,
476
+ html: html,
477
+ attachments: attachments
478
+ }, function (err, info) {
479
+ if (err) {
480
+ console.log(err);
481
+ }
482
+ });
483
+ };
484
+ MethodManager.prototype.getAWS = function () {
485
+ return this._aws;
486
+ };
487
+ MethodManager.prototype.readFile = function (fileName) {
488
+ return new Promise(function (resolve, reject) {
489
+ fs.readFile(path.join(__dirname, ('../private/' + fileName)), 'utf-8', function (err, res) {
490
+ if (err) {
491
+ reject(err);
492
+ }
493
+ else {
494
+ resolve(res);
495
+ }
496
+ });
497
+ });
498
+ };
499
+ MethodManager.prototype.readImage = function (fileName) {
500
+ return new Promise(function (resolve, reject) {
501
+ fs.readFile(path.join(__dirname, ('../private/' + fileName)), 'base64', function (err, res) {
502
+ if (err) {
503
+ reject(err);
504
+ }
505
+ else {
506
+ resolve(res);
507
+ }
508
+ });
509
+ });
510
+ };
511
+ return MethodManager;
512
+ }());
513
+ exports.MethodManager = MethodManager;
@@ -0,0 +1,14 @@
1
+ import { QueueMethodModel } from '../models/queue-method.model';
2
+ import { ServerResponseModel } from '../models/server-response.model';
3
+ export declare class QueueMethodManager {
4
+ private _mainServer;
5
+ private _sendQueue;
6
+ private _runningQueue;
7
+ private _runningMethodResponse;
8
+ constructor(mainServer: any);
9
+ addMethodToQueue(id_ws: string, id_user: string, user: string, messageId: string, method: string, data: any[]): void;
10
+ runMethodQueue(count?: number): Promise<void>;
11
+ addMethodResponse(update: QueueMethodModel, response: ServerResponseModel): void;
12
+ runMethodResponse(): Promise<void>;
13
+ }
14
+ //# sourceMappingURL=queue-method.manager.d.ts.map