122374276c25e3cfb1dede00671974d4ebd09032
[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 _0xbcf9=['stringify','existsSync','Report\x20empty\x0aThis\x20is\x20an\x20automatically\x20generated\x20email\x20–\x20please\x20do\x20not\x20reply\x20to\x20this\x20email.\x0a\x0a','sendIfEmpty','request','SendMail','error','error\x20mail','email\x20sent','catch','data','isNil','subtractNumber\x20null\x20or\x20undefined','subtractUnit','subtractUnit\x20null\x20or\x20undefined','YYYY-MM-DD\x20HH:mm:ss','DefaultReportId','exec\x20defaul\x20report\x20id:%s','runReport','then','exec\x20custom\x20report\x20id:%s','CustomReportId','no\x20report\x20id','findAll','MailServerOut','keys','jobs','map','toString','keyBy','difference','intersection','length','exists','stop','deleteJob','delete\x20cron','toRun','index','my\x20id','resolve','cron','active','isBetween','endAt','add','add\x20cron','stop\x20cron','startAt','update','update\x20cron','run','start','util','path','lodash','moment','cron-job-manager','bluebird','../logger','schedule','../../mysqldb','../../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','pass','format','name','user','Report\x20%s\x20last\x20%s\x20%s','subtractNumber','join','../../files/reports','savename','%s.%s','basename','status','Sent','message','attachments','text','Info\x20report:\x0a%s\x0aPlease\x20see\x20the\x20attachments\x0aThis\x20is\x20an\x20automatically\x20generated\x20email\x20–\x20please\x20do\x20not\x20reply\x20to\x20it.\x0a\x0a'];(function(_0x42cae2,_0x284ef8){var _0x5f256f=function(_0x41abc8){while(--_0x41abc8){_0x42cae2['push'](_0x42cae2['shift']());}};_0x5f256f(++_0x284ef8);}(_0xbcf9,0xef));var _0x9bcf=function(_0x324989,_0x1a8db4){_0x324989=_0x324989-0x0;var _0xc94fc8=_0xbcf9[_0x324989];return _0xc94fc8;};'use strict';var fs=require('fs');var util=require(_0x9bcf('0x0'));var path=require(_0x9bcf('0x1'));var _=require(_0x9bcf('0x2'));var moment=require(_0x9bcf('0x3'));var CronJobManager=require(_0x9bcf('0x4'));var jayson=require('jayson/promise');var BPromise=require(_0x9bcf('0x5'));var logger=require(_0x9bcf('0x6'))(_0x9bcf('0x7'));var db=require(_0x9bcf('0x8'))['db'];var analyticCustomReportController=require(_0x9bcf('0x9'));var analyticDefaultReportController=require(_0x9bcf('0xa'));var client=jayson[_0x9bcf('0xb')][_0x9bcf('0xc')]({'port':0x232b});var manager=new CronJobManager();function handleSendEmail(_0x46e269,_0x37ee94){return function(){logger[_0x9bcf('0xd')](_0x9bcf('0xe'),_0x46e269[_0x9bcf('0xf')]);var _0x90760d=!![];var _0x55e8bd={'tls':{'rejectUnauthorized':![]}};if(_0x46e269[_0x9bcf('0x10')][_0x9bcf('0x11')][_0x9bcf('0x12')]){_0x55e8bd[_0x9bcf('0x12')]=_0x46e269[_0x9bcf('0x10')][_0x9bcf('0x11')]['service'];}else{_0x55e8bd[_0x9bcf('0x13')]=_0x46e269[_0x9bcf('0x10')][_0x9bcf('0x11')][_0x9bcf('0x13')];_0x55e8bd[_0x9bcf('0x14')]=_0x46e269[_0x9bcf('0x10')][_0x9bcf('0x11')]['port'];_0x55e8bd[_0x9bcf('0x15')]=_0x46e269[_0x9bcf('0x10')][_0x9bcf('0x11')][_0x9bcf('0x15')];}if(_0x46e269[_0x9bcf('0x10')][_0x9bcf('0x11')][_0x9bcf('0x16')]){_0x55e8bd['auth']={'user':_0x46e269['MailAccount'][_0x9bcf('0x11')]['user'],'pass':_0x46e269[_0x9bcf('0x10')][_0x9bcf('0x11')][_0x9bcf('0x17')]};}var _0x412c16={'account':_0x55e8bd,'message':{'from':util[_0x9bcf('0x18')]('\x22%s\x22\x20<%s>',_0x46e269[_0x9bcf('0x10')][_0x9bcf('0x19')],_0x46e269['MailAccount']['email']||_0x46e269[_0x9bcf('0x10')][_0x9bcf('0x11')][_0x9bcf('0x1a')]),'to':_0x46e269['email'],'cc':_0x46e269['cc'],'bcc':_0x46e269['bcc'],'subject':util['format'](_0x9bcf('0x1b'),_0x37ee94[_0x9bcf('0x19')],_0x46e269[_0x9bcf('0x1c')],_0x46e269['subtractUnit'])}};var _0x33713d=path[_0x9bcf('0x1d')](__dirname,_0x9bcf('0x1e'));var _0x2934dc=_0x37ee94[_0x9bcf('0x1f')];var _0x2169bf=util[_0x9bcf('0x18')](_0x9bcf('0x20'),path[_0x9bcf('0x21')](_0x37ee94['savename'],path['extname'](_0x37ee94[_0x9bcf('0x1f')])),'zip');var _0x32fb2f=path[_0x9bcf('0x1d')](_0x33713d,_0x2934dc);var _0xf7608f=path[_0x9bcf('0x1d')](_0x33713d,_0x2169bf);if(fs['existsSync'](_0x32fb2f)){_0x37ee94[_0x9bcf('0x22')]=_0x9bcf('0x23');_0x412c16[_0x9bcf('0x24')][_0x9bcf('0x25')]=[{'filename':_0x2934dc,'path':_0x32fb2f}];_0x412c16['message'][_0x9bcf('0x26')]=util['format'](_0x9bcf('0x27'),JSON[_0x9bcf('0x28')](_0x37ee94,null,0x2));}else if(fs[_0x9bcf('0x29')](_0xf7608f)){_0x37ee94[_0x9bcf('0x22')]=_0x9bcf('0x23');_0x412c16[_0x9bcf('0x24')][_0x9bcf('0x25')]=[{'filename':_0x2169bf,'path':_0xf7608f}];}else{_0x412c16[_0x9bcf('0x24')][_0x9bcf('0x26')]=_0x9bcf('0x2a');if(!_0x46e269[_0x9bcf('0x2b')]){_0x90760d=![];logger['info']('empty\x20file,\x20email\x20not\x20sent');}}if(_0x90760d){return client[_0x9bcf('0x2c')](_0x9bcf('0x2d'),_0x412c16)['then'](function(_0x3294cb){if(_0x3294cb[_0x9bcf('0x2e')]){return logger[_0x9bcf('0x2e')](_0x9bcf('0x2f'),JSON['stringify'](_0x3294cb));}return logger[_0x9bcf('0xd')](_0x9bcf('0x30'));})[_0x9bcf('0x31')](function(_0xf6ff60){return logger[_0x9bcf('0x2e')](_0x9bcf('0x2f'),JSON[_0x9bcf('0x28')](_0xf6ff60));});}};}function handleError(){return function(_0x8df758){return logger[_0x9bcf('0x2e')](JSON[_0x9bcf('0x28')](_0x8df758));};}function handleResult(_0x4b602e){return function(_0x3731fb){logger[_0x9bcf('0xd')](_0x9bcf('0x32'),JSON[_0x9bcf('0x28')](_0x3731fb));if(_0x4b602e[_0x9bcf('0x10')]&&_0x4b602e[_0x9bcf('0x10')]['Smtp']&&!_['isNil'](_0x4b602e[_0x9bcf('0xf')])){setTimeout(handleSendEmail(_0x4b602e,_0x3731fb),0x3c*0x3e8);}};}function handleJob(_0x2bd5a1){return function(){try{if(_[_0x9bcf('0x33')](_0x2bd5a1[_0x9bcf('0x1c')])){return logger[_0x9bcf('0xd')](_0x9bcf('0x34'));}if(_[_0x9bcf('0x33')](_0x2bd5a1[_0x9bcf('0x35')])){return logger[_0x9bcf('0xd')](_0x9bcf('0x36'));}var _0x5250={'startDate':moment()['subtract'](_0x2bd5a1[_0x9bcf('0x1c')],_0x2bd5a1[_0x9bcf('0x35')])['format'](_0x9bcf('0x37')),'endDate':moment()[_0x9bcf('0x18')]('YYYY-MM-DD\x20HH:mm:ss'),'type':'scheduled','output':_0x2bd5a1['output'],'name':_0x2bd5a1[_0x9bcf('0x19')]};if(!_['isNil'](_0x2bd5a1[_0x9bcf('0x38')])){logger[_0x9bcf('0xd')](_0x9bcf('0x39'),_0x2bd5a1[_0x9bcf('0x38')]);return analyticDefaultReportController[_0x9bcf('0x3a')]({'id':_0x2bd5a1[_0x9bcf('0x38')]},_0x5250)[_0x9bcf('0x3b')](handleResult(_0x2bd5a1))[_0x9bcf('0x31')](handleError());}if(!_['isNil'](_0x2bd5a1['CustomReportId'])){logger[_0x9bcf('0xd')](_0x9bcf('0x3c'),_0x2bd5a1[_0x9bcf('0x3d')]);return analyticCustomReportController[_0x9bcf('0x3a')]({'id':_0x2bd5a1[_0x9bcf('0x3d')]},_0x5250)[_0x9bcf('0x3b')](handleResult(_0x2bd5a1))[_0x9bcf('0x31')](handleError());}}catch(_0x605914){logger[_0x9bcf('0x2e')](JSON[_0x9bcf('0x28')](_0x605914));}return logger['info'](_0x9bcf('0x3e'));};}function getJobs(_0x4e6d0f){var _0x250dcb={};if(_0x4e6d0f){_0x250dcb['id']=_0x4e6d0f;}return db['Schedule'][_0x9bcf('0x3f')]({'where':_0x250dcb,'include':[{'model':db[_0x9bcf('0x10')],'attributes':['id',_0x9bcf('0x19'),_0x9bcf('0xf')],'include':[{'model':db[_0x9bcf('0x40')],'as':_0x9bcf('0x11')}]}]})[_0x9bcf('0x3b')](function(_0x41b384){var _0xadfa5d=_[_0x9bcf('0x41')](manager[_0x9bcf('0x42')]);var _0x5e57a4=_[_0x9bcf('0x43')](_0x41b384,function(_0x5e67c6){return _0x5e67c6['id'][_0x9bcf('0x44')]();});var _0x4b24ea=_[_0x9bcf('0x45')](_0x41b384,function(_0xed658c){return _0xed658c['id'][_0x9bcf('0x44')]();});var _0x74397e=_0x4e6d0f?_0x5e57a4:_[_0x9bcf('0x46')](_0x5e57a4,_0xadfa5d);var _0x1d4209=_[_0x9bcf('0x47')](_0x5e57a4,_0xadfa5d);var _0x4865f2=_[_0x9bcf('0x46')](_0xadfa5d,_0x5e57a4);var _0x4e2ea3;var _0x370802;for(_0x4e2ea3=0x0;_0x4e2ea3<_0x4865f2[_0x9bcf('0x48')]&&!_0x4e6d0f;_0x4e2ea3+=0x1){_0x370802=_0x4865f2[_0x4e2ea3][_0x9bcf('0x44')]();if(manager[_0x9bcf('0x49')](_0x370802)){manager[_0x9bcf('0x4a')](_0x370802);manager[_0x9bcf('0x4b')](_0x370802);logger['info'](_0x9bcf('0x4c'),_0x370802);}}logger[_0x9bcf('0xd')](_0x9bcf('0x4d'),JSON[_0x9bcf('0x28')](_0x74397e));for(_0x4e2ea3=0x0;_0x4e2ea3<_0x74397e['length'];_0x4e2ea3+=0x1){_0x370802=_0x74397e[_0x4e2ea3][_0x9bcf('0x44')]();logger[_0x9bcf('0xd')](_0x9bcf('0x4e'),_0x370802);if(_0x4e6d0f){logger[_0x9bcf('0xd')](_0x9bcf('0x4f'),_0x4e6d0f);logger[_0x9bcf('0xd')](JSON[_0x9bcf('0x28')](_0x4b24ea[_0x370802]));BPromise[_0x9bcf('0x50')]()[_0x9bcf('0x3b')](handleJob(_0x4b24ea[_0x370802]));logger[_0x9bcf('0xd')]('run\x20job',_0x4b24ea[_0x370802][_0x9bcf('0x19')],_0x4b24ea[_0x370802][_0x9bcf('0x51')]);}else if(_0x4b24ea[_0x370802][_0x9bcf('0x51')]&&_0x4b24ea[_0x370802][_0x9bcf('0x52')]&&moment()[_0x9bcf('0x53')](_0x4b24ea[_0x370802]['startAt'],_0x4b24ea[_0x370802][_0x9bcf('0x54')])){manager[_0x9bcf('0x55')](_0x370802,_0x4b24ea[_0x370802][_0x9bcf('0x51')],handleJob(_0x4b24ea[_0x370802]));manager['start'](_0x370802);logger[_0x9bcf('0xd')](_0x9bcf('0x56'),_0x4b24ea[_0x370802]['name'],_0x4b24ea[_0x370802][_0x9bcf('0x51')]);}else if(manager[_0x9bcf('0x49')](_0x370802)){manager[_0x9bcf('0x4a')](_0x370802);manager[_0x9bcf('0x4b')](_0x370802);logger[_0x9bcf('0xd')](_0x9bcf('0x57'),_0x4b24ea[_0x370802]['name'],_0x4b24ea[_0x370802]['cron']);}}for(_0x4e2ea3=0x0,_0x370802;_0x4e2ea3<_0x1d4209[_0x9bcf('0x48')]&&!_0x4e6d0f;_0x4e2ea3+=0x1){_0x370802=_0x1d4209[_0x4e2ea3][_0x9bcf('0x44')]();if(manager['exists'](_0x370802)){if(_0x4b24ea[_0x370802][_0x9bcf('0x51')]&&_0x4b24ea[_0x370802][_0x9bcf('0x52')]&&moment()[_0x9bcf('0x53')](_0x4b24ea[_0x370802][_0x9bcf('0x58')],_0x4b24ea[_0x370802][_0x9bcf('0x54')])){manager[_0x9bcf('0x59')](_0x370802,_0x4b24ea[_0x370802][_0x9bcf('0x51')],handleJob(_0x4b24ea[_0x370802]));logger[_0x9bcf('0xd')](_0x9bcf('0x5a'),_0x4b24ea[_0x370802]['name'],_0x4b24ea[_0x370802][_0x9bcf('0x51')]);}else{manager[_0x9bcf('0x4a')](_0x370802);manager[_0x9bcf('0x4b')](_0x370802);logger['info'](_0x9bcf('0x57'),_0x4b24ea[_0x370802][_0x9bcf('0x19')],_0x4b24ea[_0x370802][_0x9bcf('0x51')]);}}}})[_0x9bcf('0x31')](function(_0x4e7fb3){logger[_0x9bcf('0x2e')](JSON[_0x9bcf('0x28')](_0x4e7fb3));});}exports[_0x9bcf('0x5b')]=function(_0x462b52){return getJobs(_0x462b52);};exports[_0x9bcf('0x5c')]=function(){setInterval(function(){return getJobs();},0x3c*0x3e8);return getJobs();};