576e31bf62f971e832230254330caa1a0e419043
[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 _0xb6bb=['set','Content-Range','apply','reject','save','then','destroy','Intervals','UserProfileResource','error','stack','name','send','map','Interval','fieldName','key','model','query','keys','intersection','attributes','fields','length','hasOwnProperty','order','sort','where','filters','pick','merge','VIRTUAL','filter','options','includeAll','include','findAll','rows','show','params','rawAttributes','find','catch','create','body','user','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','addInterval','getIntervals','findOne','nolimit','sequelize','transaction','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','path','sox','to-csv','ejs','lodash','squel','jsforce','desk.js','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','../../components/extensions/rewrite','sendStatus','status','end','json','offset','count','limit'];(function(_0x29b609,_0x19d388){var _0x54c952=function(_0xb3c38d){while(--_0xb3c38d){_0x29b609['push'](_0x29b609['shift']());}};_0x54c952(++_0x19d388);}(_0xb6bb,0xf4));var _0xbb6b=function(_0x554d68,_0x34ac83){_0x554d68=_0x554d68-0x0;var _0x40e00f=_0xb6bb[_0x554d68];return _0x40e00f;};'use strict';var emlformat=require(_0xbb6b('0x0'));var rimraf=require(_0xbb6b('0x1'));var zipdir=require(_0xbb6b('0x2'));var jsonpatch=require(_0xbb6b('0x3'));var rp=require(_0xbb6b('0x4'));var moment=require(_0xbb6b('0x5'));var BPromise=require(_0xbb6b('0x6'));var Mustache=require(_0xbb6b('0x7'));var util=require('util');var path=require(_0xbb6b('0x8'));var sox=require(_0xbb6b('0x9'));var csv=require(_0xbb6b('0xa'));var ejs=require(_0xbb6b('0xb'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xbb6b('0xc'));var squel=require(_0xbb6b('0xd'));var crypto=require('crypto');var jsforce=require(_0xbb6b('0xe'));var deskjs=require(_0xbb6b('0xf'));var toCsv=require(_0xbb6b('0xa'));var querystring=require('querystring');var Papa=require(_0xbb6b('0x10'));var Redis=require('ioredis');var authService=require(_0xbb6b('0x11'));var qs=require(_0xbb6b('0x12'));var as=require(_0xbb6b('0x13'));var hardwareService=require(_0xbb6b('0x14'));var logger=require(_0xbb6b('0x15'))(_0xbb6b('0x16'));var utils=require(_0xbb6b('0x17'));var config=require(_0xbb6b('0x18'));var licenseUtil=require(_0xbb6b('0x19'));var db=require(_0xbb6b('0x1a'))['db'];var extensions=require(_0xbb6b('0x1b'));function respondWithStatusCode(_0x5ecee9,_0x16efc3){_0x16efc3=_0x16efc3||0xcc;return function(_0x2d0be4){if(_0x2d0be4){return _0x5ecee9[_0xbb6b('0x1c')](_0x16efc3);}return _0x5ecee9[_0xbb6b('0x1d')](_0x16efc3)[_0xbb6b('0x1e')]();};}function respondWithResult(_0x45ef45,_0x3b1db4){_0x3b1db4=_0x3b1db4||0xc8;return function(_0x16624a){if(_0x16624a){return _0x45ef45[_0xbb6b('0x1d')](_0x3b1db4)[_0xbb6b('0x1f')](_0x16624a);}};}function respondWithFilteredResult(_0x51c034,_0x1b52d8){return function(_0x2b0042){if(_0x2b0042){var _0x31a258=typeof _0x1b52d8[_0xbb6b('0x20')]==='undefined'&&typeof _0x1b52d8['limit']==='undefined';var _0x3c88cd=_0x2b0042['count'];var _0x213144=_0x31a258?0x0:_0x1b52d8['offset'];var _0x53f6c7=_0x31a258?_0x2b0042[_0xbb6b('0x21')]:_0x1b52d8['offset']+_0x1b52d8[_0xbb6b('0x22')];var _0x345a2c;if(_0x53f6c7>=_0x3c88cd){_0x53f6c7=_0x3c88cd;_0x345a2c=0xc8;}else{_0x345a2c=0xce;}_0x51c034[_0xbb6b('0x1d')](_0x345a2c);return _0x51c034[_0xbb6b('0x23')](_0xbb6b('0x24'),_0x213144+'-'+_0x53f6c7+'/'+_0x3c88cd)[_0xbb6b('0x1f')](_0x2b0042);}return null;};}function patchUpdates(_0x46cdcf){return function(_0x48c7b8){try{jsonpatch[_0xbb6b('0x25')](_0x48c7b8,_0x46cdcf,!![]);}catch(_0xeae4dc){return BPromise[_0xbb6b('0x26')](_0xeae4dc);}return _0x48c7b8[_0xbb6b('0x27')]();};}function saveUpdates(_0x15d8db,_0x16fde8){return function(_0x361fa6){if(_0x361fa6){return _0x361fa6['update'](_0x15d8db)[_0xbb6b('0x28')](function(_0x342009){return _0x342009;});}return null;};}function removeEntity(_0x29d2fa,_0x2089fa){return function(_0xbc93a5){if(_0xbc93a5){return _0xbc93a5[_0xbb6b('0x29')]()[_0xbb6b('0x28')](function(){var _0x3c0e05=_0xbc93a5['get']({'plain':!![]});var _0x48819e=_0xbb6b('0x2a');return db[_0xbb6b('0x2b')][_0xbb6b('0x29')]({'where':{'type':_0x48819e,'resourceId':_0x3c0e05['id']}})[_0xbb6b('0x28')](function(){return _0xbc93a5;});})[_0xbb6b('0x28')](function(){_0x29d2fa[_0xbb6b('0x1d')](0xcc)[_0xbb6b('0x1e')]();});}};}function handleEntityNotFound(_0x26857f,_0x1ff711){return function(_0x2fd2a9){if(!_0x2fd2a9){_0x26857f[_0xbb6b('0x1c')](0x194);}return _0x2fd2a9;};}function handleError(_0x47e21c,_0xd9adf0){_0xd9adf0=_0xd9adf0||0x1f4;return function(_0x4b9475){logger[_0xbb6b('0x2c')](_0x4b9475[_0xbb6b('0x2d')]);if(_0x4b9475[_0xbb6b('0x2e')]){delete _0x4b9475[_0xbb6b('0x2e')];}_0x47e21c[_0xbb6b('0x1d')](_0xd9adf0)[_0xbb6b('0x2f')](_0x4b9475);};}exports['index']=function(_0x1fd14e,_0x4113a3){var _0x3813a7={},_0x2b577f={},_0x1ff680={'count':0x0,'rows':[]};var _0x1e6fce=_[_0xbb6b('0x30')](db[_0xbb6b('0x31')]['rawAttributes'],function(_0x47102f){return{'name':_0x47102f[_0xbb6b('0x32')],'type':_0x47102f['type'][_0xbb6b('0x33')]};});_0x2b577f[_0xbb6b('0x34')]=_[_0xbb6b('0x30')](_0x1e6fce,_0xbb6b('0x2e'));_0x2b577f[_0xbb6b('0x35')]=_[_0xbb6b('0x36')](_0x1fd14e[_0xbb6b('0x35')]);_0x2b577f['filters']=_[_0xbb6b('0x37')](_0x2b577f[_0xbb6b('0x34')],_0x2b577f[_0xbb6b('0x35')]);_0x3813a7[_0xbb6b('0x38')]=_[_0xbb6b('0x37')](_0x2b577f[_0xbb6b('0x34')],qs[_0xbb6b('0x39')](_0x1fd14e[_0xbb6b('0x35')][_0xbb6b('0x39')]));_0x3813a7[_0xbb6b('0x38')]=_0x3813a7[_0xbb6b('0x38')][_0xbb6b('0x3a')]?_0x3813a7['attributes']:_0x2b577f['model'];if(!_0x1fd14e['query'][_0xbb6b('0x3b')]('nolimit')){_0x3813a7[_0xbb6b('0x22')]=qs['limit'](_0x1fd14e[_0xbb6b('0x35')][_0xbb6b('0x22')]);_0x3813a7[_0xbb6b('0x20')]=qs['offset'](_0x1fd14e[_0xbb6b('0x35')][_0xbb6b('0x20')]);}_0x3813a7[_0xbb6b('0x3c')]=qs['sort'](_0x1fd14e['query'][_0xbb6b('0x3d')]);_0x3813a7[_0xbb6b('0x3e')]=qs[_0xbb6b('0x3f')](_[_0xbb6b('0x40')](_0x1fd14e[_0xbb6b('0x35')],_0x2b577f[_0xbb6b('0x3f')]),_0x1e6fce);if(_0x1fd14e[_0xbb6b('0x35')]['filter']){_0x3813a7[_0xbb6b('0x3e')]=_[_0xbb6b('0x41')](_0x3813a7[_0xbb6b('0x3e')],{'$or':_[_0xbb6b('0x30')](_0x1e6fce,function(_0x4e3cf3){if(_0x4e3cf3['type']!==_0xbb6b('0x42')){var _0x268b3c={};_0x268b3c[_0x4e3cf3[_0xbb6b('0x2e')]]={'$like':'%'+_0x1fd14e['query'][_0xbb6b('0x43')]+'%'};return _0x268b3c;}})});}_0x3813a7=_[_0xbb6b('0x41')]({},_0x3813a7,_0x1fd14e[_0xbb6b('0x44')]);var _0x56b765={'where':_0x3813a7[_0xbb6b('0x3e')]};return db[_0xbb6b('0x31')][_0xbb6b('0x21')](_0x56b765)[_0xbb6b('0x28')](function(_0x1af636){_0x1ff680[_0xbb6b('0x21')]=_0x1af636;if(_0x1fd14e[_0xbb6b('0x35')][_0xbb6b('0x45')]){_0x3813a7[_0xbb6b('0x46')]=[{'all':!![]}];}return db['Interval'][_0xbb6b('0x47')](_0x3813a7);})['then'](function(_0x496481){_0x1ff680[_0xbb6b('0x48')]=_0x496481;return _0x1ff680;})['then'](respondWithFilteredResult(_0x4113a3,_0x3813a7))['catch'](handleError(_0x4113a3,null));};exports[_0xbb6b('0x49')]=function(_0x5760c3,_0x842e5){var _0x5fcab4={'raw':!![],'where':{'id':_0x5760c3[_0xbb6b('0x4a')]['id']}},_0x117b69={};_0x117b69[_0xbb6b('0x34')]=_[_0xbb6b('0x36')](db[_0xbb6b('0x31')][_0xbb6b('0x4b')]);_0x117b69[_0xbb6b('0x35')]=_[_0xbb6b('0x36')](_0x5760c3[_0xbb6b('0x35')]);_0x117b69[_0xbb6b('0x3f')]=_[_0xbb6b('0x37')](_0x117b69['model'],_0x117b69[_0xbb6b('0x35')]);_0x5fcab4[_0xbb6b('0x38')]=_['intersection'](_0x117b69[_0xbb6b('0x34')],qs[_0xbb6b('0x39')](_0x5760c3['query'][_0xbb6b('0x39')]));_0x5fcab4['attributes']=_0x5fcab4[_0xbb6b('0x38')][_0xbb6b('0x3a')]?_0x5fcab4[_0xbb6b('0x38')]:_0x117b69[_0xbb6b('0x34')];if(_0x5760c3[_0xbb6b('0x35')]['includeAll']){_0x5fcab4[_0xbb6b('0x46')]=[{'all':!![]}];}_0x5fcab4=_['merge']({},_0x5fcab4,_0x5760c3[_0xbb6b('0x44')]);return db[_0xbb6b('0x31')][_0xbb6b('0x4c')](_0x5fcab4)[_0xbb6b('0x28')](handleEntityNotFound(_0x842e5,null))[_0xbb6b('0x28')](respondWithResult(_0x842e5,null))[_0xbb6b('0x4d')](handleError(_0x842e5,null));};exports[_0xbb6b('0x4e')]=function(_0x485024,_0x5986ab){return db[_0xbb6b('0x31')][_0xbb6b('0x4e')](_0x485024[_0xbb6b('0x4f')],{})[_0xbb6b('0x28')](function(_0x48bc24){var _0xff8ac6=_0x485024[_0xbb6b('0x50')][_0xbb6b('0x51')]({'plain':!![]});if(!_0xff8ac6)throw new Error(_0xbb6b('0x52'));if(_0xff8ac6[_0xbb6b('0x53')]===_0xbb6b('0x50')){var _0x121c4f=_0x48bc24['get']({'plain':!![]});var _0x47d220='Intervals';return db[_0xbb6b('0x54')][_0xbb6b('0x4c')]({'where':{'name':_0x47d220,'userProfileId':_0xff8ac6[_0xbb6b('0x55')]},'raw':!![]})[_0xbb6b('0x28')](function(_0x4443b2){if(_0x4443b2&&_0x4443b2[_0xbb6b('0x56')]===0x0){return db['UserProfileResource'][_0xbb6b('0x4e')]({'name':_0x121c4f[_0xbb6b('0x2e')],'resourceId':_0x121c4f['id'],'type':_0x4443b2[_0xbb6b('0x2e')],'sectionId':_0x4443b2['id']},{})['then'](function(){return _0x48bc24;});}else{return _0x48bc24;}})[_0xbb6b('0x4d')](function(_0x9f4129){logger[_0xbb6b('0x2c')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x9f4129);throw _0x9f4129;});}return _0x48bc24;})[_0xbb6b('0x28')](respondWithResult(_0x5986ab,0xc9))[_0xbb6b('0x4d')](handleError(_0x5986ab,null));};exports['update']=function(_0x294df5,_0x5397e0){if(_0x294df5['body']['id']){delete _0x294df5[_0xbb6b('0x4f')]['id'];}return db[_0xbb6b('0x31')][_0xbb6b('0x4c')]({'where':{'id':_0x294df5[_0xbb6b('0x4a')]['id']}})[_0xbb6b('0x28')](handleEntityNotFound(_0x5397e0,null))[_0xbb6b('0x28')](saveUpdates(_0x294df5[_0xbb6b('0x4f')],null))[_0xbb6b('0x28')](respondWithResult(_0x5397e0,null))[_0xbb6b('0x4d')](handleError(_0x5397e0,null));};exports[_0xbb6b('0x29')]=function(_0x4e05ab,_0x288fe9){return db[_0xbb6b('0x31')][_0xbb6b('0x4c')]({'where':{'id':_0x4e05ab[_0xbb6b('0x4a')]['id']}})[_0xbb6b('0x28')](handleEntityNotFound(_0x288fe9,null))['then'](removeEntity(_0x288fe9,null))[_0xbb6b('0x4d')](handleError(_0x288fe9,null));};exports[_0xbb6b('0x57')]=function(_0x518672,_0x1e9233,_0x143f86){if(_0x518672[_0xbb6b('0x4f')]['id']){delete _0x518672[_0xbb6b('0x4f')]['id'];}return db[_0xbb6b('0x31')][_0xbb6b('0x4c')]({'where':{'id':_0x518672[_0xbb6b('0x4a')]['id']}})[_0xbb6b('0x28')](handleEntityNotFound(_0x1e9233,null))[_0xbb6b('0x28')](function(_0x139568){if(_0x139568){_0x518672[_0xbb6b('0x4f')]['IntervalId']=_0x139568['id'];return db[_0xbb6b('0x31')][_0xbb6b('0x4e')](_0x518672['body']);}})[_0xbb6b('0x28')](respondWithResult(_0x1e9233,null))[_0xbb6b('0x4d')](handleError(_0x1e9233,null));};exports[_0xbb6b('0x58')]=function(_0x120d62,_0x50741e,_0x99bc8){var _0x52362f={};var _0x94120c={};var _0x472d3a;var _0x2a5590;return db['Interval'][_0xbb6b('0x59')]({'where':{'id':_0x120d62[_0xbb6b('0x4a')]['id']}})['then'](handleEntityNotFound(_0x50741e,null))[_0xbb6b('0x28')](function(_0x29ee37){if(_0x29ee37){_0x472d3a=_0x29ee37;_0x94120c[_0xbb6b('0x34')]=_[_0xbb6b('0x36')](db[_0xbb6b('0x31')][_0xbb6b('0x4b')]);_0x94120c[_0xbb6b('0x35')]=_[_0xbb6b('0x36')](_0x120d62[_0xbb6b('0x35')]);_0x94120c[_0xbb6b('0x3f')]=_[_0xbb6b('0x37')](_0x94120c['model'],_0x94120c[_0xbb6b('0x35')]);_0x52362f[_0xbb6b('0x38')]=_[_0xbb6b('0x37')](_0x94120c['model'],qs['fields'](_0x120d62[_0xbb6b('0x35')][_0xbb6b('0x39')]));_0x52362f[_0xbb6b('0x38')]=_0x52362f[_0xbb6b('0x38')][_0xbb6b('0x3a')]?_0x52362f[_0xbb6b('0x38')]:_0x94120c[_0xbb6b('0x34')];_0x52362f['order']=qs['sort'](_0x120d62[_0xbb6b('0x35')][_0xbb6b('0x3d')]);_0x52362f[_0xbb6b('0x3e')]=qs[_0xbb6b('0x3f')](_[_0xbb6b('0x40')](_0x120d62[_0xbb6b('0x35')],_0x94120c['filters']));if(_0x120d62[_0xbb6b('0x35')][_0xbb6b('0x43')]){_0x52362f[_0xbb6b('0x3e')]=_[_0xbb6b('0x41')](_0x52362f[_0xbb6b('0x3e')],{'$or':_[_0xbb6b('0x30')](_0x52362f[_0xbb6b('0x38')],function(_0x4012cd){var _0x203e99={};_0x203e99[_0x4012cd]={'$like':'%'+_0x120d62[_0xbb6b('0x35')]['filter']+'%'};return _0x203e99;})});}_0x52362f=_[_0xbb6b('0x41')]({},_0x52362f,_0x120d62[_0xbb6b('0x44')]);return _0x472d3a[_0xbb6b('0x58')](_0x52362f);}})[_0xbb6b('0x28')](function(_0x2a90ba){if(_0x2a90ba){_0x2a5590=_0x2a90ba[_0xbb6b('0x3a')];if(!_0x120d62[_0xbb6b('0x35')]['hasOwnProperty'](_0xbb6b('0x5a'))){_0x52362f['limit']=qs[_0xbb6b('0x22')](_0x120d62['query']['limit']);_0x52362f[_0xbb6b('0x20')]=qs[_0xbb6b('0x20')](_0x120d62[_0xbb6b('0x35')][_0xbb6b('0x20')]);}return _0x472d3a[_0xbb6b('0x58')](_0x52362f);}})[_0xbb6b('0x28')](function(_0x1ba2e6){if(_0x1ba2e6){return _0x1ba2e6?{'count':_0x2a5590,'rows':_0x1ba2e6}:null;}})[_0xbb6b('0x28')](respondWithResult(_0x50741e,null))[_0xbb6b('0x4d')](handleError(_0x50741e,null));};exports['addIntervals']=function(_0x491bfc,_0xa9178d,_0x2371ad){return db[_0xbb6b('0x31')][_0xbb6b('0x59')]({'where':{'id':_0x491bfc['params']['id']}})[_0xbb6b('0x28')](handleEntityNotFound(_0xa9178d,null))[_0xbb6b('0x28')](function(_0x47f1b2){if(_0x47f1b2){return db[_0xbb6b('0x5b')][_0xbb6b('0x5c')](function(_0x1c3ee5){return db[_0xbb6b('0x31')][_0xbb6b('0x29')]({'where':{'IntervalId':_0x491bfc[_0xbb6b('0x4a')]['id']},'transaction':_0x1c3ee5})[_0xbb6b('0x28')](function(_0x110618){var _0x2e2113=_[_0xbb6b('0x30')](_0x491bfc[_0xbb6b('0x4f')],function(_0x587274){delete _0x587274['id'];_0x587274['IntervalId']=_0x491bfc[_0xbb6b('0x4a')]['id'];return _0x587274;});return db[_0xbb6b('0x31')]['bulkCreate'](_0x2e2113,{'transaction':_0x1c3ee5});});})[_0xbb6b('0x28')](function(){return db[_0xbb6b('0x31')]['findAll']({'where':{'IntervalId':_0x491bfc[_0xbb6b('0x4a')]['id']}});});}})[_0xbb6b('0x28')](respondWithResult(_0xa9178d,null))[_0xbb6b('0x4d')](handleError(_0xa9178d,null));};