Built motion from commit f7863d46.|2.5.41
[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 _0x6754=['merge','type','VIRTUAL','includeAll','include','findAll','rows','show','params','keys','options','find','catch','create','body','user','UserProfileSection','userProfileId','autoAssociation','addInterval','IntervalId','getIntervals','findOne','addIntervals','sequelize','transaction','bulkCreate','eml-format','zip-dir','fast-json-patch','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','squel','crypto','desk.js','querystring','papaparse','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/license/util','../../mysqldb','../../components/extensions/rewrite','status','end','json','offset','undefined','count','set','reject','update','then','destroy','get','Intervals','UserProfileResource','sendStatus','error','stack','name','send','index','map','Interval','rawAttributes','fieldName','key','model','query','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','limit','order','sort','where','pick','filter'];(function(_0x44a64f,_0x38475c){var _0x52024b=function(_0x2cd055){while(--_0x2cd055){_0x44a64f['push'](_0x44a64f['shift']());}};_0x52024b(++_0x38475c);}(_0x6754,0x76));var _0x4675=function(_0x5d7635,_0x54dc7c){_0x5d7635=_0x5d7635-0x0;var _0x39f10e=_0x6754[_0x5d7635];return _0x39f10e;};'use strict';var emlformat=require(_0x4675('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x4675('0x1'));var jsonpatch=require(_0x4675('0x2'));var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0x4675('0x3'));var Mustache=require(_0x4675('0x4'));var util=require(_0x4675('0x5'));var path=require(_0x4675('0x6'));var sox=require(_0x4675('0x7'));var csv=require(_0x4675('0x8'));var ejs=require(_0x4675('0x9'));var fs=require('fs');var fs_extra=require(_0x4675('0xa'));var _=require('lodash');var squel=require(_0x4675('0xb'));var crypto=require(_0x4675('0xc'));var jsforce=require('jsforce');var deskjs=require(_0x4675('0xd'));var toCsv=require(_0x4675('0x8'));var querystring=require(_0x4675('0xe'));var Papa=require(_0x4675('0xf'));var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require(_0x4675('0x10'));var as=require(_0x4675('0x11'));var hardwareService=require(_0x4675('0x12'));var logger=require(_0x4675('0x13'))('api');var utils=require(_0x4675('0x14'));var config=require('../../config/environment');var licenseUtil=require(_0x4675('0x15'));var db=require(_0x4675('0x16'))['db'];var extensions=require(_0x4675('0x17'));function respondWithStatusCode(_0x5a1cc4,_0x5d801b){_0x5d801b=_0x5d801b||0xcc;return function(_0x2333cf){if(_0x2333cf){return _0x5a1cc4['sendStatus'](_0x5d801b);}return _0x5a1cc4[_0x4675('0x18')](_0x5d801b)[_0x4675('0x19')]();};}function respondWithResult(_0x5c610f,_0x3c2425){_0x3c2425=_0x3c2425||0xc8;return function(_0x20a2c5){if(_0x20a2c5){return _0x5c610f[_0x4675('0x18')](_0x3c2425)[_0x4675('0x1a')](_0x20a2c5);}};}function respondWithFilteredResult(_0x4d8116,_0x4431d0){return function(_0x445f59){if(_0x445f59){var _0x43f681=typeof _0x4431d0[_0x4675('0x1b')]==='undefined'&&typeof _0x4431d0['limit']===_0x4675('0x1c');var _0xaebf2=_0x445f59['count'];var _0xbb1532=_0x43f681?0x0:_0x4431d0[_0x4675('0x1b')];var _0x1551a7=_0x43f681?_0x445f59[_0x4675('0x1d')]:_0x4431d0[_0x4675('0x1b')]+_0x4431d0['limit'];var _0x598c5e;if(_0x1551a7>=_0xaebf2){_0x1551a7=_0xaebf2;_0x598c5e=0xc8;}else{_0x598c5e=0xce;}_0x4d8116[_0x4675('0x18')](_0x598c5e);return _0x4d8116[_0x4675('0x1e')]('Content-Range',_0xbb1532+'-'+_0x1551a7+'/'+_0xaebf2)[_0x4675('0x1a')](_0x445f59);}return null;};}function patchUpdates(_0x53317a){return function(_0x55488f){try{jsonpatch['apply'](_0x55488f,_0x53317a,!![]);}catch(_0x40b081){return BPromise[_0x4675('0x1f')](_0x40b081);}return _0x55488f['save']();};}function saveUpdates(_0x11ef6e,_0x106b54){return function(_0x204a14){if(_0x204a14){return _0x204a14[_0x4675('0x20')](_0x11ef6e)[_0x4675('0x21')](function(_0x268e74){return _0x268e74;});}return null;};}function removeEntity(_0x44496f,_0x44f408){return function(_0xab6694){if(_0xab6694){return _0xab6694[_0x4675('0x22')]()[_0x4675('0x21')](function(){var _0x51e5a1=_0xab6694[_0x4675('0x23')]({'plain':!![]});var _0x3f7a4e=_0x4675('0x24');return db[_0x4675('0x25')]['destroy']({'where':{'type':_0x3f7a4e,'resourceId':_0x51e5a1['id']}})[_0x4675('0x21')](function(){return _0xab6694;});})['then'](function(){_0x44496f[_0x4675('0x18')](0xcc)['end']();});}};}function handleEntityNotFound(_0x10704f,_0x57d3dd){return function(_0x122db6){if(!_0x122db6){_0x10704f[_0x4675('0x26')](0x194);}return _0x122db6;};}function handleError(_0x46af64,_0x8cf033){_0x8cf033=_0x8cf033||0x1f4;return function(_0x1c419a){logger[_0x4675('0x27')](_0x1c419a[_0x4675('0x28')]);if(_0x1c419a[_0x4675('0x29')]){delete _0x1c419a[_0x4675('0x29')];}_0x46af64[_0x4675('0x18')](_0x8cf033)[_0x4675('0x2a')](_0x1c419a);};}exports[_0x4675('0x2b')]=function(_0x37dcc9,_0x30454d){var _0x8db7fc={},_0x3578e4={},_0x4f1c9b={'count':0x0,'rows':[]};var _0x33cbef=_[_0x4675('0x2c')](db[_0x4675('0x2d')][_0x4675('0x2e')],function(_0x21fe62){return{'name':_0x21fe62[_0x4675('0x2f')],'type':_0x21fe62['type'][_0x4675('0x30')]};});_0x3578e4[_0x4675('0x31')]=_['map'](_0x33cbef,'name');_0x3578e4[_0x4675('0x32')]=_['keys'](_0x37dcc9[_0x4675('0x32')]);_0x3578e4[_0x4675('0x33')]=_[_0x4675('0x34')](_0x3578e4[_0x4675('0x31')],_0x3578e4[_0x4675('0x32')]);_0x8db7fc[_0x4675('0x35')]=_['intersection'](_0x3578e4['model'],qs[_0x4675('0x36')](_0x37dcc9[_0x4675('0x32')][_0x4675('0x36')]));_0x8db7fc['attributes']=_0x8db7fc[_0x4675('0x35')][_0x4675('0x37')]?_0x8db7fc[_0x4675('0x35')]:_0x3578e4['model'];if(!_0x37dcc9['query'][_0x4675('0x38')](_0x4675('0x39'))){_0x8db7fc['limit']=qs[_0x4675('0x3a')](_0x37dcc9[_0x4675('0x32')]['limit']);_0x8db7fc[_0x4675('0x1b')]=qs[_0x4675('0x1b')](_0x37dcc9[_0x4675('0x32')]['offset']);}_0x8db7fc[_0x4675('0x3b')]=qs[_0x4675('0x3c')](_0x37dcc9[_0x4675('0x32')][_0x4675('0x3c')]);_0x8db7fc[_0x4675('0x3d')]=qs[_0x4675('0x33')](_[_0x4675('0x3e')](_0x37dcc9['query'],_0x3578e4[_0x4675('0x33')]),_0x33cbef);if(_0x37dcc9[_0x4675('0x32')][_0x4675('0x3f')]){_0x8db7fc[_0x4675('0x3d')]=_[_0x4675('0x40')](_0x8db7fc[_0x4675('0x3d')],{'$or':_[_0x4675('0x2c')](_0x33cbef,function(_0x4be503){if(_0x4be503[_0x4675('0x41')]!==_0x4675('0x42')){var _0x413aa9={};_0x413aa9[_0x4be503[_0x4675('0x29')]]={'$like':'%'+_0x37dcc9[_0x4675('0x32')]['filter']+'%'};return _0x413aa9;}})});}_0x8db7fc=_[_0x4675('0x40')]({},_0x8db7fc,_0x37dcc9['options']);var _0x2838fa={'where':_0x8db7fc[_0x4675('0x3d')]};return db['Interval'][_0x4675('0x1d')](_0x2838fa)[_0x4675('0x21')](function(_0x550be1){_0x4f1c9b[_0x4675('0x1d')]=_0x550be1;if(_0x37dcc9['query'][_0x4675('0x43')]){_0x8db7fc[_0x4675('0x44')]=[{'all':!![]}];}return db[_0x4675('0x2d')][_0x4675('0x45')](_0x8db7fc);})[_0x4675('0x21')](function(_0x1e0196){_0x4f1c9b[_0x4675('0x46')]=_0x1e0196;return _0x4f1c9b;})['then'](respondWithFilteredResult(_0x30454d,_0x8db7fc))['catch'](handleError(_0x30454d,null));};exports[_0x4675('0x47')]=function(_0x42db2d,_0x258e4c){var _0x35dcd5={'raw':!![],'where':{'id':_0x42db2d[_0x4675('0x48')]['id']}},_0x102b52={};_0x102b52[_0x4675('0x31')]=_[_0x4675('0x49')](db[_0x4675('0x2d')][_0x4675('0x2e')]);_0x102b52[_0x4675('0x32')]=_[_0x4675('0x49')](_0x42db2d[_0x4675('0x32')]);_0x102b52[_0x4675('0x33')]=_[_0x4675('0x34')](_0x102b52['model'],_0x102b52[_0x4675('0x32')]);_0x35dcd5['attributes']=_[_0x4675('0x34')](_0x102b52[_0x4675('0x31')],qs[_0x4675('0x36')](_0x42db2d[_0x4675('0x32')]['fields']));_0x35dcd5[_0x4675('0x35')]=_0x35dcd5[_0x4675('0x35')][_0x4675('0x37')]?_0x35dcd5['attributes']:_0x102b52[_0x4675('0x31')];if(_0x42db2d[_0x4675('0x32')][_0x4675('0x43')]){_0x35dcd5[_0x4675('0x44')]=[{'all':!![]}];}_0x35dcd5=_[_0x4675('0x40')]({},_0x35dcd5,_0x42db2d[_0x4675('0x4a')]);return db['Interval'][_0x4675('0x4b')](_0x35dcd5)[_0x4675('0x21')](handleEntityNotFound(_0x258e4c,null))[_0x4675('0x21')](respondWithResult(_0x258e4c,null))[_0x4675('0x4c')](handleError(_0x258e4c,null));};exports[_0x4675('0x4d')]=function(_0x4c8cc8,_0x4fbacf){return db['Interval'][_0x4675('0x4d')](_0x4c8cc8[_0x4675('0x4e')],{})['then'](function(_0x32ab9a){var _0x50d924=_0x4c8cc8[_0x4675('0x4f')][_0x4675('0x23')]({'plain':!![]});if(!_0x50d924)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x50d924['role']===_0x4675('0x4f')){var _0x52fa11=_0x32ab9a[_0x4675('0x23')]({'plain':!![]});var _0xd01377='Intervals';return db[_0x4675('0x50')]['find']({'where':{'name':_0xd01377,'userProfileId':_0x50d924[_0x4675('0x51')]},'raw':!![]})[_0x4675('0x21')](function(_0x5acb51){if(_0x5acb51&&_0x5acb51[_0x4675('0x52')]===0x0){return db[_0x4675('0x25')][_0x4675('0x4d')]({'name':_0x52fa11[_0x4675('0x29')],'resourceId':_0x52fa11['id'],'type':_0x5acb51[_0x4675('0x29')],'sectionId':_0x5acb51['id']},{})[_0x4675('0x21')](function(){return _0x32ab9a;});}else{return _0x32ab9a;}})[_0x4675('0x4c')](function(_0x2b4453){logger[_0x4675('0x27')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x2b4453);throw _0x2b4453;});}return _0x32ab9a;})[_0x4675('0x21')](respondWithResult(_0x4fbacf,0xc9))['catch'](handleError(_0x4fbacf,null));};exports[_0x4675('0x20')]=function(_0x2a555b,_0x5c83e5){if(_0x2a555b[_0x4675('0x4e')]['id']){delete _0x2a555b[_0x4675('0x4e')]['id'];}return db['Interval'][_0x4675('0x4b')]({'where':{'id':_0x2a555b['params']['id']}})['then'](handleEntityNotFound(_0x5c83e5,null))[_0x4675('0x21')](saveUpdates(_0x2a555b[_0x4675('0x4e')],null))[_0x4675('0x21')](respondWithResult(_0x5c83e5,null))[_0x4675('0x4c')](handleError(_0x5c83e5,null));};exports['destroy']=function(_0xd740f4,_0x2fa6f5){return db[_0x4675('0x2d')]['find']({'where':{'id':_0xd740f4[_0x4675('0x48')]['id']}})['then'](handleEntityNotFound(_0x2fa6f5,null))[_0x4675('0x21')](removeEntity(_0x2fa6f5,null))[_0x4675('0x4c')](handleError(_0x2fa6f5,null));};exports[_0x4675('0x53')]=function(_0x2da0be,_0x30e2ba,_0x3da6a6){if(_0x2da0be['body']['id']){delete _0x2da0be[_0x4675('0x4e')]['id'];}return db[_0x4675('0x2d')][_0x4675('0x4b')]({'where':{'id':_0x2da0be[_0x4675('0x48')]['id']}})[_0x4675('0x21')](handleEntityNotFound(_0x30e2ba,null))[_0x4675('0x21')](function(_0x276621){if(_0x276621){_0x2da0be['body'][_0x4675('0x54')]=_0x276621['id'];return db[_0x4675('0x2d')][_0x4675('0x4d')](_0x2da0be[_0x4675('0x4e')]);}})[_0x4675('0x21')](respondWithResult(_0x30e2ba,null))[_0x4675('0x4c')](handleError(_0x30e2ba,null));};exports[_0x4675('0x55')]=function(_0x132f6c,_0x4116fe,_0x4e29ea){var _0xefcba6={};var _0x2a945f={};var _0x54ec55;var _0x5668d8;return db[_0x4675('0x2d')][_0x4675('0x56')]({'where':{'id':_0x132f6c[_0x4675('0x48')]['id']}})['then'](handleEntityNotFound(_0x4116fe,null))[_0x4675('0x21')](function(_0x13215f){if(_0x13215f){_0x54ec55=_0x13215f;_0x2a945f['model']=_['keys'](db[_0x4675('0x2d')][_0x4675('0x2e')]);_0x2a945f[_0x4675('0x32')]=_['keys'](_0x132f6c[_0x4675('0x32')]);_0x2a945f[_0x4675('0x33')]=_[_0x4675('0x34')](_0x2a945f[_0x4675('0x31')],_0x2a945f['query']);_0xefcba6['attributes']=_['intersection'](_0x2a945f[_0x4675('0x31')],qs[_0x4675('0x36')](_0x132f6c[_0x4675('0x32')][_0x4675('0x36')]));_0xefcba6['attributes']=_0xefcba6[_0x4675('0x35')][_0x4675('0x37')]?_0xefcba6[_0x4675('0x35')]:_0x2a945f['model'];_0xefcba6[_0x4675('0x3b')]=qs[_0x4675('0x3c')](_0x132f6c['query'][_0x4675('0x3c')]);_0xefcba6[_0x4675('0x3d')]=qs[_0x4675('0x33')](_[_0x4675('0x3e')](_0x132f6c['query'],_0x2a945f['filters']));if(_0x132f6c[_0x4675('0x32')][_0x4675('0x3f')]){_0xefcba6[_0x4675('0x3d')]=_[_0x4675('0x40')](_0xefcba6[_0x4675('0x3d')],{'$or':_[_0x4675('0x2c')](_0xefcba6['attributes'],function(_0x4a84e){var _0x305055={};_0x305055[_0x4a84e]={'$like':'%'+_0x132f6c['query'][_0x4675('0x3f')]+'%'};return _0x305055;})});}_0xefcba6=_['merge']({},_0xefcba6,_0x132f6c[_0x4675('0x4a')]);return _0x54ec55[_0x4675('0x55')](_0xefcba6);}})[_0x4675('0x21')](function(_0x15d676){if(_0x15d676){_0x5668d8=_0x15d676[_0x4675('0x37')];if(!_0x132f6c[_0x4675('0x32')][_0x4675('0x38')](_0x4675('0x39'))){_0xefcba6[_0x4675('0x3a')]=qs[_0x4675('0x3a')](_0x132f6c[_0x4675('0x32')][_0x4675('0x3a')]);_0xefcba6['offset']=qs[_0x4675('0x1b')](_0x132f6c['query']['offset']);}return _0x54ec55['getIntervals'](_0xefcba6);}})['then'](function(_0x578ea4){if(_0x578ea4){return _0x578ea4?{'count':_0x5668d8,'rows':_0x578ea4}:null;}})[_0x4675('0x21')](respondWithResult(_0x4116fe,null))[_0x4675('0x4c')](handleError(_0x4116fe,null));};exports[_0x4675('0x57')]=function(_0x32834d,_0x1ee895,_0x1206a4){return db[_0x4675('0x2d')][_0x4675('0x56')]({'where':{'id':_0x32834d['params']['id']}})[_0x4675('0x21')](handleEntityNotFound(_0x1ee895,null))['then'](function(_0x130cf3){if(_0x130cf3){return db[_0x4675('0x58')][_0x4675('0x59')](function(_0x49c9c1){return db[_0x4675('0x2d')][_0x4675('0x22')]({'where':{'IntervalId':_0x32834d[_0x4675('0x48')]['id']},'transaction':_0x49c9c1})[_0x4675('0x21')](function(_0x37c210){var _0xe5310f=_['map'](_0x32834d['body'],function(_0x589d5f){delete _0x589d5f['id'];_0x589d5f[_0x4675('0x54')]=_0x32834d['params']['id'];return _0x589d5f;});return db[_0x4675('0x2d')][_0x4675('0x5a')](_0xe5310f,{'transaction':_0x49c9c1});});})[_0x4675('0x21')](function(){return db[_0x4675('0x2d')][_0x4675('0x45')]({'where':{'IntervalId':_0x32834d[_0x4675('0x48')]['id']}});});}})[_0x4675('0x21')](respondWithResult(_0x1ee895,null))['catch'](handleError(_0x1ee895,null));};