Built motion from commit d1eab355.|2.6.28
[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 _0xffc2=['sort','filters','pick','filter','where','VIRTUAL','merge','options','Interval','include','findAll','catch','show','rawAttributes','length','find','create','body','user','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','Intervals','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','addInterval','params','IntervalId','getIntervals','order','addIntervals','findOne','sequelize','eml-format','zip-dir','request-promise','bluebird','mustache','util','path','to-csv','ejs','fs-extra','lodash','desk.js','querystring','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/license/util','../../mysqldb','status','end','json','offset','undefined','limit','count','set','reject','then','destroy','UserProfileResource','sendStatus','error','stack','name','map','fieldName','type','key','model','query','keys','intersection','fields','attributes','hasOwnProperty','nolimit'];(function(_0x138adb,_0x193fee){var _0x1b27f2=function(_0x5e97a2){while(--_0x5e97a2){_0x138adb['push'](_0x138adb['shift']());}};_0x1b27f2(++_0x193fee);}(_0xffc2,0x173));var _0x2ffc=function(_0x198d2a,_0x510eb7){_0x198d2a=_0x198d2a-0x0;var _0xfda34d=_0xffc2[_0x198d2a];return _0xfda34d;};'use strict';var emlformat=require(_0x2ffc('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x2ffc('0x1'));var jsonpatch=require('fast-json-patch');var rp=require(_0x2ffc('0x2'));var moment=require('moment');var BPromise=require(_0x2ffc('0x3'));var Mustache=require(_0x2ffc('0x4'));var util=require(_0x2ffc('0x5'));var path=require(_0x2ffc('0x6'));var sox=require('sox');var csv=require(_0x2ffc('0x7'));var ejs=require(_0x2ffc('0x8'));var fs=require('fs');var fs_extra=require(_0x2ffc('0x9'));var _=require(_0x2ffc('0xa'));var squel=require('squel');var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require(_0x2ffc('0xb'));var toCsv=require(_0x2ffc('0x7'));var querystring=require(_0x2ffc('0xc'));var Papa=require(_0x2ffc('0xd'));var Redis=require(_0x2ffc('0xe'));var authService=require('../../components/auth/service');var qs=require(_0x2ffc('0xf'));var as=require(_0x2ffc('0x10'));var hardwareService=require(_0x2ffc('0x11'));var logger=require('../../config/logger')(_0x2ffc('0x12'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0x2ffc('0x13'));var db=require(_0x2ffc('0x14'))['db'];var extensions=require('../../components/extensions/rewrite');function respondWithStatusCode(_0xb2dc15,_0x32a380){_0x32a380=_0x32a380||0xcc;return function(_0x5c4aac){if(_0x5c4aac){return _0xb2dc15['sendStatus'](_0x32a380);}return _0xb2dc15[_0x2ffc('0x15')](_0x32a380)[_0x2ffc('0x16')]();};}function respondWithResult(_0x3bceca,_0x373402){_0x373402=_0x373402||0xc8;return function(_0x367540){if(_0x367540){return _0x3bceca[_0x2ffc('0x15')](_0x373402)[_0x2ffc('0x17')](_0x367540);}};}function respondWithFilteredResult(_0x48d87e,_0xfcb5f1){return function(_0x43ac2f){if(_0x43ac2f){var _0xba5c12=typeof _0xfcb5f1[_0x2ffc('0x18')]===_0x2ffc('0x19')&&typeof _0xfcb5f1[_0x2ffc('0x1a')]===_0x2ffc('0x19');var _0x3690fa=_0x43ac2f[_0x2ffc('0x1b')];var _0x2702a0=_0xba5c12?0x0:_0xfcb5f1[_0x2ffc('0x18')];var _0x5f57b4=_0xba5c12?_0x43ac2f[_0x2ffc('0x1b')]:_0xfcb5f1[_0x2ffc('0x18')]+_0xfcb5f1[_0x2ffc('0x1a')];var _0x4deee8;if(_0x5f57b4>=_0x3690fa){_0x5f57b4=_0x3690fa;_0x4deee8=0xc8;}else{_0x4deee8=0xce;}_0x48d87e[_0x2ffc('0x15')](_0x4deee8);return _0x48d87e[_0x2ffc('0x1c')]('Content-Range',_0x2702a0+'-'+_0x5f57b4+'/'+_0x3690fa)[_0x2ffc('0x17')](_0x43ac2f);}return null;};}function patchUpdates(_0x322932){return function(_0x5f09ea){try{jsonpatch['apply'](_0x5f09ea,_0x322932,!![]);}catch(_0x912ddc){return BPromise[_0x2ffc('0x1d')](_0x912ddc);}return _0x5f09ea['save']();};}function saveUpdates(_0xad8dae,_0x372970){return function(_0x28596b){if(_0x28596b){return _0x28596b['update'](_0xad8dae)[_0x2ffc('0x1e')](function(_0x1e3938){return _0x1e3938;});}return null;};}function removeEntity(_0x32fb7a,_0x2c46b9){return function(_0x486760){if(_0x486760){return _0x486760[_0x2ffc('0x1f')]()['then'](function(){var _0x131fb7=_0x486760['get']({'plain':!![]});var _0x3cff3e='Intervals';return db[_0x2ffc('0x20')]['destroy']({'where':{'type':_0x3cff3e,'resourceId':_0x131fb7['id']}})['then'](function(){return _0x486760;});})[_0x2ffc('0x1e')](function(){_0x32fb7a['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x3fca9b,_0x60861c){return function(_0x372a39){if(!_0x372a39){_0x3fca9b[_0x2ffc('0x21')](0x194);}return _0x372a39;};}function handleError(_0xff26f3,_0x4af848){_0x4af848=_0x4af848||0x1f4;return function(_0x4ed0e6){logger[_0x2ffc('0x22')](_0x4ed0e6[_0x2ffc('0x23')]);if(_0x4ed0e6[_0x2ffc('0x24')]){delete _0x4ed0e6[_0x2ffc('0x24')];}_0xff26f3[_0x2ffc('0x15')](_0x4af848)['send'](_0x4ed0e6);};}exports['index']=function(_0x52fc74,_0x58f5bc){var _0x1bf2ad={},_0x311bcb={},_0x3edb71={'count':0x0,'rows':[]};var _0x4cd9ea=_[_0x2ffc('0x25')](db['Interval']['rawAttributes'],function(_0x1c1621){return{'name':_0x1c1621[_0x2ffc('0x26')],'type':_0x1c1621[_0x2ffc('0x27')][_0x2ffc('0x28')]};});_0x311bcb[_0x2ffc('0x29')]=_[_0x2ffc('0x25')](_0x4cd9ea,'name');_0x311bcb[_0x2ffc('0x2a')]=_[_0x2ffc('0x2b')](_0x52fc74[_0x2ffc('0x2a')]);_0x311bcb['filters']=_[_0x2ffc('0x2c')](_0x311bcb[_0x2ffc('0x29')],_0x311bcb[_0x2ffc('0x2a')]);_0x1bf2ad['attributes']=_['intersection'](_0x311bcb['model'],qs[_0x2ffc('0x2d')](_0x52fc74[_0x2ffc('0x2a')][_0x2ffc('0x2d')]));_0x1bf2ad[_0x2ffc('0x2e')]=_0x1bf2ad[_0x2ffc('0x2e')]['length']?_0x1bf2ad[_0x2ffc('0x2e')]:_0x311bcb['model'];if(!_0x52fc74[_0x2ffc('0x2a')][_0x2ffc('0x2f')](_0x2ffc('0x30'))){_0x1bf2ad[_0x2ffc('0x1a')]=qs[_0x2ffc('0x1a')](_0x52fc74['query'][_0x2ffc('0x1a')]);_0x1bf2ad[_0x2ffc('0x18')]=qs[_0x2ffc('0x18')](_0x52fc74[_0x2ffc('0x2a')][_0x2ffc('0x18')]);}_0x1bf2ad['order']=qs[_0x2ffc('0x31')](_0x52fc74[_0x2ffc('0x2a')][_0x2ffc('0x31')]);_0x1bf2ad['where']=qs[_0x2ffc('0x32')](_[_0x2ffc('0x33')](_0x52fc74[_0x2ffc('0x2a')],_0x311bcb[_0x2ffc('0x32')]),_0x4cd9ea);if(_0x52fc74[_0x2ffc('0x2a')][_0x2ffc('0x34')]){_0x1bf2ad[_0x2ffc('0x35')]=_['merge'](_0x1bf2ad[_0x2ffc('0x35')],{'$or':_[_0x2ffc('0x25')](_0x4cd9ea,function(_0x115c97){if(_0x115c97[_0x2ffc('0x27')]!==_0x2ffc('0x36')){var _0x325b16={};_0x325b16[_0x115c97['name']]={'$like':'%'+_0x52fc74['query'][_0x2ffc('0x34')]+'%'};return _0x325b16;}})});}_0x1bf2ad=_[_0x2ffc('0x37')]({},_0x1bf2ad,_0x52fc74[_0x2ffc('0x38')]);var _0x4264cd={'where':_0x1bf2ad[_0x2ffc('0x35')]};return db[_0x2ffc('0x39')][_0x2ffc('0x1b')](_0x4264cd)['then'](function(_0x43a6a9){_0x3edb71[_0x2ffc('0x1b')]=_0x43a6a9;if(_0x52fc74['query']['includeAll']){_0x1bf2ad[_0x2ffc('0x3a')]=[{'all':!![]}];}return db['Interval'][_0x2ffc('0x3b')](_0x1bf2ad);})['then'](function(_0x2b900d){_0x3edb71['rows']=_0x2b900d;return _0x3edb71;})['then'](respondWithFilteredResult(_0x58f5bc,_0x1bf2ad))[_0x2ffc('0x3c')](handleError(_0x58f5bc,null));};exports[_0x2ffc('0x3d')]=function(_0x5420d2,_0xd7b5b){var _0x35f263={'raw':!![],'where':{'id':_0x5420d2['params']['id']}},_0x13eddd={};_0x13eddd['model']=_[_0x2ffc('0x2b')](db[_0x2ffc('0x39')][_0x2ffc('0x3e')]);_0x13eddd[_0x2ffc('0x2a')]=_[_0x2ffc('0x2b')](_0x5420d2[_0x2ffc('0x2a')]);_0x13eddd[_0x2ffc('0x32')]=_[_0x2ffc('0x2c')](_0x13eddd[_0x2ffc('0x29')],_0x13eddd[_0x2ffc('0x2a')]);_0x35f263[_0x2ffc('0x2e')]=_[_0x2ffc('0x2c')](_0x13eddd[_0x2ffc('0x29')],qs[_0x2ffc('0x2d')](_0x5420d2[_0x2ffc('0x2a')][_0x2ffc('0x2d')]));_0x35f263['attributes']=_0x35f263[_0x2ffc('0x2e')][_0x2ffc('0x3f')]?_0x35f263[_0x2ffc('0x2e')]:_0x13eddd[_0x2ffc('0x29')];if(_0x5420d2[_0x2ffc('0x2a')]['includeAll']){_0x35f263[_0x2ffc('0x3a')]=[{'all':!![]}];}_0x35f263=_[_0x2ffc('0x37')]({},_0x35f263,_0x5420d2[_0x2ffc('0x38')]);return db['Interval'][_0x2ffc('0x40')](_0x35f263)['then'](handleEntityNotFound(_0xd7b5b,null))['then'](respondWithResult(_0xd7b5b,null))[_0x2ffc('0x3c')](handleError(_0xd7b5b,null));};exports[_0x2ffc('0x41')]=function(_0x3831d0,_0x81e60b){return db['Interval'][_0x2ffc('0x41')](_0x3831d0[_0x2ffc('0x42')],{})[_0x2ffc('0x1e')](function(_0x53b7bd){var _0x553c56=_0x3831d0[_0x2ffc('0x43')][_0x2ffc('0x44')]({'plain':!![]});if(!_0x553c56)throw new Error(_0x2ffc('0x45'));if(_0x553c56[_0x2ffc('0x46')]===_0x2ffc('0x43')){var _0x132b21=_0x53b7bd['get']({'plain':!![]});var _0x542bb8=_0x2ffc('0x47');return db[_0x2ffc('0x48')][_0x2ffc('0x40')]({'where':{'name':_0x542bb8,'userProfileId':_0x553c56[_0x2ffc('0x49')]},'raw':!![]})[_0x2ffc('0x1e')](function(_0x311867){if(_0x311867&&_0x311867[_0x2ffc('0x4a')]===0x0){return db['UserProfileResource'][_0x2ffc('0x41')]({'name':_0x132b21[_0x2ffc('0x24')],'resourceId':_0x132b21['id'],'type':_0x311867[_0x2ffc('0x24')],'sectionId':_0x311867['id']},{})[_0x2ffc('0x1e')](function(){return _0x53b7bd;});}else{return _0x53b7bd;}})[_0x2ffc('0x3c')](function(_0x870726){logger[_0x2ffc('0x22')](_0x2ffc('0x4b'),_0x870726);throw _0x870726;});}return _0x53b7bd;})[_0x2ffc('0x1e')](respondWithResult(_0x81e60b,0xc9))[_0x2ffc('0x3c')](handleError(_0x81e60b,null));};exports['update']=function(_0x273434,_0x3b3ca6){if(_0x273434[_0x2ffc('0x42')]['id']){delete _0x273434[_0x2ffc('0x42')]['id'];}return db[_0x2ffc('0x39')]['find']({'where':{'id':_0x273434['params']['id']}})[_0x2ffc('0x1e')](handleEntityNotFound(_0x3b3ca6,null))['then'](saveUpdates(_0x273434[_0x2ffc('0x42')],null))['then'](respondWithResult(_0x3b3ca6,null))[_0x2ffc('0x3c')](handleError(_0x3b3ca6,null));};exports['destroy']=function(_0xdab3,_0x1d5342){return db[_0x2ffc('0x39')][_0x2ffc('0x40')]({'where':{'id':_0xdab3['params']['id']}})['then'](handleEntityNotFound(_0x1d5342,null))[_0x2ffc('0x1e')](removeEntity(_0x1d5342,null))[_0x2ffc('0x3c')](handleError(_0x1d5342,null));};exports[_0x2ffc('0x4c')]=function(_0x1c551e,_0x162c8c,_0x17a9de){if(_0x1c551e[_0x2ffc('0x42')]['id']){delete _0x1c551e[_0x2ffc('0x42')]['id'];}return db['Interval'][_0x2ffc('0x40')]({'where':{'id':_0x1c551e[_0x2ffc('0x4d')]['id']}})[_0x2ffc('0x1e')](handleEntityNotFound(_0x162c8c,null))[_0x2ffc('0x1e')](function(_0x43c9f2){if(_0x43c9f2){_0x1c551e[_0x2ffc('0x42')][_0x2ffc('0x4e')]=_0x43c9f2['id'];return db[_0x2ffc('0x39')]['create'](_0x1c551e[_0x2ffc('0x42')]);}})['then'](respondWithResult(_0x162c8c,null))[_0x2ffc('0x3c')](handleError(_0x162c8c,null));};exports[_0x2ffc('0x4f')]=function(_0x403bfc,_0x599d81,_0x44e1c3){var _0x3c810b={};var _0x5ecc88={};var _0x383b5f;var _0x56919c;return db[_0x2ffc('0x39')]['findOne']({'where':{'id':_0x403bfc[_0x2ffc('0x4d')]['id']}})['then'](handleEntityNotFound(_0x599d81,null))[_0x2ffc('0x1e')](function(_0x4606ae){if(_0x4606ae){_0x383b5f=_0x4606ae;_0x5ecc88[_0x2ffc('0x29')]=_[_0x2ffc('0x2b')](db[_0x2ffc('0x39')][_0x2ffc('0x3e')]);_0x5ecc88[_0x2ffc('0x2a')]=_[_0x2ffc('0x2b')](_0x403bfc[_0x2ffc('0x2a')]);_0x5ecc88[_0x2ffc('0x32')]=_[_0x2ffc('0x2c')](_0x5ecc88[_0x2ffc('0x29')],_0x5ecc88['query']);_0x3c810b[_0x2ffc('0x2e')]=_[_0x2ffc('0x2c')](_0x5ecc88[_0x2ffc('0x29')],qs[_0x2ffc('0x2d')](_0x403bfc['query'][_0x2ffc('0x2d')]));_0x3c810b[_0x2ffc('0x2e')]=_0x3c810b[_0x2ffc('0x2e')][_0x2ffc('0x3f')]?_0x3c810b[_0x2ffc('0x2e')]:_0x5ecc88[_0x2ffc('0x29')];_0x3c810b[_0x2ffc('0x50')]=qs[_0x2ffc('0x31')](_0x403bfc['query'][_0x2ffc('0x31')]);_0x3c810b[_0x2ffc('0x35')]=qs[_0x2ffc('0x32')](_[_0x2ffc('0x33')](_0x403bfc[_0x2ffc('0x2a')],_0x5ecc88[_0x2ffc('0x32')]));if(_0x403bfc['query'][_0x2ffc('0x34')]){_0x3c810b['where']=_[_0x2ffc('0x37')](_0x3c810b[_0x2ffc('0x35')],{'$or':_[_0x2ffc('0x25')](_0x3c810b[_0x2ffc('0x2e')],function(_0x54212e){var _0x105158={};_0x105158[_0x54212e]={'$like':'%'+_0x403bfc[_0x2ffc('0x2a')]['filter']+'%'};return _0x105158;})});}_0x3c810b=_[_0x2ffc('0x37')]({},_0x3c810b,_0x403bfc[_0x2ffc('0x38')]);return _0x383b5f['getIntervals'](_0x3c810b);}})[_0x2ffc('0x1e')](function(_0x12ae17){if(_0x12ae17){_0x56919c=_0x12ae17[_0x2ffc('0x3f')];if(!_0x403bfc[_0x2ffc('0x2a')][_0x2ffc('0x2f')]('nolimit')){_0x3c810b[_0x2ffc('0x1a')]=qs[_0x2ffc('0x1a')](_0x403bfc[_0x2ffc('0x2a')]['limit']);_0x3c810b[_0x2ffc('0x18')]=qs['offset'](_0x403bfc['query'][_0x2ffc('0x18')]);}return _0x383b5f[_0x2ffc('0x4f')](_0x3c810b);}})[_0x2ffc('0x1e')](function(_0x40d38e){if(_0x40d38e){return _0x40d38e?{'count':_0x56919c,'rows':_0x40d38e}:null;}})[_0x2ffc('0x1e')](respondWithResult(_0x599d81,null))[_0x2ffc('0x3c')](handleError(_0x599d81,null));};exports[_0x2ffc('0x51')]=function(_0x339453,_0x40ddc2,_0x38cc57){return db['Interval'][_0x2ffc('0x52')]({'where':{'id':_0x339453[_0x2ffc('0x4d')]['id']}})['then'](handleEntityNotFound(_0x40ddc2,null))['then'](function(_0x46ae1a){if(_0x46ae1a){return db[_0x2ffc('0x53')]['transaction'](function(_0xca7ca8){return db[_0x2ffc('0x39')][_0x2ffc('0x1f')]({'where':{'IntervalId':_0x339453[_0x2ffc('0x4d')]['id']},'transaction':_0xca7ca8})['then'](function(_0x48e58d){var _0x29fe35=_[_0x2ffc('0x25')](_0x339453[_0x2ffc('0x42')],function(_0x5290f6){delete _0x5290f6['id'];_0x5290f6['IntervalId']=_0x339453[_0x2ffc('0x4d')]['id'];return _0x5290f6;});return db['Interval']['bulkCreate'](_0x29fe35,{'transaction':_0xca7ca8});});})[_0x2ffc('0x1e')](function(){return db[_0x2ffc('0x39')]['findAll']({'where':{'IntervalId':_0x339453['params']['id']}});});}})['then'](respondWithResult(_0x40ddc2,null))[_0x2ffc('0x3c')](handleError(_0x40ddc2,null));};