32fecaae79832468b81891c5dd0296bab9aca81d
[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 _0x8fe6=['startAt','endAt','update\x20cron','run','util','path','lodash','moment','jayson/promise','bluebird','../logger','schedule','../../mysqldb','../../api/analyticCustomReport/analyticCustomReport.controller','../../api/analyticDefaultReport/analyticDefaultReport.controller','client','Try\x20to\x20send\x20a\x20mail\x20to','Smtp','service','MailAccount','host','port','secure','authentication','\x22%s\x22\x20<%s>','name','email','bcc','subtractNumber','subtractUnit','../../files/reports','format','%s.%s','savename','extname','zip','join','existsSync','status','attachments','stringify','Sent','message','text','Report\x20empty\x0aThis\x20is\x20an\x20automatically\x20generated\x20email\x20–\x20please\x20do\x20not\x20reply\x20to\x20this\x20email.\x0a\x0a','sendIfEmpty','info','empty\x20file,\x20email\x20not\x20sent','request','SendMail','then','error','error\x20mail','catch','data','isNil','subtractNumber\x20null\x20or\x20undefined','subtractUnit\x20null\x20or\x20undefined','subtract','YYYY-MM-DD\x20HH:mm:ss','output','DefaultReportId','exec\x20defaul\x20report\x20id:%s','CustomReportId','runReport','no\x20report\x20id','findAll','MailServerOut','keys','toString','keyBy','difference','intersection','exists','stop','delete\x20cron','length','index','my\x20id','resolve','run\x20job','cron','isBetween','add\x20cron','deleteJob','stop\x20cron'];(function(_0x182dc7,_0x581b22){var _0x3574f3=function(_0x47996c){while(--_0x47996c){_0x182dc7['push'](_0x182dc7['shift']());}};_0x3574f3(++_0x581b22);}(_0x8fe6,0xb0));var _0x68fe=function(_0x1ca037,_0x33e239){_0x1ca037=_0x1ca037-0x0;var _0x4f8516=_0x8fe6[_0x1ca037];return _0x4f8516;};'use strict';var fs=require('fs');var util=require(_0x68fe('0x0'));var path=require(_0x68fe('0x1'));var _=require(_0x68fe('0x2'));var moment=require(_0x68fe('0x3'));var CronJobManager=require('cron-job-manager');var jayson=require(_0x68fe('0x4'));var BPromise=require(_0x68fe('0x5'));var logger=require(_0x68fe('0x6'))(_0x68fe('0x7'));var db=require(_0x68fe('0x8'))['db'];var analyticCustomReportController=require(_0x68fe('0x9'));var analyticDefaultReportController=require(_0x68fe('0xa'));var client=jayson[_0x68fe('0xb')]['http']({'port':0x232b});var manager=new CronJobManager();function handleSendEmail(_0x5230a4,_0x4b78cc){return function(){logger['info'](_0x68fe('0xc'),_0x5230a4['email']);var _0x442374=!![];var _0x458299={'tls':{'rejectUnauthorized':![]}};if(_0x5230a4['MailAccount'][_0x68fe('0xd')][_0x68fe('0xe')]){_0x458299[_0x68fe('0xe')]=_0x5230a4[_0x68fe('0xf')][_0x68fe('0xd')][_0x68fe('0xe')];}else{_0x458299[_0x68fe('0x10')]=_0x5230a4[_0x68fe('0xf')][_0x68fe('0xd')][_0x68fe('0x10')];_0x458299[_0x68fe('0x11')]=_0x5230a4[_0x68fe('0xf')][_0x68fe('0xd')]['port'];_0x458299[_0x68fe('0x12')]=_0x5230a4[_0x68fe('0xf')]['Smtp'][_0x68fe('0x12')];}if(_0x5230a4[_0x68fe('0xf')][_0x68fe('0xd')][_0x68fe('0x13')]){_0x458299['auth']={'user':_0x5230a4[_0x68fe('0xf')]['Smtp']['user'],'pass':_0x5230a4[_0x68fe('0xf')][_0x68fe('0xd')]['pass']};}var _0x5784e0={'account':_0x458299,'message':{'from':util['format'](_0x68fe('0x14'),_0x5230a4[_0x68fe('0xf')][_0x68fe('0x15')],_0x5230a4[_0x68fe('0xf')][_0x68fe('0x16')]||_0x5230a4['MailAccount'][_0x68fe('0xd')]['user']),'to':_0x5230a4[_0x68fe('0x16')],'cc':_0x5230a4['cc'],'bcc':_0x5230a4[_0x68fe('0x17')],'subject':util['format']('Report\x20%s\x20last\x20%s\x20%s',_0x4b78cc[_0x68fe('0x15')],_0x5230a4[_0x68fe('0x18')],_0x5230a4[_0x68fe('0x19')])}};var _0x355bb1=path['join'](__dirname,_0x68fe('0x1a'));var _0x16cb56=_0x4b78cc['savename'];var _0x767b42=util[_0x68fe('0x1b')](_0x68fe('0x1c'),path['basename'](_0x4b78cc[_0x68fe('0x1d')],path[_0x68fe('0x1e')](_0x4b78cc[_0x68fe('0x1d')])),_0x68fe('0x1f'));var _0x4d1802=path[_0x68fe('0x20')](_0x355bb1,_0x16cb56);var _0x49a276=path[_0x68fe('0x20')](_0x355bb1,_0x767b42);if(fs[_0x68fe('0x21')](_0x4d1802)){_0x4b78cc[_0x68fe('0x22')]='Sent';_0x5784e0['message'][_0x68fe('0x23')]=[{'filename':_0x16cb56,'path':_0x4d1802}];_0x5784e0['message']['text']=util[_0x68fe('0x1b')]('Info\x20report:\x0a%s\x0aPlease\x20see\x20the\x20attachments\x0aThis\x20is\x20an\x20automatically\x20generated\x20email\x20–\x20please\x20do\x20not\x20reply\x20to\x20it.\x0a\x0a',JSON[_0x68fe('0x24')](_0x4b78cc,null,0x2));}else if(fs['existsSync'](_0x49a276)){_0x4b78cc[_0x68fe('0x22')]=_0x68fe('0x25');_0x5784e0[_0x68fe('0x26')][_0x68fe('0x23')]=[{'filename':_0x767b42,'path':_0x49a276}];}else{_0x5784e0[_0x68fe('0x26')][_0x68fe('0x27')]=_0x68fe('0x28');if(!_0x5230a4[_0x68fe('0x29')]){_0x442374=![];logger[_0x68fe('0x2a')](_0x68fe('0x2b'));}}if(_0x442374){return client[_0x68fe('0x2c')](_0x68fe('0x2d'),_0x5784e0)[_0x68fe('0x2e')](function(_0x5c822a){if(_0x5c822a[_0x68fe('0x2f')]){return logger[_0x68fe('0x2f')](_0x68fe('0x30'),JSON[_0x68fe('0x24')](_0x5c822a));}return logger[_0x68fe('0x2a')]('email\x20sent');})[_0x68fe('0x31')](function(_0x58f32f){return logger[_0x68fe('0x2f')]('error\x20mail',JSON['stringify'](_0x58f32f));});}};}function handleError(){return function(_0xff4a38){return logger[_0x68fe('0x2f')](JSON['stringify'](_0xff4a38));};}function handleResult(_0x3c60d9){return function(_0x458028){logger[_0x68fe('0x2a')](_0x68fe('0x32'),JSON[_0x68fe('0x24')](_0x458028));if(_0x3c60d9[_0x68fe('0xf')]&&_0x3c60d9[_0x68fe('0xf')][_0x68fe('0xd')]&&!_[_0x68fe('0x33')](_0x3c60d9[_0x68fe('0x16')])){setTimeout(handleSendEmail(_0x3c60d9,_0x458028),0x3c*0x3e8);}};}function handleJob(_0x28bbf0){return function(){try{if(_[_0x68fe('0x33')](_0x28bbf0[_0x68fe('0x18')])){return logger[_0x68fe('0x2a')](_0x68fe('0x34'));}if(_['isNil'](_0x28bbf0[_0x68fe('0x19')])){return logger[_0x68fe('0x2a')](_0x68fe('0x35'));}var _0x3f8efd={'startDate':moment()[_0x68fe('0x36')](_0x28bbf0[_0x68fe('0x18')],_0x28bbf0[_0x68fe('0x19')])['format'](_0x68fe('0x37')),'endDate':moment()[_0x68fe('0x1b')](_0x68fe('0x37')),'type':'scheduled','output':_0x28bbf0[_0x68fe('0x38')],'name':_0x28bbf0[_0x68fe('0x15')]};if(!_[_0x68fe('0x33')](_0x28bbf0[_0x68fe('0x39')])){logger['info'](_0x68fe('0x3a'),_0x28bbf0[_0x68fe('0x39')]);return analyticDefaultReportController['runReport']({'id':_0x28bbf0[_0x68fe('0x39')]},_0x3f8efd)[_0x68fe('0x2e')](handleResult(_0x28bbf0))['catch'](handleError());}if(!_['isNil'](_0x28bbf0[_0x68fe('0x3b')])){logger['info']('exec\x20custom\x20report\x20id:%s',_0x28bbf0[_0x68fe('0x3b')]);return analyticCustomReportController[_0x68fe('0x3c')]({'id':_0x28bbf0[_0x68fe('0x3b')]},_0x3f8efd)['then'](handleResult(_0x28bbf0))[_0x68fe('0x31')](handleError());}}catch(_0x10bb4d){logger['error'](JSON[_0x68fe('0x24')](_0x10bb4d));}return logger[_0x68fe('0x2a')](_0x68fe('0x3d'));};}function getJobs(_0x26de67){var _0x3ae0e0={};if(_0x26de67){_0x3ae0e0['id']=_0x26de67;}return db['Schedule'][_0x68fe('0x3e')]({'where':_0x3ae0e0,'include':[{'model':db['MailAccount'],'attributes':['id',_0x68fe('0x15'),_0x68fe('0x16')],'include':[{'model':db[_0x68fe('0x3f')],'as':'Smtp'}]}]})[_0x68fe('0x2e')](function(_0x26889a){var _0x6c9c5f=_[_0x68fe('0x40')](manager['jobs']);var _0x5c3848=_['map'](_0x26889a,function(_0x15af6d){return _0x15af6d['id'][_0x68fe('0x41')]();});var _0x334e6b=_[_0x68fe('0x42')](_0x26889a,function(_0x202247){return _0x202247['id'][_0x68fe('0x41')]();});var _0x4f4ce7=_0x26de67?_0x5c3848:_[_0x68fe('0x43')](_0x5c3848,_0x6c9c5f);var _0x365c39=_[_0x68fe('0x44')](_0x5c3848,_0x6c9c5f);var _0x35324e=_['difference'](_0x6c9c5f,_0x5c3848);var _0x1ed6f0;var _0x3d2c44;for(_0x1ed6f0=0x0;_0x1ed6f0<_0x35324e['length']&&!_0x26de67;_0x1ed6f0+=0x1){_0x3d2c44=_0x35324e[_0x1ed6f0][_0x68fe('0x41')]();if(manager[_0x68fe('0x45')](_0x3d2c44)){manager[_0x68fe('0x46')](_0x3d2c44);manager['deleteJob'](_0x3d2c44);logger[_0x68fe('0x2a')](_0x68fe('0x47'),_0x3d2c44);}}logger[_0x68fe('0x2a')]('toRun',JSON['stringify'](_0x4f4ce7));for(_0x1ed6f0=0x0;_0x1ed6f0<_0x4f4ce7[_0x68fe('0x48')];_0x1ed6f0+=0x1){_0x3d2c44=_0x4f4ce7[_0x1ed6f0][_0x68fe('0x41')]();logger[_0x68fe('0x2a')](_0x68fe('0x49'),_0x3d2c44);if(_0x26de67){logger[_0x68fe('0x2a')](_0x68fe('0x4a'),_0x26de67);logger[_0x68fe('0x2a')](JSON[_0x68fe('0x24')](_0x334e6b[_0x3d2c44]));BPromise[_0x68fe('0x4b')]()[_0x68fe('0x2e')](handleJob(_0x334e6b[_0x3d2c44]));logger[_0x68fe('0x2a')](_0x68fe('0x4c'),_0x334e6b[_0x3d2c44][_0x68fe('0x15')],_0x334e6b[_0x3d2c44][_0x68fe('0x4d')]);}else if(_0x334e6b[_0x3d2c44]['cron']&&_0x334e6b[_0x3d2c44]['active']&&moment()[_0x68fe('0x4e')](_0x334e6b[_0x3d2c44]['startAt'],_0x334e6b[_0x3d2c44]['endAt'])){manager['add'](_0x3d2c44,_0x334e6b[_0x3d2c44][_0x68fe('0x4d')],handleJob(_0x334e6b[_0x3d2c44]));manager['start'](_0x3d2c44);logger[_0x68fe('0x2a')](_0x68fe('0x4f'),_0x334e6b[_0x3d2c44][_0x68fe('0x15')],_0x334e6b[_0x3d2c44][_0x68fe('0x4d')]);}else if(manager[_0x68fe('0x45')](_0x3d2c44)){manager[_0x68fe('0x46')](_0x3d2c44);manager[_0x68fe('0x50')](_0x3d2c44);logger[_0x68fe('0x2a')](_0x68fe('0x51'),_0x334e6b[_0x3d2c44][_0x68fe('0x15')],_0x334e6b[_0x3d2c44][_0x68fe('0x4d')]);}}for(_0x1ed6f0=0x0,_0x3d2c44;_0x1ed6f0<_0x365c39[_0x68fe('0x48')]&&!_0x26de67;_0x1ed6f0+=0x1){_0x3d2c44=_0x365c39[_0x1ed6f0][_0x68fe('0x41')]();if(manager[_0x68fe('0x45')](_0x3d2c44)){if(_0x334e6b[_0x3d2c44]['cron']&&_0x334e6b[_0x3d2c44]['active']&&moment()['isBetween'](_0x334e6b[_0x3d2c44][_0x68fe('0x52')],_0x334e6b[_0x3d2c44][_0x68fe('0x53')])){manager['update'](_0x3d2c44,_0x334e6b[_0x3d2c44][_0x68fe('0x4d')],handleJob(_0x334e6b[_0x3d2c44]));logger[_0x68fe('0x2a')](_0x68fe('0x54'),_0x334e6b[_0x3d2c44][_0x68fe('0x15')],_0x334e6b[_0x3d2c44]['cron']);}else{manager['stop'](_0x3d2c44);manager[_0x68fe('0x50')](_0x3d2c44);logger[_0x68fe('0x2a')]('stop\x20cron',_0x334e6b[_0x3d2c44]['name'],_0x334e6b[_0x3d2c44][_0x68fe('0x4d')]);}}}})[_0x68fe('0x31')](function(_0x570b88){logger[_0x68fe('0x2f')](JSON[_0x68fe('0x24')](_0x570b88));});}exports[_0x68fe('0x55')]=function(_0x402206){return getJobs(_0x402206);};exports['start']=function(){setInterval(function(){return getJobs();},0x3c*0x3e8);return getJobs();};