Built motion from commit 67e5df37.|2.0.66
[motion2.git] / server / api / team / team.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 _0xb58b=['nolimit','limit','order','sort','where','day','add','merge','filter','map','options','includeAll','include','findAll','catch','show','params','find','create','body','findOne','isValidChannel','channel','Sequelize','ValidationError','capitalize','toLowerCase','Queue','pick','get','Queues','ids','removeQueues','Unknown\x20channel','remove','addAgents','omit','userTeam:save','removeAgents','emit','userTeam:remove','getAgents','request-promise','moment','bluebird','util','path','to-csv','lodash','squel','crypto','desk.js','querystring','papaparse','../../components/parsers/qs','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','socket.io-emitter','./team.socket','sendStatus','end','status','json','count','offset','apply','reject','save','update','then','destroy','error','stack','name','send','model','keys','Team','rawAttributes','query','dateStart','dateEnd','filters','intersection','fields','attributes','length','hasOwnProperty'];(function(_0x1e6dd7,_0x275cb){var _0x405461=function(_0x55f643){while(--_0x55f643){_0x1e6dd7['push'](_0x1e6dd7['shift']());}};_0x405461(++_0x275cb);}(_0xb58b,0x1f1));var _0xbb58=function(_0x3662d0,_0x143082){_0x3662d0=_0x3662d0-0x0;var _0x3a2227=_0xb58b[_0x3662d0];return _0x3a2227;};'use strict';var jsonpatch=require('fast-json-patch');var rp=require(_0xbb58('0x0'));var moment=require(_0xbb58('0x1'));var BPromise=require(_0xbb58('0x2'));var Mustache=require('mustache');var util=require(_0xbb58('0x3'));var path=require(_0xbb58('0x4'));var sox=require('sox');var csv=require(_0xbb58('0x5'));var ejs=require('ejs');var fs=require('fs');var _=require(_0xbb58('0x6'));var squel=require(_0xbb58('0x7'));var crypto=require(_0xbb58('0x8'));var jsforce=require('jsforce');var deskjs=require(_0xbb58('0x9'));var toCsv=require(_0xbb58('0x5'));var querystring=require(_0xbb58('0xa'));var Papa=require(_0xbb58('0xb'));var qs=require(_0xbb58('0xc'));var logger=require(_0xbb58('0xd'))(_0xbb58('0xe'));var utils=require(_0xbb58('0xf'));var config=require(_0xbb58('0x10'));var db=require(_0xbb58('0x11'))['db'];var socket=require(_0xbb58('0x12'))({'host':'localhost','port':0x18eb});require(_0xbb58('0x13'))['register'](socket);function respondWithStatusCode(_0x8fe999,_0x440392){_0x440392=_0x440392||0xcc;return function(_0x26d8e6){if(_0x26d8e6){return _0x8fe999[_0xbb58('0x14')](_0x440392);}return _0x8fe999['status'](_0x440392)[_0xbb58('0x15')]();};}function respondWithResult(_0x18810f,_0x4cda21){_0x4cda21=_0x4cda21||0xc8;return function(_0x132511){if(_0x132511){return _0x18810f[_0xbb58('0x16')](_0x4cda21)[_0xbb58('0x17')](_0x132511);}};}function respondWithFilteredResult(_0x3b9406,_0x19ceda){return function(_0x58245f){if(_0x58245f){var _0x110944=_0x58245f[_0xbb58('0x18')],_0x23fd15=_0x19ceda[_0xbb58('0x19')],_0x5f4751=_0x19ceda[_0xbb58('0x19')]+_0x19ceda['limit'],_0x4ba30e;if(_0x5f4751>=_0x110944){_0x5f4751=_0x110944;_0x4ba30e=0xc8;}else{_0x4ba30e=0xce;}_0x3b9406['status'](_0x4ba30e);return _0x3b9406['set']('Content-Range',_0x23fd15+'-'+_0x5f4751+'/'+_0x110944)[_0xbb58('0x17')](_0x58245f);}return null;};}function patchUpdates(_0x3134ea){return function(_0x48f3e0){try{jsonpatch[_0xbb58('0x1a')](_0x48f3e0,_0x3134ea,!![]);}catch(_0x497653){return BPromise[_0xbb58('0x1b')](_0x497653);}return _0x48f3e0[_0xbb58('0x1c')]();};}function saveUpdates(_0x3c33d9,_0xb8f982){return function(_0x3d961a){if(_0x3d961a){return _0x3d961a[_0xbb58('0x1d')](_0x3c33d9)[_0xbb58('0x1e')](function(_0x497fd0){return _0x497fd0;});}return null;};}function removeEntity(_0x84f34,_0x2d8c0f){return function(_0x3e64fe){if(_0x3e64fe){return _0x3e64fe[_0xbb58('0x1f')]()[_0xbb58('0x1e')](function(){_0x84f34[_0xbb58('0x16')](0xcc)[_0xbb58('0x15')]();});}};}function handleEntityNotFound(_0x350be7,_0x27bbec){return function(_0x4f74a0){if(!_0x4f74a0){_0x350be7[_0xbb58('0x14')](0x194);}return _0x4f74a0;};}function handleError(_0x53b693,_0x21964d){_0x21964d=_0x21964d||0x1f4;return function(_0x287822){logger[_0xbb58('0x20')](_0x287822[_0xbb58('0x21')]);if(_0x287822[_0xbb58('0x22')]){delete _0x287822[_0xbb58('0x22')];}_0x53b693[_0xbb58('0x16')](_0x21964d)[_0xbb58('0x23')](_0x287822);};}exports['index']=function(_0x283dff,_0x3bd550){var _0x1be0ae={},_0x129094={},_0x8e9ed9={'count':0x0,'rows':[]};_0x129094[_0xbb58('0x24')]=_[_0xbb58('0x25')](db[_0xbb58('0x26')][_0xbb58('0x27')]);var _0x2d5b39={'dateStart':_0x283dff[_0xbb58('0x28')][_0xbb58('0x29')],'dateEnd':_0x283dff[_0xbb58('0x28')][_0xbb58('0x2a')]};delete _0x283dff[_0xbb58('0x28')]['dateStart'];delete _0x283dff[_0xbb58('0x28')][_0xbb58('0x2a')];_0x129094[_0xbb58('0x28')]=_[_0xbb58('0x25')](_0x283dff[_0xbb58('0x28')]);_0x129094[_0xbb58('0x2b')]=_[_0xbb58('0x2c')](_0x129094[_0xbb58('0x24')],_0x129094[_0xbb58('0x28')]);_0x1be0ae['attributes']=_[_0xbb58('0x2c')](_0x129094[_0xbb58('0x24')],qs[_0xbb58('0x2d')](_0x283dff[_0xbb58('0x28')][_0xbb58('0x2d')]));_0x1be0ae[_0xbb58('0x2e')]=_0x1be0ae[_0xbb58('0x2e')][_0xbb58('0x2f')]?_0x1be0ae[_0xbb58('0x2e')]:_0x129094[_0xbb58('0x24')];if(!_0x283dff[_0xbb58('0x28')][_0xbb58('0x30')](_0xbb58('0x31'))){_0x1be0ae['limit']=qs['limit'](_0x283dff['query'][_0xbb58('0x32')]);_0x1be0ae[_0xbb58('0x19')]=qs[_0xbb58('0x19')](_0x283dff[_0xbb58('0x28')][_0xbb58('0x19')]);}_0x1be0ae[_0xbb58('0x33')]=qs[_0xbb58('0x34')](_0x283dff[_0xbb58('0x28')]['sort']);_0x1be0ae[_0xbb58('0x35')]=qs[_0xbb58('0x2b')](_['pick'](_0x283dff[_0xbb58('0x28')],_0x129094['filters']));if(_0x2d5b39[_0xbb58('0x29')]){if(_0x2d5b39['dateEnd']){_0x2d5b39[_0xbb58('0x2a')]=moment(_0x2d5b39[_0xbb58('0x2a')])['add'](0x1,_0xbb58('0x36'));}else{_0x2d5b39[_0xbb58('0x2a')]=moment(_0x2d5b39[_0xbb58('0x29')])[_0xbb58('0x37')](0x1,_0xbb58('0x36'));}_[_0xbb58('0x38')](_0x1be0ae[_0xbb58('0x35')],{'createdAt':{'$gte':_0x2d5b39[_0xbb58('0x29')],'$lte':_0x2d5b39['dateEnd']}});}if(_0x283dff[_0xbb58('0x28')][_0xbb58('0x39')]){_0x1be0ae[_0xbb58('0x35')]=_['merge'](_0x1be0ae['where'],{'$or':_[_0xbb58('0x3a')](_0x1be0ae['attributes'],function(_0x6c719a){var _0x11fa59={};_0x11fa59[_0x6c719a]={'$like':'%'+_0x283dff[_0xbb58('0x28')]['filter']+'%'};return _0x11fa59;})});}_0x1be0ae=_['merge']({},_0x1be0ae,_0x283dff[_0xbb58('0x3b')]);var _0x184b84={'where':_0x1be0ae[_0xbb58('0x35')]};return db[_0xbb58('0x26')][_0xbb58('0x18')](_0x184b84)[_0xbb58('0x1e')](function(_0x5797e7){_0x8e9ed9[_0xbb58('0x18')]=_0x5797e7;if(_0x283dff[_0xbb58('0x28')][_0xbb58('0x3c')]){_0x1be0ae[_0xbb58('0x3d')]=[{'all':!![]}];}return db[_0xbb58('0x26')][_0xbb58('0x3e')](_0x1be0ae);})['then'](function(_0x360765){_0x8e9ed9['rows']=_0x360765;return _0x8e9ed9;})[_0xbb58('0x1e')](respondWithFilteredResult(_0x3bd550,_0x1be0ae))[_0xbb58('0x3f')](handleError(_0x3bd550,null));};exports[_0xbb58('0x40')]=function(_0x45817f,_0x54099f){var _0x324be0={'raw':![],'where':{'id':_0x45817f[_0xbb58('0x41')]['id']}},_0x447a03={};_0x447a03[_0xbb58('0x24')]=_['keys'](db[_0xbb58('0x26')]['rawAttributes']);_0x447a03[_0xbb58('0x28')]=_[_0xbb58('0x25')](_0x45817f[_0xbb58('0x28')]);_0x447a03[_0xbb58('0x2b')]=_[_0xbb58('0x2c')](_0x447a03[_0xbb58('0x24')],_0x447a03[_0xbb58('0x28')]);_0x324be0[_0xbb58('0x2e')]=_[_0xbb58('0x2c')](_0x447a03[_0xbb58('0x24')],qs[_0xbb58('0x2d')](_0x45817f[_0xbb58('0x28')][_0xbb58('0x2d')]));_0x324be0['attributes']=_0x324be0[_0xbb58('0x2e')]['length']?_0x324be0['attributes']:_0x447a03[_0xbb58('0x24')];if(_0x45817f[_0xbb58('0x28')][_0xbb58('0x3c')]){_0x324be0[_0xbb58('0x3d')]=[{'all':!![]}];}_0x324be0=_[_0xbb58('0x38')]({},_0x324be0,_0x45817f[_0xbb58('0x3b')]);return db[_0xbb58('0x26')][_0xbb58('0x42')](_0x324be0)[_0xbb58('0x1e')](handleEntityNotFound(_0x54099f,null))[_0xbb58('0x1e')](respondWithResult(_0x54099f,null))[_0xbb58('0x3f')](handleError(_0x54099f,null));};exports['create']=function(_0x2ec9be,_0x23871d){return db['Team'][_0xbb58('0x43')](_0x2ec9be[_0xbb58('0x44')],{})[_0xbb58('0x1e')](respondWithResult(_0x23871d,0xc9))[_0xbb58('0x3f')](handleError(_0x23871d,null));};exports['update']=function(_0x17418c,_0x54d658){if(_0x17418c[_0xbb58('0x44')]['id']){delete _0x17418c[_0xbb58('0x44')]['id'];}return db[_0xbb58('0x26')]['find']({'where':{'id':_0x17418c[_0xbb58('0x41')]['id']}})[_0xbb58('0x1e')](handleEntityNotFound(_0x54d658,null))['then'](saveUpdates(_0x17418c[_0xbb58('0x44')],null))['then'](respondWithResult(_0x54d658,null))[_0xbb58('0x3f')](handleError(_0x54d658,null));};exports[_0xbb58('0x1f')]=function(_0xdb9816,_0x1ec976){return db[_0xbb58('0x26')][_0xbb58('0x42')]({'where':{'id':_0xdb9816[_0xbb58('0x41')]['id']}})['then'](handleEntityNotFound(_0x1ec976,null))[_0xbb58('0x1e')](removeEntity(_0x1ec976,null))[_0xbb58('0x3f')](handleError(_0x1ec976,null));};exports['getQueues']=function(_0x3efbc6,_0x98157,_0x5c19b7){return db[_0xbb58('0x26')][_0xbb58('0x45')]({'where':{'id':_0x3efbc6[_0xbb58('0x41')]['id']}})['then'](handleEntityNotFound(_0x98157,null))[_0xbb58('0x1e')](function(_0x22b56e){if(!_0x3efbc6[_0xbb58('0x28')]['channel']||!utils[_0xbb58('0x46')](_0x3efbc6['query'][_0xbb58('0x47')])){throw new db[(_0xbb58('0x48'))][(_0xbb58('0x49'))]('Unknown\x20channel');}var _0x5aaace={};var _0x1573d1={};_0x1573d1[_0xbb58('0x24')]=_[_0xbb58('0x25')](db[_[_0xbb58('0x4a')](_0x3efbc6[_0xbb58('0x28')]['channel'][_0xbb58('0x4b')]())+_0xbb58('0x4c')][_0xbb58('0x27')]);_0x1573d1['query']=_[_0xbb58('0x25')](_0x3efbc6[_0xbb58('0x28')]);_0x1573d1[_0xbb58('0x2b')]=_[_0xbb58('0x2c')](_0x1573d1[_0xbb58('0x24')],_0x1573d1['query']);_0x5aaace[_0xbb58('0x2e')]=_[_0xbb58('0x2c')](_0x1573d1[_0xbb58('0x24')],qs[_0xbb58('0x2d')](_0x3efbc6[_0xbb58('0x28')][_0xbb58('0x2d')]));_0x5aaace[_0xbb58('0x2e')]=_0x5aaace[_0xbb58('0x2e')]['length']?_0x5aaace[_0xbb58('0x2e')]:_0x1573d1['model'];_0x5aaace[_0xbb58('0x32')]=qs[_0xbb58('0x32')](_0x3efbc6[_0xbb58('0x28')][_0xbb58('0x32')]);_0x5aaace[_0xbb58('0x19')]=qs[_0xbb58('0x19')](_0x3efbc6[_0xbb58('0x28')][_0xbb58('0x19')]);_0x5aaace[_0xbb58('0x33')]=qs[_0xbb58('0x34')](_0x3efbc6[_0xbb58('0x28')]['sort']);_0x5aaace[_0xbb58('0x35')]=qs[_0xbb58('0x2b')](_[_0xbb58('0x4d')](_0x3efbc6['query'],_0x1573d1[_0xbb58('0x2b')]));_0x5aaace=_['merge']({},_0x5aaace,_0x3efbc6[_0xbb58('0x3b')]);return _0x22b56e[_0xbb58('0x4e')+_['capitalize'](_0x3efbc6[_0xbb58('0x28')][_0xbb58('0x47')][_0xbb58('0x4b')]())+_0xbb58('0x4f')](_0x5aaace);})[_0xbb58('0x1e')](respondWithResult(_0x98157,null))['catch'](handleError(_0x98157,null));};exports['addQueues']=function(_0x18dc15,_0x2f0db7,_0x390b94){return db['Team'][_0xbb58('0x45')]({'where':{'id':_0x18dc15[_0xbb58('0x41')]['id']}})['then'](function(_0xfcad4c){if(_0xfcad4c){if(!_0x18dc15[_0xbb58('0x44')]['channel']||!utils[_0xbb58('0x46')](_0x18dc15[_0xbb58('0x44')][_0xbb58('0x47')])){throw new db[(_0xbb58('0x48'))][(_0xbb58('0x49'))]('Unknown\x20channel');}return _0xfcad4c[_0xbb58('0x37')+_[_0xbb58('0x4a')](_0x18dc15[_0xbb58('0x44')][_0xbb58('0x47')][_0xbb58('0x4b')]())+'Queues'](_0x18dc15['body'][_0xbb58('0x50')]);}return null;})['then'](handleEntityNotFound(_0x2f0db7,null))[_0xbb58('0x1e')](respondWithStatusCode(_0x2f0db7,null))[_0xbb58('0x3f')](handleError(_0x2f0db7,null));};exports[_0xbb58('0x51')]=function(_0xe5b33a,_0x3211a9,_0x3e63a6){return db[_0xbb58('0x26')][_0xbb58('0x45')]({'where':{'id':_0xe5b33a[_0xbb58('0x41')]['id']}})[_0xbb58('0x1e')](function(_0x4c1162){if(_0x4c1162){if(!_0xe5b33a[_0xbb58('0x28')][_0xbb58('0x47')]||!utils[_0xbb58('0x46')](_0xe5b33a['query'][_0xbb58('0x47')])){throw new db['Sequelize']['ValidationError'](_0xbb58('0x52'));}return _0x4c1162[_0xbb58('0x53')+_[_0xbb58('0x4a')](_0xe5b33a[_0xbb58('0x28')]['channel']['toLowerCase']())+_0xbb58('0x4f')](_0xe5b33a[_0xbb58('0x28')]['ids']);}return null;})[_0xbb58('0x1e')](handleEntityNotFound(_0x3211a9,null))[_0xbb58('0x1e')](respondWithStatusCode(_0x3211a9,null))[_0xbb58('0x3f')](handleError(_0x3211a9,null));};exports[_0xbb58('0x54')]=function(_0x382d16,_0x55d3c5,_0x30b8d2){return db['Team']['find']({'where':{'id':_0x382d16[_0xbb58('0x41')]['id']}})[_0xbb58('0x1e')](handleEntityNotFound(_0x55d3c5,null))[_0xbb58('0x1e')](function(_0x548b58){if(_0x548b58){return _0x548b58[_0xbb58('0x54')](_0x382d16[_0xbb58('0x44')][_0xbb58('0x50')],_[_0xbb58('0x55')](_0x382d16['body'],[_0xbb58('0x50'),'id'])||{})['spread'](function(_0x5ae61c){for(var _0x3addb7=0x0;_0x3addb7<_0x382d16[_0xbb58('0x44')][_0xbb58('0x50')]['length'];_0x3addb7+=0x1){socket['emit'](_0xbb58('0x56'),{'UserId':_0x382d16[_0xbb58('0x44')][_0xbb58('0x50')][_0x3addb7],'TeamId':_0x382d16[_0xbb58('0x41')]['id']});}return _0x5ae61c;});}})[_0xbb58('0x1e')](respondWithResult(_0x55d3c5,null))[_0xbb58('0x3f')](handleError(_0x55d3c5,null));};exports[_0xbb58('0x57')]=function(_0x37ed76,_0x126716,_0x163ca4){var _0x2d9741;return db['Team']['find']({'where':{'id':_0x37ed76[_0xbb58('0x41')]['id']}})[_0xbb58('0x1e')](handleEntityNotFound(_0x126716,null))[_0xbb58('0x1e')](function(_0x54cd7d){if(_0x54cd7d){return _0x54cd7d[_0xbb58('0x57')](_0x37ed76[_0xbb58('0x28')][_0xbb58('0x50')])['then'](function(_0x2d70d2){if(_['isArray'](_0x37ed76[_0xbb58('0x28')]['ids'])){for(var _0x13935d=0x0;_0x13935d<_0x37ed76[_0xbb58('0x28')][_0xbb58('0x50')][_0xbb58('0x2f')];_0x13935d+=0x1){socket[_0xbb58('0x58')](_0xbb58('0x59'),{'UserId':_0x37ed76['query']['ids'][_0x13935d],'TeamId':_0x37ed76[_0xbb58('0x41')]['id']});}}else{socket[_0xbb58('0x58')]('userTeam:remove',{'UserId':_0x37ed76[_0xbb58('0x28')][_0xbb58('0x50')],'TeamId':_0x37ed76[_0xbb58('0x41')]['id']});}});}})['then'](respondWithStatusCode(_0x126716,null))[_0xbb58('0x3f')](handleError(_0x126716,null));};exports['getAgents']=function(_0x2fe0b5,_0x2616ad,_0x3cc9d6){var _0x2326ae={};var _0x389cd3={};var _0xe57872;var _0x195a23;return db['Team'][_0xbb58('0x45')]({'where':{'id':_0x2fe0b5[_0xbb58('0x41')]['id']}})[_0xbb58('0x1e')](handleEntityNotFound(_0x2616ad,null))['then'](function(_0x25fb59){if(_0x25fb59){_0xe57872=_0x25fb59;_0x389cd3[_0xbb58('0x24')]=_['keys'](db['User'][_0xbb58('0x27')]);_0x389cd3[_0xbb58('0x28')]=_[_0xbb58('0x25')](_0x2fe0b5['query']);_0x389cd3['filters']=_[_0xbb58('0x2c')](_0x389cd3[_0xbb58('0x24')],_0x389cd3[_0xbb58('0x28')]);_0x2326ae[_0xbb58('0x2e')]=_[_0xbb58('0x2c')](_0x389cd3[_0xbb58('0x24')],qs[_0xbb58('0x2d')](_0x2fe0b5[_0xbb58('0x28')][_0xbb58('0x2d')]));_0x2326ae[_0xbb58('0x2e')]=_0x2326ae[_0xbb58('0x2e')][_0xbb58('0x2f')]?_0x2326ae[_0xbb58('0x2e')]:_0x389cd3[_0xbb58('0x24')];_0x2326ae[_0xbb58('0x33')]=qs['sort'](_0x2fe0b5[_0xbb58('0x28')]['sort']);_0x2326ae[_0xbb58('0x35')]=qs[_0xbb58('0x2b')](_[_0xbb58('0x4d')](_0x2fe0b5[_0xbb58('0x28')],_0x389cd3[_0xbb58('0x2b')]));if(_0x2fe0b5[_0xbb58('0x28')][_0xbb58('0x39')]){_0x2326ae[_0xbb58('0x35')]=_[_0xbb58('0x38')](_0x2326ae[_0xbb58('0x35')],{'$or':_['map'](_0x2326ae[_0xbb58('0x2e')],function(_0x1002fd){var _0x2c0ee1={};_0x2c0ee1[_0x1002fd]={'$like':'%'+_0x2fe0b5[_0xbb58('0x28')][_0xbb58('0x39')]+'%'};return _0x2c0ee1;})});}_0x2326ae=_['merge']({},_0x2326ae,_0x2fe0b5[_0xbb58('0x3b')]);return _0xe57872['getAgents'](_0x2326ae);}})['then'](function(_0x3b9c4c){if(_0x3b9c4c){_0x195a23=_0x3b9c4c['length'];if(!_0x2fe0b5[_0xbb58('0x28')]['hasOwnProperty'](_0xbb58('0x31'))){_0x2326ae['limit']=qs[_0xbb58('0x32')](_0x2fe0b5[_0xbb58('0x28')][_0xbb58('0x32')]);_0x2326ae[_0xbb58('0x19')]=qs[_0xbb58('0x19')](_0x2fe0b5[_0xbb58('0x28')][_0xbb58('0x19')]);}return _0xe57872[_0xbb58('0x5a')](_0x2326ae);}})['then'](function(_0x5f4c0d){if(_0x5f4c0d){return _0x5f4c0d?{'count':_0x195a23,'rows':_0x5f4c0d}:null;}})[_0xbb58('0x1e')](respondWithResult(_0x2616ad,null))[_0xbb58('0x3f')](handleError(_0x2616ad,null));};