39a8f1e338d8b4f9f2f850c5c755e42805d67860
[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 _0x558b=['get','Scheduler','UserProfileResource','stack','name','index','map','Schedule','fieldName','type','key','model','query','filters','intersection','attributes','fields','length','hasOwnProperty','order','sort','where','pick','filter','merge','VIRTUAL','includeAll','include','catch','params','keys','rawAttributes','options','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','autoAssociation','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','run','../../config/schedule','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','util','path','sox','ejs','fs-extra','squel','crypto','jsforce','desk.js','to-csv','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','reject','save','update','then','destroy'];(function(_0x2d7745,_0x4fe6a6){var _0xab8b8=function(_0x424d56){while(--_0x424d56){_0x2d7745['push'](_0x2d7745['shift']());}};_0xab8b8(++_0x4fe6a6);}(_0x558b,0x83));var _0xb558=function(_0x8c4e2f,_0x416ff0){_0x8c4e2f=_0x8c4e2f-0x0;var _0x1c051f=_0x558b[_0x8c4e2f];return _0x1c051f;};'use strict';var emlformat=require(_0xb558('0x0'));var rimraf=require(_0xb558('0x1'));var zipdir=require(_0xb558('0x2'));var jsonpatch=require(_0xb558('0x3'));var rp=require(_0xb558('0x4'));var moment=require(_0xb558('0x5'));var BPromise=require(_0xb558('0x6'));var Mustache=require('mustache');var util=require(_0xb558('0x7'));var path=require(_0xb558('0x8'));var sox=require(_0xb558('0x9'));var csv=require('to-csv');var ejs=require(_0xb558('0xa'));var fs=require('fs');var fs_extra=require(_0xb558('0xb'));var _=require('lodash');var squel=require(_0xb558('0xc'));var crypto=require(_0xb558('0xd'));var jsforce=require(_0xb558('0xe'));var deskjs=require(_0xb558('0xf'));var toCsv=require(_0xb558('0x10'));var querystring=require(_0xb558('0x11'));var Papa=require(_0xb558('0x12'));var Redis=require('ioredis');var authService=require(_0xb558('0x13'));var qs=require(_0xb558('0x14'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xb558('0x15'));var logger=require(_0xb558('0x16'))(_0xb558('0x17'));var utils=require(_0xb558('0x18'));var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require(_0xb558('0x19'))['db'];function respondWithStatusCode(_0x96ae6b,_0x3914fa){_0x3914fa=_0x3914fa||0xcc;return function(_0x1a601f){if(_0x1a601f){return _0x96ae6b[_0xb558('0x1a')](_0x3914fa);}return _0x96ae6b[_0xb558('0x1b')](_0x3914fa)[_0xb558('0x1c')]();};}function respondWithResult(_0x377d1c,_0x3ef995){_0x3ef995=_0x3ef995||0xc8;return function(_0x2ac9cf){if(_0x2ac9cf){return _0x377d1c[_0xb558('0x1b')](_0x3ef995)[_0xb558('0x1d')](_0x2ac9cf);}};}function respondWithFilteredResult(_0x4a891b,_0x4cc125){return function(_0xb662b1){if(_0xb662b1){var _0x593fee=typeof _0x4cc125[_0xb558('0x1e')]===_0xb558('0x1f')&&typeof _0x4cc125[_0xb558('0x20')]===_0xb558('0x1f');var _0x1636cb=_0xb662b1['count'];var _0x30d86e=_0x593fee?0x0:_0x4cc125[_0xb558('0x1e')];var _0x25beae=_0x593fee?_0xb662b1[_0xb558('0x21')]:_0x4cc125[_0xb558('0x1e')]+_0x4cc125[_0xb558('0x20')];var _0x25b72f;if(_0x25beae>=_0x1636cb){_0x25beae=_0x1636cb;_0x25b72f=0xc8;}else{_0x25b72f=0xce;}_0x4a891b[_0xb558('0x1b')](_0x25b72f);return _0x4a891b[_0xb558('0x22')](_0xb558('0x23'),_0x30d86e+'-'+_0x25beae+'/'+_0x1636cb)[_0xb558('0x1d')](_0xb662b1);}return null;};}function patchUpdates(_0x2032f5){return function(_0x1ecabe){try{jsonpatch['apply'](_0x1ecabe,_0x2032f5,!![]);}catch(_0x1724a4){return BPromise[_0xb558('0x24')](_0x1724a4);}return _0x1ecabe[_0xb558('0x25')]();};}function saveUpdates(_0x34c43c,_0x13d464){return function(_0x151416){if(_0x151416){return _0x151416[_0xb558('0x26')](_0x34c43c)[_0xb558('0x27')](function(_0x54ad0d){return _0x54ad0d;});}return null;};}function removeEntity(_0x8bbb02,_0x2b6ab5){return function(_0x13d738){if(_0x13d738){return _0x13d738[_0xb558('0x28')]()[_0xb558('0x27')](function(){var _0xe8debe=_0x13d738[_0xb558('0x29')]({'plain':!![]});var _0x479b0a=_0xb558('0x2a');return db[_0xb558('0x2b')]['destroy']({'where':{'type':_0x479b0a,'resourceId':_0xe8debe['id']}})[_0xb558('0x27')](function(){return _0x13d738;});})[_0xb558('0x27')](function(){_0x8bbb02['status'](0xcc)[_0xb558('0x1c')]();});}};}function handleEntityNotFound(_0x14bf35,_0x23ba0f){return function(_0xefbae9){if(!_0xefbae9){_0x14bf35[_0xb558('0x1a')](0x194);}return _0xefbae9;};}function handleError(_0x15a50d,_0x2d22ca){_0x2d22ca=_0x2d22ca||0x1f4;return function(_0x3cd7a4){logger['error'](_0x3cd7a4[_0xb558('0x2c')]);if(_0x3cd7a4[_0xb558('0x2d')]){delete _0x3cd7a4['name'];}_0x15a50d['status'](_0x2d22ca)['send'](_0x3cd7a4);};}exports[_0xb558('0x2e')]=function(_0x2a03c2,_0x3c05fa){var _0x19f55f={},_0x518ddc={},_0x200247={'count':0x0,'rows':[]};var _0x52b694=_[_0xb558('0x2f')](db[_0xb558('0x30')]['rawAttributes'],function(_0x504d73){return{'name':_0x504d73[_0xb558('0x31')],'type':_0x504d73[_0xb558('0x32')][_0xb558('0x33')]};});_0x518ddc[_0xb558('0x34')]=_['map'](_0x52b694,'name');_0x518ddc[_0xb558('0x35')]=_['keys'](_0x2a03c2[_0xb558('0x35')]);_0x518ddc[_0xb558('0x36')]=_[_0xb558('0x37')](_0x518ddc[_0xb558('0x34')],_0x518ddc[_0xb558('0x35')]);_0x19f55f[_0xb558('0x38')]=_['intersection'](_0x518ddc[_0xb558('0x34')],qs['fields'](_0x2a03c2[_0xb558('0x35')][_0xb558('0x39')]));_0x19f55f[_0xb558('0x38')]=_0x19f55f[_0xb558('0x38')][_0xb558('0x3a')]?_0x19f55f['attributes']:_0x518ddc[_0xb558('0x34')];if(!_0x2a03c2[_0xb558('0x35')][_0xb558('0x3b')]('nolimit')){_0x19f55f[_0xb558('0x20')]=qs[_0xb558('0x20')](_0x2a03c2[_0xb558('0x35')][_0xb558('0x20')]);_0x19f55f[_0xb558('0x1e')]=qs[_0xb558('0x1e')](_0x2a03c2[_0xb558('0x35')][_0xb558('0x1e')]);}_0x19f55f[_0xb558('0x3c')]=qs[_0xb558('0x3d')](_0x2a03c2[_0xb558('0x35')][_0xb558('0x3d')]);_0x19f55f[_0xb558('0x3e')]=qs[_0xb558('0x36')](_[_0xb558('0x3f')](_0x2a03c2[_0xb558('0x35')],_0x518ddc[_0xb558('0x36')]),_0x52b694);if(_0x2a03c2[_0xb558('0x35')][_0xb558('0x40')]){_0x19f55f[_0xb558('0x3e')]=_[_0xb558('0x41')](_0x19f55f[_0xb558('0x3e')],{'$or':_[_0xb558('0x2f')](_0x52b694,function(_0x1eee32){if(_0x1eee32['type']!==_0xb558('0x42')){var _0x87b27e={};_0x87b27e[_0x1eee32[_0xb558('0x2d')]]={'$like':'%'+_0x2a03c2[_0xb558('0x35')][_0xb558('0x40')]+'%'};return _0x87b27e;}})});}_0x19f55f=_[_0xb558('0x41')]({},_0x19f55f,_0x2a03c2['options']);var _0x28e522={'where':_0x19f55f['where']};return db[_0xb558('0x30')][_0xb558('0x21')](_0x28e522)[_0xb558('0x27')](function(_0x70302b){_0x200247['count']=_0x70302b;if(_0x2a03c2[_0xb558('0x35')][_0xb558('0x43')]){_0x19f55f[_0xb558('0x44')]=[{'all':!![]}];}return db['Schedule']['findAll'](_0x19f55f);})[_0xb558('0x27')](function(_0x586d37){_0x200247['rows']=_0x586d37;return _0x200247;})[_0xb558('0x27')](respondWithFilteredResult(_0x3c05fa,_0x19f55f))[_0xb558('0x45')](handleError(_0x3c05fa,null));};exports['show']=function(_0xedcef4,_0x58e547){var _0x13ae8a={'raw':![],'where':{'id':_0xedcef4[_0xb558('0x46')]['id']}},_0x195f91={};_0x195f91['model']=_[_0xb558('0x47')](db[_0xb558('0x30')][_0xb558('0x48')]);_0x195f91['query']=_[_0xb558('0x47')](_0xedcef4[_0xb558('0x35')]);_0x195f91['filters']=_[_0xb558('0x37')](_0x195f91[_0xb558('0x34')],_0x195f91[_0xb558('0x35')]);_0x13ae8a[_0xb558('0x38')]=_[_0xb558('0x37')](_0x195f91[_0xb558('0x34')],qs[_0xb558('0x39')](_0xedcef4[_0xb558('0x35')][_0xb558('0x39')]));_0x13ae8a[_0xb558('0x38')]=_0x13ae8a[_0xb558('0x38')][_0xb558('0x3a')]?_0x13ae8a['attributes']:_0x195f91['model'];if(_0xedcef4[_0xb558('0x35')][_0xb558('0x43')]){_0x13ae8a['include']=[{'all':!![]}];}_0x13ae8a=_[_0xb558('0x41')]({},_0x13ae8a,_0xedcef4[_0xb558('0x49')]);return db['Schedule'][_0xb558('0x4a')](_0x13ae8a)['then'](handleEntityNotFound(_0x58e547,null))['then'](respondWithResult(_0x58e547,null))[_0xb558('0x45')](handleError(_0x58e547,null));};exports[_0xb558('0x4b')]=function(_0x291314,_0x2efa2b){return db[_0xb558('0x30')][_0xb558('0x4b')](_0x291314[_0xb558('0x4c')],{})['then'](function(_0x15fb37){var _0x51054e=_0x291314[_0xb558('0x4d')]['get']({'plain':!![]});if(!_0x51054e)throw new Error(_0xb558('0x4e'));if(_0x51054e[_0xb558('0x4f')]==='user'){var _0x212b10=_0x15fb37[_0xb558('0x29')]({'plain':!![]});var _0x4450f6=_0xb558('0x2a');return db[_0xb558('0x50')]['find']({'where':{'name':_0x4450f6,'userProfileId':_0x51054e['userProfileId']},'raw':!![]})[_0xb558('0x27')](function(_0x553482){if(_0x553482&&_0x553482[_0xb558('0x51')]===0x0){return db[_0xb558('0x2b')][_0xb558('0x4b')]({'name':_0x212b10[_0xb558('0x2d')],'resourceId':_0x212b10['id'],'type':_0x553482[_0xb558('0x2d')],'sectionId':_0x553482['id']},{})['then'](function(){return _0x15fb37;});}else{return _0x15fb37;}})[_0xb558('0x45')](function(_0x306447){logger[_0xb558('0x52')](_0xb558('0x53'),_0x306447);throw _0x306447;});}return _0x15fb37;})[_0xb558('0x27')](respondWithResult(_0x2efa2b,0xc9))[_0xb558('0x45')](handleError(_0x2efa2b,null));};exports[_0xb558('0x26')]=function(_0x4b2fe9,_0x30001f){if(_0x4b2fe9[_0xb558('0x4c')]['id']){delete _0x4b2fe9[_0xb558('0x4c')]['id'];}return db[_0xb558('0x30')][_0xb558('0x4a')]({'where':{'id':_0x4b2fe9[_0xb558('0x46')]['id']}})[_0xb558('0x27')](handleEntityNotFound(_0x30001f,null))[_0xb558('0x27')](saveUpdates(_0x4b2fe9[_0xb558('0x4c')],null))[_0xb558('0x27')](respondWithResult(_0x30001f,null))[_0xb558('0x45')](handleError(_0x30001f,null));};exports[_0xb558('0x28')]=function(_0x362336,_0x36f1f5){return db[_0xb558('0x30')][_0xb558('0x4a')]({'where':{'id':_0x362336[_0xb558('0x46')]['id']}})['then'](handleEntityNotFound(_0x36f1f5,null))[_0xb558('0x27')](removeEntity(_0x36f1f5,null))[_0xb558('0x45')](handleError(_0x36f1f5,null));};exports[_0xb558('0x54')]=function(_0x4d5c1a,_0x2848bf,_0xb01af1){var _0x1898f0=require(_0xb558('0x55'));return db['Schedule'][_0xb558('0x4a')]({'where':{'id':_0x4d5c1a['params']['id']},'raw':!![]})[_0xb558('0x27')](handleEntityNotFound(_0x2848bf,null))[_0xb558('0x27')](function(){_0x1898f0[_0xb558('0x54')](_0x4d5c1a[_0xb558('0x46')]['id']);})['then'](respondWithStatusCode(_0x2848bf,null))[_0xb558('0x45')](handleError(_0x2848bf,null));};