Built motion from commit a679711e.|2.6.15
[motion2.git] / server / api / schedule / schedule.controller.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 _0xacde=['fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','options','includeAll','findAll','rows','catch','show','include','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','Scheduler','userProfileId','UserProfileResource','update','params','run','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','end','json','undefined','limit','count','offset','set','reject','save','then','get','destroy','status','error','stack','name','send','index','Schedule','rawAttributes','type','key','model','query','keys','filters','attributes','intersection'];(function(_0x296144,_0x26bbbf){var _0x5329b1=function(_0x5d321b){while(--_0x5d321b){_0x296144['push'](_0x296144['shift']());}};_0x5329b1(++_0x26bbbf);}(_0xacde,0x110));var _0xeacd=function(_0x2b63a0,_0x14fc48){_0x2b63a0=_0x2b63a0-0x0;var _0x4f0434=_0xacde[_0x2b63a0];return _0x4f0434;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0xeacd('0x0'));var moment=require(_0xeacd('0x1'));var BPromise=require(_0xeacd('0x2'));var Mustache=require(_0xeacd('0x3'));var util=require(_0xeacd('0x4'));var path=require(_0xeacd('0x5'));var sox=require(_0xeacd('0x6'));var csv=require(_0xeacd('0x7'));var ejs=require(_0xeacd('0x8'));var fs=require('fs');var fs_extra=require(_0xeacd('0x9'));var _=require(_0xeacd('0xa'));var squel=require(_0xeacd('0xb'));var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require(_0xeacd('0xc'));var toCsv=require(_0xeacd('0x7'));var querystring=require(_0xeacd('0xd'));var Papa=require(_0xeacd('0xe'));var Redis=require('ioredis');var authService=require(_0xeacd('0xf'));var qs=require(_0xeacd('0x10'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xeacd('0x11'));var logger=require(_0xeacd('0x12'))(_0xeacd('0x13'));var utils=require('../../config/utils');var config=require(_0xeacd('0x14'));var licenseUtil=require(_0xeacd('0x15'));var db=require(_0xeacd('0x16'))['db'];function respondWithStatusCode(_0x3de56d,_0x3f11c3){_0x3f11c3=_0x3f11c3||0xcc;return function(_0x116f73){if(_0x116f73){return _0x3de56d[_0xeacd('0x17')](_0x3f11c3);}return _0x3de56d['status'](_0x3f11c3)[_0xeacd('0x18')]();};}function respondWithResult(_0x1b67a6,_0x598256){_0x598256=_0x598256||0xc8;return function(_0x3101e2){if(_0x3101e2){return _0x1b67a6['status'](_0x598256)[_0xeacd('0x19')](_0x3101e2);}};}function respondWithFilteredResult(_0x51a5f6,_0x1f6554){return function(_0x1652a6){if(_0x1652a6){var _0x503802=typeof _0x1f6554['offset']===_0xeacd('0x1a')&&typeof _0x1f6554[_0xeacd('0x1b')]===_0xeacd('0x1a');var _0x5cfb31=_0x1652a6[_0xeacd('0x1c')];var _0x401147=_0x503802?0x0:_0x1f6554[_0xeacd('0x1d')];var _0x217ed7=_0x503802?_0x1652a6['count']:_0x1f6554[_0xeacd('0x1d')]+_0x1f6554[_0xeacd('0x1b')];var _0x25094e;if(_0x217ed7>=_0x5cfb31){_0x217ed7=_0x5cfb31;_0x25094e=0xc8;}else{_0x25094e=0xce;}_0x51a5f6['status'](_0x25094e);return _0x51a5f6[_0xeacd('0x1e')]('Content-Range',_0x401147+'-'+_0x217ed7+'/'+_0x5cfb31)[_0xeacd('0x19')](_0x1652a6);}return null;};}function patchUpdates(_0x9519a4){return function(_0xdf5d5a){try{jsonpatch['apply'](_0xdf5d5a,_0x9519a4,!![]);}catch(_0x3bd689){return BPromise[_0xeacd('0x1f')](_0x3bd689);}return _0xdf5d5a[_0xeacd('0x20')]();};}function saveUpdates(_0x3c9460,_0x25bd92){return function(_0x1ade83){if(_0x1ade83){return _0x1ade83['update'](_0x3c9460)[_0xeacd('0x21')](function(_0x51804b){return _0x51804b;});}return null;};}function removeEntity(_0x17adeb,_0x33f38f){return function(_0x127f9d){if(_0x127f9d){return _0x127f9d['destroy']()[_0xeacd('0x21')](function(){var _0x1e29bb=_0x127f9d[_0xeacd('0x22')]({'plain':!![]});var _0x174593='Scheduler';return db['UserProfileResource'][_0xeacd('0x23')]({'where':{'type':_0x174593,'resourceId':_0x1e29bb['id']}})[_0xeacd('0x21')](function(){return _0x127f9d;});})['then'](function(){_0x17adeb[_0xeacd('0x24')](0xcc)[_0xeacd('0x18')]();});}};}function handleEntityNotFound(_0x39d0c4,_0x29cccf){return function(_0x30e4ad){if(!_0x30e4ad){_0x39d0c4[_0xeacd('0x17')](0x194);}return _0x30e4ad;};}function handleError(_0x8b389d,_0x3147e7){_0x3147e7=_0x3147e7||0x1f4;return function(_0x28630c){logger[_0xeacd('0x25')](_0x28630c[_0xeacd('0x26')]);if(_0x28630c[_0xeacd('0x27')]){delete _0x28630c['name'];}_0x8b389d[_0xeacd('0x24')](_0x3147e7)[_0xeacd('0x28')](_0x28630c);};}exports[_0xeacd('0x29')]=function(_0x4eb7d1,_0x2707e5){var _0x12cd65={},_0xcad8d1={},_0x52e8a5={'count':0x0,'rows':[]};var _0x4b0061=_['map'](db[_0xeacd('0x2a')][_0xeacd('0x2b')],function(_0x39d9b2){return{'name':_0x39d9b2['fieldName'],'type':_0x39d9b2[_0xeacd('0x2c')][_0xeacd('0x2d')]};});_0xcad8d1[_0xeacd('0x2e')]=_['map'](_0x4b0061,'name');_0xcad8d1[_0xeacd('0x2f')]=_[_0xeacd('0x30')](_0x4eb7d1[_0xeacd('0x2f')]);_0xcad8d1[_0xeacd('0x31')]=_['intersection'](_0xcad8d1[_0xeacd('0x2e')],_0xcad8d1[_0xeacd('0x2f')]);_0x12cd65[_0xeacd('0x32')]=_[_0xeacd('0x33')](_0xcad8d1[_0xeacd('0x2e')],qs[_0xeacd('0x34')](_0x4eb7d1[_0xeacd('0x2f')][_0xeacd('0x34')]));_0x12cd65[_0xeacd('0x32')]=_0x12cd65[_0xeacd('0x32')][_0xeacd('0x35')]?_0x12cd65['attributes']:_0xcad8d1[_0xeacd('0x2e')];if(!_0x4eb7d1[_0xeacd('0x2f')][_0xeacd('0x36')](_0xeacd('0x37'))){_0x12cd65[_0xeacd('0x1b')]=qs['limit'](_0x4eb7d1[_0xeacd('0x2f')][_0xeacd('0x1b')]);_0x12cd65[_0xeacd('0x1d')]=qs[_0xeacd('0x1d')](_0x4eb7d1[_0xeacd('0x2f')][_0xeacd('0x1d')]);}_0x12cd65[_0xeacd('0x38')]=qs[_0xeacd('0x39')](_0x4eb7d1[_0xeacd('0x2f')][_0xeacd('0x39')]);_0x12cd65[_0xeacd('0x3a')]=qs[_0xeacd('0x31')](_[_0xeacd('0x3b')](_0x4eb7d1['query'],_0xcad8d1[_0xeacd('0x31')]),_0x4b0061);if(_0x4eb7d1[_0xeacd('0x2f')][_0xeacd('0x3c')]){_0x12cd65['where']=_[_0xeacd('0x3d')](_0x12cd65[_0xeacd('0x3a')],{'$or':_['map'](_0x4b0061,function(_0xb50e84){if(_0xb50e84[_0xeacd('0x2c')]!=='VIRTUAL'){var _0xd1b94d={};_0xd1b94d[_0xb50e84[_0xeacd('0x27')]]={'$like':'%'+_0x4eb7d1[_0xeacd('0x2f')]['filter']+'%'};return _0xd1b94d;}})});}_0x12cd65=_[_0xeacd('0x3d')]({},_0x12cd65,_0x4eb7d1[_0xeacd('0x3e')]);var _0x51a583={'where':_0x12cd65[_0xeacd('0x3a')]};return db[_0xeacd('0x2a')]['count'](_0x51a583)[_0xeacd('0x21')](function(_0x474223){_0x52e8a5[_0xeacd('0x1c')]=_0x474223;if(_0x4eb7d1[_0xeacd('0x2f')][_0xeacd('0x3f')]){_0x12cd65['include']=[{'all':!![]}];}return db[_0xeacd('0x2a')][_0xeacd('0x40')](_0x12cd65);})[_0xeacd('0x21')](function(_0x353af6){_0x52e8a5[_0xeacd('0x41')]=_0x353af6;return _0x52e8a5;})[_0xeacd('0x21')](respondWithFilteredResult(_0x2707e5,_0x12cd65))[_0xeacd('0x42')](handleError(_0x2707e5,null));};exports[_0xeacd('0x43')]=function(_0x458853,_0x92321d){var _0x1c1f99={'raw':![],'where':{'id':_0x458853['params']['id']}},_0x36c146={};_0x36c146[_0xeacd('0x2e')]=_['keys'](db['Schedule'][_0xeacd('0x2b')]);_0x36c146[_0xeacd('0x2f')]=_[_0xeacd('0x30')](_0x458853[_0xeacd('0x2f')]);_0x36c146[_0xeacd('0x31')]=_[_0xeacd('0x33')](_0x36c146[_0xeacd('0x2e')],_0x36c146['query']);_0x1c1f99[_0xeacd('0x32')]=_[_0xeacd('0x33')](_0x36c146[_0xeacd('0x2e')],qs[_0xeacd('0x34')](_0x458853[_0xeacd('0x2f')][_0xeacd('0x34')]));_0x1c1f99[_0xeacd('0x32')]=_0x1c1f99[_0xeacd('0x32')][_0xeacd('0x35')]?_0x1c1f99[_0xeacd('0x32')]:_0x36c146[_0xeacd('0x2e')];if(_0x458853[_0xeacd('0x2f')]['includeAll']){_0x1c1f99[_0xeacd('0x44')]=[{'all':!![]}];}_0x1c1f99=_[_0xeacd('0x3d')]({},_0x1c1f99,_0x458853[_0xeacd('0x3e')]);return db[_0xeacd('0x2a')][_0xeacd('0x45')](_0x1c1f99)['then'](handleEntityNotFound(_0x92321d,null))[_0xeacd('0x21')](respondWithResult(_0x92321d,null))[_0xeacd('0x42')](handleError(_0x92321d,null));};exports['create']=function(_0x32a64e,_0x475dc5){return db[_0xeacd('0x2a')][_0xeacd('0x46')](_0x32a64e[_0xeacd('0x47')],{})[_0xeacd('0x21')](function(_0x5daabb){var _0x239a5f=_0x32a64e[_0xeacd('0x48')][_0xeacd('0x22')]({'plain':!![]});if(!_0x239a5f)throw new Error(_0xeacd('0x49'));if(_0x239a5f[_0xeacd('0x4a')]==='user'){var _0x1a1def=_0x5daabb[_0xeacd('0x22')]({'plain':!![]});var _0x25ba35=_0xeacd('0x4b');return db['UserProfileSection']['find']({'where':{'name':_0x25ba35,'userProfileId':_0x239a5f[_0xeacd('0x4c')]},'raw':!![]})[_0xeacd('0x21')](function(_0x3aee5b){if(_0x3aee5b&&_0x3aee5b['autoAssociation']===0x0){return db[_0xeacd('0x4d')][_0xeacd('0x46')]({'name':_0x1a1def[_0xeacd('0x27')],'resourceId':_0x1a1def['id'],'type':_0x3aee5b[_0xeacd('0x27')],'sectionId':_0x3aee5b['id']},{})[_0xeacd('0x21')](function(){return _0x5daabb;});}else{return _0x5daabb;}})[_0xeacd('0x42')](function(_0x134098){logger[_0xeacd('0x25')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x134098);throw _0x134098;});}return _0x5daabb;})[_0xeacd('0x21')](respondWithResult(_0x475dc5,0xc9))[_0xeacd('0x42')](handleError(_0x475dc5,null));};exports[_0xeacd('0x4e')]=function(_0x41786f,_0x5cb3fd){if(_0x41786f[_0xeacd('0x47')]['id']){delete _0x41786f[_0xeacd('0x47')]['id'];}return db['Schedule'][_0xeacd('0x45')]({'where':{'id':_0x41786f[_0xeacd('0x4f')]['id']}})[_0xeacd('0x21')](handleEntityNotFound(_0x5cb3fd,null))[_0xeacd('0x21')](saveUpdates(_0x41786f[_0xeacd('0x47')],null))[_0xeacd('0x21')](respondWithResult(_0x5cb3fd,null))[_0xeacd('0x42')](handleError(_0x5cb3fd,null));};exports[_0xeacd('0x23')]=function(_0x4eb693,_0x1fc35a){return db['Schedule'][_0xeacd('0x45')]({'where':{'id':_0x4eb693[_0xeacd('0x4f')]['id']}})['then'](handleEntityNotFound(_0x1fc35a,null))[_0xeacd('0x21')](removeEntity(_0x1fc35a,null))[_0xeacd('0x42')](handleError(_0x1fc35a,null));};exports['run']=function(_0x35bdd3,_0xfec734,_0x5c81bb){var _0x2ea81d=require('../../config/schedule');return db[_0xeacd('0x2a')][_0xeacd('0x45')]({'where':{'id':_0x35bdd3[_0xeacd('0x4f')]['id']},'raw':!![]})['then'](handleEntityNotFound(_0xfec734,null))[_0xeacd('0x21')](function(){_0x2ea81d[_0xeacd('0x50')](_0x35bdd3[_0xeacd('0x4f')]['id']);})[_0xeacd('0x21')](respondWithStatusCode(_0xfec734,null))[_0xeacd('0x42')](handleError(_0xfec734,null));};