165d8788ad21a9a260b83066321d934eeedd9dd3
[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 _0x68f9=['jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','end','offset','undefined','count','limit','status','set','json','apply','reject','then','destroy','get','Scheduler','UserProfileResource','error','stack','name','send','index','Schedule','rawAttributes','type','key','model','map','keys','query','intersection','fields','attributes','length','order','sort','where','filters','pick','filter','merge','VIRTUAL','options','includeAll','include','findAll','rows','show','find','catch','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','params','run','eml-format','request-promise','moment','bluebird','util','path','ejs','lodash','crypto'];(function(_0x486f4a,_0x478dc7){var _0x135d99=function(_0x434008){while(--_0x434008){_0x486f4a['push'](_0x486f4a['shift']());}};_0x135d99(++_0x478dc7);}(_0x68f9,0x1e9));var _0x968f=function(_0x45c591,_0x20280a){_0x45c591=_0x45c591-0x0;var _0x364e23=_0x68f9[_0x45c591];return _0x364e23;};'use strict';var emlformat=require(_0x968f('0x0'));var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0x968f('0x1'));var moment=require(_0x968f('0x2'));var BPromise=require(_0x968f('0x3'));var Mustache=require('mustache');var util=require(_0x968f('0x4'));var path=require(_0x968f('0x5'));var sox=require('sox');var csv=require('to-csv');var ejs=require(_0x968f('0x6'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x968f('0x7'));var squel=require('squel');var crypto=require(_0x968f('0x8'));var jsforce=require(_0x968f('0x9'));var deskjs=require(_0x968f('0xa'));var toCsv=require('to-csv');var querystring=require(_0x968f('0xb'));var Papa=require(_0x968f('0xc'));var Redis=require(_0x968f('0xd'));var authService=require(_0x968f('0xe'));var qs=require(_0x968f('0xf'));var as=require(_0x968f('0x10'));var hardwareService=require(_0x968f('0x11'));var logger=require('../../config/logger')(_0x968f('0x12'));var utils=require(_0x968f('0x13'));var config=require(_0x968f('0x14'));var licenseUtil=require(_0x968f('0x15'));var db=require(_0x968f('0x16'))['db'];function respondWithStatusCode(_0x115332,_0x13b6d6){_0x13b6d6=_0x13b6d6||0xcc;return function(_0x210bee){if(_0x210bee){return _0x115332['sendStatus'](_0x13b6d6);}return _0x115332['status'](_0x13b6d6)[_0x968f('0x17')]();};}function respondWithResult(_0x504a57,_0x51c7ed){_0x51c7ed=_0x51c7ed||0xc8;return function(_0x4b78ef){if(_0x4b78ef){return _0x504a57['status'](_0x51c7ed)['json'](_0x4b78ef);}};}function respondWithFilteredResult(_0x136e19,_0x2150eb){return function(_0x1e2037){if(_0x1e2037){var _0x369ce3=typeof _0x2150eb[_0x968f('0x18')]==='undefined'&&typeof _0x2150eb['limit']===_0x968f('0x19');var _0x275d53=_0x1e2037[_0x968f('0x1a')];var _0x4ef5ef=_0x369ce3?0x0:_0x2150eb[_0x968f('0x18')];var _0x316ba1=_0x369ce3?_0x1e2037[_0x968f('0x1a')]:_0x2150eb[_0x968f('0x18')]+_0x2150eb[_0x968f('0x1b')];var _0x93637b;if(_0x316ba1>=_0x275d53){_0x316ba1=_0x275d53;_0x93637b=0xc8;}else{_0x93637b=0xce;}_0x136e19[_0x968f('0x1c')](_0x93637b);return _0x136e19[_0x968f('0x1d')]('Content-Range',_0x4ef5ef+'-'+_0x316ba1+'/'+_0x275d53)[_0x968f('0x1e')](_0x1e2037);}return null;};}function patchUpdates(_0x5b8ed2){return function(_0x2105fe){try{jsonpatch[_0x968f('0x1f')](_0x2105fe,_0x5b8ed2,!![]);}catch(_0x5616f3){return BPromise[_0x968f('0x20')](_0x5616f3);}return _0x2105fe['save']();};}function saveUpdates(_0x805eb,_0x4dae08){return function(_0x7cb335){if(_0x7cb335){return _0x7cb335['update'](_0x805eb)[_0x968f('0x21')](function(_0x2482df){return _0x2482df;});}return null;};}function removeEntity(_0x3cc69d,_0x408a8e){return function(_0x11211a){if(_0x11211a){return _0x11211a[_0x968f('0x22')]()['then'](function(){var _0x1af1ac=_0x11211a[_0x968f('0x23')]({'plain':!![]});var _0x18767f=_0x968f('0x24');return db[_0x968f('0x25')]['destroy']({'where':{'type':_0x18767f,'resourceId':_0x1af1ac['id']}})[_0x968f('0x21')](function(){return _0x11211a;});})[_0x968f('0x21')](function(){_0x3cc69d[_0x968f('0x1c')](0xcc)[_0x968f('0x17')]();});}};}function handleEntityNotFound(_0x59220d,_0x4c1fbc){return function(_0x23d8cf){if(!_0x23d8cf){_0x59220d['sendStatus'](0x194);}return _0x23d8cf;};}function handleError(_0x114de2,_0x70d2d2){_0x70d2d2=_0x70d2d2||0x1f4;return function(_0xfe7a73){logger[_0x968f('0x26')](_0xfe7a73[_0x968f('0x27')]);if(_0xfe7a73[_0x968f('0x28')]){delete _0xfe7a73[_0x968f('0x28')];}_0x114de2[_0x968f('0x1c')](_0x70d2d2)[_0x968f('0x29')](_0xfe7a73);};}exports[_0x968f('0x2a')]=function(_0x1be245,_0x16a436){var _0x3e2b89={},_0x194ae8={},_0x13ab28={'count':0x0,'rows':[]};var _0x30216f=_['map'](db[_0x968f('0x2b')][_0x968f('0x2c')],function(_0x5a75d3){return{'name':_0x5a75d3['fieldName'],'type':_0x5a75d3[_0x968f('0x2d')][_0x968f('0x2e')]};});_0x194ae8[_0x968f('0x2f')]=_[_0x968f('0x30')](_0x30216f,'name');_0x194ae8['query']=_[_0x968f('0x31')](_0x1be245[_0x968f('0x32')]);_0x194ae8['filters']=_[_0x968f('0x33')](_0x194ae8[_0x968f('0x2f')],_0x194ae8[_0x968f('0x32')]);_0x3e2b89['attributes']=_['intersection'](_0x194ae8[_0x968f('0x2f')],qs[_0x968f('0x34')](_0x1be245[_0x968f('0x32')][_0x968f('0x34')]));_0x3e2b89[_0x968f('0x35')]=_0x3e2b89[_0x968f('0x35')][_0x968f('0x36')]?_0x3e2b89['attributes']:_0x194ae8['model'];if(!_0x1be245['query']['hasOwnProperty']('nolimit')){_0x3e2b89[_0x968f('0x1b')]=qs[_0x968f('0x1b')](_0x1be245[_0x968f('0x32')]['limit']);_0x3e2b89[_0x968f('0x18')]=qs[_0x968f('0x18')](_0x1be245['query'][_0x968f('0x18')]);}_0x3e2b89[_0x968f('0x37')]=qs[_0x968f('0x38')](_0x1be245[_0x968f('0x32')][_0x968f('0x38')]);_0x3e2b89[_0x968f('0x39')]=qs[_0x968f('0x3a')](_[_0x968f('0x3b')](_0x1be245[_0x968f('0x32')],_0x194ae8[_0x968f('0x3a')]),_0x30216f);if(_0x1be245[_0x968f('0x32')][_0x968f('0x3c')]){_0x3e2b89['where']=_[_0x968f('0x3d')](_0x3e2b89['where'],{'$or':_[_0x968f('0x30')](_0x30216f,function(_0x223581){if(_0x223581[_0x968f('0x2d')]!==_0x968f('0x3e')){var _0xcf0bd3={};_0xcf0bd3[_0x223581[_0x968f('0x28')]]={'$like':'%'+_0x1be245[_0x968f('0x32')][_0x968f('0x3c')]+'%'};return _0xcf0bd3;}})});}_0x3e2b89=_[_0x968f('0x3d')]({},_0x3e2b89,_0x1be245[_0x968f('0x3f')]);var _0xc03c2b={'where':_0x3e2b89['where']};return db[_0x968f('0x2b')]['count'](_0xc03c2b)['then'](function(_0x2562b1){_0x13ab28[_0x968f('0x1a')]=_0x2562b1;if(_0x1be245[_0x968f('0x32')][_0x968f('0x40')]){_0x3e2b89[_0x968f('0x41')]=[{'all':!![]}];}return db[_0x968f('0x2b')][_0x968f('0x42')](_0x3e2b89);})[_0x968f('0x21')](function(_0x502fdf){_0x13ab28[_0x968f('0x43')]=_0x502fdf;return _0x13ab28;})['then'](respondWithFilteredResult(_0x16a436,_0x3e2b89))['catch'](handleError(_0x16a436,null));};exports[_0x968f('0x44')]=function(_0x5b44a0,_0x10eb3a){var _0x1e2d34={'raw':![],'where':{'id':_0x5b44a0['params']['id']}},_0x4e5bb2={};_0x4e5bb2[_0x968f('0x2f')]=_[_0x968f('0x31')](db[_0x968f('0x2b')]['rawAttributes']);_0x4e5bb2[_0x968f('0x32')]=_[_0x968f('0x31')](_0x5b44a0[_0x968f('0x32')]);_0x4e5bb2[_0x968f('0x3a')]=_[_0x968f('0x33')](_0x4e5bb2[_0x968f('0x2f')],_0x4e5bb2[_0x968f('0x32')]);_0x1e2d34[_0x968f('0x35')]=_[_0x968f('0x33')](_0x4e5bb2['model'],qs[_0x968f('0x34')](_0x5b44a0[_0x968f('0x32')]['fields']));_0x1e2d34['attributes']=_0x1e2d34[_0x968f('0x35')]['length']?_0x1e2d34[_0x968f('0x35')]:_0x4e5bb2[_0x968f('0x2f')];if(_0x5b44a0['query']['includeAll']){_0x1e2d34[_0x968f('0x41')]=[{'all':!![]}];}_0x1e2d34=_[_0x968f('0x3d')]({},_0x1e2d34,_0x5b44a0[_0x968f('0x3f')]);return db[_0x968f('0x2b')][_0x968f('0x45')](_0x1e2d34)[_0x968f('0x21')](handleEntityNotFound(_0x10eb3a,null))['then'](respondWithResult(_0x10eb3a,null))[_0x968f('0x46')](handleError(_0x10eb3a,null));};exports[_0x968f('0x47')]=function(_0x23da25,_0x2af513){return db[_0x968f('0x2b')][_0x968f('0x47')](_0x23da25[_0x968f('0x48')],{})['then'](function(_0x1baffa){var _0x4dd3bf=_0x23da25[_0x968f('0x49')]['get']({'plain':!![]});if(!_0x4dd3bf)throw new Error(_0x968f('0x4a'));if(_0x4dd3bf[_0x968f('0x4b')]===_0x968f('0x49')){var _0x4f961e=_0x1baffa[_0x968f('0x23')]({'plain':!![]});var _0x3672af='Scheduler';return db[_0x968f('0x4c')]['find']({'where':{'name':_0x3672af,'userProfileId':_0x4dd3bf[_0x968f('0x4d')]},'raw':!![]})[_0x968f('0x21')](function(_0x2ec4c6){if(_0x2ec4c6&&_0x2ec4c6[_0x968f('0x4e')]===0x0){return db[_0x968f('0x25')][_0x968f('0x47')]({'name':_0x4f961e['name'],'resourceId':_0x4f961e['id'],'type':_0x2ec4c6[_0x968f('0x28')],'sectionId':_0x2ec4c6['id']},{})[_0x968f('0x21')](function(){return _0x1baffa;});}else{return _0x1baffa;}})['catch'](function(_0x17ea25){logger[_0x968f('0x26')](_0x968f('0x4f'),_0x17ea25);throw _0x17ea25;});}return _0x1baffa;})[_0x968f('0x21')](respondWithResult(_0x2af513,0xc9))[_0x968f('0x46')](handleError(_0x2af513,null));};exports[_0x968f('0x50')]=function(_0x106f78,_0x28e67c){if(_0x106f78[_0x968f('0x48')]['id']){delete _0x106f78[_0x968f('0x48')]['id'];}return db[_0x968f('0x2b')][_0x968f('0x45')]({'where':{'id':_0x106f78['params']['id']}})[_0x968f('0x21')](handleEntityNotFound(_0x28e67c,null))[_0x968f('0x21')](saveUpdates(_0x106f78['body'],null))[_0x968f('0x21')](respondWithResult(_0x28e67c,null))['catch'](handleError(_0x28e67c,null));};exports[_0x968f('0x22')]=function(_0x524d8f,_0x2203e3){return db['Schedule'][_0x968f('0x45')]({'where':{'id':_0x524d8f[_0x968f('0x51')]['id']}})[_0x968f('0x21')](handleEntityNotFound(_0x2203e3,null))['then'](removeEntity(_0x2203e3,null))[_0x968f('0x46')](handleError(_0x2203e3,null));};exports[_0x968f('0x52')]=function(_0x3911a8,_0x53127f,_0x844591){var _0x39439d=require('../../config/schedule');return db[_0x968f('0x2b')][_0x968f('0x45')]({'where':{'id':_0x3911a8['params']['id']},'raw':!![]})['then'](handleEntityNotFound(_0x53127f,null))[_0x968f('0x21')](function(){_0x39439d[_0x968f('0x52')](_0x3911a8[_0x968f('0x51')]['id']);})[_0x968f('0x21')](respondWithStatusCode(_0x53127f,null))['catch'](handleError(_0x53127f,null));};