166f6a4e9a36459b884eb464182135c5a918a78d
[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 _0xf546=['model','query','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','merge','VIRTUAL','filter','options','includeAll','include','Team','then','rows','catch','show','params','keys','find','create','body','user','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','Teams','UserProfileSection','userProfileId','autoAssociation','UserProfileResource','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','destroy','sequelize','transaction','Sequelize','QueryTypes','UserId','SELECT\x20VoiceQueueId\x20FROM\x20team_has_voice_queues\x20WHERE\x20TeamId\x20=\x20','SELECT','UserVoiceQueue','MailQueueId','UserMailQueue','SmsQueueId','UserSmsQueue','SELECT\x20ChatQueueId\x20FROM\x20team_has_chat_queues\x20WHERE\x20TeamId\x20=\x20','ChatQueueId','UserChatQueue','UserFaxQueue','SELECT\x20OpenchannelQueueId\x20FROM\x20team_has_openchannel_queues\x20WHERE\x20TeamId\x20=\x20','OpenchannelQueueId','UserOpenchannelQueue','SELECT\x20WhatsappQueueId\x20FROM\x20team_has_whatsapp_queues\x20WHERE\x20TeamId\x20=\x20','WhatsappQueueId','TeamVoicePrefix','UserVoicePrefix','VoicePrefixId','findOne','channel','ValidationError','Unknown\x20channel','toLowerCase','Queue','capitalize','addQueues','add','Queues','ids','removeQueues','isValidChannel','remove','addAgents','isArray','Voice','Chat','Sms','Fax','Openchannel','through','penalty','QueueId','TeamVoiceQueue','User','findOrCreate','Queue:save','findAll','online','interface','UserVoiceQueueRt','isNil','voicePause','all','upsert','removeAgents','Mail','Whatsapp','Queue:remove','getAgents','eml-format','zip-dir','moment','bluebird','mustache','util','sox','to-csv','ejs','fs-extra','lodash','squel','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','redis','defaults','localhost','socket.io-emitter','./team.socket','register','sendStatus','end','status','offset','undefined','count','limit','json','apply','reject','save','update','error','stack','name','send','index','map','rawAttributes','fieldName','type','key'];(function(_0x103801,_0x483bc4){var _0x4dfd13=function(_0x4e8871){while(--_0x4e8871){_0x103801['push'](_0x103801['shift']());}};_0x4dfd13(++_0x483bc4);}(_0xf546,0x1a1));var _0x6f54=function(_0x599b88,_0x3c4ac2){_0x599b88=_0x599b88-0x0;var _0x58fd4e=_0xf546[_0x599b88];return _0x58fd4e;};'use strict';var emlformat=require(_0x6f54('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x6f54('0x1'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0x6f54('0x2'));var BPromise=require(_0x6f54('0x3'));var Mustache=require(_0x6f54('0x4'));var util=require(_0x6f54('0x5'));var path=require('path');var sox=require(_0x6f54('0x6'));var csv=require(_0x6f54('0x7'));var ejs=require(_0x6f54('0x8'));var fs=require('fs');var fs_extra=require(_0x6f54('0x9'));var _=require(_0x6f54('0xa'));var squel=require(_0x6f54('0xb'));var crypto=require('crypto');var jsforce=require(_0x6f54('0xc'));var deskjs=require(_0x6f54('0xd'));var toCsv=require(_0x6f54('0x7'));var querystring=require(_0x6f54('0xe'));var Papa=require(_0x6f54('0xf'));var Redis=require(_0x6f54('0x10'));var authService=require(_0x6f54('0x11'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require('../../config/license/hardware');var logger=require(_0x6f54('0x12'))(_0x6f54('0x13'));var utils=require(_0x6f54('0x14'));var config=require('../../config/environment');var licenseUtil=require(_0x6f54('0x15'));var db=require(_0x6f54('0x16'))['db'];config[_0x6f54('0x17')]=_[_0x6f54('0x18')](config[_0x6f54('0x17')],{'host':_0x6f54('0x19'),'port':0x18eb});var socket=require(_0x6f54('0x1a'))(new Redis(config[_0x6f54('0x17')]));require(_0x6f54('0x1b'))[_0x6f54('0x1c')](socket);function respondWithStatusCode(_0x375380,_0x4ac50e){_0x4ac50e=_0x4ac50e||0xcc;return function(_0x5d19f5){if(_0x5d19f5){return _0x375380[_0x6f54('0x1d')](_0x4ac50e);}return _0x375380['status'](_0x4ac50e)[_0x6f54('0x1e')]();};}function respondWithResult(_0x4b93b3,_0x2e2099){_0x2e2099=_0x2e2099||0xc8;return function(_0x39c0ca){if(_0x39c0ca){return _0x4b93b3[_0x6f54('0x1f')](_0x2e2099)['json'](_0x39c0ca);}};}function respondWithFilteredResult(_0x2cde80,_0x554d64){return function(_0x4d67ae){if(_0x4d67ae){var _0x3fbedc=typeof _0x554d64[_0x6f54('0x20')]===_0x6f54('0x21')&&typeof _0x554d64['limit']==='undefined';var _0x39be9f=_0x4d67ae[_0x6f54('0x22')];var _0x3606ed=_0x3fbedc?0x0:_0x554d64[_0x6f54('0x20')];var _0x534aba=_0x3fbedc?_0x4d67ae[_0x6f54('0x22')]:_0x554d64[_0x6f54('0x20')]+_0x554d64[_0x6f54('0x23')];var _0x17bda5;if(_0x534aba>=_0x39be9f){_0x534aba=_0x39be9f;_0x17bda5=0xc8;}else{_0x17bda5=0xce;}_0x2cde80[_0x6f54('0x1f')](_0x17bda5);return _0x2cde80['set']('Content-Range',_0x3606ed+'-'+_0x534aba+'/'+_0x39be9f)[_0x6f54('0x24')](_0x4d67ae);}return null;};}function patchUpdates(_0x5f20ef){return function(_0x3de0f2){try{jsonpatch[_0x6f54('0x25')](_0x3de0f2,_0x5f20ef,!![]);}catch(_0x55483a){return BPromise[_0x6f54('0x26')](_0x55483a);}return _0x3de0f2[_0x6f54('0x27')]();};}function saveUpdates(_0x1af5ee,_0x38d75f){return function(_0x416d80){if(_0x416d80){return _0x416d80[_0x6f54('0x28')](_0x1af5ee)['then'](function(_0x35c3bc){return _0x35c3bc;});}return null;};}function handleEntityNotFound(_0x4c9a20,_0x1675ed){return function(_0x228a36){if(!_0x228a36){_0x4c9a20[_0x6f54('0x1d')](0x194);}return _0x228a36;};}function handleError(_0x5dfe2a,_0x2308ac){_0x2308ac=_0x2308ac||0x1f4;return function(_0x27eb53){logger[_0x6f54('0x29')](_0x27eb53[_0x6f54('0x2a')]);if(_0x27eb53[_0x6f54('0x2b')]){delete _0x27eb53[_0x6f54('0x2b')];}_0x5dfe2a[_0x6f54('0x1f')](_0x2308ac)[_0x6f54('0x2c')](_0x27eb53);};}exports[_0x6f54('0x2d')]=function(_0xf6015e,_0x48138c){var _0x107b32={},_0x53a8d6={},_0x16e8a0={'count':0x0,'rows':[]};var _0x5d53db=_[_0x6f54('0x2e')](db['Team'][_0x6f54('0x2f')],function(_0xc0c315){return{'name':_0xc0c315[_0x6f54('0x30')],'type':_0xc0c315[_0x6f54('0x31')][_0x6f54('0x32')]};});_0x53a8d6[_0x6f54('0x33')]=_[_0x6f54('0x2e')](_0x5d53db,'name');_0x53a8d6[_0x6f54('0x34')]=_['keys'](_0xf6015e[_0x6f54('0x34')]);_0x53a8d6[_0x6f54('0x35')]=_[_0x6f54('0x36')](_0x53a8d6[_0x6f54('0x33')],_0x53a8d6['query']);_0x107b32[_0x6f54('0x37')]=_[_0x6f54('0x36')](_0x53a8d6[_0x6f54('0x33')],qs[_0x6f54('0x38')](_0xf6015e['query']['fields']));_0x107b32[_0x6f54('0x37')]=_0x107b32[_0x6f54('0x37')][_0x6f54('0x39')]?_0x107b32['attributes']:_0x53a8d6[_0x6f54('0x33')];if(!_0xf6015e[_0x6f54('0x34')][_0x6f54('0x3a')](_0x6f54('0x3b'))){_0x107b32['limit']=qs[_0x6f54('0x23')](_0xf6015e[_0x6f54('0x34')][_0x6f54('0x23')]);_0x107b32[_0x6f54('0x20')]=qs['offset'](_0xf6015e['query'][_0x6f54('0x20')]);}_0x107b32[_0x6f54('0x3c')]=qs[_0x6f54('0x3d')](_0xf6015e[_0x6f54('0x34')][_0x6f54('0x3d')]);_0x107b32[_0x6f54('0x3e')]=qs[_0x6f54('0x35')](_[_0x6f54('0x3f')](_0xf6015e[_0x6f54('0x34')],_0x53a8d6[_0x6f54('0x35')]),_0x5d53db);if(_0xf6015e['query']['filter']){_0x107b32['where']=_[_0x6f54('0x40')](_0x107b32[_0x6f54('0x3e')],{'$or':_['map'](_0x5d53db,function(_0x1f7c26){if(_0x1f7c26[_0x6f54('0x31')]!==_0x6f54('0x41')){var _0x7cf962={};_0x7cf962[_0x1f7c26[_0x6f54('0x2b')]]={'$like':'%'+_0xf6015e['query'][_0x6f54('0x42')]+'%'};return _0x7cf962;}})});}_0x107b32=_[_0x6f54('0x40')]({},_0x107b32,_0xf6015e[_0x6f54('0x43')]);var _0x350f0c={'where':_0x107b32[_0x6f54('0x3e')]};return db['Team'][_0x6f54('0x22')](_0x350f0c)['then'](function(_0x16a806){_0x16e8a0['count']=_0x16a806;if(_0xf6015e[_0x6f54('0x34')][_0x6f54('0x44')]){_0x107b32[_0x6f54('0x45')]=[{'all':!![]}];}return db[_0x6f54('0x46')]['findAll'](_0x107b32);})[_0x6f54('0x47')](function(_0x4b65c5){_0x16e8a0[_0x6f54('0x48')]=_0x4b65c5;return _0x16e8a0;})[_0x6f54('0x47')](respondWithFilteredResult(_0x48138c,_0x107b32))[_0x6f54('0x49')](handleError(_0x48138c,null));};exports[_0x6f54('0x4a')]=function(_0x227fc3,_0x283850){var _0x196a72={'raw':![],'where':{'id':_0x227fc3[_0x6f54('0x4b')]['id']}},_0x2dc01a={};_0x2dc01a[_0x6f54('0x33')]=_[_0x6f54('0x4c')](db[_0x6f54('0x46')][_0x6f54('0x2f')]);_0x2dc01a[_0x6f54('0x34')]=_[_0x6f54('0x4c')](_0x227fc3[_0x6f54('0x34')]);_0x2dc01a[_0x6f54('0x35')]=_[_0x6f54('0x36')](_0x2dc01a[_0x6f54('0x33')],_0x2dc01a[_0x6f54('0x34')]);_0x196a72[_0x6f54('0x37')]=_[_0x6f54('0x36')](_0x2dc01a['model'],qs[_0x6f54('0x38')](_0x227fc3['query'][_0x6f54('0x38')]));_0x196a72[_0x6f54('0x37')]=_0x196a72[_0x6f54('0x37')][_0x6f54('0x39')]?_0x196a72[_0x6f54('0x37')]:_0x2dc01a[_0x6f54('0x33')];if(_0x227fc3['query'][_0x6f54('0x44')]){_0x196a72[_0x6f54('0x45')]=[{'all':!![]}];}_0x196a72=_[_0x6f54('0x40')]({},_0x196a72,_0x227fc3['options']);return db[_0x6f54('0x46')][_0x6f54('0x4d')](_0x196a72)[_0x6f54('0x47')](handleEntityNotFound(_0x283850,null))[_0x6f54('0x47')](respondWithResult(_0x283850,null))['catch'](handleError(_0x283850,null));};exports[_0x6f54('0x4e')]=function(_0x18b97d,_0x11074b){return db[_0x6f54('0x46')][_0x6f54('0x4e')](_0x18b97d[_0x6f54('0x4f')],{})[_0x6f54('0x47')](function(_0x2af0c8){var _0x5efcf8=_0x18b97d[_0x6f54('0x50')][_0x6f54('0x51')]({'plain':!![]});if(!_0x5efcf8)throw new Error(_0x6f54('0x52'));if(_0x5efcf8[_0x6f54('0x53')]===_0x6f54('0x50')){var _0x316070=_0x2af0c8[_0x6f54('0x51')]({'plain':!![]});var _0x37e545=_0x6f54('0x54');return db[_0x6f54('0x55')]['find']({'where':{'name':_0x37e545,'userProfileId':_0x5efcf8[_0x6f54('0x56')]},'raw':!![]})['then'](function(_0x5a15d0){if(_0x5a15d0&&_0x5a15d0[_0x6f54('0x57')]===0x0){return db[_0x6f54('0x58')][_0x6f54('0x4e')]({'name':_0x316070[_0x6f54('0x2b')],'resourceId':_0x316070['id'],'type':_0x5a15d0['name'],'sectionId':_0x5a15d0['id']},{})[_0x6f54('0x47')](function(){return _0x2af0c8;});}else{return _0x2af0c8;}})[_0x6f54('0x49')](function(_0x450efb){logger[_0x6f54('0x29')](_0x6f54('0x59'),_0x450efb);throw _0x450efb;});}return _0x2af0c8;})['then'](respondWithResult(_0x11074b,0xc9))[_0x6f54('0x49')](handleError(_0x11074b,null));};exports[_0x6f54('0x28')]=function(_0x199223,_0x7b1a7a){if(_0x199223[_0x6f54('0x4f')]['id']){delete _0x199223[_0x6f54('0x4f')]['id'];}return db[_0x6f54('0x46')][_0x6f54('0x4d')]({'where':{'id':_0x199223[_0x6f54('0x4b')]['id']}})[_0x6f54('0x47')](handleEntityNotFound(_0x7b1a7a,null))['then'](saveUpdates(_0x199223[_0x6f54('0x4f')],null))[_0x6f54('0x47')](respondWithResult(_0x7b1a7a,null))[_0x6f54('0x49')](handleError(_0x7b1a7a,null));};exports[_0x6f54('0x5a')]=function(_0x818073,_0x2dece8){var _0x25821d=_0x818073[_0x6f54('0x4b')]['id'];var _0x39ece0=[];return db[_0x6f54('0x5b')][_0x6f54('0x5c')](function(_0x25b317){var _0x378ab7='SELECT\x20UserId\x20FROM\x20user_has_teams\x20WHERE\x20TeamId\x20=\x20'+_0x25821d;return db[_0x6f54('0x5b')]['query'](_0x378ab7,{'type':db[_0x6f54('0x5d')][_0x6f54('0x5e')]['SELECT'],'raw':!![],'transaction':_0x25b317})['then'](function(_0xff1d48){_0x39ece0=_[_0x6f54('0x2e')](_0xff1d48,function(_0x224653){return Number(_0x224653[_0x6f54('0x5f')]);});})['then'](function(){var _0x50521e=_0x6f54('0x60')+_0x25821d;return db[_0x6f54('0x5b')][_0x6f54('0x34')](_0x50521e,{'type':db['Sequelize'][_0x6f54('0x5e')][_0x6f54('0x61')],'raw':!![],'transaction':_0x25b317});})['then'](function(_0x8d66){var _0x1a85b4=_['map'](_0x8d66,function(_0x29e3cf){return Number(_0x29e3cf['VoiceQueueId']);});return db[_0x6f54('0x62')]['destroy']({'where':{'UserId':{'$in':_0x39ece0},'VoiceQueueId':{'$in':_0x1a85b4}},'transaction':_0x25b317});})[_0x6f54('0x47')](function(){var _0x5e770b='SELECT\x20MailQueueId\x20FROM\x20team_has_mail_queues\x20WHERE\x20TeamId\x20=\x20'+_0x25821d;return db[_0x6f54('0x5b')]['query'](_0x5e770b,{'type':db[_0x6f54('0x5d')][_0x6f54('0x5e')][_0x6f54('0x61')],'raw':!![],'transaction':_0x25b317});})[_0x6f54('0x47')](function(_0x1234e0){var _0x1ffde5=_[_0x6f54('0x2e')](_0x1234e0,function(_0x1a16b8){return Number(_0x1a16b8[_0x6f54('0x63')]);});return db[_0x6f54('0x64')][_0x6f54('0x5a')]({'where':{'UserId':{'$in':_0x39ece0},'MailQueueId':{'$in':_0x1ffde5}},'transaction':_0x25b317});})[_0x6f54('0x47')](function(){var _0x3ef2dd='SELECT\x20SmsQueueId\x20FROM\x20team_has_sms_queues\x20WHERE\x20TeamId\x20=\x20'+_0x25821d;return db['sequelize'][_0x6f54('0x34')](_0x3ef2dd,{'type':db[_0x6f54('0x5d')][_0x6f54('0x5e')][_0x6f54('0x61')],'raw':!![],'transaction':_0x25b317});})[_0x6f54('0x47')](function(_0xff812e){var _0x1ca2d4=_[_0x6f54('0x2e')](_0xff812e,function(_0x213c2f){return Number(_0x213c2f[_0x6f54('0x65')]);});return db[_0x6f54('0x66')]['destroy']({'where':{'UserId':{'$in':_0x39ece0},'SmsQueueId':{'$in':_0x1ca2d4}},'transaction':_0x25b317});})['then'](function(){var _0x20ceb9=_0x6f54('0x67')+_0x25821d;return db[_0x6f54('0x5b')][_0x6f54('0x34')](_0x20ceb9,{'type':db['Sequelize'][_0x6f54('0x5e')]['SELECT'],'raw':!![],'transaction':_0x25b317});})[_0x6f54('0x47')](function(_0x331ed6){var _0x50919a=_[_0x6f54('0x2e')](_0x331ed6,function(_0x5bce88){return Number(_0x5bce88[_0x6f54('0x68')]);});return db[_0x6f54('0x69')]['destroy']({'where':{'UserId':{'$in':_0x39ece0},'ChatQueueId':{'$in':_0x50919a}},'transaction':_0x25b317});})[_0x6f54('0x47')](function(){var _0x20ac8e='SELECT\x20FaxQueueId\x20FROM\x20team_has_fax_queues\x20WHERE\x20TeamId\x20=\x20'+_0x25821d;return db[_0x6f54('0x5b')][_0x6f54('0x34')](_0x20ac8e,{'type':db[_0x6f54('0x5d')]['QueryTypes']['SELECT'],'raw':!![],'transaction':_0x25b317});})['then'](function(_0xa297b3){var _0x389b36=_[_0x6f54('0x2e')](_0xa297b3,function(_0x3d68ac){return Number(_0x3d68ac['FaxQueueId']);});return db[_0x6f54('0x6a')][_0x6f54('0x5a')]({'where':{'UserId':{'$in':_0x39ece0},'FaxQueueId':{'$in':_0x389b36}},'transaction':_0x25b317});})[_0x6f54('0x47')](function(){var _0x494175=_0x6f54('0x6b')+_0x25821d;return db['sequelize'][_0x6f54('0x34')](_0x494175,{'type':db[_0x6f54('0x5d')]['QueryTypes']['SELECT'],'raw':!![],'transaction':_0x25b317});})['then'](function(_0x541061){var _0x542658=_[_0x6f54('0x2e')](_0x541061,function(_0x1a7e9e){return Number(_0x1a7e9e[_0x6f54('0x6c')]);});return db[_0x6f54('0x6d')][_0x6f54('0x5a')]({'where':{'UserId':{'$in':_0x39ece0},'OpenchannelQueueId':{'$in':_0x542658}},'transaction':_0x25b317});})[_0x6f54('0x47')](function(){var _0x4b72da=_0x6f54('0x6e')+_0x25821d;return db[_0x6f54('0x5b')][_0x6f54('0x34')](_0x4b72da,{'type':db[_0x6f54('0x5d')]['QueryTypes'][_0x6f54('0x61')],'raw':!![],'transaction':_0x25b317});})[_0x6f54('0x47')](function(_0x42d77b){var _0x2fac5a=_['map'](_0x42d77b,function(_0x21dd65){return Number(_0x21dd65[_0x6f54('0x6f')]);});return db['UserWhatsappQueue']['destroy']({'where':{'UserId':{'$in':_0x39ece0},'WhatsappQueueId':{'$in':_0x2fac5a}},'transaction':_0x25b317});})[_0x6f54('0x47')](function(){return db[_0x6f54('0x70')]['find']({'where':{'TeamId':_0x25821d}});})[_0x6f54('0x47')](function(_0x3467f1){if(_0x3467f1===null){return;}return db[_0x6f54('0x71')][_0x6f54('0x5a')]({'where':{'UserId':{'$in':_0x39ece0},'VoicePrefixId':_0x3467f1[_0x6f54('0x72')]},'transaction':_0x25b317});})[_0x6f54('0x47')](function(){return db[_0x6f54('0x46')][_0x6f54('0x5a')]({'where':{'id':_0x25821d},'cascade':!![],'transaction':_0x25b317});})['then'](function(_0x5d6e88){if(_0x5d6e88===0x0){_0x2dece8[_0x6f54('0x1d')](0x194);}else{return db[_0x6f54('0x58')][_0x6f54('0x5a')]({'where':{'type':_0x6f54('0x54'),'resourceId':_0x25821d},'cascade':!![],'transaction':_0x25b317})[_0x6f54('0x47')](function(){_0x2dece8[_0x6f54('0x1f')](0xcc)['end']();});}})[_0x6f54('0x49')](function(_0x54ccc1){_0x2dece8[_0x6f54('0x1f')](0x1f4)['send'](_0x54ccc1);});});};exports['getQueues']=function(_0x21b4c6,_0x1c9637,_0x554d9a){return db[_0x6f54('0x46')][_0x6f54('0x73')]({'where':{'id':_0x21b4c6['params']['id']}})[_0x6f54('0x47')](handleEntityNotFound(_0x1c9637,null))[_0x6f54('0x47')](function(_0x4fac4e){if(!_0x21b4c6[_0x6f54('0x34')][_0x6f54('0x74')]||!utils['isValidChannel'](_0x21b4c6[_0x6f54('0x34')][_0x6f54('0x74')])){throw new db[(_0x6f54('0x5d'))][(_0x6f54('0x75'))](_0x6f54('0x76'));}var _0x2a1bb2={};var _0x319afc={};_0x319afc['model']=_['keys'](db[_['capitalize'](_0x21b4c6[_0x6f54('0x34')][_0x6f54('0x74')][_0x6f54('0x77')]())+_0x6f54('0x78')][_0x6f54('0x2f')]);_0x319afc[_0x6f54('0x34')]=_[_0x6f54('0x4c')](_0x21b4c6['query']);_0x319afc[_0x6f54('0x35')]=_[_0x6f54('0x36')](_0x319afc[_0x6f54('0x33')],_0x319afc[_0x6f54('0x34')]);_0x2a1bb2[_0x6f54('0x37')]=_[_0x6f54('0x36')](_0x319afc[_0x6f54('0x33')],qs[_0x6f54('0x38')](_0x21b4c6['query'][_0x6f54('0x38')]));_0x2a1bb2[_0x6f54('0x37')]=_0x2a1bb2[_0x6f54('0x37')][_0x6f54('0x39')]?_0x2a1bb2['attributes']:_0x319afc['model'];_0x2a1bb2['limit']=qs[_0x6f54('0x23')](_0x21b4c6[_0x6f54('0x34')][_0x6f54('0x23')]);_0x2a1bb2[_0x6f54('0x20')]=qs[_0x6f54('0x20')](_0x21b4c6[_0x6f54('0x34')]['offset']);_0x2a1bb2[_0x6f54('0x3c')]=qs[_0x6f54('0x3d')](_0x21b4c6['query']['sort']);_0x2a1bb2[_0x6f54('0x3e')]=qs['filters'](_[_0x6f54('0x3f')](_0x21b4c6['query'],_0x319afc[_0x6f54('0x35')]));_0x2a1bb2=_['merge']({},_0x2a1bb2,_0x21b4c6[_0x6f54('0x43')]);return _0x4fac4e[_0x6f54('0x51')+_[_0x6f54('0x79')](_0x21b4c6[_0x6f54('0x34')][_0x6f54('0x74')][_0x6f54('0x77')]())+'Queues'](_0x2a1bb2);})[_0x6f54('0x47')](respondWithResult(_0x1c9637,null))['catch'](handleError(_0x1c9637,null));};exports[_0x6f54('0x7a')]=function(_0x3f324e,_0x516cee,_0x49a397){return db[_0x6f54('0x46')][_0x6f54('0x73')]({'where':{'id':_0x3f324e[_0x6f54('0x4b')]['id']}})[_0x6f54('0x47')](function(_0x4b3ceb){if(_0x4b3ceb){if(!_0x3f324e[_0x6f54('0x4f')][_0x6f54('0x74')]||!utils['isValidChannel'](_0x3f324e['body'][_0x6f54('0x74')])){throw new db[(_0x6f54('0x5d'))]['ValidationError'](_0x6f54('0x76'));}return _0x4b3ceb[_0x6f54('0x7b')+_[_0x6f54('0x79')](_0x3f324e['body'][_0x6f54('0x74')][_0x6f54('0x77')]())+_0x6f54('0x7c')](_0x3f324e[_0x6f54('0x4f')][_0x6f54('0x7d')]);}return null;})[_0x6f54('0x47')](handleEntityNotFound(_0x516cee,null))['then'](respondWithStatusCode(_0x516cee,null))[_0x6f54('0x49')](handleError(_0x516cee,null));};exports[_0x6f54('0x7e')]=function(_0x517742,_0xffd8b7,_0x5cdf3e){return db[_0x6f54('0x46')][_0x6f54('0x73')]({'where':{'id':_0x517742[_0x6f54('0x4b')]['id']}})['then'](function(_0x477b36){if(_0x477b36){if(!_0x517742['query'][_0x6f54('0x74')]||!utils[_0x6f54('0x7f')](_0x517742[_0x6f54('0x34')]['channel'])){throw new db[(_0x6f54('0x5d'))]['ValidationError']('Unknown\x20channel');}return _0x477b36[_0x6f54('0x80')+_['capitalize'](_0x517742[_0x6f54('0x34')]['channel'][_0x6f54('0x77')]())+_0x6f54('0x7c')](_0x517742[_0x6f54('0x34')][_0x6f54('0x7d')]);}return null;})['then'](handleEntityNotFound(_0xffd8b7,null))[_0x6f54('0x47')](respondWithStatusCode(_0xffd8b7,null))[_0x6f54('0x49')](handleError(_0xffd8b7,null));};exports[_0x6f54('0x81')]=function(_0xc51c6c,_0x247b9b){var _0x2a8113=_[_0x6f54('0x82')](_0xc51c6c[_0x6f54('0x4f')]['ids'])?_0xc51c6c[_0x6f54('0x4f')][_0x6f54('0x7d')]:[_0xc51c6c[_0x6f54('0x4f')]['ids']];var _0x11f8f1=_0xc51c6c[_0x6f54('0x4b')]['id'];return db['Team'][_0x6f54('0x4d')]({'where':{'id':_0x11f8f1}})[_0x6f54('0x47')](handleEntityNotFound(_0x247b9b,null))[_0x6f54('0x47')](function(_0x232a10){if(_0x232a10){var _0x5e8b31=[_0x6f54('0x83'),_0x6f54('0x84'),'Mail',_0x6f54('0x85'),_0x6f54('0x86'),_0x6f54('0x87'),'Whatsapp'];return db[_0x6f54('0x5b')][_0x6f54('0x5c')](function(_0x4738d7){var _0x12ca36=_[_0x6f54('0x2e')](_0x5e8b31,function(_0x144e77){var _0x3b831c={'attributes':['id',_0x6f54('0x2b')]};if(_0x144e77===_0x6f54('0x83')){_0x3b831c[_0x6f54('0x88')]={'attributes':[_0x6f54('0x89')]};}return _0x232a10[_0x6f54('0x51')+_0x144e77+_0x6f54('0x7c')](_0x3b831c)[_0x6f54('0x47')](function(_0x9e724f){var _0x2eec7a=_[_0x6f54('0x2e')](_0x9e724f,function(_0x20eb61){var _0x465b34=_[_0x6f54('0x2e')](_0x2a8113,function(_0x58a640){var _0x219ee0={'where':{'UserId':_0x58a640,[_0x144e77+_0x6f54('0x8a')]:_0x20eb61['id']},'transaction':_0x4738d7};if(_0x144e77===_0x6f54('0x83')){_0x219ee0['defaults']={'penalty':_0x20eb61[_0x6f54('0x8b')][_0x6f54('0x89')]};}return db[_0x6f54('0x8c')+_0x144e77+_0x6f54('0x78')][_0x6f54('0x8d')](_0x219ee0)[_0x6f54('0x47')](function(){socket['emit'](_0x6f54('0x50')+_0x144e77+_0x6f54('0x8e'),{'UserId':_0x58a640,[_0x144e77+_0x6f54('0x8a')]:_0x20eb61['id']});if(_0x144e77!==_0x6f54('0x83')){return;}return db[_0x6f54('0x8c')][_0x6f54('0x8f')]({'where':{'id':_0x2a8113,'online':0x1},'attributes':['id',_0x6f54('0x2b'),_0x6f54('0x90'),'voicePause',_0x6f54('0x91')],'raw':!![]})[_0x6f54('0x47')](function(_0x73d27d){var _0x275895=_[_0x6f54('0x2e')](_0x73d27d,function(_0x3c89ae){return db[_0x6f54('0x92')]['upsert']({'membername':_0x3c89ae[_0x6f54('0x2b')],'UserId':_0x3c89ae['id'],'queue_name':_0x20eb61[_0x6f54('0x2b')],'VoiceQueueId':_0x20eb61['id'],'interface':_[_0x6f54('0x93')](_0x3c89ae['interface'])?util['format']('SIP/%s',_0x3c89ae['name']):_0x3c89ae[_0x6f54('0x91')],'paused':_0x3c89ae[_0x6f54('0x94')]||![],'penalty':_0x20eb61['TeamVoiceQueue']['penalty']},{'transaction':_0x4738d7});});return Promise['all'](_0x275895);});});});return Promise[_0x6f54('0x95')](_0x465b34);});return Promise[_0x6f54('0x95')](_0x2eec7a);})[_0x6f54('0x47')](function(){return db['TeamVoicePrefix'][_0x6f54('0x73')]({'where':{'teamId':_0x11f8f1}})['then'](function(_0x1fb152){if(_0x1fb152){var _0x36d07b=_['map'](_0x2a8113,function(_0x2e1070){db[_0x6f54('0x71')][_0x6f54('0x96')]({'UserId':_0x2e1070,'VoicePrefixId':_0x1fb152[_0x6f54('0x72')]});});return Promise[_0x6f54('0x95')](_0x36d07b);}});});});return Promise[_0x6f54('0x95')](_0x12ca36)[_0x6f54('0x47')](function(){return _0x232a10[_0x6f54('0x81')](_0x2a8113,{'transaction':_0x4738d7})['spread'](function(_0x2ea842){return _0x2ea842;});});});}})[_0x6f54('0x47')](respondWithResult(_0x247b9b,null))[_0x6f54('0x49')](handleError(_0x247b9b,null));};exports[_0x6f54('0x97')]=function(_0x4add51,_0x5db883){var _0x2505a0=_[_0x6f54('0x82')](_0x4add51['query'][_0x6f54('0x7d')])?_0x4add51[_0x6f54('0x34')][_0x6f54('0x7d')]:[_0x4add51[_0x6f54('0x34')][_0x6f54('0x7d')]];var _0x4ff1b5=_0x4add51[_0x6f54('0x4b')]['id'];return db[_0x6f54('0x46')][_0x6f54('0x4d')]({'where':{'id':_0x4ff1b5}})[_0x6f54('0x47')](handleEntityNotFound(_0x5db883,null))[_0x6f54('0x47')](function(_0x592456){if(_0x592456){var _0x16f38e=[_0x6f54('0x83'),'Chat',_0x6f54('0x98'),_0x6f54('0x85'),_0x6f54('0x86'),_0x6f54('0x87'),_0x6f54('0x99')];return db[_0x6f54('0x5b')]['transaction'](function(_0x14b9b2){var _0x5c446f=_[_0x6f54('0x2e')](_0x16f38e,function(_0x26273d){var _0x3c620b={'attributes':['id','name']};return _0x592456['get'+_0x26273d+_0x6f54('0x7c')](_0x3c620b)[_0x6f54('0x47')](function(_0x2f47f0){var _0x532ade=_[_0x6f54('0x2e')](_0x2f47f0,function(_0x3ba86b){return _0x3ba86b[_0x6f54('0x97')](_0x2505a0,{'transaction':_0x14b9b2})[_0x6f54('0x47')](function(){var _0x557457=_[_0x6f54('0x2e')](_0x2505a0,function(_0x28f31d){socket['emit']('user'+_0x26273d+_0x6f54('0x9a'),{'UserId':Number(_0x28f31d),[_0x26273d+'QueueId']:_0x3ba86b['id']});if(_0x26273d!==_0x6f54('0x83')){return;}return db[_0x6f54('0x92')][_0x6f54('0x5a')]({'where':{'VoiceQueueId':_0x3ba86b['id'],'UserId':_0x28f31d},'transaction':_0x14b9b2});});return Promise[_0x6f54('0x95')](_0x557457);});});return Promise[_0x6f54('0x95')](_0x532ade);});});return Promise['all'](_0x5c446f)[_0x6f54('0x47')](function(){return _0x592456[_0x6f54('0x97')](_0x2505a0,{'transaction':_0x14b9b2});})[_0x6f54('0x47')](function(){return db[_0x6f54('0x70')][_0x6f54('0x73')]({'where':{'teamId':_0x4ff1b5}})['then'](function(_0x57f81d){if(_0x57f81d){var _0x312096=_['map'](_0x2505a0,function(_0x3c72d9){db[_0x6f54('0x71')][_0x6f54('0x5a')]({'where':{'UserId':_0x3c72d9,'VoicePrefixId':_0x57f81d[_0x6f54('0x72')]}});});return Promise[_0x6f54('0x95')](_0x312096);}});});});}})[_0x6f54('0x47')](respondWithStatusCode(_0x5db883,null))[_0x6f54('0x49')](handleError(_0x5db883,null));};exports['getAgents']=function(_0x20ebeb,_0x474449,_0x3ec6bd){var _0x4bc81e={};var _0x185906={};var _0x3e7a24;var _0x258111;return db[_0x6f54('0x46')][_0x6f54('0x73')]({'where':{'id':_0x20ebeb[_0x6f54('0x4b')]['id']}})['then'](handleEntityNotFound(_0x474449,null))[_0x6f54('0x47')](function(_0x2e78d4){if(_0x2e78d4){_0x3e7a24=_0x2e78d4;_0x185906[_0x6f54('0x33')]=_[_0x6f54('0x4c')](db[_0x6f54('0x8c')][_0x6f54('0x2f')]);_0x185906[_0x6f54('0x34')]=_[_0x6f54('0x4c')](_0x20ebeb[_0x6f54('0x34')]);_0x185906[_0x6f54('0x35')]=_[_0x6f54('0x36')](_0x185906[_0x6f54('0x33')],_0x185906[_0x6f54('0x34')]);_0x4bc81e[_0x6f54('0x37')]=_[_0x6f54('0x36')](_0x185906[_0x6f54('0x33')],qs[_0x6f54('0x38')](_0x20ebeb['query'][_0x6f54('0x38')]));_0x4bc81e[_0x6f54('0x37')]=_0x4bc81e['attributes'][_0x6f54('0x39')]?_0x4bc81e[_0x6f54('0x37')]:_0x185906['model'];_0x4bc81e[_0x6f54('0x3c')]=qs['sort'](_0x20ebeb[_0x6f54('0x34')][_0x6f54('0x3d')]);_0x4bc81e['where']=qs[_0x6f54('0x35')](_[_0x6f54('0x3f')](_0x20ebeb[_0x6f54('0x34')],_0x185906['filters']));if(_0x20ebeb['query']['filter']){_0x4bc81e[_0x6f54('0x3e')]=_[_0x6f54('0x40')](_0x4bc81e[_0x6f54('0x3e')],{'$or':_[_0x6f54('0x2e')](_0x4bc81e[_0x6f54('0x37')],function(_0x25f398){var _0x4e41c8={};_0x4e41c8[_0x25f398]={'$like':'%'+_0x20ebeb[_0x6f54('0x34')]['filter']+'%'};return _0x4e41c8;})});}_0x4bc81e=_[_0x6f54('0x40')]({},_0x4bc81e,_0x20ebeb[_0x6f54('0x43')]);return _0x3e7a24['getAgents'](_0x4bc81e);}})[_0x6f54('0x47')](function(_0x20a5ad){if(_0x20a5ad){_0x258111=_0x20a5ad['length'];if(!_0x20ebeb['query'][_0x6f54('0x3a')](_0x6f54('0x3b'))){_0x4bc81e[_0x6f54('0x23')]=qs[_0x6f54('0x23')](_0x20ebeb[_0x6f54('0x34')]['limit']);_0x4bc81e[_0x6f54('0x20')]=qs[_0x6f54('0x20')](_0x20ebeb['query'][_0x6f54('0x20')]);}return _0x3e7a24[_0x6f54('0x9b')](_0x4bc81e);}})[_0x6f54('0x47')](function(_0x3f1560){if(_0x3f1560){return _0x3f1560?{'count':_0x258111,'rows':_0x3f1560}:null;}})[_0x6f54('0x47')](respondWithResult(_0x474449,null))['catch'](handleError(_0x474449,null));};