Built motion from commit 9d1906d1.|2.5.40
[motion2.git] / server / api / interval / interval.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 _0x17f2=['filter','merge','VIRTUAL','options','where','count','include','findAll','rows','catch','show','params','includeAll','find','create','body','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','user','userProfileId','autoAssociation','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','addInterval','IntervalId','getIntervals','findOne','pick','addIntervals','sequelize','bulkCreate','eml-format','rimraf','request-promise','moment','mustache','util','sox','ejs','fs-extra','lodash','squel','crypto','jsforce','to-csv','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','../../config/utils','../../config/environment','../../mysqldb','../../components/extensions/rewrite','sendStatus','status','end','json','offset','limit','set','Content-Range','apply','reject','save','update','then','get','Intervals','UserProfileResource','destroy','stack','name','send','index','map','Interval','rawAttributes','key','model','query','keys','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','order','sort'];(function(_0x4f3c9b,_0x28e078){var _0x84e58a=function(_0x37bf49){while(--_0x37bf49){_0x4f3c9b['push'](_0x4f3c9b['shift']());}};_0x84e58a(++_0x28e078);}(_0x17f2,0x1eb));var _0x217f=function(_0xcd5a3d,_0x3c5b33){_0xcd5a3d=_0xcd5a3d-0x0;var _0xd43207=_0x17f2[_0xcd5a3d];return _0xd43207;};'use strict';var emlformat=require(_0x217f('0x0'));var rimraf=require(_0x217f('0x1'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0x217f('0x2'));var moment=require(_0x217f('0x3'));var BPromise=require('bluebird');var Mustache=require(_0x217f('0x4'));var util=require(_0x217f('0x5'));var path=require('path');var sox=require(_0x217f('0x6'));var csv=require('to-csv');var ejs=require(_0x217f('0x7'));var fs=require('fs');var fs_extra=require(_0x217f('0x8'));var _=require(_0x217f('0x9'));var squel=require(_0x217f('0xa'));var crypto=require(_0x217f('0xb'));var jsforce=require(_0x217f('0xc'));var deskjs=require('desk.js');var toCsv=require(_0x217f('0xd'));var querystring=require(_0x217f('0xe'));var Papa=require(_0x217f('0xf'));var Redis=require('ioredis');var authService=require(_0x217f('0x10'));var qs=require(_0x217f('0x11'));var as=require(_0x217f('0x12'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x217f('0x13'))('api');var utils=require(_0x217f('0x14'));var config=require(_0x217f('0x15'));var licenseUtil=require('../../config/license/util');var db=require(_0x217f('0x16'))['db'];var extensions=require(_0x217f('0x17'));function respondWithStatusCode(_0x36d7d9,_0x25a06c){_0x25a06c=_0x25a06c||0xcc;return function(_0x589e98){if(_0x589e98){return _0x36d7d9[_0x217f('0x18')](_0x25a06c);}return _0x36d7d9[_0x217f('0x19')](_0x25a06c)[_0x217f('0x1a')]();};}function respondWithResult(_0x4332b4,_0x5bdb65){_0x5bdb65=_0x5bdb65||0xc8;return function(_0x11b05e){if(_0x11b05e){return _0x4332b4[_0x217f('0x19')](_0x5bdb65)[_0x217f('0x1b')](_0x11b05e);}};}function respondWithFilteredResult(_0x34739b,_0x33489e){return function(_0x2d7a04){if(_0x2d7a04){var _0x125980=typeof _0x33489e[_0x217f('0x1c')]==='undefined'&&typeof _0x33489e[_0x217f('0x1d')]==='undefined';var _0x36fbdf=_0x2d7a04['count'];var _0x5b2e35=_0x125980?0x0:_0x33489e[_0x217f('0x1c')];var _0x12b967=_0x125980?_0x2d7a04['count']:_0x33489e['offset']+_0x33489e[_0x217f('0x1d')];var _0x3844cf;if(_0x12b967>=_0x36fbdf){_0x12b967=_0x36fbdf;_0x3844cf=0xc8;}else{_0x3844cf=0xce;}_0x34739b[_0x217f('0x19')](_0x3844cf);return _0x34739b[_0x217f('0x1e')](_0x217f('0x1f'),_0x5b2e35+'-'+_0x12b967+'/'+_0x36fbdf)[_0x217f('0x1b')](_0x2d7a04);}return null;};}function patchUpdates(_0x2368cf){return function(_0x57bf17){try{jsonpatch[_0x217f('0x20')](_0x57bf17,_0x2368cf,!![]);}catch(_0x3b9ba3){return BPromise[_0x217f('0x21')](_0x3b9ba3);}return _0x57bf17[_0x217f('0x22')]();};}function saveUpdates(_0x239951,_0x2e0d0f){return function(_0x5c14d6){if(_0x5c14d6){return _0x5c14d6[_0x217f('0x23')](_0x239951)[_0x217f('0x24')](function(_0x2e96c2){return _0x2e96c2;});}return null;};}function removeEntity(_0x485cf1,_0xf2f4ed){return function(_0x5e9e63){if(_0x5e9e63){return _0x5e9e63['destroy']()[_0x217f('0x24')](function(){var _0x3106bb=_0x5e9e63[_0x217f('0x25')]({'plain':!![]});var _0x386404=_0x217f('0x26');return db[_0x217f('0x27')][_0x217f('0x28')]({'where':{'type':_0x386404,'resourceId':_0x3106bb['id']}})[_0x217f('0x24')](function(){return _0x5e9e63;});})['then'](function(){_0x485cf1[_0x217f('0x19')](0xcc)[_0x217f('0x1a')]();});}};}function handleEntityNotFound(_0x2427d2,_0x2ae735){return function(_0x402f5d){if(!_0x402f5d){_0x2427d2[_0x217f('0x18')](0x194);}return _0x402f5d;};}function handleError(_0x523289,_0x29b329){_0x29b329=_0x29b329||0x1f4;return function(_0x431207){logger['error'](_0x431207[_0x217f('0x29')]);if(_0x431207[_0x217f('0x2a')]){delete _0x431207[_0x217f('0x2a')];}_0x523289[_0x217f('0x19')](_0x29b329)[_0x217f('0x2b')](_0x431207);};}exports[_0x217f('0x2c')]=function(_0xcef02,_0x4abd78){var _0x23792d={},_0x22c0be={},_0x3a3543={'count':0x0,'rows':[]};var _0x445c3c=_[_0x217f('0x2d')](db[_0x217f('0x2e')][_0x217f('0x2f')],function(_0x104b8e){return{'name':_0x104b8e['fieldName'],'type':_0x104b8e['type'][_0x217f('0x30')]};});_0x22c0be[_0x217f('0x31')]=_[_0x217f('0x2d')](_0x445c3c,_0x217f('0x2a'));_0x22c0be[_0x217f('0x32')]=_[_0x217f('0x33')](_0xcef02[_0x217f('0x32')]);_0x22c0be[_0x217f('0x34')]=_[_0x217f('0x35')](_0x22c0be[_0x217f('0x31')],_0x22c0be[_0x217f('0x32')]);_0x23792d['attributes']=_[_0x217f('0x35')](_0x22c0be[_0x217f('0x31')],qs[_0x217f('0x36')](_0xcef02[_0x217f('0x32')][_0x217f('0x36')]));_0x23792d['attributes']=_0x23792d[_0x217f('0x37')][_0x217f('0x38')]?_0x23792d[_0x217f('0x37')]:_0x22c0be[_0x217f('0x31')];if(!_0xcef02[_0x217f('0x32')][_0x217f('0x39')](_0x217f('0x3a'))){_0x23792d['limit']=qs[_0x217f('0x1d')](_0xcef02[_0x217f('0x32')][_0x217f('0x1d')]);_0x23792d['offset']=qs[_0x217f('0x1c')](_0xcef02[_0x217f('0x32')][_0x217f('0x1c')]);}_0x23792d[_0x217f('0x3b')]=qs[_0x217f('0x3c')](_0xcef02[_0x217f('0x32')]['sort']);_0x23792d['where']=qs['filters'](_['pick'](_0xcef02[_0x217f('0x32')],_0x22c0be[_0x217f('0x34')]),_0x445c3c);if(_0xcef02[_0x217f('0x32')][_0x217f('0x3d')]){_0x23792d['where']=_[_0x217f('0x3e')](_0x23792d['where'],{'$or':_[_0x217f('0x2d')](_0x445c3c,function(_0x143e26){if(_0x143e26['type']!==_0x217f('0x3f')){var _0x2d416a={};_0x2d416a[_0x143e26[_0x217f('0x2a')]]={'$like':'%'+_0xcef02[_0x217f('0x32')][_0x217f('0x3d')]+'%'};return _0x2d416a;}})});}_0x23792d=_[_0x217f('0x3e')]({},_0x23792d,_0xcef02[_0x217f('0x40')]);var _0x9519a8={'where':_0x23792d[_0x217f('0x41')]};return db[_0x217f('0x2e')]['count'](_0x9519a8)[_0x217f('0x24')](function(_0x201137){_0x3a3543[_0x217f('0x42')]=_0x201137;if(_0xcef02[_0x217f('0x32')]['includeAll']){_0x23792d[_0x217f('0x43')]=[{'all':!![]}];}return db[_0x217f('0x2e')][_0x217f('0x44')](_0x23792d);})[_0x217f('0x24')](function(_0x458114){_0x3a3543[_0x217f('0x45')]=_0x458114;return _0x3a3543;})[_0x217f('0x24')](respondWithFilteredResult(_0x4abd78,_0x23792d))[_0x217f('0x46')](handleError(_0x4abd78,null));};exports[_0x217f('0x47')]=function(_0x5c8e9c,_0x347822){var _0x38e18a={'raw':!![],'where':{'id':_0x5c8e9c[_0x217f('0x48')]['id']}},_0x5494e6={};_0x5494e6[_0x217f('0x31')]=_[_0x217f('0x33')](db[_0x217f('0x2e')][_0x217f('0x2f')]);_0x5494e6[_0x217f('0x32')]=_[_0x217f('0x33')](_0x5c8e9c[_0x217f('0x32')]);_0x5494e6[_0x217f('0x34')]=_[_0x217f('0x35')](_0x5494e6[_0x217f('0x31')],_0x5494e6[_0x217f('0x32')]);_0x38e18a[_0x217f('0x37')]=_[_0x217f('0x35')](_0x5494e6[_0x217f('0x31')],qs[_0x217f('0x36')](_0x5c8e9c[_0x217f('0x32')][_0x217f('0x36')]));_0x38e18a[_0x217f('0x37')]=_0x38e18a[_0x217f('0x37')][_0x217f('0x38')]?_0x38e18a[_0x217f('0x37')]:_0x5494e6['model'];if(_0x5c8e9c[_0x217f('0x32')][_0x217f('0x49')]){_0x38e18a[_0x217f('0x43')]=[{'all':!![]}];}_0x38e18a=_[_0x217f('0x3e')]({},_0x38e18a,_0x5c8e9c['options']);return db['Interval'][_0x217f('0x4a')](_0x38e18a)['then'](handleEntityNotFound(_0x347822,null))[_0x217f('0x24')](respondWithResult(_0x347822,null))[_0x217f('0x46')](handleError(_0x347822,null));};exports[_0x217f('0x4b')]=function(_0x43646,_0x18dc49){return db[_0x217f('0x2e')][_0x217f('0x4b')](_0x43646[_0x217f('0x4c')],{})[_0x217f('0x24')](function(_0x17fd84){var _0x5ed6d4=_0x43646['user'][_0x217f('0x25')]({'plain':!![]});if(!_0x5ed6d4)throw new Error(_0x217f('0x4d'));if(_0x5ed6d4[_0x217f('0x4e')]===_0x217f('0x4f')){var _0xf52976=_0x17fd84['get']({'plain':!![]});var _0x494507=_0x217f('0x26');return db['UserProfileSection']['find']({'where':{'name':_0x494507,'userProfileId':_0x5ed6d4[_0x217f('0x50')]},'raw':!![]})[_0x217f('0x24')](function(_0x250318){if(_0x250318&&_0x250318[_0x217f('0x51')]===0x0){return db[_0x217f('0x27')][_0x217f('0x4b')]({'name':_0xf52976[_0x217f('0x2a')],'resourceId':_0xf52976['id'],'type':_0x250318[_0x217f('0x2a')],'sectionId':_0x250318['id']},{})[_0x217f('0x24')](function(){return _0x17fd84;});}else{return _0x17fd84;}})[_0x217f('0x46')](function(_0x36315d){logger[_0x217f('0x52')](_0x217f('0x53'),_0x36315d);throw _0x36315d;});}return _0x17fd84;})[_0x217f('0x24')](respondWithResult(_0x18dc49,0xc9))[_0x217f('0x46')](handleError(_0x18dc49,null));};exports[_0x217f('0x23')]=function(_0x58fbfb,_0x3e6cf1){if(_0x58fbfb['body']['id']){delete _0x58fbfb[_0x217f('0x4c')]['id'];}return db[_0x217f('0x2e')][_0x217f('0x4a')]({'where':{'id':_0x58fbfb['params']['id']}})['then'](handleEntityNotFound(_0x3e6cf1,null))['then'](saveUpdates(_0x58fbfb[_0x217f('0x4c')],null))['then'](respondWithResult(_0x3e6cf1,null))['catch'](handleError(_0x3e6cf1,null));};exports[_0x217f('0x28')]=function(_0x71bd5f,_0x2a3841){return db[_0x217f('0x2e')][_0x217f('0x4a')]({'where':{'id':_0x71bd5f[_0x217f('0x48')]['id']}})[_0x217f('0x24')](handleEntityNotFound(_0x2a3841,null))[_0x217f('0x24')](removeEntity(_0x2a3841,null))[_0x217f('0x46')](handleError(_0x2a3841,null));};exports[_0x217f('0x54')]=function(_0x1a6bea,_0x64d872,_0x29cbb8){if(_0x1a6bea['body']['id']){delete _0x1a6bea[_0x217f('0x4c')]['id'];}return db[_0x217f('0x2e')][_0x217f('0x4a')]({'where':{'id':_0x1a6bea[_0x217f('0x48')]['id']}})['then'](handleEntityNotFound(_0x64d872,null))[_0x217f('0x24')](function(_0x41637d){if(_0x41637d){_0x1a6bea[_0x217f('0x4c')][_0x217f('0x55')]=_0x41637d['id'];return db[_0x217f('0x2e')][_0x217f('0x4b')](_0x1a6bea[_0x217f('0x4c')]);}})[_0x217f('0x24')](respondWithResult(_0x64d872,null))[_0x217f('0x46')](handleError(_0x64d872,null));};exports[_0x217f('0x56')]=function(_0x577b00,_0x23c2d0,_0x298f1f){var _0x4a2a0f={};var _0x4bf7b3={};var _0x2dd596;var _0x2dc958;return db[_0x217f('0x2e')][_0x217f('0x57')]({'where':{'id':_0x577b00['params']['id']}})[_0x217f('0x24')](handleEntityNotFound(_0x23c2d0,null))['then'](function(_0x500089){if(_0x500089){_0x2dd596=_0x500089;_0x4bf7b3[_0x217f('0x31')]=_[_0x217f('0x33')](db[_0x217f('0x2e')][_0x217f('0x2f')]);_0x4bf7b3[_0x217f('0x32')]=_[_0x217f('0x33')](_0x577b00[_0x217f('0x32')]);_0x4bf7b3[_0x217f('0x34')]=_[_0x217f('0x35')](_0x4bf7b3[_0x217f('0x31')],_0x4bf7b3['query']);_0x4a2a0f[_0x217f('0x37')]=_[_0x217f('0x35')](_0x4bf7b3['model'],qs[_0x217f('0x36')](_0x577b00['query'][_0x217f('0x36')]));_0x4a2a0f['attributes']=_0x4a2a0f[_0x217f('0x37')][_0x217f('0x38')]?_0x4a2a0f['attributes']:_0x4bf7b3[_0x217f('0x31')];_0x4a2a0f[_0x217f('0x3b')]=qs[_0x217f('0x3c')](_0x577b00[_0x217f('0x32')][_0x217f('0x3c')]);_0x4a2a0f[_0x217f('0x41')]=qs['filters'](_[_0x217f('0x58')](_0x577b00['query'],_0x4bf7b3[_0x217f('0x34')]));if(_0x577b00[_0x217f('0x32')][_0x217f('0x3d')]){_0x4a2a0f[_0x217f('0x41')]=_[_0x217f('0x3e')](_0x4a2a0f[_0x217f('0x41')],{'$or':_['map'](_0x4a2a0f[_0x217f('0x37')],function(_0x360b48){var _0x223390={};_0x223390[_0x360b48]={'$like':'%'+_0x577b00[_0x217f('0x32')]['filter']+'%'};return _0x223390;})});}_0x4a2a0f=_[_0x217f('0x3e')]({},_0x4a2a0f,_0x577b00[_0x217f('0x40')]);return _0x2dd596[_0x217f('0x56')](_0x4a2a0f);}})['then'](function(_0x7a633d){if(_0x7a633d){_0x2dc958=_0x7a633d[_0x217f('0x38')];if(!_0x577b00[_0x217f('0x32')]['hasOwnProperty'](_0x217f('0x3a'))){_0x4a2a0f[_0x217f('0x1d')]=qs[_0x217f('0x1d')](_0x577b00['query'][_0x217f('0x1d')]);_0x4a2a0f[_0x217f('0x1c')]=qs[_0x217f('0x1c')](_0x577b00[_0x217f('0x32')][_0x217f('0x1c')]);}return _0x2dd596[_0x217f('0x56')](_0x4a2a0f);}})[_0x217f('0x24')](function(_0x49ede5){if(_0x49ede5){return _0x49ede5?{'count':_0x2dc958,'rows':_0x49ede5}:null;}})['then'](respondWithResult(_0x23c2d0,null))[_0x217f('0x46')](handleError(_0x23c2d0,null));};exports[_0x217f('0x59')]=function(_0x4d3785,_0x2c489f,_0x952d47){return db[_0x217f('0x2e')][_0x217f('0x57')]({'where':{'id':_0x4d3785[_0x217f('0x48')]['id']}})[_0x217f('0x24')](handleEntityNotFound(_0x2c489f,null))['then'](function(_0x3ff14b){if(_0x3ff14b){return db[_0x217f('0x5a')]['transaction'](function(_0x238647){return db['Interval']['destroy']({'where':{'IntervalId':_0x4d3785[_0x217f('0x48')]['id']},'transaction':_0x238647})[_0x217f('0x24')](function(_0x11d133){var _0x51ba14=_[_0x217f('0x2d')](_0x4d3785[_0x217f('0x4c')],function(_0x596d5c){delete _0x596d5c['id'];_0x596d5c[_0x217f('0x55')]=_0x4d3785[_0x217f('0x48')]['id'];return _0x596d5c;});return db['Interval'][_0x217f('0x5b')](_0x51ba14,{'transaction':_0x238647});});})['then'](function(){return db[_0x217f('0x2e')]['findAll']({'where':{'IntervalId':_0x4d3785[_0x217f('0x48')]['id']}});});}})[_0x217f('0x24')](respondWithResult(_0x2c489f,null))['catch'](handleError(_0x2c489f,null));};