b96166c3a12942e0f225024a2af9512ce396f49a
[motion2.git] / server / config / schedule / index.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0x5ff4=['MailServerOut','keys','jobs','map','toString','difference','intersection','length','exists','stop','deleteJob','delete\x20cron','toRun','index','resolve','cron','isBetween','startAt','endAt','add','start','add\x20cron','active','update','update\x20cron','stop\x20cron','run','util','path','lodash','moment','cron-job-manager','jayson/promise','../logger','schedule','../../api/analyticCustomReport/analyticCustomReport.controller','../../api/analyticDefaultReport/analyticDefaultReport.controller','client','http','info','Try\x20to\x20send\x20a\x20mail\x20to','email','MailAccount','Smtp','service','host','port','secure','authentication','auth','user','pass','format','\x22%s\x22\x20<%s>','name','Report\x20%s\x20last\x20%s\x20%s','subtractUnit','../../files/reports','savename','basename','extname','zip','join','existsSync','Sent','attachments','message','text','Info\x20report:\x0a%s\x0aPlease\x20see\x20the\x20attachments\x0aThis\x20is\x20an\x20automatically\x20generated\x20email\x20–\x20please\x20do\x20not\x20reply\x20to\x20it.\x0a\x0a','status','Report\x20empty\x0aThis\x20is\x20an\x20automatically\x20generated\x20email\x20–\x20please\x20do\x20not\x20reply\x20to\x20this\x20email.\x0a\x0a','sendIfEmpty','SendMail','then','error','error\x20mail','stringify','catch','data','isNil','subtractNumber\x20null\x20or\x20undefined','subtractUnit\x20null\x20or\x20undefined','subtract','subtractNumber','YYYY-MM-DD\x20HH:mm:ss','scheduled','output','exec\x20defaul\x20report\x20id:%s','DefaultReportId','CustomReportId','runReport','no\x20report\x20id','Schedule','findAll'];(function(_0x321bb2,_0x202794){var _0x9c12ed=function(_0x263dd3){while(--_0x263dd3){_0x321bb2['push'](_0x321bb2['shift']());}};_0x9c12ed(++_0x202794);}(_0x5ff4,0x135));var _0x45ff=function(_0x4fde08,_0x463a36){_0x4fde08=_0x4fde08-0x0;var _0x12e725=_0x5ff4[_0x4fde08];return _0x12e725;};'use strict';var fs=require('fs');var util=require(_0x45ff('0x0'));var path=require(_0x45ff('0x1'));var _=require(_0x45ff('0x2'));var moment=require(_0x45ff('0x3'));var CronJobManager=require(_0x45ff('0x4'));var jayson=require(_0x45ff('0x5'));var BPromise=require('bluebird');var logger=require(_0x45ff('0x6'))(_0x45ff('0x7'));var db=require('../../mysqldb')['db'];var analyticCustomReportController=require(_0x45ff('0x8'));var analyticDefaultReportController=require(_0x45ff('0x9'));var client=jayson[_0x45ff('0xa')][_0x45ff('0xb')]({'port':0x232b});var manager=new CronJobManager();function handleSendEmail(_0x549a86,_0x3e2b31){return function(){logger[_0x45ff('0xc')](_0x45ff('0xd'),_0x549a86[_0x45ff('0xe')]);var _0x3abf1a=!![];var _0x1b2f42={'tls':{'rejectUnauthorized':![]}};if(_0x549a86[_0x45ff('0xf')][_0x45ff('0x10')][_0x45ff('0x11')]){_0x1b2f42['service']=_0x549a86[_0x45ff('0xf')][_0x45ff('0x10')][_0x45ff('0x11')];}else{_0x1b2f42[_0x45ff('0x12')]=_0x549a86[_0x45ff('0xf')][_0x45ff('0x10')]['host'];_0x1b2f42['port']=_0x549a86[_0x45ff('0xf')]['Smtp'][_0x45ff('0x13')];_0x1b2f42[_0x45ff('0x14')]=_0x549a86[_0x45ff('0xf')][_0x45ff('0x10')][_0x45ff('0x14')];}if(_0x549a86[_0x45ff('0xf')]['Smtp'][_0x45ff('0x15')]){_0x1b2f42[_0x45ff('0x16')]={'user':_0x549a86[_0x45ff('0xf')][_0x45ff('0x10')][_0x45ff('0x17')],'pass':_0x549a86[_0x45ff('0xf')][_0x45ff('0x10')][_0x45ff('0x18')]};}var _0x3df753={'account':_0x1b2f42,'message':{'from':util[_0x45ff('0x19')](_0x45ff('0x1a'),_0x549a86[_0x45ff('0xf')][_0x45ff('0x1b')],_0x549a86['MailAccount']['email']||_0x549a86[_0x45ff('0xf')]['Smtp'][_0x45ff('0x17')]),'to':_0x549a86['email'],'cc':_0x549a86['cc'],'bcc':_0x549a86['bcc'],'subject':util[_0x45ff('0x19')](_0x45ff('0x1c'),_0x3e2b31[_0x45ff('0x1b')],_0x549a86['subtractNumber'],_0x549a86[_0x45ff('0x1d')])}};var _0x1f72a8=path['join'](__dirname,_0x45ff('0x1e'));var _0x237f34=_0x3e2b31[_0x45ff('0x1f')];var _0x2b3763=util[_0x45ff('0x19')]('%s.%s',path[_0x45ff('0x20')](_0x3e2b31[_0x45ff('0x1f')],path[_0x45ff('0x21')](_0x3e2b31['savename'])),_0x45ff('0x22'));var _0x16c415=path[_0x45ff('0x23')](_0x1f72a8,_0x237f34);var _0x4589ea=path[_0x45ff('0x23')](_0x1f72a8,_0x2b3763);if(fs[_0x45ff('0x24')](_0x16c415)){_0x3e2b31['status']=_0x45ff('0x25');_0x3df753['message'][_0x45ff('0x26')]=[{'filename':_0x237f34,'path':_0x16c415}];_0x3df753[_0x45ff('0x27')][_0x45ff('0x28')]=util[_0x45ff('0x19')](_0x45ff('0x29'),JSON['stringify'](_0x3e2b31,null,0x2));}else if(fs['existsSync'](_0x4589ea)){_0x3e2b31[_0x45ff('0x2a')]='Sent';_0x3df753[_0x45ff('0x27')][_0x45ff('0x26')]=[{'filename':_0x2b3763,'path':_0x4589ea}];}else{_0x3df753['message'][_0x45ff('0x28')]=_0x45ff('0x2b');if(!_0x549a86[_0x45ff('0x2c')]){_0x3abf1a=![];logger[_0x45ff('0xc')]('empty\x20file,\x20email\x20not\x20sent');}}if(_0x3abf1a){return client['request'](_0x45ff('0x2d'),_0x3df753)[_0x45ff('0x2e')](function(_0xb15ca1){if(_0xb15ca1[_0x45ff('0x2f')]){return logger[_0x45ff('0x2f')](_0x45ff('0x30'),JSON[_0x45ff('0x31')](_0xb15ca1));}return logger['info']('email\x20sent');})[_0x45ff('0x32')](function(_0x59656e){return logger['error'](_0x45ff('0x30'),JSON['stringify'](_0x59656e));});}};}function handleError(){return function(_0x3b1d19){return logger['error'](JSON[_0x45ff('0x31')](_0x3b1d19));};}function handleResult(_0x355131){return function(_0x52f52b){logger[_0x45ff('0xc')](_0x45ff('0x33'),JSON[_0x45ff('0x31')](_0x52f52b));if(_0x355131['MailAccount']&&_0x355131[_0x45ff('0xf')][_0x45ff('0x10')]&&!_[_0x45ff('0x34')](_0x355131[_0x45ff('0xe')])){setTimeout(handleSendEmail(_0x355131,_0x52f52b),0x3c*0x3e8);}};}function handleJob(_0x2e159f){return function(){try{if(_[_0x45ff('0x34')](_0x2e159f['subtractNumber'])){return logger['info'](_0x45ff('0x35'));}if(_[_0x45ff('0x34')](_0x2e159f[_0x45ff('0x1d')])){return logger[_0x45ff('0xc')](_0x45ff('0x36'));}var _0x544679={'startDate':moment()[_0x45ff('0x37')](_0x2e159f[_0x45ff('0x38')],_0x2e159f[_0x45ff('0x1d')])[_0x45ff('0x19')](_0x45ff('0x39')),'endDate':moment()[_0x45ff('0x19')](_0x45ff('0x39')),'type':_0x45ff('0x3a'),'output':_0x2e159f[_0x45ff('0x3b')],'name':_0x2e159f['name']};if(!_['isNil'](_0x2e159f['DefaultReportId'])){logger[_0x45ff('0xc')](_0x45ff('0x3c'),_0x2e159f[_0x45ff('0x3d')]);return analyticDefaultReportController['runReport']({'id':_0x2e159f[_0x45ff('0x3d')]},_0x544679)[_0x45ff('0x2e')](handleResult(_0x2e159f))['catch'](handleError());}if(!_['isNil'](_0x2e159f['CustomReportId'])){logger[_0x45ff('0xc')]('exec\x20custom\x20report\x20id:%s',_0x2e159f[_0x45ff('0x3e')]);return analyticCustomReportController[_0x45ff('0x3f')]({'id':_0x2e159f[_0x45ff('0x3e')]},_0x544679)[_0x45ff('0x2e')](handleResult(_0x2e159f))[_0x45ff('0x32')](handleError());}}catch(_0x2e9673){logger['error'](JSON[_0x45ff('0x31')](_0x2e9673));}return logger[_0x45ff('0xc')](_0x45ff('0x40'));};}function getJobs(_0x3285ed){var _0x332260={};if(_0x3285ed){_0x332260['id']=_0x3285ed;}return db[_0x45ff('0x41')][_0x45ff('0x42')]({'where':_0x332260,'include':[{'model':db['MailAccount'],'attributes':['id','name',_0x45ff('0xe')],'include':[{'model':db[_0x45ff('0x43')],'as':_0x45ff('0x10')}]}]})[_0x45ff('0x2e')](function(_0x3f8bf9){var _0x531fa9=_[_0x45ff('0x44')](manager[_0x45ff('0x45')]);var _0x39d54c=_[_0x45ff('0x46')](_0x3f8bf9,function(_0x1866f2){return _0x1866f2['id'][_0x45ff('0x47')]();});var _0x23c51e=_['keyBy'](_0x3f8bf9,function(_0x12b8fe){return _0x12b8fe['id'][_0x45ff('0x47')]();});var _0x21ca65=_0x3285ed?_0x39d54c:_[_0x45ff('0x48')](_0x39d54c,_0x531fa9);var _0x127ba6=_[_0x45ff('0x49')](_0x39d54c,_0x531fa9);var _0x2f2501=_[_0x45ff('0x48')](_0x531fa9,_0x39d54c);var _0x450953;var _0x15abfc;for(_0x450953=0x0;_0x450953<_0x2f2501[_0x45ff('0x4a')]&&!_0x3285ed;_0x450953+=0x1){_0x15abfc=_0x2f2501[_0x450953][_0x45ff('0x47')]();if(manager[_0x45ff('0x4b')](_0x15abfc)){manager[_0x45ff('0x4c')](_0x15abfc);manager[_0x45ff('0x4d')](_0x15abfc);logger['info'](_0x45ff('0x4e'),_0x15abfc);}}logger[_0x45ff('0xc')](_0x45ff('0x4f'),JSON['stringify'](_0x21ca65));for(_0x450953=0x0;_0x450953<_0x21ca65['length'];_0x450953+=0x1){_0x15abfc=_0x21ca65[_0x450953][_0x45ff('0x47')]();logger[_0x45ff('0xc')](_0x45ff('0x50'),_0x15abfc);if(_0x3285ed){logger[_0x45ff('0xc')]('my\x20id',_0x3285ed);logger[_0x45ff('0xc')](JSON[_0x45ff('0x31')](_0x23c51e[_0x15abfc]));BPromise[_0x45ff('0x51')]()[_0x45ff('0x2e')](handleJob(_0x23c51e[_0x15abfc]));logger[_0x45ff('0xc')]('run\x20job',_0x23c51e[_0x15abfc][_0x45ff('0x1b')],_0x23c51e[_0x15abfc][_0x45ff('0x52')]);}else if(_0x23c51e[_0x15abfc][_0x45ff('0x52')]&&_0x23c51e[_0x15abfc]['active']&&moment()[_0x45ff('0x53')](_0x23c51e[_0x15abfc][_0x45ff('0x54')],_0x23c51e[_0x15abfc][_0x45ff('0x55')])){manager[_0x45ff('0x56')](_0x15abfc,_0x23c51e[_0x15abfc][_0x45ff('0x52')],handleJob(_0x23c51e[_0x15abfc]));manager[_0x45ff('0x57')](_0x15abfc);logger[_0x45ff('0xc')](_0x45ff('0x58'),_0x23c51e[_0x15abfc][_0x45ff('0x1b')],_0x23c51e[_0x15abfc][_0x45ff('0x52')]);}else if(manager[_0x45ff('0x4b')](_0x15abfc)){manager[_0x45ff('0x4c')](_0x15abfc);manager[_0x45ff('0x4d')](_0x15abfc);logger[_0x45ff('0xc')]('stop\x20cron',_0x23c51e[_0x15abfc][_0x45ff('0x1b')],_0x23c51e[_0x15abfc]['cron']);}}for(_0x450953=0x0,_0x15abfc;_0x450953<_0x127ba6[_0x45ff('0x4a')]&&!_0x3285ed;_0x450953+=0x1){_0x15abfc=_0x127ba6[_0x450953][_0x45ff('0x47')]();if(manager[_0x45ff('0x4b')](_0x15abfc)){if(_0x23c51e[_0x15abfc][_0x45ff('0x52')]&&_0x23c51e[_0x15abfc][_0x45ff('0x59')]&&moment()[_0x45ff('0x53')](_0x23c51e[_0x15abfc]['startAt'],_0x23c51e[_0x15abfc][_0x45ff('0x55')])){manager[_0x45ff('0x5a')](_0x15abfc,_0x23c51e[_0x15abfc][_0x45ff('0x52')],handleJob(_0x23c51e[_0x15abfc]));logger['info'](_0x45ff('0x5b'),_0x23c51e[_0x15abfc][_0x45ff('0x1b')],_0x23c51e[_0x15abfc][_0x45ff('0x52')]);}else{manager['stop'](_0x15abfc);manager[_0x45ff('0x4d')](_0x15abfc);logger[_0x45ff('0xc')](_0x45ff('0x5c'),_0x23c51e[_0x15abfc][_0x45ff('0x1b')],_0x23c51e[_0x15abfc][_0x45ff('0x52')]);}}}})[_0x45ff('0x32')](function(_0x5d46a4){logger[_0x45ff('0x2f')](JSON[_0x45ff('0x31')](_0x5d46a4));});}exports[_0x45ff('0x5d')]=function(_0x1cd046){return getJobs(_0x1cd046);};exports['start']=function(){setInterval(function(){return getJobs();},0x3c*0x3e8);return getJobs();};