Built motion from commit (unavailable).|2.4.13
[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 _0x0b3b=['ioredis','../../components/auth/service','../../config/license/hardware','../../config/logger','api','../../config/environment','sendStatus','status','count','offset','limit','set','Content-Range','json','reject','save','update','destroy','then','Intervals','UserProfileResource','end','error','stack','name','index','Interval','rawAttributes','model','keys','query','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','sort','where','type','VIRTUAL','field','filter','merge','options','includeAll','include','rows','catch','show','params','find','create','body','user','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','userProfileId','autoAssociation','IntervalId','getIntervals','findOne','order','pick','map','sequelize','findAll','html-pdf','rimraf','util','path','sox','ejs','lodash','squel','crypto','desk.js','to-csv','querystring','papaparse'];(function(_0x81334d,_0x50c0e9){var _0x18923a=function(_0x360e24){while(--_0x360e24){_0x81334d['push'](_0x81334d['shift']());}};_0x18923a(++_0x50c0e9);}(_0x0b3b,0x99));var _0xb0b3=function(_0x409fa8,_0x3bd8d2){_0x409fa8=_0x409fa8-0x0;var _0x2642a4=_0x0b3b[_0x409fa8];return _0x2642a4;};'use strict';var pdf=require(_0xb0b3('0x0'));var emlformat=require('eml-format');var rimraf=require(_0xb0b3('0x1'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require('moment');var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0xb0b3('0x2'));var path=require(_0xb0b3('0x3'));var sox=require(_0xb0b3('0x4'));var csv=require('to-csv');var ejs=require(_0xb0b3('0x5'));var fs=require('fs');var _=require(_0xb0b3('0x6'));var squel=require(_0xb0b3('0x7'));var crypto=require(_0xb0b3('0x8'));var jsforce=require('jsforce');var deskjs=require(_0xb0b3('0x9'));var toCsv=require(_0xb0b3('0xa'));var querystring=require(_0xb0b3('0xb'));var Papa=require(_0xb0b3('0xc'));var Redis=require(_0xb0b3('0xd'));var authService=require(_0xb0b3('0xe'));var qs=require('../../components/parsers/qs');var hardwareService=require(_0xb0b3('0xf'));var logger=require(_0xb0b3('0x10'))(_0xb0b3('0x11'));var utils=require('../../config/utils');var config=require(_0xb0b3('0x12'));var db=require('../../mysqldb')['db'];var extensions=require('../../components/extensions/rewrite');function respondWithStatusCode(_0x38e95f,_0x23b5da){_0x23b5da=_0x23b5da||0xcc;return function(_0x233288){if(_0x233288){return _0x38e95f[_0xb0b3('0x13')](_0x23b5da);}return _0x38e95f['status'](_0x23b5da)['end']();};}function respondWithResult(_0x2d258b,_0x31d6b7){_0x31d6b7=_0x31d6b7||0xc8;return function(_0x4996aa){if(_0x4996aa){return _0x2d258b[_0xb0b3('0x14')](_0x31d6b7)['json'](_0x4996aa);}};}function respondWithFilteredResult(_0x20d7cc,_0x5c013c){return function(_0x37ac0d){if(_0x37ac0d){var _0x58f6da=_0x37ac0d[_0xb0b3('0x15')],_0x384578=_0x5c013c[_0xb0b3('0x16')],_0x551b3c=_0x5c013c['offset']+_0x5c013c[_0xb0b3('0x17')],_0xd97526;if(_0x551b3c>=_0x58f6da){_0x551b3c=_0x58f6da;_0xd97526=0xc8;}else{_0xd97526=0xce;}_0x20d7cc[_0xb0b3('0x14')](_0xd97526);return _0x20d7cc[_0xb0b3('0x18')](_0xb0b3('0x19'),_0x384578+'-'+_0x551b3c+'/'+_0x58f6da)[_0xb0b3('0x1a')](_0x37ac0d);}return null;};}function patchUpdates(_0x18f20e){return function(_0x233944){try{jsonpatch['apply'](_0x233944,_0x18f20e,!![]);}catch(_0x4da0d6){return BPromise[_0xb0b3('0x1b')](_0x4da0d6);}return _0x233944[_0xb0b3('0x1c')]();};}function saveUpdates(_0x28e966,_0x1bca04){return function(_0x1993d8){if(_0x1993d8){return _0x1993d8[_0xb0b3('0x1d')](_0x28e966)['then'](function(_0x4c70ce){return _0x4c70ce;});}return null;};}function removeEntity(_0x46ce29,_0x5eb8a7){return function(_0x49185f){if(_0x49185f){return _0x49185f[_0xb0b3('0x1e')]()[_0xb0b3('0x1f')](function(){var _0x3a3985=_0x49185f['get']({'plain':!![]});var _0x36f5af=_0xb0b3('0x20');return db[_0xb0b3('0x21')]['destroy']({'where':{'type':_0x36f5af,'resourceId':_0x3a3985['id']}})[_0xb0b3('0x1f')](function(){return _0x49185f;});})[_0xb0b3('0x1f')](function(){_0x46ce29['status'](0xcc)[_0xb0b3('0x22')]();});}};}function handleEntityNotFound(_0x20d2b8,_0x51dca2){return function(_0x2734c9){if(!_0x2734c9){_0x20d2b8[_0xb0b3('0x13')](0x194);}return _0x2734c9;};}function handleError(_0x2e2f5c,_0x63a25b){_0x63a25b=_0x63a25b||0x1f4;return function(_0x1260a6){logger[_0xb0b3('0x23')](_0x1260a6[_0xb0b3('0x24')]);if(_0x1260a6[_0xb0b3('0x25')]){delete _0x1260a6[_0xb0b3('0x25')];}_0x2e2f5c[_0xb0b3('0x14')](_0x63a25b)['send'](_0x1260a6);};}exports[_0xb0b3('0x26')]=function(_0x31a464,_0x4a85ac){var _0x12b1f4={},_0xad6b04={},_0x14196a={'count':0x0,'rows':[]};var _0x51773a=db[_0xb0b3('0x27')][_0xb0b3('0x28')];_0xad6b04[_0xb0b3('0x29')]=_[_0xb0b3('0x2a')](_0x51773a);_0xad6b04[_0xb0b3('0x2b')]=_[_0xb0b3('0x2a')](_0x31a464['query']);_0xad6b04[_0xb0b3('0x2c')]=_[_0xb0b3('0x2d')](_0xad6b04[_0xb0b3('0x29')],_0xad6b04['query']);_0x12b1f4['attributes']=_['intersection'](_0xad6b04[_0xb0b3('0x29')],qs[_0xb0b3('0x2e')](_0x31a464[_0xb0b3('0x2b')]['fields']));_0x12b1f4[_0xb0b3('0x2f')]=_0x12b1f4[_0xb0b3('0x2f')][_0xb0b3('0x30')]?_0x12b1f4['attributes']:_0xad6b04['model'];if(!_0x31a464[_0xb0b3('0x2b')][_0xb0b3('0x31')](_0xb0b3('0x32'))){_0x12b1f4[_0xb0b3('0x17')]=qs[_0xb0b3('0x17')](_0x31a464['query'][_0xb0b3('0x17')]);_0x12b1f4[_0xb0b3('0x16')]=qs[_0xb0b3('0x16')](_0x31a464[_0xb0b3('0x2b')]['offset']);}_0x12b1f4['order']=qs[_0xb0b3('0x33')](_0x31a464[_0xb0b3('0x2b')][_0xb0b3('0x33')]);_0x12b1f4[_0xb0b3('0x34')]=qs[_0xb0b3('0x2c')](_['pick'](_0x31a464[_0xb0b3('0x2b')],_0xad6b04['filters']));if(_0x31a464[_0xb0b3('0x2b')]['filter']){_0x12b1f4[_0xb0b3('0x34')]=_['merge'](_0x12b1f4['where'],{'$or':_['map'](_0x51773a,function(_0x462b73){if(_0x462b73[_0xb0b3('0x35')]['key']!==_0xb0b3('0x36')){var _0x3fbfee={};_0x3fbfee[_0x462b73[_0xb0b3('0x37')]]={'$like':'%'+_0x31a464[_0xb0b3('0x2b')][_0xb0b3('0x38')]+'%'};return _0x3fbfee;}})});}_0x12b1f4=_[_0xb0b3('0x39')]({},_0x12b1f4,_0x31a464[_0xb0b3('0x3a')]);var _0x20f4ae={'where':_0x12b1f4[_0xb0b3('0x34')]};return db['Interval'][_0xb0b3('0x15')](_0x20f4ae)['then'](function(_0x2d3d51){_0x14196a['count']=_0x2d3d51;if(_0x31a464[_0xb0b3('0x2b')][_0xb0b3('0x3b')]){_0x12b1f4[_0xb0b3('0x3c')]=[{'all':!![]}];}return db[_0xb0b3('0x27')]['findAll'](_0x12b1f4);})[_0xb0b3('0x1f')](function(_0xc339f8){_0x14196a[_0xb0b3('0x3d')]=_0xc339f8;return _0x14196a;})[_0xb0b3('0x1f')](respondWithFilteredResult(_0x4a85ac,_0x12b1f4))[_0xb0b3('0x3e')](handleError(_0x4a85ac,null));};exports[_0xb0b3('0x3f')]=function(_0xbd0830,_0x1565a1){var _0x4160bf={'raw':!![],'where':{'id':_0xbd0830[_0xb0b3('0x40')]['id']}},_0x42b48c={};_0x42b48c[_0xb0b3('0x29')]=_[_0xb0b3('0x2a')](db[_0xb0b3('0x27')][_0xb0b3('0x28')]);_0x42b48c[_0xb0b3('0x2b')]=_[_0xb0b3('0x2a')](_0xbd0830[_0xb0b3('0x2b')]);_0x42b48c[_0xb0b3('0x2c')]=_[_0xb0b3('0x2d')](_0x42b48c['model'],_0x42b48c[_0xb0b3('0x2b')]);_0x4160bf[_0xb0b3('0x2f')]=_['intersection'](_0x42b48c[_0xb0b3('0x29')],qs[_0xb0b3('0x2e')](_0xbd0830[_0xb0b3('0x2b')][_0xb0b3('0x2e')]));_0x4160bf[_0xb0b3('0x2f')]=_0x4160bf['attributes'][_0xb0b3('0x30')]?_0x4160bf[_0xb0b3('0x2f')]:_0x42b48c['model'];if(_0xbd0830[_0xb0b3('0x2b')]['includeAll']){_0x4160bf[_0xb0b3('0x3c')]=[{'all':!![]}];}_0x4160bf=_[_0xb0b3('0x39')]({},_0x4160bf,_0xbd0830['options']);return db[_0xb0b3('0x27')][_0xb0b3('0x41')](_0x4160bf)[_0xb0b3('0x1f')](handleEntityNotFound(_0x1565a1,null))['then'](respondWithResult(_0x1565a1,null))[_0xb0b3('0x3e')](handleError(_0x1565a1,null));};exports[_0xb0b3('0x42')]=function(_0xb673fe,_0x1056a4){return db['Interval'][_0xb0b3('0x42')](_0xb673fe[_0xb0b3('0x43')],{})[_0xb0b3('0x1f')](function(_0x32db51){var _0x5db763=_0xb673fe[_0xb0b3('0x44')][_0xb0b3('0x45')]({'plain':!![]});if(!_0x5db763)throw new Error(_0xb0b3('0x46'));if(_0x5db763[_0xb0b3('0x47')]===_0xb0b3('0x44')){var _0x6b2b1d=_0x32db51['get']({'plain':!![]});var _0x4e2239=_0xb0b3('0x20');return db[_0xb0b3('0x48')][_0xb0b3('0x41')]({'where':{'name':_0x4e2239,'userProfileId':_0x5db763[_0xb0b3('0x49')]},'raw':!![]})['then'](function(_0x24d746){if(_0x24d746&&_0x24d746[_0xb0b3('0x4a')]===0x0){return db[_0xb0b3('0x21')][_0xb0b3('0x42')]({'name':_0x6b2b1d[_0xb0b3('0x25')],'resourceId':_0x6b2b1d['id'],'type':_0x24d746[_0xb0b3('0x25')],'sectionId':_0x24d746['id']},{})[_0xb0b3('0x1f')](function(){return _0x32db51;});}else{return _0x32db51;}})[_0xb0b3('0x3e')](function(_0x1e567c){logger['error']('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x1e567c);throw _0x1e567c;});}return _0x32db51;})[_0xb0b3('0x1f')](respondWithResult(_0x1056a4,0xc9))['catch'](handleError(_0x1056a4,null));};exports[_0xb0b3('0x1d')]=function(_0xb422ea,_0x1f7f13){if(_0xb422ea[_0xb0b3('0x43')]['id']){delete _0xb422ea[_0xb0b3('0x43')]['id'];}return db['Interval'][_0xb0b3('0x41')]({'where':{'id':_0xb422ea[_0xb0b3('0x40')]['id']}})[_0xb0b3('0x1f')](handleEntityNotFound(_0x1f7f13,null))[_0xb0b3('0x1f')](saveUpdates(_0xb422ea['body'],null))[_0xb0b3('0x1f')](respondWithResult(_0x1f7f13,null))[_0xb0b3('0x3e')](handleError(_0x1f7f13,null));};exports[_0xb0b3('0x1e')]=function(_0x5ae5d0,_0x4ee7fe){return db[_0xb0b3('0x27')][_0xb0b3('0x41')]({'where':{'id':_0x5ae5d0[_0xb0b3('0x40')]['id']}})['then'](handleEntityNotFound(_0x4ee7fe,null))['then'](removeEntity(_0x4ee7fe,null))['catch'](handleError(_0x4ee7fe,null));};exports['addInterval']=function(_0x135e02,_0x49c14c,_0x4fc88f){if(_0x135e02['body']['id']){delete _0x135e02['body']['id'];}return db[_0xb0b3('0x27')][_0xb0b3('0x41')]({'where':{'id':_0x135e02[_0xb0b3('0x40')]['id']}})[_0xb0b3('0x1f')](handleEntityNotFound(_0x49c14c,null))[_0xb0b3('0x1f')](function(_0xae4b7b){if(_0xae4b7b){_0x135e02[_0xb0b3('0x43')][_0xb0b3('0x4b')]=_0xae4b7b['id'];return db[_0xb0b3('0x27')][_0xb0b3('0x42')](_0x135e02[_0xb0b3('0x43')]);}})[_0xb0b3('0x1f')](respondWithResult(_0x49c14c,null))[_0xb0b3('0x3e')](handleError(_0x49c14c,null));};exports[_0xb0b3('0x4c')]=function(_0x36e708,_0x248187,_0x258dc6){var _0x5165e4={};var _0x524206={};var _0x5f2582;var _0x1627a2;return db[_0xb0b3('0x27')][_0xb0b3('0x4d')]({'where':{'id':_0x36e708[_0xb0b3('0x40')]['id']}})[_0xb0b3('0x1f')](handleEntityNotFound(_0x248187,null))[_0xb0b3('0x1f')](function(_0x236153){if(_0x236153){_0x5f2582=_0x236153;_0x524206['model']=_['keys'](db[_0xb0b3('0x27')][_0xb0b3('0x28')]);_0x524206[_0xb0b3('0x2b')]=_[_0xb0b3('0x2a')](_0x36e708[_0xb0b3('0x2b')]);_0x524206[_0xb0b3('0x2c')]=_[_0xb0b3('0x2d')](_0x524206[_0xb0b3('0x29')],_0x524206[_0xb0b3('0x2b')]);_0x5165e4[_0xb0b3('0x2f')]=_[_0xb0b3('0x2d')](_0x524206[_0xb0b3('0x29')],qs[_0xb0b3('0x2e')](_0x36e708[_0xb0b3('0x2b')][_0xb0b3('0x2e')]));_0x5165e4[_0xb0b3('0x2f')]=_0x5165e4['attributes'][_0xb0b3('0x30')]?_0x5165e4[_0xb0b3('0x2f')]:_0x524206['model'];_0x5165e4[_0xb0b3('0x4e')]=qs['sort'](_0x36e708['query']['sort']);_0x5165e4[_0xb0b3('0x34')]=qs[_0xb0b3('0x2c')](_[_0xb0b3('0x4f')](_0x36e708['query'],_0x524206[_0xb0b3('0x2c')]));if(_0x36e708[_0xb0b3('0x2b')][_0xb0b3('0x38')]){_0x5165e4['where']=_['merge'](_0x5165e4['where'],{'$or':_[_0xb0b3('0x50')](_0x5165e4[_0xb0b3('0x2f')],function(_0x3d1f85){var _0x47f04f={};_0x47f04f[_0x3d1f85]={'$like':'%'+_0x36e708['query'][_0xb0b3('0x38')]+'%'};return _0x47f04f;})});}_0x5165e4=_[_0xb0b3('0x39')]({},_0x5165e4,_0x36e708[_0xb0b3('0x3a')]);return _0x5f2582[_0xb0b3('0x4c')](_0x5165e4);}})[_0xb0b3('0x1f')](function(_0x4f62e5){if(_0x4f62e5){_0x1627a2=_0x4f62e5['length'];if(!_0x36e708[_0xb0b3('0x2b')]['hasOwnProperty'](_0xb0b3('0x32'))){_0x5165e4[_0xb0b3('0x17')]=qs[_0xb0b3('0x17')](_0x36e708[_0xb0b3('0x2b')][_0xb0b3('0x17')]);_0x5165e4['offset']=qs[_0xb0b3('0x16')](_0x36e708[_0xb0b3('0x2b')]['offset']);}return _0x5f2582[_0xb0b3('0x4c')](_0x5165e4);}})[_0xb0b3('0x1f')](function(_0x4f7bf9){if(_0x4f7bf9){return _0x4f7bf9?{'count':_0x1627a2,'rows':_0x4f7bf9}:null;}})[_0xb0b3('0x1f')](respondWithResult(_0x248187,null))['catch'](handleError(_0x248187,null));};exports['addIntervals']=function(_0x2a986e,_0x1d17cc,_0x1c44cd){return db[_0xb0b3('0x27')][_0xb0b3('0x4d')]({'where':{'id':_0x2a986e[_0xb0b3('0x40')]['id']}})[_0xb0b3('0x1f')](handleEntityNotFound(_0x1d17cc,null))['then'](function(_0x39d8c8){if(_0x39d8c8){return db[_0xb0b3('0x51')]['transaction'](function(_0x164bbc){return db[_0xb0b3('0x27')][_0xb0b3('0x1e')]({'where':{'IntervalId':_0x2a986e[_0xb0b3('0x40')]['id']},'transaction':_0x164bbc})[_0xb0b3('0x1f')](function(_0x3ad4ef){var _0x25af9f=_['map'](_0x2a986e['body'],function(_0x10889c){delete _0x10889c['id'];_0x10889c['IntervalId']=_0x2a986e[_0xb0b3('0x40')]['id'];return _0x10889c;});return db[_0xb0b3('0x27')]['bulkCreate'](_0x25af9f,{'transaction':_0x164bbc});});})[_0xb0b3('0x1f')](function(){return db['Interval'][_0xb0b3('0x52')]({'where':{'IntervalId':_0x2a986e['params']['id']}});});}})[_0xb0b3('0x1f')](respondWithResult(_0x1d17cc,null))['catch'](handleError(_0x1d17cc,null));};