Built motion from commit 449495f6.|2.6.27
[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 _0x03f4=['key','model','keys','query','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','filters','pick','filter','where','merge','options','includeAll','include','rows','show','params','Schedule','find','catch','create','body','role','UserProfileSection','userProfileId','autoAssociation','run','../../config/schedule','eml-format','zip-dir','request-promise','bluebird','mustache','util','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','status','end','json','offset','undefined','count','limit','set','Content-Range','apply','save','update','then','destroy','get','Scheduler','UserProfileResource','sendStatus','error','stack','name','send','index','map','fieldName','type'];(function(_0x49d41c,_0x2bdc35){var _0xb64c5f=function(_0x28f535){while(--_0x28f535){_0x49d41c['push'](_0x49d41c['shift']());}};_0xb64c5f(++_0x2bdc35);}(_0x03f4,0xce));var _0x403f=function(_0xc3519a,_0x2bf606){_0xc3519a=_0xc3519a-0x0;var _0x1eda04=_0x03f4[_0xc3519a];return _0x1eda04;};'use strict';var emlformat=require(_0x403f('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x403f('0x1'));var jsonpatch=require('fast-json-patch');var rp=require(_0x403f('0x2'));var moment=require('moment');var BPromise=require(_0x403f('0x3'));var Mustache=require(_0x403f('0x4'));var util=require(_0x403f('0x5'));var path=require('path');var sox=require(_0x403f('0x6'));var csv=require(_0x403f('0x7'));var ejs=require(_0x403f('0x8'));var fs=require('fs');var fs_extra=require(_0x403f('0x9'));var _=require(_0x403f('0xa'));var squel=require(_0x403f('0xb'));var crypto=require(_0x403f('0xc'));var jsforce=require(_0x403f('0xd'));var deskjs=require(_0x403f('0xe'));var toCsv=require('to-csv');var querystring=require(_0x403f('0xf'));var Papa=require('papaparse');var Redis=require(_0x403f('0x10'));var authService=require(_0x403f('0x11'));var qs=require(_0x403f('0x12'));var as=require(_0x403f('0x13'));var hardwareService=require(_0x403f('0x14'));var logger=require(_0x403f('0x15'))(_0x403f('0x16'));var utils=require(_0x403f('0x17'));var config=require(_0x403f('0x18'));var licenseUtil=require('../../config/license/util');var db=require(_0x403f('0x19'))['db'];function respondWithStatusCode(_0x11b06d,_0x8a85a0){_0x8a85a0=_0x8a85a0||0xcc;return function(_0xed4e75){if(_0xed4e75){return _0x11b06d['sendStatus'](_0x8a85a0);}return _0x11b06d[_0x403f('0x1a')](_0x8a85a0)[_0x403f('0x1b')]();};}function respondWithResult(_0x40ff07,_0x4a7e5c){_0x4a7e5c=_0x4a7e5c||0xc8;return function(_0x48f6b9){if(_0x48f6b9){return _0x40ff07['status'](_0x4a7e5c)[_0x403f('0x1c')](_0x48f6b9);}};}function respondWithFilteredResult(_0x2d3184,_0x510b79){return function(_0xf11dcd){if(_0xf11dcd){var _0x4a6853=typeof _0x510b79[_0x403f('0x1d')]===_0x403f('0x1e')&&typeof _0x510b79['limit']==='undefined';var _0x5914c2=_0xf11dcd[_0x403f('0x1f')];var _0x5da64b=_0x4a6853?0x0:_0x510b79['offset'];var _0x4ae3d2=_0x4a6853?_0xf11dcd[_0x403f('0x1f')]:_0x510b79[_0x403f('0x1d')]+_0x510b79[_0x403f('0x20')];var _0x39681c;if(_0x4ae3d2>=_0x5914c2){_0x4ae3d2=_0x5914c2;_0x39681c=0xc8;}else{_0x39681c=0xce;}_0x2d3184[_0x403f('0x1a')](_0x39681c);return _0x2d3184[_0x403f('0x21')](_0x403f('0x22'),_0x5da64b+'-'+_0x4ae3d2+'/'+_0x5914c2)[_0x403f('0x1c')](_0xf11dcd);}return null;};}function patchUpdates(_0x1661d8){return function(_0x1dde88){try{jsonpatch[_0x403f('0x23')](_0x1dde88,_0x1661d8,!![]);}catch(_0x3dc4a7){return BPromise['reject'](_0x3dc4a7);}return _0x1dde88[_0x403f('0x24')]();};}function saveUpdates(_0x3e93d1,_0x3825af){return function(_0x46ff78){if(_0x46ff78){return _0x46ff78[_0x403f('0x25')](_0x3e93d1)[_0x403f('0x26')](function(_0x5c33bf){return _0x5c33bf;});}return null;};}function removeEntity(_0x2e4fe6,_0x2977aa){return function(_0x21500c){if(_0x21500c){return _0x21500c[_0x403f('0x27')]()[_0x403f('0x26')](function(){var _0x52523c=_0x21500c[_0x403f('0x28')]({'plain':!![]});var _0x562462=_0x403f('0x29');return db[_0x403f('0x2a')][_0x403f('0x27')]({'where':{'type':_0x562462,'resourceId':_0x52523c['id']}})[_0x403f('0x26')](function(){return _0x21500c;});})[_0x403f('0x26')](function(){_0x2e4fe6[_0x403f('0x1a')](0xcc)[_0x403f('0x1b')]();});}};}function handleEntityNotFound(_0x42aee9,_0x2de6d5){return function(_0x3db145){if(!_0x3db145){_0x42aee9[_0x403f('0x2b')](0x194);}return _0x3db145;};}function handleError(_0x50c688,_0x3a1585){_0x3a1585=_0x3a1585||0x1f4;return function(_0x55e20d){logger[_0x403f('0x2c')](_0x55e20d[_0x403f('0x2d')]);if(_0x55e20d[_0x403f('0x2e')]){delete _0x55e20d[_0x403f('0x2e')];}_0x50c688[_0x403f('0x1a')](_0x3a1585)[_0x403f('0x2f')](_0x55e20d);};}exports[_0x403f('0x30')]=function(_0x1c7f41,_0x306dbb){var _0x5793eb={},_0x55cb5d={},_0x386611={'count':0x0,'rows':[]};var _0x3a9a4b=_[_0x403f('0x31')](db['Schedule']['rawAttributes'],function(_0x16af04){return{'name':_0x16af04[_0x403f('0x32')],'type':_0x16af04[_0x403f('0x33')][_0x403f('0x34')]};});_0x55cb5d[_0x403f('0x35')]=_['map'](_0x3a9a4b,_0x403f('0x2e'));_0x55cb5d['query']=_[_0x403f('0x36')](_0x1c7f41[_0x403f('0x37')]);_0x55cb5d['filters']=_[_0x403f('0x38')](_0x55cb5d[_0x403f('0x35')],_0x55cb5d[_0x403f('0x37')]);_0x5793eb[_0x403f('0x39')]=_[_0x403f('0x38')](_0x55cb5d[_0x403f('0x35')],qs[_0x403f('0x3a')](_0x1c7f41['query'][_0x403f('0x3a')]));_0x5793eb[_0x403f('0x39')]=_0x5793eb['attributes'][_0x403f('0x3b')]?_0x5793eb['attributes']:_0x55cb5d['model'];if(!_0x1c7f41['query'][_0x403f('0x3c')](_0x403f('0x3d'))){_0x5793eb[_0x403f('0x20')]=qs[_0x403f('0x20')](_0x1c7f41[_0x403f('0x37')][_0x403f('0x20')]);_0x5793eb[_0x403f('0x1d')]=qs[_0x403f('0x1d')](_0x1c7f41['query'][_0x403f('0x1d')]);}_0x5793eb[_0x403f('0x3e')]=qs[_0x403f('0x3f')](_0x1c7f41['query']['sort']);_0x5793eb['where']=qs[_0x403f('0x40')](_[_0x403f('0x41')](_0x1c7f41[_0x403f('0x37')],_0x55cb5d['filters']),_0x3a9a4b);if(_0x1c7f41[_0x403f('0x37')][_0x403f('0x42')]){_0x5793eb[_0x403f('0x43')]=_[_0x403f('0x44')](_0x5793eb[_0x403f('0x43')],{'$or':_['map'](_0x3a9a4b,function(_0x4ae18c){if(_0x4ae18c[_0x403f('0x33')]!=='VIRTUAL'){var _0x17f0f4={};_0x17f0f4[_0x4ae18c['name']]={'$like':'%'+_0x1c7f41[_0x403f('0x37')][_0x403f('0x42')]+'%'};return _0x17f0f4;}})});}_0x5793eb=_['merge']({},_0x5793eb,_0x1c7f41[_0x403f('0x45')]);var _0x30da2b={'where':_0x5793eb[_0x403f('0x43')]};return db['Schedule'][_0x403f('0x1f')](_0x30da2b)[_0x403f('0x26')](function(_0x24c37e){_0x386611[_0x403f('0x1f')]=_0x24c37e;if(_0x1c7f41[_0x403f('0x37')][_0x403f('0x46')]){_0x5793eb[_0x403f('0x47')]=[{'all':!![]}];}return db['Schedule']['findAll'](_0x5793eb);})[_0x403f('0x26')](function(_0x5d8101){_0x386611[_0x403f('0x48')]=_0x5d8101;return _0x386611;})[_0x403f('0x26')](respondWithFilteredResult(_0x306dbb,_0x5793eb))['catch'](handleError(_0x306dbb,null));};exports[_0x403f('0x49')]=function(_0x52901b,_0x2346e3){var _0xc31c98={'raw':![],'where':{'id':_0x52901b[_0x403f('0x4a')]['id']}},_0x51b7eb={};_0x51b7eb[_0x403f('0x35')]=_[_0x403f('0x36')](db[_0x403f('0x4b')]['rawAttributes']);_0x51b7eb[_0x403f('0x37')]=_[_0x403f('0x36')](_0x52901b[_0x403f('0x37')]);_0x51b7eb[_0x403f('0x40')]=_['intersection'](_0x51b7eb['model'],_0x51b7eb[_0x403f('0x37')]);_0xc31c98[_0x403f('0x39')]=_[_0x403f('0x38')](_0x51b7eb[_0x403f('0x35')],qs[_0x403f('0x3a')](_0x52901b['query'][_0x403f('0x3a')]));_0xc31c98[_0x403f('0x39')]=_0xc31c98[_0x403f('0x39')]['length']?_0xc31c98[_0x403f('0x39')]:_0x51b7eb['model'];if(_0x52901b[_0x403f('0x37')][_0x403f('0x46')]){_0xc31c98['include']=[{'all':!![]}];}_0xc31c98=_[_0x403f('0x44')]({},_0xc31c98,_0x52901b[_0x403f('0x45')]);return db[_0x403f('0x4b')][_0x403f('0x4c')](_0xc31c98)[_0x403f('0x26')](handleEntityNotFound(_0x2346e3,null))[_0x403f('0x26')](respondWithResult(_0x2346e3,null))[_0x403f('0x4d')](handleError(_0x2346e3,null));};exports['create']=function(_0x2febdd,_0x1b9101){return db[_0x403f('0x4b')][_0x403f('0x4e')](_0x2febdd[_0x403f('0x4f')],{})[_0x403f('0x26')](function(_0x5d12e6){var _0x224f2f=_0x2febdd['user'][_0x403f('0x28')]({'plain':!![]});if(!_0x224f2f)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x224f2f[_0x403f('0x50')]==='user'){var _0x46d865=_0x5d12e6[_0x403f('0x28')]({'plain':!![]});var _0xd3da08=_0x403f('0x29');return db[_0x403f('0x51')][_0x403f('0x4c')]({'where':{'name':_0xd3da08,'userProfileId':_0x224f2f[_0x403f('0x52')]},'raw':!![]})[_0x403f('0x26')](function(_0x3bb965){if(_0x3bb965&&_0x3bb965[_0x403f('0x53')]===0x0){return db['UserProfileResource'][_0x403f('0x4e')]({'name':_0x46d865['name'],'resourceId':_0x46d865['id'],'type':_0x3bb965[_0x403f('0x2e')],'sectionId':_0x3bb965['id']},{})[_0x403f('0x26')](function(){return _0x5d12e6;});}else{return _0x5d12e6;}})[_0x403f('0x4d')](function(_0xa24407){logger[_0x403f('0x2c')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0xa24407);throw _0xa24407;});}return _0x5d12e6;})[_0x403f('0x26')](respondWithResult(_0x1b9101,0xc9))['catch'](handleError(_0x1b9101,null));};exports[_0x403f('0x25')]=function(_0x431708,_0x3679de){if(_0x431708['body']['id']){delete _0x431708['body']['id'];}return db[_0x403f('0x4b')][_0x403f('0x4c')]({'where':{'id':_0x431708[_0x403f('0x4a')]['id']}})['then'](handleEntityNotFound(_0x3679de,null))[_0x403f('0x26')](saveUpdates(_0x431708[_0x403f('0x4f')],null))[_0x403f('0x26')](respondWithResult(_0x3679de,null))['catch'](handleError(_0x3679de,null));};exports[_0x403f('0x27')]=function(_0x5c101b,_0x3e71e6){return db[_0x403f('0x4b')][_0x403f('0x4c')]({'where':{'id':_0x5c101b[_0x403f('0x4a')]['id']}})[_0x403f('0x26')](handleEntityNotFound(_0x3e71e6,null))[_0x403f('0x26')](removeEntity(_0x3e71e6,null))[_0x403f('0x4d')](handleError(_0x3e71e6,null));};exports[_0x403f('0x54')]=function(_0x2616de,_0xf94f68,_0x5584b4){var _0x4b8e07=require(_0x403f('0x55'));return db[_0x403f('0x4b')]['find']({'where':{'id':_0x2616de['params']['id']},'raw':!![]})['then'](handleEntityNotFound(_0xf94f68,null))[_0x403f('0x26')](function(){_0x4b8e07[_0x403f('0x54')](_0x2616de[_0x403f('0x4a')]['id']);})[_0x403f('0x26')](respondWithStatusCode(_0xf94f68,null))[_0x403f('0x4d')](handleError(_0xf94f68,null));};