Built motion from commit (unavailable).|2.5.22
[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 _0x2f73=['message','attachments','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','email\x20sent','stringify','isNil','subtractNumber\x20null\x20or\x20undefined','subtractUnit\x20null\x20or\x20undefined','subtract','YYYY-MM-DD\x20HH:mm:ss','scheduled','output','DefaultReportId','runReport','catch','CustomReportId','exec\x20custom\x20report\x20id:%s','no\x20report\x20id','Schedule','MailServerOut','jobs','map','keyBy','toString','difference','intersection','length','exists','stop','deleteJob','toRun','my\x20id','resolve','run\x20job','cron','active','startAt','endAt','add','start','stop\x20cron','isBetween','update','update\x20cron','run','lodash','moment','bluebird','../logger','../../mysqldb','../../api/analyticCustomReport/analyticCustomReport.controller','client','http','Try\x20to\x20send\x20a\x20mail\x20to','email','MailAccount','Smtp','host','port','secure','auth','user','pass','format','\x22%s\x22\x20<%s>','name','bcc','subtractNumber','subtractUnit','../../files/reports','savename','%s.%s','zip','join','existsSync','status','Sent'];(function(_0x521929,_0x479be5){var _0x632436=function(_0x288290){while(--_0x288290){_0x521929['push'](_0x521929['shift']());}};_0x632436(++_0x479be5);}(_0x2f73,0x18e));var _0x32f7=function(_0x2fa457,_0x7021df){_0x2fa457=_0x2fa457-0x0;var _0x4f250a=_0x2f73[_0x2fa457];return _0x4f250a;};'use strict';var fs=require('fs');var util=require('util');var path=require('path');var _=require(_0x32f7('0x0'));var moment=require(_0x32f7('0x1'));var CronJobManager=require('cron-job-manager');var jayson=require('jayson/promise');var BPromise=require(_0x32f7('0x2'));var logger=require(_0x32f7('0x3'))('schedule');var db=require(_0x32f7('0x4'))['db'];var analyticCustomReportController=require(_0x32f7('0x5'));var analyticDefaultReportController=require('../../api/analyticDefaultReport/analyticDefaultReport.controller');var client=jayson[_0x32f7('0x6')][_0x32f7('0x7')]({'port':0x232b});var manager=new CronJobManager();function handleSendEmail(_0x458bf4,_0x44f891){return function(){logger['info'](_0x32f7('0x8'),_0x458bf4[_0x32f7('0x9')]);var _0x275b2f=!![];var _0x210b92={'tls':{'rejectUnauthorized':![]}};if(_0x458bf4[_0x32f7('0xa')]['Smtp']['service']){_0x210b92['service']=_0x458bf4[_0x32f7('0xa')][_0x32f7('0xb')]['service'];}else{_0x210b92[_0x32f7('0xc')]=_0x458bf4[_0x32f7('0xa')][_0x32f7('0xb')]['host'];_0x210b92[_0x32f7('0xd')]=_0x458bf4[_0x32f7('0xa')][_0x32f7('0xb')][_0x32f7('0xd')];_0x210b92[_0x32f7('0xe')]=_0x458bf4[_0x32f7('0xa')][_0x32f7('0xb')]['secure'];}if(_0x458bf4[_0x32f7('0xa')]['Smtp']['authentication']){_0x210b92[_0x32f7('0xf')]={'user':_0x458bf4[_0x32f7('0xa')][_0x32f7('0xb')][_0x32f7('0x10')],'pass':_0x458bf4[_0x32f7('0xa')][_0x32f7('0xb')][_0x32f7('0x11')]};}var _0x31b2f0={'account':_0x210b92,'message':{'from':util[_0x32f7('0x12')](_0x32f7('0x13'),_0x458bf4[_0x32f7('0xa')][_0x32f7('0x14')],_0x458bf4[_0x32f7('0xa')][_0x32f7('0x9')]||_0x458bf4[_0x32f7('0xa')]['Smtp'][_0x32f7('0x10')]),'to':_0x458bf4['email'],'cc':_0x458bf4['cc'],'bcc':_0x458bf4[_0x32f7('0x15')],'subject':util[_0x32f7('0x12')]('Report\x20%s\x20last\x20%s\x20%s',_0x44f891[_0x32f7('0x14')],_0x458bf4[_0x32f7('0x16')],_0x458bf4[_0x32f7('0x17')])}};var _0x56bbff=path['join'](__dirname,_0x32f7('0x18'));var _0x238cd0=_0x44f891[_0x32f7('0x19')];var _0xb07d11=util[_0x32f7('0x12')](_0x32f7('0x1a'),path['basename'](_0x44f891['savename'],path['extname'](_0x44f891[_0x32f7('0x19')])),_0x32f7('0x1b'));var _0x10b795=path[_0x32f7('0x1c')](_0x56bbff,_0x238cd0);var _0x4a6ef4=path[_0x32f7('0x1c')](_0x56bbff,_0xb07d11);if(fs[_0x32f7('0x1d')](_0x10b795)){_0x44f891[_0x32f7('0x1e')]=_0x32f7('0x1f');_0x31b2f0[_0x32f7('0x20')][_0x32f7('0x21')]=[{'filename':_0x238cd0,'path':_0x10b795}];_0x31b2f0[_0x32f7('0x20')][_0x32f7('0x22')]=util[_0x32f7('0x12')]('Info\x20report:\x0a%s\x0aPlease\x20see\x20the\x20attachments\x0aThis\x20is\x20an\x20automatically\x20generated\x20email\x20–\x20please\x20do\x20not\x20reply\x20to\x20it.\x0a\x0a',JSON['stringify'](_0x44f891,null,0x2));}else if(fs['existsSync'](_0x4a6ef4)){_0x44f891[_0x32f7('0x1e')]=_0x32f7('0x1f');_0x31b2f0['message']['attachments']=[{'filename':_0xb07d11,'path':_0x4a6ef4}];}else{_0x31b2f0[_0x32f7('0x20')]['text']=_0x32f7('0x23');if(!_0x458bf4[_0x32f7('0x24')]){_0x275b2f=![];logger[_0x32f7('0x25')](_0x32f7('0x26'));}}if(_0x275b2f){return client[_0x32f7('0x27')](_0x32f7('0x28'),_0x31b2f0)[_0x32f7('0x29')](function(_0x1df565){if(_0x1df565[_0x32f7('0x2a')]){return logger[_0x32f7('0x2a')](_0x32f7('0x2b'),JSON['stringify'](_0x1df565));}return logger[_0x32f7('0x25')](_0x32f7('0x2c'));})['catch'](function(_0x3f63a9){return logger[_0x32f7('0x2a')](_0x32f7('0x2b'),JSON[_0x32f7('0x2d')](_0x3f63a9));});}};}function handleError(){return function(_0x46850b){return logger[_0x32f7('0x2a')](JSON[_0x32f7('0x2d')](_0x46850b));};}function handleResult(_0x69742){return function(_0x3a8409){logger[_0x32f7('0x25')]('data',JSON[_0x32f7('0x2d')](_0x3a8409));if(_0x69742['MailAccount']&&_0x69742['MailAccount'][_0x32f7('0xb')]&&!_['isNil'](_0x69742[_0x32f7('0x9')])){setTimeout(handleSendEmail(_0x69742,_0x3a8409),0x3c*0x3e8);}};}function handleJob(_0x50487d){return function(){try{if(_[_0x32f7('0x2e')](_0x50487d[_0x32f7('0x16')])){return logger[_0x32f7('0x25')](_0x32f7('0x2f'));}if(_[_0x32f7('0x2e')](_0x50487d[_0x32f7('0x17')])){return logger[_0x32f7('0x25')](_0x32f7('0x30'));}var _0x48a97d={'startDate':moment()[_0x32f7('0x31')](_0x50487d['subtractNumber'],_0x50487d[_0x32f7('0x17')])[_0x32f7('0x12')](_0x32f7('0x32')),'endDate':moment()[_0x32f7('0x12')](_0x32f7('0x32')),'type':_0x32f7('0x33'),'output':_0x50487d[_0x32f7('0x34')],'name':_0x50487d[_0x32f7('0x14')]};if(!_[_0x32f7('0x2e')](_0x50487d[_0x32f7('0x35')])){logger[_0x32f7('0x25')]('exec\x20defaul\x20report\x20id:%s',_0x50487d[_0x32f7('0x35')]);return analyticDefaultReportController[_0x32f7('0x36')]({'id':_0x50487d[_0x32f7('0x35')]},_0x48a97d)[_0x32f7('0x29')](handleResult(_0x50487d))[_0x32f7('0x37')](handleError());}if(!_[_0x32f7('0x2e')](_0x50487d[_0x32f7('0x38')])){logger[_0x32f7('0x25')](_0x32f7('0x39'),_0x50487d['CustomReportId']);return analyticCustomReportController[_0x32f7('0x36')]({'id':_0x50487d[_0x32f7('0x38')]},_0x48a97d)[_0x32f7('0x29')](handleResult(_0x50487d))[_0x32f7('0x37')](handleError());}}catch(_0x40b002){logger['error'](JSON[_0x32f7('0x2d')](_0x40b002));}return logger[_0x32f7('0x25')](_0x32f7('0x3a'));};}function getJobs(_0x4095df){var _0x34cd70={};if(_0x4095df){_0x34cd70['id']=_0x4095df;}return db[_0x32f7('0x3b')]['findAll']({'where':_0x34cd70,'include':[{'model':db['MailAccount'],'attributes':['id',_0x32f7('0x14'),_0x32f7('0x9')],'include':[{'model':db[_0x32f7('0x3c')],'as':_0x32f7('0xb')}]}]})[_0x32f7('0x29')](function(_0x130b30){var _0x59832f=_['keys'](manager[_0x32f7('0x3d')]);var _0xbe817a=_[_0x32f7('0x3e')](_0x130b30,function(_0x31b0f5){return _0x31b0f5['id']['toString']();});var _0x5f9200=_[_0x32f7('0x3f')](_0x130b30,function(_0x329a17){return _0x329a17['id'][_0x32f7('0x40')]();});var _0x50ac0a=_0x4095df?_0xbe817a:_[_0x32f7('0x41')](_0xbe817a,_0x59832f);var _0x113a1b=_[_0x32f7('0x42')](_0xbe817a,_0x59832f);var _0x12eb1f=_[_0x32f7('0x41')](_0x59832f,_0xbe817a);var _0x4ebfcf;var _0x306a6d;for(_0x4ebfcf=0x0;_0x4ebfcf<_0x12eb1f[_0x32f7('0x43')]&&!_0x4095df;_0x4ebfcf+=0x1){_0x306a6d=_0x12eb1f[_0x4ebfcf][_0x32f7('0x40')]();if(manager[_0x32f7('0x44')](_0x306a6d)){manager[_0x32f7('0x45')](_0x306a6d);manager[_0x32f7('0x46')](_0x306a6d);logger[_0x32f7('0x25')]('delete\x20cron',_0x306a6d);}}logger[_0x32f7('0x25')](_0x32f7('0x47'),JSON[_0x32f7('0x2d')](_0x50ac0a));for(_0x4ebfcf=0x0;_0x4ebfcf<_0x50ac0a[_0x32f7('0x43')];_0x4ebfcf+=0x1){_0x306a6d=_0x50ac0a[_0x4ebfcf][_0x32f7('0x40')]();logger[_0x32f7('0x25')]('index',_0x306a6d);if(_0x4095df){logger[_0x32f7('0x25')](_0x32f7('0x48'),_0x4095df);logger[_0x32f7('0x25')](JSON[_0x32f7('0x2d')](_0x5f9200[_0x306a6d]));BPromise[_0x32f7('0x49')]()[_0x32f7('0x29')](handleJob(_0x5f9200[_0x306a6d]));logger[_0x32f7('0x25')](_0x32f7('0x4a'),_0x5f9200[_0x306a6d]['name'],_0x5f9200[_0x306a6d][_0x32f7('0x4b')]);}else if(_0x5f9200[_0x306a6d][_0x32f7('0x4b')]&&_0x5f9200[_0x306a6d][_0x32f7('0x4c')]&&moment()['isBetween'](_0x5f9200[_0x306a6d][_0x32f7('0x4d')],_0x5f9200[_0x306a6d][_0x32f7('0x4e')])){manager[_0x32f7('0x4f')](_0x306a6d,_0x5f9200[_0x306a6d][_0x32f7('0x4b')],handleJob(_0x5f9200[_0x306a6d]));manager[_0x32f7('0x50')](_0x306a6d);logger[_0x32f7('0x25')]('add\x20cron',_0x5f9200[_0x306a6d][_0x32f7('0x14')],_0x5f9200[_0x306a6d][_0x32f7('0x4b')]);}else if(manager['exists'](_0x306a6d)){manager['stop'](_0x306a6d);manager[_0x32f7('0x46')](_0x306a6d);logger[_0x32f7('0x25')](_0x32f7('0x51'),_0x5f9200[_0x306a6d][_0x32f7('0x14')],_0x5f9200[_0x306a6d]['cron']);}}for(_0x4ebfcf=0x0,_0x306a6d;_0x4ebfcf<_0x113a1b['length']&&!_0x4095df;_0x4ebfcf+=0x1){_0x306a6d=_0x113a1b[_0x4ebfcf][_0x32f7('0x40')]();if(manager[_0x32f7('0x44')](_0x306a6d)){if(_0x5f9200[_0x306a6d][_0x32f7('0x4b')]&&_0x5f9200[_0x306a6d][_0x32f7('0x4c')]&&moment()[_0x32f7('0x52')](_0x5f9200[_0x306a6d][_0x32f7('0x4d')],_0x5f9200[_0x306a6d][_0x32f7('0x4e')])){manager[_0x32f7('0x53')](_0x306a6d,_0x5f9200[_0x306a6d][_0x32f7('0x4b')],handleJob(_0x5f9200[_0x306a6d]));logger[_0x32f7('0x25')](_0x32f7('0x54'),_0x5f9200[_0x306a6d][_0x32f7('0x14')],_0x5f9200[_0x306a6d][_0x32f7('0x4b')]);}else{manager[_0x32f7('0x45')](_0x306a6d);manager[_0x32f7('0x46')](_0x306a6d);logger[_0x32f7('0x25')](_0x32f7('0x51'),_0x5f9200[_0x306a6d][_0x32f7('0x14')],_0x5f9200[_0x306a6d][_0x32f7('0x4b')]);}}}})[_0x32f7('0x37')](function(_0x324219){logger[_0x32f7('0x2a')](JSON[_0x32f7('0x2d')](_0x324219));});}exports[_0x32f7('0x55')]=function(_0x42668d){return getJobs(_0x42668d);};exports[_0x32f7('0x50')]=function(){setInterval(function(){return getJobs();},0x3c*0x3e8);return getJobs();};