9a4d966809567ece2fdd735cd58eeca71d64e67b
[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 _0x45f2=['options','includeAll','include','findAll','rows','catch','show','params','rawAttributes','find','create','user','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','run','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','sox','ejs','lodash','jsforce','desk.js','to-csv','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','offset','limit','undefined','count','set','Content-Range','json','apply','save','update','then','destroy','Scheduler','UserProfileResource','error','stack','name','send','merge','body','contact','message','agent','interaction','account','index','map','Schedule','fieldName','type','key','model','query','keys','filters','attributes','intersection','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','VIRTUAL','filter'];(function(_0x5cd7a3,_0x1aa14f){var _0x21235d=function(_0x3152a9){while(--_0x3152a9){_0x5cd7a3['push'](_0x5cd7a3['shift']());}};_0x21235d(++_0x1aa14f);}(_0x45f2,0x71));var _0x245f=function(_0xbb1c6a,_0x4a6693){_0xbb1c6a=_0xbb1c6a-0x0;var _0x27e68c=_0x45f2[_0xbb1c6a];return _0x27e68c;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x245f('0x0'));var zipdir=require(_0x245f('0x1'));var jsonpatch=require(_0x245f('0x2'));var rp=require(_0x245f('0x3'));var moment=require(_0x245f('0x4'));var BPromise=require(_0x245f('0x5'));var Mustache=require(_0x245f('0x6'));var util=require('util');var path=require('path');var sox=require(_0x245f('0x7'));var csv=require('to-csv');var ejs=require(_0x245f('0x8'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0x245f('0x9'));var squel=require('squel');var crypto=require('crypto');var jsforce=require(_0x245f('0xa'));var deskjs=require(_0x245f('0xb'));var toCsv=require(_0x245f('0xc'));var querystring=require(_0x245f('0xd'));var Papa=require(_0x245f('0xe'));var Redis=require(_0x245f('0xf'));var authService=require(_0x245f('0x10'));var qs=require(_0x245f('0x11'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require('../../config/license/hardware');var logger=require('../../config/logger')(_0x245f('0x12'));var utils=require(_0x245f('0x13'));var config=require(_0x245f('0x14'));var licenseUtil=require(_0x245f('0x15'));var db=require(_0x245f('0x16'))['db'];function respondWithStatusCode(_0x38122d,_0x2b399e){_0x2b399e=_0x2b399e||0xcc;return function(_0x3888ee){if(_0x3888ee){return _0x38122d[_0x245f('0x17')](_0x2b399e);}return _0x38122d[_0x245f('0x18')](_0x2b399e)[_0x245f('0x19')]();};}function respondWithResult(_0x5e6c4c,_0x5eef33){_0x5eef33=_0x5eef33||0xc8;return function(_0xfa9dd8){if(_0xfa9dd8){return _0x5e6c4c[_0x245f('0x18')](_0x5eef33)['json'](_0xfa9dd8);}};}function respondWithFilteredResult(_0x28bcdd,_0x275469){return function(_0x59c6c3){if(_0x59c6c3){var _0x560d98=typeof _0x275469[_0x245f('0x1a')]==='undefined'&&typeof _0x275469[_0x245f('0x1b')]===_0x245f('0x1c');var _0x271f47=_0x59c6c3[_0x245f('0x1d')];var _0x2e8f4f=_0x560d98?0x0:_0x275469[_0x245f('0x1a')];var _0x40754c=_0x560d98?_0x59c6c3[_0x245f('0x1d')]:_0x275469['offset']+_0x275469['limit'];var _0x1e27fe;if(_0x40754c>=_0x271f47){_0x40754c=_0x271f47;_0x1e27fe=0xc8;}else{_0x1e27fe=0xce;}_0x28bcdd[_0x245f('0x18')](_0x1e27fe);return _0x28bcdd[_0x245f('0x1e')](_0x245f('0x1f'),_0x2e8f4f+'-'+_0x40754c+'/'+_0x271f47)[_0x245f('0x20')](_0x59c6c3);}return null;};}function patchUpdates(_0x1dc2c2){return function(_0x309f0d){try{jsonpatch[_0x245f('0x21')](_0x309f0d,_0x1dc2c2,!![]);}catch(_0x27bc98){return BPromise['reject'](_0x27bc98);}return _0x309f0d[_0x245f('0x22')]();};}function saveUpdates(_0x1fe9c4,_0x38d555){return function(_0x52a18b){if(_0x52a18b){return _0x52a18b[_0x245f('0x23')](_0x1fe9c4)[_0x245f('0x24')](function(_0x2ce7c2){return _0x2ce7c2;});}return null;};}function removeEntity(_0x2318a3,_0x54a426){return function(_0x1726f4){if(_0x1726f4){return _0x1726f4[_0x245f('0x25')]()[_0x245f('0x24')](function(){var _0x79a9a1=_0x1726f4['get']({'plain':!![]});var _0x2db471=_0x245f('0x26');return db[_0x245f('0x27')][_0x245f('0x25')]({'where':{'type':_0x2db471,'resourceId':_0x79a9a1['id']}})[_0x245f('0x24')](function(){return _0x1726f4;});})['then'](function(){_0x2318a3[_0x245f('0x18')](0xcc)[_0x245f('0x19')]();});}};}function handleEntityNotFound(_0x26d8d8,_0x2b3bce){return function(_0x195b2d){if(!_0x195b2d){_0x26d8d8[_0x245f('0x17')](0x194);}return _0x195b2d;};}function handleError(_0x56aa8d,_0x5d3670){_0x5d3670=_0x5d3670||0x1f4;return function(_0x44666b){logger[_0x245f('0x28')](_0x44666b[_0x245f('0x29')]);if(_0x44666b[_0x245f('0x2a')]){delete _0x44666b[_0x245f('0x2a')];}_0x56aa8d[_0x245f('0x18')](_0x5d3670)[_0x245f('0x2b')](_0x44666b);};}function getMustacheRender(_0x5c9344,_0xc13518){return Mustache['render'](_0x5c9344,_[_0x245f('0x2c')](_0xc13518[_0x245f('0x2d')],{'contact':_0xc13518[_0x245f('0x2e')],'message':_0xc13518[_0x245f('0x2f')],'agent':_0xc13518[_0x245f('0x30')],'interaction':_0xc13518[_0x245f('0x31')],'account':_0xc13518[_0x245f('0x32')]}))||'';}exports[_0x245f('0x33')]=function(_0x406d79,_0x1109fd){var _0x563156={},_0x581d65={},_0xebd4b={'count':0x0,'rows':[]};var _0x4e20eb=_[_0x245f('0x34')](db[_0x245f('0x35')]['rawAttributes'],function(_0x427d57){return{'name':_0x427d57[_0x245f('0x36')],'type':_0x427d57[_0x245f('0x37')][_0x245f('0x38')]};});_0x581d65[_0x245f('0x39')]=_[_0x245f('0x34')](_0x4e20eb,_0x245f('0x2a'));_0x581d65[_0x245f('0x3a')]=_[_0x245f('0x3b')](_0x406d79[_0x245f('0x3a')]);_0x581d65[_0x245f('0x3c')]=_['intersection'](_0x581d65[_0x245f('0x39')],_0x581d65[_0x245f('0x3a')]);_0x563156[_0x245f('0x3d')]=_[_0x245f('0x3e')](_0x581d65[_0x245f('0x39')],qs[_0x245f('0x3f')](_0x406d79[_0x245f('0x3a')][_0x245f('0x3f')]));_0x563156[_0x245f('0x3d')]=_0x563156['attributes'][_0x245f('0x40')]?_0x563156['attributes']:_0x581d65[_0x245f('0x39')];if(!_0x406d79['query'][_0x245f('0x41')](_0x245f('0x42'))){_0x563156['limit']=qs[_0x245f('0x1b')](_0x406d79[_0x245f('0x3a')]['limit']);_0x563156[_0x245f('0x1a')]=qs['offset'](_0x406d79[_0x245f('0x3a')]['offset']);}_0x563156[_0x245f('0x43')]=qs['sort'](_0x406d79[_0x245f('0x3a')][_0x245f('0x44')]);_0x563156[_0x245f('0x45')]=qs['filters'](_[_0x245f('0x46')](_0x406d79[_0x245f('0x3a')],_0x581d65['filters']),_0x4e20eb);if(_0x406d79['query']['filter']){_0x563156[_0x245f('0x45')]=_['merge'](_0x563156[_0x245f('0x45')],{'$or':_[_0x245f('0x34')](_0x4e20eb,function(_0x13f5b0){if(_0x13f5b0[_0x245f('0x37')]!==_0x245f('0x47')){var _0x58adca={};_0x58adca[_0x13f5b0[_0x245f('0x2a')]]={'$like':'%'+_0x406d79[_0x245f('0x3a')][_0x245f('0x48')]+'%'};return _0x58adca;}})});}_0x563156=_[_0x245f('0x2c')]({},_0x563156,_0x406d79[_0x245f('0x49')]);var _0x4e44f2={'where':_0x563156[_0x245f('0x45')]};return db['Schedule']['count'](_0x4e44f2)[_0x245f('0x24')](function(_0x5bfed7){_0xebd4b[_0x245f('0x1d')]=_0x5bfed7;if(_0x406d79[_0x245f('0x3a')][_0x245f('0x4a')]){_0x563156[_0x245f('0x4b')]=[{'all':!![]}];}return db[_0x245f('0x35')][_0x245f('0x4c')](_0x563156);})['then'](function(_0xe0e191){_0xebd4b[_0x245f('0x4d')]=_0xe0e191;return _0xebd4b;})[_0x245f('0x24')](respondWithFilteredResult(_0x1109fd,_0x563156))[_0x245f('0x4e')](handleError(_0x1109fd,null));};exports[_0x245f('0x4f')]=function(_0x211251,_0x5abd21){var _0x4648b8={'raw':![],'where':{'id':_0x211251[_0x245f('0x50')]['id']}},_0x4852c7={};_0x4852c7['model']=_['keys'](db['Schedule'][_0x245f('0x51')]);_0x4852c7['query']=_['keys'](_0x211251['query']);_0x4852c7[_0x245f('0x3c')]=_['intersection'](_0x4852c7[_0x245f('0x39')],_0x4852c7[_0x245f('0x3a')]);_0x4648b8['attributes']=_['intersection'](_0x4852c7[_0x245f('0x39')],qs[_0x245f('0x3f')](_0x211251[_0x245f('0x3a')][_0x245f('0x3f')]));_0x4648b8[_0x245f('0x3d')]=_0x4648b8[_0x245f('0x3d')][_0x245f('0x40')]?_0x4648b8['attributes']:_0x4852c7['model'];if(_0x211251['query']['includeAll']){_0x4648b8[_0x245f('0x4b')]=[{'all':!![]}];}_0x4648b8=_[_0x245f('0x2c')]({},_0x4648b8,_0x211251[_0x245f('0x49')]);return db[_0x245f('0x35')][_0x245f('0x52')](_0x4648b8)['then'](handleEntityNotFound(_0x5abd21,null))[_0x245f('0x24')](respondWithResult(_0x5abd21,null))['catch'](handleError(_0x5abd21,null));};exports['create']=function(_0x3f8fea,_0x5c7f68){return db[_0x245f('0x35')][_0x245f('0x53')](_0x3f8fea[_0x245f('0x2d')],{})[_0x245f('0x24')](function(_0x37f89f){var _0x3e1a04=_0x3f8fea[_0x245f('0x54')][_0x245f('0x55')]({'plain':!![]});if(!_0x3e1a04)throw new Error(_0x245f('0x56'));if(_0x3e1a04[_0x245f('0x57')]===_0x245f('0x54')){var _0x58dca1=_0x37f89f[_0x245f('0x55')]({'plain':!![]});var _0x5448bb='Scheduler';return db[_0x245f('0x58')][_0x245f('0x52')]({'where':{'name':_0x5448bb,'userProfileId':_0x3e1a04[_0x245f('0x59')]},'raw':!![]})[_0x245f('0x24')](function(_0x1cc063){if(_0x1cc063&&_0x1cc063[_0x245f('0x5a')]===0x0){return db['UserProfileResource'][_0x245f('0x53')]({'name':_0x58dca1[_0x245f('0x2a')],'resourceId':_0x58dca1['id'],'type':_0x1cc063['name'],'sectionId':_0x1cc063['id']},{})['then'](function(){return _0x37f89f;});}else{return _0x37f89f;}})[_0x245f('0x4e')](function(_0x1b017e){logger[_0x245f('0x28')](_0x245f('0x5b'),_0x1b017e);throw _0x1b017e;});}return _0x37f89f;})['then'](respondWithResult(_0x5c7f68,0xc9))[_0x245f('0x4e')](handleError(_0x5c7f68,null));};exports[_0x245f('0x23')]=function(_0x1ad215,_0x10f72b){if(_0x1ad215[_0x245f('0x2d')]['id']){delete _0x1ad215['body']['id'];}return db['Schedule']['find']({'where':{'id':_0x1ad215[_0x245f('0x50')]['id']}})[_0x245f('0x24')](handleEntityNotFound(_0x10f72b,null))[_0x245f('0x24')](saveUpdates(_0x1ad215[_0x245f('0x2d')],null))['then'](respondWithResult(_0x10f72b,null))[_0x245f('0x4e')](handleError(_0x10f72b,null));};exports[_0x245f('0x25')]=function(_0x2c2ff2,_0x9e3398){return db['Schedule']['find']({'where':{'id':_0x2c2ff2[_0x245f('0x50')]['id']}})['then'](handleEntityNotFound(_0x9e3398,null))[_0x245f('0x24')](removeEntity(_0x9e3398,null))[_0x245f('0x4e')](handleError(_0x9e3398,null));};exports[_0x245f('0x5c')]=function(_0x4dbbfa,_0x59d018,_0x4c5830){var _0x5d604a=require('../../config/schedule');return db[_0x245f('0x35')]['find']({'where':{'id':_0x4dbbfa['params']['id']},'raw':!![]})[_0x245f('0x24')](handleEntityNotFound(_0x59d018,null))['then'](function(){_0x5d604a[_0x245f('0x5c')](_0x4dbbfa[_0x245f('0x50')]['id']);})[_0x245f('0x24')](respondWithStatusCode(_0x59d018,null))['catch'](handleError(_0x59d018,null));};