6a49ee72211c7ce2154df55feb21fa0fc221afc1
[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 _0x46fb=['length','stop','toRun','my\x20id','cron','active','isBetween','endAt','add','start','add\x20cron','exists','stop\x20cron','update','update\x20cron','deleteJob','run','util','path','lodash','cron-job-manager','jayson/promise','bluebird','../logger','../../mysqldb','../../api/analyticCustomReport/analyticCustomReport.controller','../../api/analyticDefaultReport/analyticDefaultReport.controller','client','http','Try\x20to\x20send\x20a\x20mail\x20to','email','MailAccount','Smtp','service','host','port','secure','auth','pass','format','\x22%s\x22\x20<%s>','name','user','bcc','Report\x20%s\x20last\x20%s\x20%s','subtractNumber','subtractUnit','../../files/reports','savename','extname','zip','join','existsSync','Sent','message','attachments','Info\x20report:\x0a%s\x0aPlease\x20see\x20the\x20attachments\x0aThis\x20is\x20an\x20automatically\x20generated\x20email\x20–\x20please\x20do\x20not\x20reply\x20to\x20it.\x0a\x0a','stringify','status','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','subtractUnit\x20null\x20or\x20undefined','subtract','YYYY-MM-DD\x20HH:mm:ss','scheduled','output','DefaultReportId','exec\x20defaul\x20report\x20id:%s','CustomReportId','runReport','Schedule','findAll','MailServerOut','keys','jobs','map','toString','intersection','difference'];(function(_0x3d42d8,_0x4cffaa){var _0x27fc41=function(_0x3a363b){while(--_0x3a363b){_0x3d42d8['push'](_0x3d42d8['shift']());}};_0x27fc41(++_0x4cffaa);}(_0x46fb,0x11f));var _0xb46f=function(_0x4b0f43,_0x2742bb){_0x4b0f43=_0x4b0f43-0x0;var _0x41bfeb=_0x46fb[_0x4b0f43];return _0x41bfeb;};'use strict';var fs=require('fs');var util=require(_0xb46f('0x0'));var path=require(_0xb46f('0x1'));var _=require(_0xb46f('0x2'));var moment=require('moment');var CronJobManager=require(_0xb46f('0x3'));var jayson=require(_0xb46f('0x4'));var BPromise=require(_0xb46f('0x5'));var logger=require(_0xb46f('0x6'))('schedule');var db=require(_0xb46f('0x7'))['db'];var analyticCustomReportController=require(_0xb46f('0x8'));var analyticDefaultReportController=require(_0xb46f('0x9'));var client=jayson[_0xb46f('0xa')][_0xb46f('0xb')]({'port':0x232b});var manager=new CronJobManager();function handleSendEmail(_0x7a170b,_0x17a376){return function(){logger['info'](_0xb46f('0xc'),_0x7a170b[_0xb46f('0xd')]);var _0xe5bf5b=!![];var _0x21f8eb={'tls':{'rejectUnauthorized':![]}};if(_0x7a170b[_0xb46f('0xe')][_0xb46f('0xf')][_0xb46f('0x10')]){_0x21f8eb['service']=_0x7a170b[_0xb46f('0xe')][_0xb46f('0xf')][_0xb46f('0x10')];}else{_0x21f8eb[_0xb46f('0x11')]=_0x7a170b[_0xb46f('0xe')]['Smtp'][_0xb46f('0x11')];_0x21f8eb['port']=_0x7a170b[_0xb46f('0xe')]['Smtp'][_0xb46f('0x12')];_0x21f8eb[_0xb46f('0x13')]=_0x7a170b[_0xb46f('0xe')][_0xb46f('0xf')][_0xb46f('0x13')];}if(_0x7a170b[_0xb46f('0xe')]['Smtp']['authentication']){_0x21f8eb[_0xb46f('0x14')]={'user':_0x7a170b['MailAccount'][_0xb46f('0xf')]['user'],'pass':_0x7a170b['MailAccount'][_0xb46f('0xf')][_0xb46f('0x15')]};}var _0x5daeba={'account':_0x21f8eb,'message':{'from':util[_0xb46f('0x16')](_0xb46f('0x17'),_0x7a170b['MailAccount'][_0xb46f('0x18')],_0x7a170b[_0xb46f('0xe')][_0xb46f('0xd')]||_0x7a170b[_0xb46f('0xe')][_0xb46f('0xf')][_0xb46f('0x19')]),'to':_0x7a170b[_0xb46f('0xd')],'cc':_0x7a170b['cc'],'bcc':_0x7a170b[_0xb46f('0x1a')],'subject':util[_0xb46f('0x16')](_0xb46f('0x1b'),_0x17a376[_0xb46f('0x18')],_0x7a170b[_0xb46f('0x1c')],_0x7a170b[_0xb46f('0x1d')])}};var _0x317398=path['join'](__dirname,_0xb46f('0x1e'));var _0x413c46=_0x17a376[_0xb46f('0x1f')];var _0xc84370=util['format']('%s.%s',path['basename'](_0x17a376['savename'],path[_0xb46f('0x20')](_0x17a376[_0xb46f('0x1f')])),_0xb46f('0x21'));var _0x5d337f=path[_0xb46f('0x22')](_0x317398,_0x413c46);var _0x10dac3=path['join'](_0x317398,_0xc84370);if(fs[_0xb46f('0x23')](_0x5d337f)){_0x17a376['status']=_0xb46f('0x24');_0x5daeba[_0xb46f('0x25')][_0xb46f('0x26')]=[{'filename':_0x413c46,'path':_0x5d337f}];_0x5daeba[_0xb46f('0x25')]['text']=util['format'](_0xb46f('0x27'),JSON[_0xb46f('0x28')](_0x17a376,null,0x2));}else if(fs[_0xb46f('0x23')](_0x10dac3)){_0x17a376[_0xb46f('0x29')]=_0xb46f('0x24');_0x5daeba[_0xb46f('0x25')][_0xb46f('0x26')]=[{'filename':_0xc84370,'path':_0x10dac3}];}else{_0x5daeba['message'][_0xb46f('0x2a')]=_0xb46f('0x2b');if(!_0x7a170b[_0xb46f('0x2c')]){_0xe5bf5b=![];logger[_0xb46f('0x2d')](_0xb46f('0x2e'));}}if(_0xe5bf5b){return client[_0xb46f('0x2f')](_0xb46f('0x30'),_0x5daeba)[_0xb46f('0x31')](function(_0x4323c5){if(_0x4323c5[_0xb46f('0x32')]){return logger[_0xb46f('0x32')](_0xb46f('0x33'),JSON[_0xb46f('0x28')](_0x4323c5));}return logger[_0xb46f('0x2d')]('email\x20sent');})[_0xb46f('0x34')](function(_0x55ff4c){return logger[_0xb46f('0x32')](_0xb46f('0x33'),JSON['stringify'](_0x55ff4c));});}};}function handleError(){return function(_0x58e493){return logger['error'](JSON[_0xb46f('0x28')](_0x58e493));};}function handleResult(_0x3e2a01){return function(_0x4dc7e8){logger[_0xb46f('0x2d')](_0xb46f('0x35'),JSON[_0xb46f('0x28')](_0x4dc7e8));if(_0x3e2a01[_0xb46f('0xe')]&&_0x3e2a01['MailAccount'][_0xb46f('0xf')]&&!_[_0xb46f('0x36')](_0x3e2a01[_0xb46f('0xd')])){setTimeout(handleSendEmail(_0x3e2a01,_0x4dc7e8),0x3c*0x3e8);}};}function handleJob(_0x22076a){return function(){try{if(_[_0xb46f('0x36')](_0x22076a[_0xb46f('0x1c')])){return logger['info']('subtractNumber\x20null\x20or\x20undefined');}if(_['isNil'](_0x22076a['subtractUnit'])){return logger[_0xb46f('0x2d')](_0xb46f('0x37'));}var _0x13ff24={'startDate':moment()[_0xb46f('0x38')](_0x22076a[_0xb46f('0x1c')],_0x22076a[_0xb46f('0x1d')])[_0xb46f('0x16')](_0xb46f('0x39')),'endDate':moment()[_0xb46f('0x16')](_0xb46f('0x39')),'type':_0xb46f('0x3a'),'output':_0x22076a[_0xb46f('0x3b')],'name':_0x22076a[_0xb46f('0x18')]};if(!_[_0xb46f('0x36')](_0x22076a[_0xb46f('0x3c')])){logger[_0xb46f('0x2d')](_0xb46f('0x3d'),_0x22076a['DefaultReportId']);return analyticDefaultReportController['runReport']({'id':_0x22076a['DefaultReportId']},_0x13ff24)[_0xb46f('0x31')](handleResult(_0x22076a))[_0xb46f('0x34')](handleError());}if(!_[_0xb46f('0x36')](_0x22076a['CustomReportId'])){logger[_0xb46f('0x2d')]('exec\x20custom\x20report\x20id:%s',_0x22076a[_0xb46f('0x3e')]);return analyticCustomReportController[_0xb46f('0x3f')]({'id':_0x22076a[_0xb46f('0x3e')]},_0x13ff24)['then'](handleResult(_0x22076a))[_0xb46f('0x34')](handleError());}}catch(_0x31b1ec){logger['error'](JSON['stringify'](_0x31b1ec));}return logger[_0xb46f('0x2d')]('no\x20report\x20id');};}function getJobs(_0xf00e2){var _0x5a9805={};if(_0xf00e2){_0x5a9805['id']=_0xf00e2;}return db[_0xb46f('0x40')][_0xb46f('0x41')]({'where':_0x5a9805,'include':[{'model':db[_0xb46f('0xe')],'attributes':['id',_0xb46f('0x18'),'email'],'include':[{'model':db[_0xb46f('0x42')],'as':_0xb46f('0xf')}]}]})[_0xb46f('0x31')](function(_0x2d769a){var _0x25a5f6=_[_0xb46f('0x43')](manager[_0xb46f('0x44')]);var _0x252c7d=_[_0xb46f('0x45')](_0x2d769a,function(_0x5af50d){return _0x5af50d['id'][_0xb46f('0x46')]();});var _0x4a2c2c=_['keyBy'](_0x2d769a,function(_0x3d256f){return _0x3d256f['id'][_0xb46f('0x46')]();});var _0x5b83a6=_0xf00e2?_0x252c7d:_['difference'](_0x252c7d,_0x25a5f6);var _0xb8112b=_[_0xb46f('0x47')](_0x252c7d,_0x25a5f6);var _0x5d4365=_[_0xb46f('0x48')](_0x25a5f6,_0x252c7d);var _0x21ece2;var _0xe11119;for(_0x21ece2=0x0;_0x21ece2<_0x5d4365[_0xb46f('0x49')]&&!_0xf00e2;_0x21ece2+=0x1){_0xe11119=_0x5d4365[_0x21ece2][_0xb46f('0x46')]();if(manager['exists'](_0xe11119)){manager[_0xb46f('0x4a')](_0xe11119);manager['deleteJob'](_0xe11119);logger[_0xb46f('0x2d')]('delete\x20cron',_0xe11119);}}logger[_0xb46f('0x2d')](_0xb46f('0x4b'),JSON[_0xb46f('0x28')](_0x5b83a6));for(_0x21ece2=0x0;_0x21ece2<_0x5b83a6[_0xb46f('0x49')];_0x21ece2+=0x1){_0xe11119=_0x5b83a6[_0x21ece2][_0xb46f('0x46')]();logger['info']('index',_0xe11119);if(_0xf00e2){logger[_0xb46f('0x2d')](_0xb46f('0x4c'),_0xf00e2);logger[_0xb46f('0x2d')](JSON[_0xb46f('0x28')](_0x4a2c2c[_0xe11119]));BPromise['resolve']()[_0xb46f('0x31')](handleJob(_0x4a2c2c[_0xe11119]));logger[_0xb46f('0x2d')]('run\x20job',_0x4a2c2c[_0xe11119][_0xb46f('0x18')],_0x4a2c2c[_0xe11119][_0xb46f('0x4d')]);}else if(_0x4a2c2c[_0xe11119][_0xb46f('0x4d')]&&_0x4a2c2c[_0xe11119][_0xb46f('0x4e')]&&moment()[_0xb46f('0x4f')](_0x4a2c2c[_0xe11119]['startAt'],_0x4a2c2c[_0xe11119][_0xb46f('0x50')])){manager[_0xb46f('0x51')](_0xe11119,_0x4a2c2c[_0xe11119][_0xb46f('0x4d')],handleJob(_0x4a2c2c[_0xe11119]));manager[_0xb46f('0x52')](_0xe11119);logger['info'](_0xb46f('0x53'),_0x4a2c2c[_0xe11119][_0xb46f('0x18')],_0x4a2c2c[_0xe11119][_0xb46f('0x4d')]);}else if(manager[_0xb46f('0x54')](_0xe11119)){manager['stop'](_0xe11119);manager['deleteJob'](_0xe11119);logger['info'](_0xb46f('0x55'),_0x4a2c2c[_0xe11119][_0xb46f('0x18')],_0x4a2c2c[_0xe11119]['cron']);}}for(_0x21ece2=0x0,_0xe11119;_0x21ece2<_0xb8112b['length']&&!_0xf00e2;_0x21ece2+=0x1){_0xe11119=_0xb8112b[_0x21ece2][_0xb46f('0x46')]();if(manager[_0xb46f('0x54')](_0xe11119)){if(_0x4a2c2c[_0xe11119][_0xb46f('0x4d')]&&_0x4a2c2c[_0xe11119][_0xb46f('0x4e')]&&moment()[_0xb46f('0x4f')](_0x4a2c2c[_0xe11119]['startAt'],_0x4a2c2c[_0xe11119]['endAt'])){manager[_0xb46f('0x56')](_0xe11119,_0x4a2c2c[_0xe11119][_0xb46f('0x4d')],handleJob(_0x4a2c2c[_0xe11119]));logger[_0xb46f('0x2d')](_0xb46f('0x57'),_0x4a2c2c[_0xe11119]['name'],_0x4a2c2c[_0xe11119][_0xb46f('0x4d')]);}else{manager[_0xb46f('0x4a')](_0xe11119);manager[_0xb46f('0x58')](_0xe11119);logger[_0xb46f('0x2d')](_0xb46f('0x55'),_0x4a2c2c[_0xe11119][_0xb46f('0x18')],_0x4a2c2c[_0xe11119][_0xb46f('0x4d')]);}}}})['catch'](function(_0x466fbe){logger[_0xb46f('0x32')](JSON[_0xb46f('0x28')](_0x466fbe));});}exports[_0xb46f('0x59')]=function(_0xfeb3f7){return getJobs(_0xfeb3f7);};exports[_0xb46f('0x52')]=function(){setInterval(function(){return getJobs();},0x3c*0x3e8);return getJobs();};