cf744cd347134fc20b12bd7cb2234bf9cae2ca64
[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 _0xb575=['query','attributes','intersection','fields','hasOwnProperty','nolimit','order','where','pick','filter','merge','VIRTUAL','includeAll','include','findAll','rows','show','params','length','create','body','user','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','find','userProfileId','UserProfileResource','catch','update','addInterval','IntervalId','getIntervals','findOne','sort','options','addIntervals','sequelize','transaction','html-pdf','eml-format','zip-dir','bluebird','mustache','path','sox','to-csv','ejs','lodash','crypto','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','../../config/environment','../../config/license/util','../../mysqldb','../../components/extensions/rewrite','sendStatus','status','end','count','offset','limit','set','Content-Range','json','apply','reject','then','destroy','Intervals','error','stack','name','send','index','map','Interval','rawAttributes','type','key','model','keys','filters'];(function(_0x5e1520,_0x29f4c4){var _0x3885e1=function(_0x5d964f){while(--_0x5d964f){_0x5e1520['push'](_0x5e1520['shift']());}};_0x3885e1(++_0x29f4c4);}(_0xb575,0xda));var _0x5b57=function(_0x32c60e,_0x436224){_0x32c60e=_0x32c60e-0x0;var _0x50cb42=_0xb575[_0x32c60e];return _0x50cb42;};'use strict';var pdf=require(_0x5b57('0x0'));var emlformat=require(_0x5b57('0x1'));var rimraf=require('rimraf');var zipdir=require(_0x5b57('0x2'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0x5b57('0x3'));var Mustache=require(_0x5b57('0x4'));var util=require('util');var path=require(_0x5b57('0x5'));var sox=require(_0x5b57('0x6'));var csv=require(_0x5b57('0x7'));var ejs=require(_0x5b57('0x8'));var fs=require('fs');var _=require(_0x5b57('0x9'));var squel=require('squel');var crypto=require(_0x5b57('0xa'));var jsforce=require('jsforce');var deskjs=require(_0x5b57('0xb'));var toCsv=require(_0x5b57('0x7'));var querystring=require(_0x5b57('0xc'));var Papa=require('papaparse');var Redis=require(_0x5b57('0xd'));var authService=require(_0x5b57('0xe'));var qs=require(_0x5b57('0xf'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x5b57('0x10'));var logger=require(_0x5b57('0x11'))('api');var utils=require('../../config/utils');var config=require(_0x5b57('0x12'));var licenseUtil=require(_0x5b57('0x13'));var db=require(_0x5b57('0x14'))['db'];var extensions=require(_0x5b57('0x15'));function respondWithStatusCode(_0x355dab,_0x45cb5c){_0x45cb5c=_0x45cb5c||0xcc;return function(_0x410a67){if(_0x410a67){return _0x355dab[_0x5b57('0x16')](_0x45cb5c);}return _0x355dab[_0x5b57('0x17')](_0x45cb5c)[_0x5b57('0x18')]();};}function respondWithResult(_0x53df3e,_0x16ffe0){_0x16ffe0=_0x16ffe0||0xc8;return function(_0x3182cf){if(_0x3182cf){return _0x53df3e[_0x5b57('0x17')](_0x16ffe0)['json'](_0x3182cf);}};}function respondWithFilteredResult(_0x2a0a1b,_0x3b4631){return function(_0x132430){if(_0x132430){var _0x770fc3=_0x132430[_0x5b57('0x19')],_0x396e33=_0x3b4631['offset'],_0x1e7cc0=_0x3b4631[_0x5b57('0x1a')]+_0x3b4631[_0x5b57('0x1b')],_0x86f63e;if(_0x1e7cc0>=_0x770fc3){_0x1e7cc0=_0x770fc3;_0x86f63e=0xc8;}else{_0x86f63e=0xce;}_0x2a0a1b[_0x5b57('0x17')](_0x86f63e);return _0x2a0a1b[_0x5b57('0x1c')](_0x5b57('0x1d'),_0x396e33+'-'+_0x1e7cc0+'/'+_0x770fc3)[_0x5b57('0x1e')](_0x132430);}return null;};}function patchUpdates(_0x5f0dc6){return function(_0x2fc33d){try{jsonpatch[_0x5b57('0x1f')](_0x2fc33d,_0x5f0dc6,!![]);}catch(_0x41df30){return BPromise[_0x5b57('0x20')](_0x41df30);}return _0x2fc33d['save']();};}function saveUpdates(_0x3c7f4f,_0x2dfaae){return function(_0x3cb5c0){if(_0x3cb5c0){return _0x3cb5c0['update'](_0x3c7f4f)[_0x5b57('0x21')](function(_0xd8567a){return _0xd8567a;});}return null;};}function removeEntity(_0x3a4488,_0x28721f){return function(_0x429146){if(_0x429146){return _0x429146[_0x5b57('0x22')]()['then'](function(){var _0x139214=_0x429146['get']({'plain':!![]});var _0x11c149=_0x5b57('0x23');return db['UserProfileResource']['destroy']({'where':{'type':_0x11c149,'resourceId':_0x139214['id']}})[_0x5b57('0x21')](function(){return _0x429146;});})['then'](function(){_0x3a4488[_0x5b57('0x17')](0xcc)[_0x5b57('0x18')]();});}};}function handleEntityNotFound(_0x1ab440,_0x20a95a){return function(_0x39b2f4){if(!_0x39b2f4){_0x1ab440[_0x5b57('0x16')](0x194);}return _0x39b2f4;};}function handleError(_0x43a231,_0x10ee0f){_0x10ee0f=_0x10ee0f||0x1f4;return function(_0xf0375b){logger[_0x5b57('0x24')](_0xf0375b[_0x5b57('0x25')]);if(_0xf0375b[_0x5b57('0x26')]){delete _0xf0375b[_0x5b57('0x26')];}_0x43a231[_0x5b57('0x17')](_0x10ee0f)[_0x5b57('0x27')](_0xf0375b);};}exports[_0x5b57('0x28')]=function(_0x3e8bc6,_0x4da4a8){var _0x42b276={},_0x5e21a1={},_0x477c82={'count':0x0,'rows':[]};var _0x2743ce=_[_0x5b57('0x29')](db[_0x5b57('0x2a')][_0x5b57('0x2b')],function(_0x41434b){return{'name':_0x41434b['fieldName'],'type':_0x41434b[_0x5b57('0x2c')][_0x5b57('0x2d')]};});_0x5e21a1[_0x5b57('0x2e')]=_[_0x5b57('0x29')](_0x2743ce,_0x5b57('0x26'));_0x5e21a1['query']=_[_0x5b57('0x2f')](_0x3e8bc6['query']);_0x5e21a1[_0x5b57('0x30')]=_['intersection'](_0x5e21a1[_0x5b57('0x2e')],_0x5e21a1[_0x5b57('0x31')]);_0x42b276[_0x5b57('0x32')]=_[_0x5b57('0x33')](_0x5e21a1[_0x5b57('0x2e')],qs[_0x5b57('0x34')](_0x3e8bc6[_0x5b57('0x31')][_0x5b57('0x34')]));_0x42b276[_0x5b57('0x32')]=_0x42b276[_0x5b57('0x32')]['length']?_0x42b276[_0x5b57('0x32')]:_0x5e21a1[_0x5b57('0x2e')];if(!_0x3e8bc6[_0x5b57('0x31')][_0x5b57('0x35')](_0x5b57('0x36'))){_0x42b276[_0x5b57('0x1b')]=qs[_0x5b57('0x1b')](_0x3e8bc6[_0x5b57('0x31')][_0x5b57('0x1b')]);_0x42b276[_0x5b57('0x1a')]=qs[_0x5b57('0x1a')](_0x3e8bc6[_0x5b57('0x31')][_0x5b57('0x1a')]);}_0x42b276[_0x5b57('0x37')]=qs['sort'](_0x3e8bc6[_0x5b57('0x31')]['sort']);_0x42b276[_0x5b57('0x38')]=qs[_0x5b57('0x30')](_[_0x5b57('0x39')](_0x3e8bc6[_0x5b57('0x31')],_0x5e21a1[_0x5b57('0x30')]),_0x2743ce);if(_0x3e8bc6[_0x5b57('0x31')][_0x5b57('0x3a')]){_0x42b276['where']=_[_0x5b57('0x3b')](_0x42b276[_0x5b57('0x38')],{'$or':_[_0x5b57('0x29')](_0x2743ce,function(_0x569c87){if(_0x569c87[_0x5b57('0x2c')]!==_0x5b57('0x3c')){var _0x5eea89={};_0x5eea89[_0x569c87[_0x5b57('0x26')]]={'$like':'%'+_0x3e8bc6[_0x5b57('0x31')][_0x5b57('0x3a')]+'%'};return _0x5eea89;}})});}_0x42b276=_['merge']({},_0x42b276,_0x3e8bc6['options']);var _0x88eb0d={'where':_0x42b276[_0x5b57('0x38')]};return db['Interval'][_0x5b57('0x19')](_0x88eb0d)['then'](function(_0xd6bda6){_0x477c82[_0x5b57('0x19')]=_0xd6bda6;if(_0x3e8bc6['query'][_0x5b57('0x3d')]){_0x42b276[_0x5b57('0x3e')]=[{'all':!![]}];}return db[_0x5b57('0x2a')][_0x5b57('0x3f')](_0x42b276);})[_0x5b57('0x21')](function(_0x7e02f8){_0x477c82[_0x5b57('0x40')]=_0x7e02f8;return _0x477c82;})['then'](respondWithFilteredResult(_0x4da4a8,_0x42b276))['catch'](handleError(_0x4da4a8,null));};exports[_0x5b57('0x41')]=function(_0x48fb91,_0x19dc27){var _0x561502={'raw':!![],'where':{'id':_0x48fb91[_0x5b57('0x42')]['id']}},_0x240f7d={};_0x240f7d[_0x5b57('0x2e')]=_[_0x5b57('0x2f')](db[_0x5b57('0x2a')][_0x5b57('0x2b')]);_0x240f7d[_0x5b57('0x31')]=_[_0x5b57('0x2f')](_0x48fb91[_0x5b57('0x31')]);_0x240f7d[_0x5b57('0x30')]=_[_0x5b57('0x33')](_0x240f7d[_0x5b57('0x2e')],_0x240f7d[_0x5b57('0x31')]);_0x561502[_0x5b57('0x32')]=_[_0x5b57('0x33')](_0x240f7d[_0x5b57('0x2e')],qs[_0x5b57('0x34')](_0x48fb91[_0x5b57('0x31')][_0x5b57('0x34')]));_0x561502['attributes']=_0x561502[_0x5b57('0x32')][_0x5b57('0x43')]?_0x561502[_0x5b57('0x32')]:_0x240f7d['model'];if(_0x48fb91[_0x5b57('0x31')][_0x5b57('0x3d')]){_0x561502[_0x5b57('0x3e')]=[{'all':!![]}];}_0x561502=_[_0x5b57('0x3b')]({},_0x561502,_0x48fb91['options']);return db['Interval']['find'](_0x561502)[_0x5b57('0x21')](handleEntityNotFound(_0x19dc27,null))[_0x5b57('0x21')](respondWithResult(_0x19dc27,null))['catch'](handleError(_0x19dc27,null));};exports[_0x5b57('0x44')]=function(_0x36c76b,_0x4259e6){return db[_0x5b57('0x2a')][_0x5b57('0x44')](_0x36c76b[_0x5b57('0x45')],{})[_0x5b57('0x21')](function(_0x2027b4){var _0x4179c8=_0x36c76b[_0x5b57('0x46')][_0x5b57('0x47')]({'plain':!![]});if(!_0x4179c8)throw new Error(_0x5b57('0x48'));if(_0x4179c8[_0x5b57('0x49')]===_0x5b57('0x46')){var _0xd1ad9c=_0x2027b4[_0x5b57('0x47')]({'plain':!![]});var _0x22b024=_0x5b57('0x23');return db[_0x5b57('0x4a')][_0x5b57('0x4b')]({'where':{'name':_0x22b024,'userProfileId':_0x4179c8[_0x5b57('0x4c')]},'raw':!![]})[_0x5b57('0x21')](function(_0x4cdab5){if(_0x4cdab5&&_0x4cdab5['autoAssociation']===0x0){return db[_0x5b57('0x4d')]['create']({'name':_0xd1ad9c[_0x5b57('0x26')],'resourceId':_0xd1ad9c['id'],'type':_0x4cdab5['name'],'sectionId':_0x4cdab5['id']},{})[_0x5b57('0x21')](function(){return _0x2027b4;});}else{return _0x2027b4;}})[_0x5b57('0x4e')](function(_0x3087b7){logger['error']('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x3087b7);throw _0x3087b7;});}return _0x2027b4;})['then'](respondWithResult(_0x4259e6,0xc9))['catch'](handleError(_0x4259e6,null));};exports[_0x5b57('0x4f')]=function(_0x5b40df,_0x4f8d97){if(_0x5b40df['body']['id']){delete _0x5b40df[_0x5b57('0x45')]['id'];}return db[_0x5b57('0x2a')][_0x5b57('0x4b')]({'where':{'id':_0x5b40df[_0x5b57('0x42')]['id']}})[_0x5b57('0x21')](handleEntityNotFound(_0x4f8d97,null))[_0x5b57('0x21')](saveUpdates(_0x5b40df['body'],null))[_0x5b57('0x21')](respondWithResult(_0x4f8d97,null))[_0x5b57('0x4e')](handleError(_0x4f8d97,null));};exports['destroy']=function(_0x118a1e,_0x23841a){return db[_0x5b57('0x2a')][_0x5b57('0x4b')]({'where':{'id':_0x118a1e['params']['id']}})['then'](handleEntityNotFound(_0x23841a,null))[_0x5b57('0x21')](removeEntity(_0x23841a,null))[_0x5b57('0x4e')](handleError(_0x23841a,null));};exports[_0x5b57('0x50')]=function(_0x126cca,_0x40e2c1,_0x4a102a){if(_0x126cca['body']['id']){delete _0x126cca[_0x5b57('0x45')]['id'];}return db[_0x5b57('0x2a')][_0x5b57('0x4b')]({'where':{'id':_0x126cca['params']['id']}})['then'](handleEntityNotFound(_0x40e2c1,null))['then'](function(_0x4a3d25){if(_0x4a3d25){_0x126cca[_0x5b57('0x45')][_0x5b57('0x51')]=_0x4a3d25['id'];return db[_0x5b57('0x2a')][_0x5b57('0x44')](_0x126cca[_0x5b57('0x45')]);}})[_0x5b57('0x21')](respondWithResult(_0x40e2c1,null))[_0x5b57('0x4e')](handleError(_0x40e2c1,null));};exports[_0x5b57('0x52')]=function(_0x53b907,_0x2ff0ab,_0x32c06f){var _0x268166={};var _0x1e5b89={};var _0x5d2033;var _0x9b331;return db['Interval'][_0x5b57('0x53')]({'where':{'id':_0x53b907[_0x5b57('0x42')]['id']}})['then'](handleEntityNotFound(_0x2ff0ab,null))[_0x5b57('0x21')](function(_0x56f910){if(_0x56f910){_0x5d2033=_0x56f910;_0x1e5b89['model']=_['keys'](db[_0x5b57('0x2a')]['rawAttributes']);_0x1e5b89[_0x5b57('0x31')]=_[_0x5b57('0x2f')](_0x53b907[_0x5b57('0x31')]);_0x1e5b89[_0x5b57('0x30')]=_[_0x5b57('0x33')](_0x1e5b89[_0x5b57('0x2e')],_0x1e5b89[_0x5b57('0x31')]);_0x268166[_0x5b57('0x32')]=_['intersection'](_0x1e5b89[_0x5b57('0x2e')],qs[_0x5b57('0x34')](_0x53b907[_0x5b57('0x31')][_0x5b57('0x34')]));_0x268166['attributes']=_0x268166[_0x5b57('0x32')][_0x5b57('0x43')]?_0x268166['attributes']:_0x1e5b89['model'];_0x268166[_0x5b57('0x37')]=qs[_0x5b57('0x54')](_0x53b907[_0x5b57('0x31')][_0x5b57('0x54')]);_0x268166[_0x5b57('0x38')]=qs[_0x5b57('0x30')](_['pick'](_0x53b907[_0x5b57('0x31')],_0x1e5b89[_0x5b57('0x30')]));if(_0x53b907[_0x5b57('0x31')][_0x5b57('0x3a')]){_0x268166['where']=_[_0x5b57('0x3b')](_0x268166[_0x5b57('0x38')],{'$or':_['map'](_0x268166[_0x5b57('0x32')],function(_0x43348f){var _0x3bdb34={};_0x3bdb34[_0x43348f]={'$like':'%'+_0x53b907['query'][_0x5b57('0x3a')]+'%'};return _0x3bdb34;})});}_0x268166=_[_0x5b57('0x3b')]({},_0x268166,_0x53b907[_0x5b57('0x55')]);return _0x5d2033[_0x5b57('0x52')](_0x268166);}})['then'](function(_0x3d160){if(_0x3d160){_0x9b331=_0x3d160['length'];if(!_0x53b907[_0x5b57('0x31')]['hasOwnProperty']('nolimit')){_0x268166[_0x5b57('0x1b')]=qs[_0x5b57('0x1b')](_0x53b907[_0x5b57('0x31')]['limit']);_0x268166[_0x5b57('0x1a')]=qs[_0x5b57('0x1a')](_0x53b907[_0x5b57('0x31')][_0x5b57('0x1a')]);}return _0x5d2033['getIntervals'](_0x268166);}})[_0x5b57('0x21')](function(_0x53bc5e){if(_0x53bc5e){return _0x53bc5e?{'count':_0x9b331,'rows':_0x53bc5e}:null;}})['then'](respondWithResult(_0x2ff0ab,null))[_0x5b57('0x4e')](handleError(_0x2ff0ab,null));};exports[_0x5b57('0x56')]=function(_0x59d443,_0x2f89ee,_0x57053d){return db['Interval'][_0x5b57('0x53')]({'where':{'id':_0x59d443['params']['id']}})['then'](handleEntityNotFound(_0x2f89ee,null))['then'](function(_0x2d7434){if(_0x2d7434){return db[_0x5b57('0x57')][_0x5b57('0x58')](function(_0x2835a4){return db[_0x5b57('0x2a')][_0x5b57('0x22')]({'where':{'IntervalId':_0x59d443[_0x5b57('0x42')]['id']},'transaction':_0x2835a4})[_0x5b57('0x21')](function(_0x12faca){var _0x52b376=_[_0x5b57('0x29')](_0x59d443[_0x5b57('0x45')],function(_0x437eff){delete _0x437eff['id'];_0x437eff[_0x5b57('0x51')]=_0x59d443[_0x5b57('0x42')]['id'];return _0x437eff;});return db[_0x5b57('0x2a')]['bulkCreate'](_0x52b376,{'transaction':_0x2835a4});});})[_0x5b57('0x21')](function(){return db[_0x5b57('0x2a')][_0x5b57('0x3f')]({'where':{'IntervalId':_0x59d443[_0x5b57('0x42')]['id']}});});}})[_0x5b57('0x21')](respondWithResult(_0x2f89ee,null))[_0x5b57('0x4e')](handleError(_0x2f89ee,null));};