Built motion from commit 85328c08.|2.5.50
[motion2.git] / server / api / mailQueue / mailQueue.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 _0xbccf=['params','rawAttributes','length','MailQueue','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','describe','UserMailQueue','nolimit','MailQueueId','filter','User','findAndCountAll','UserId','mailPause','createdAt','format','SIP','updatedAt','getTeams','findOne','Team','addTeams','ids','Agents','voicePause','flatMap','sequelize','forEach','emit','removeTeams','tq.TeamId','team_has_mail_queues','join','user_has_teams','ut.TeamId\x20=\x20tq.TeamId','MailQueueId\x20=\x20?','clone','ut.UserId\x20=\x20?','toString','SELECT','TeamId','all','union','compact','value','transaction','removeAgents','userMailQueue:remove','addAgents','omit','spread','userMailQueue:save','isArray','getAgents','eml-format','zip-dir','request-promise','moment','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/license/util','../../mysqldb','redis','localhost','./mailQueue.socket','register','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','save','update','then','destroy','get','MailQueues','UserProfileResource','sendStatus','error','name','send','index','map','type','key','model','query','keys','filters','intersection','fields','attributes','hasOwnProperty','order','sort','where','pick','merge','VIRTUAL','options','includeAll','include','findAll','rows','catch','show'];(function(_0x33f366,_0x131b8b){var _0x249aa5=function(_0x2b3e91){while(--_0x2b3e91){_0x33f366['push'](_0x33f366['shift']());}};_0x249aa5(++_0x131b8b);}(_0xbccf,0x1d2));var _0xfbcc=function(_0x623e7,_0x1bef7d){_0x623e7=_0x623e7-0x0;var _0x121131=_0xbccf[_0x623e7];return _0x121131;};'use strict';var emlformat=require(_0xfbcc('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xfbcc('0x1'));var jsonpatch=require('fast-json-patch');var rp=require(_0xfbcc('0x2'));var moment=require(_0xfbcc('0x3'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0xfbcc('0x4'));var path=require(_0xfbcc('0x5'));var sox=require(_0xfbcc('0x6'));var csv=require(_0xfbcc('0x7'));var ejs=require(_0xfbcc('0x8'));var fs=require('fs');var fs_extra=require(_0xfbcc('0x9'));var _=require(_0xfbcc('0xa'));var squel=require(_0xfbcc('0xb'));var crypto=require(_0xfbcc('0xc'));var jsforce=require(_0xfbcc('0xd'));var deskjs=require(_0xfbcc('0xe'));var toCsv=require(_0xfbcc('0x7'));var querystring=require(_0xfbcc('0xf'));var Papa=require(_0xfbcc('0x10'));var Redis=require(_0xfbcc('0x11'));var authService=require(_0xfbcc('0x12'));var qs=require('../../components/parsers/qs');var as=require(_0xfbcc('0x13'));var hardwareService=require(_0xfbcc('0x14'));var logger=require('../../config/logger')(_0xfbcc('0x15'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0xfbcc('0x16'));var db=require(_0xfbcc('0x17'))['db'];config[_0xfbcc('0x18')]=_['defaults'](config[_0xfbcc('0x18')],{'host':_0xfbcc('0x19'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0xfbcc('0x18')]));require(_0xfbcc('0x1a'))[_0xfbcc('0x1b')](socket);function respondWithStatusCode(_0x42aa92,_0x4d3d5e){_0x4d3d5e=_0x4d3d5e||0xcc;return function(_0xc2e8f8){if(_0xc2e8f8){return _0x42aa92['sendStatus'](_0x4d3d5e);}return _0x42aa92[_0xfbcc('0x1c')](_0x4d3d5e)[_0xfbcc('0x1d')]();};}function respondWithResult(_0x356c05,_0x4e4dd0){_0x4e4dd0=_0x4e4dd0||0xc8;return function(_0x4ef826){if(_0x4ef826){return _0x356c05[_0xfbcc('0x1c')](_0x4e4dd0)[_0xfbcc('0x1e')](_0x4ef826);}};}function respondWithFilteredResult(_0x21f910,_0x3fbaa7){return function(_0x3fe600){if(_0x3fe600){var _0x5d6fcf=typeof _0x3fbaa7[_0xfbcc('0x1f')]===_0xfbcc('0x20')&&typeof _0x3fbaa7[_0xfbcc('0x21')]===_0xfbcc('0x20');var _0x23f0e0=_0x3fe600[_0xfbcc('0x22')];var _0x11cf3d=_0x5d6fcf?0x0:_0x3fbaa7['offset'];var _0x481e6f=_0x5d6fcf?_0x3fe600['count']:_0x3fbaa7['offset']+_0x3fbaa7['limit'];var _0x2c1f80;if(_0x481e6f>=_0x23f0e0){_0x481e6f=_0x23f0e0;_0x2c1f80=0xc8;}else{_0x2c1f80=0xce;}_0x21f910[_0xfbcc('0x1c')](_0x2c1f80);return _0x21f910[_0xfbcc('0x23')](_0xfbcc('0x24'),_0x11cf3d+'-'+_0x481e6f+'/'+_0x23f0e0)[_0xfbcc('0x1e')](_0x3fe600);}return null;};}function patchUpdates(_0x5c9ff4){return function(_0xf5bfd3){try{jsonpatch[_0xfbcc('0x25')](_0xf5bfd3,_0x5c9ff4,!![]);}catch(_0x2c3228){return BPromise['reject'](_0x2c3228);}return _0xf5bfd3[_0xfbcc('0x26')]();};}function saveUpdates(_0x384443,_0x206969){return function(_0x2d6b0c){if(_0x2d6b0c){return _0x2d6b0c[_0xfbcc('0x27')](_0x384443)[_0xfbcc('0x28')](function(_0x56b1fd){return _0x56b1fd;});}return null;};}function removeEntity(_0x48025d,_0xc29958){return function(_0x5cb3e0){if(_0x5cb3e0){return _0x5cb3e0[_0xfbcc('0x29')]()[_0xfbcc('0x28')](function(){var _0x479b0b=_0x5cb3e0[_0xfbcc('0x2a')]({'plain':!![]});var _0x574e63=_0xfbcc('0x2b');return db[_0xfbcc('0x2c')][_0xfbcc('0x29')]({'where':{'type':_0x574e63,'resourceId':_0x479b0b['id']}})[_0xfbcc('0x28')](function(){return _0x5cb3e0;});})[_0xfbcc('0x28')](function(){_0x48025d[_0xfbcc('0x1c')](0xcc)[_0xfbcc('0x1d')]();});}};}function handleEntityNotFound(_0x3bfe8e,_0x31328f){return function(_0x4835cc){if(!_0x4835cc){_0x3bfe8e[_0xfbcc('0x2d')](0x194);}return _0x4835cc;};}function handleError(_0x4af4af,_0x487fea){_0x487fea=_0x487fea||0x1f4;return function(_0x5a9a0a){logger[_0xfbcc('0x2e')](_0x5a9a0a['stack']);if(_0x5a9a0a[_0xfbcc('0x2f')]){delete _0x5a9a0a['name'];}_0x4af4af[_0xfbcc('0x1c')](_0x487fea)[_0xfbcc('0x30')](_0x5a9a0a);};}exports[_0xfbcc('0x31')]=function(_0x511405,_0x4822b6){var _0x4c3fbe={},_0x3ecd37={},_0x3e9a82={'count':0x0,'rows':[]};var _0x417af4=_[_0xfbcc('0x32')](db['MailQueue']['rawAttributes'],function(_0x10363e){return{'name':_0x10363e['fieldName'],'type':_0x10363e[_0xfbcc('0x33')][_0xfbcc('0x34')]};});_0x3ecd37[_0xfbcc('0x35')]=_[_0xfbcc('0x32')](_0x417af4,_0xfbcc('0x2f'));_0x3ecd37[_0xfbcc('0x36')]=_[_0xfbcc('0x37')](_0x511405['query']);_0x3ecd37[_0xfbcc('0x38')]=_[_0xfbcc('0x39')](_0x3ecd37['model'],_0x3ecd37['query']);_0x4c3fbe['attributes']=_['intersection'](_0x3ecd37['model'],qs['fields'](_0x511405[_0xfbcc('0x36')][_0xfbcc('0x3a')]));_0x4c3fbe[_0xfbcc('0x3b')]=_0x4c3fbe[_0xfbcc('0x3b')]['length']?_0x4c3fbe[_0xfbcc('0x3b')]:_0x3ecd37[_0xfbcc('0x35')];if(!_0x511405[_0xfbcc('0x36')][_0xfbcc('0x3c')]('nolimit')){_0x4c3fbe[_0xfbcc('0x21')]=qs[_0xfbcc('0x21')](_0x511405['query'][_0xfbcc('0x21')]);_0x4c3fbe[_0xfbcc('0x1f')]=qs[_0xfbcc('0x1f')](_0x511405[_0xfbcc('0x36')][_0xfbcc('0x1f')]);}_0x4c3fbe[_0xfbcc('0x3d')]=qs[_0xfbcc('0x3e')](_0x511405[_0xfbcc('0x36')][_0xfbcc('0x3e')]);_0x4c3fbe[_0xfbcc('0x3f')]=qs[_0xfbcc('0x38')](_[_0xfbcc('0x40')](_0x511405['query'],_0x3ecd37[_0xfbcc('0x38')]),_0x417af4);if(_0x511405[_0xfbcc('0x36')]['filter']){_0x4c3fbe[_0xfbcc('0x3f')]=_[_0xfbcc('0x41')](_0x4c3fbe['where'],{'$or':_['map'](_0x417af4,function(_0x3d936b){if(_0x3d936b[_0xfbcc('0x33')]!==_0xfbcc('0x42')){var _0x2e8165={};_0x2e8165[_0x3d936b['name']]={'$like':'%'+_0x511405['query']['filter']+'%'};return _0x2e8165;}})});}_0x4c3fbe=_['merge']({},_0x4c3fbe,_0x511405[_0xfbcc('0x43')]);var _0x37e3ee={'where':_0x4c3fbe[_0xfbcc('0x3f')]};return db['MailQueue'][_0xfbcc('0x22')](_0x37e3ee)[_0xfbcc('0x28')](function(_0x6430bc){_0x3e9a82[_0xfbcc('0x22')]=_0x6430bc;if(_0x511405[_0xfbcc('0x36')][_0xfbcc('0x44')]){_0x4c3fbe[_0xfbcc('0x45')]=[{'all':!![]}];}return db['MailQueue'][_0xfbcc('0x46')](_0x4c3fbe);})['then'](function(_0x196409){_0x3e9a82[_0xfbcc('0x47')]=_0x196409;return _0x3e9a82;})['then'](respondWithFilteredResult(_0x4822b6,_0x4c3fbe))[_0xfbcc('0x48')](handleError(_0x4822b6,null));};exports[_0xfbcc('0x49')]=function(_0x216a06,_0x47cc23){var _0x302939={'raw':!![],'where':{'id':_0x216a06[_0xfbcc('0x4a')]['id']}},_0x263822={};_0x263822[_0xfbcc('0x35')]=_['keys'](db['MailQueue'][_0xfbcc('0x4b')]);_0x263822[_0xfbcc('0x36')]=_[_0xfbcc('0x37')](_0x216a06[_0xfbcc('0x36')]);_0x263822[_0xfbcc('0x38')]=_['intersection'](_0x263822[_0xfbcc('0x35')],_0x263822[_0xfbcc('0x36')]);_0x302939[_0xfbcc('0x3b')]=_['intersection'](_0x263822['model'],qs['fields'](_0x216a06[_0xfbcc('0x36')][_0xfbcc('0x3a')]));_0x302939[_0xfbcc('0x3b')]=_0x302939[_0xfbcc('0x3b')][_0xfbcc('0x4c')]?_0x302939['attributes']:_0x263822[_0xfbcc('0x35')];if(_0x216a06['query'][_0xfbcc('0x44')]){_0x302939[_0xfbcc('0x45')]=[{'all':!![]}];}_0x302939=_[_0xfbcc('0x41')]({},_0x302939,_0x216a06[_0xfbcc('0x43')]);return db[_0xfbcc('0x4d')][_0xfbcc('0x4e')](_0x302939)[_0xfbcc('0x28')](handleEntityNotFound(_0x47cc23,null))[_0xfbcc('0x28')](respondWithResult(_0x47cc23,null))['catch'](handleError(_0x47cc23,null));};exports[_0xfbcc('0x4f')]=function(_0x44f724,_0x319383){return db[_0xfbcc('0x4d')][_0xfbcc('0x4f')](_0x44f724[_0xfbcc('0x50')],{})[_0xfbcc('0x28')](function(_0x690224){var _0x44fed0=_0x44f724[_0xfbcc('0x51')][_0xfbcc('0x2a')]({'plain':!![]});if(!_0x44fed0)throw new Error(_0xfbcc('0x52'));if(_0x44fed0['role']===_0xfbcc('0x51')){var _0x148c46=_0x690224[_0xfbcc('0x2a')]({'plain':!![]});var _0x18e27a=_0xfbcc('0x2b');return db[_0xfbcc('0x53')][_0xfbcc('0x4e')]({'where':{'name':_0x18e27a,'userProfileId':_0x44fed0['userProfileId']},'raw':!![]})[_0xfbcc('0x28')](function(_0x594e59){if(_0x594e59&&_0x594e59[_0xfbcc('0x54')]===0x0){return db[_0xfbcc('0x2c')][_0xfbcc('0x4f')]({'name':_0x148c46['name'],'resourceId':_0x148c46['id'],'type':_0x594e59[_0xfbcc('0x2f')],'sectionId':_0x594e59['id']},{})['then'](function(){return _0x690224;});}else{return _0x690224;}})[_0xfbcc('0x48')](function(_0x44a430){logger[_0xfbcc('0x2e')](_0xfbcc('0x55'),_0x44a430);throw _0x44a430;});}return _0x690224;})[_0xfbcc('0x28')](respondWithResult(_0x319383,0xc9))[_0xfbcc('0x48')](handleError(_0x319383,null));};exports[_0xfbcc('0x27')]=function(_0x701a9,_0xfc3aab){if(_0x701a9[_0xfbcc('0x50')]['id']){delete _0x701a9[_0xfbcc('0x50')]['id'];}return db[_0xfbcc('0x4d')][_0xfbcc('0x4e')]({'where':{'id':_0x701a9['params']['id']}})[_0xfbcc('0x28')](handleEntityNotFound(_0xfc3aab,null))['then'](saveUpdates(_0x701a9[_0xfbcc('0x50')],null))['then'](respondWithResult(_0xfc3aab,null))[_0xfbcc('0x48')](handleError(_0xfc3aab,null));};exports[_0xfbcc('0x29')]=function(_0x783ce0,_0x5a25e6){return db[_0xfbcc('0x4d')][_0xfbcc('0x4e')]({'where':{'id':_0x783ce0[_0xfbcc('0x4a')]['id']}})[_0xfbcc('0x28')](handleEntityNotFound(_0x5a25e6,null))[_0xfbcc('0x28')](removeEntity(_0x5a25e6,null))['catch'](handleError(_0x5a25e6,null));};exports[_0xfbcc('0x56')]=function(_0x11fe1f,_0x52a2db){return db['MailQueue'][_0xfbcc('0x56')]()[_0xfbcc('0x28')](respondWithResult(_0x52a2db,null))[_0xfbcc('0x48')](handleError(_0x52a2db,null));};exports['getMembers']=function(_0x55b094,_0x45ec01,_0x64a6d8){var _0x479a2d={'raw':!![],'where':{}},_0x5b87f3={},_0x3197b1;return db[_0xfbcc('0x4d')]['findOne']({'where':{'id':_0x55b094[_0xfbcc('0x4a')]['id']}})[_0xfbcc('0x28')](handleEntityNotFound(_0x45ec01,null))['then'](function(_0x25245c){if(_0x25245c){_0x3197b1=_0x25245c[_0xfbcc('0x2a')]({'plain':!![]});_0x5b87f3[_0xfbcc('0x35')]=_[_0xfbcc('0x37')](db[_0xfbcc('0x57')][_0xfbcc('0x4b')]);_0x5b87f3[_0xfbcc('0x36')]=_[_0xfbcc('0x37')](_0x55b094['query']);_0x5b87f3[_0xfbcc('0x38')]=_[_0xfbcc('0x39')](_0x5b87f3[_0xfbcc('0x35')],_0x5b87f3[_0xfbcc('0x36')]);_0x479a2d[_0xfbcc('0x3b')]=_[_0xfbcc('0x39')](_0x5b87f3[_0xfbcc('0x35')],qs[_0xfbcc('0x3a')](_0x55b094['query']['fields']));_0x479a2d[_0xfbcc('0x3b')]=_0x479a2d[_0xfbcc('0x3b')][_0xfbcc('0x4c')]?_0x479a2d[_0xfbcc('0x3b')]:_0x5b87f3['model'];if(!_0x55b094['query'][_0xfbcc('0x3c')](_0xfbcc('0x58'))){_0x479a2d[_0xfbcc('0x21')]=qs[_0xfbcc('0x21')](_0x55b094[_0xfbcc('0x36')][_0xfbcc('0x21')]);_0x479a2d[_0xfbcc('0x1f')]=qs[_0xfbcc('0x1f')](_0x55b094[_0xfbcc('0x36')][_0xfbcc('0x1f')]);}_0x479a2d[_0xfbcc('0x3d')]=qs[_0xfbcc('0x3e')](_0x55b094[_0xfbcc('0x36')][_0xfbcc('0x3e')]);_0x479a2d[_0xfbcc('0x3f')]=qs['filters'](_['pick'](_0x55b094['query'],_0x5b87f3[_0xfbcc('0x38')]));_0x479a2d[_0xfbcc('0x3f')][_0xfbcc('0x59')]=_0x25245c['id'];if(_0x55b094[_0xfbcc('0x36')][_0xfbcc('0x5a')]){_0x479a2d['where']=_[_0xfbcc('0x41')](_0x479a2d[_0xfbcc('0x3f')],{'$or':_[_0xfbcc('0x32')](_0x479a2d[_0xfbcc('0x3b')],function(_0x3c5f67){var _0x2f1afb={};_0x2f1afb[_0x3c5f67]={'$like':'%'+_0x55b094[_0xfbcc('0x36')][_0xfbcc('0x5a')]+'%'};return _0x2f1afb;})});}_0x479a2d=_[_0xfbcc('0x41')]({},_0x479a2d,_0x55b094[_0xfbcc('0x43')]);return db[_0xfbcc('0x57')][_0xfbcc('0x46')](_0x479a2d);}})['then'](function(_0x325914){if(_0x325914){return db[_0xfbcc('0x5b')][_0xfbcc('0x5c')]({'where':{'id':_[_0xfbcc('0x32')](_0x325914,_0xfbcc('0x5d')),'role':'agent'},'attributes':['id','name',_0xfbcc('0x5e'),'updatedAt',_0xfbcc('0x5f')]});}})[_0xfbcc('0x28')](function(_0x4bb34b){if(_0x4bb34b){return{'count':_0x4bb34b['count'],'rows':_[_0xfbcc('0x32')](_0x4bb34b[_0xfbcc('0x47')],function(_0x30116e){return{'membername':_0x30116e['name'],'UserId':_0x30116e['id'],'queue_name':_0x3197b1['name'],'MailQueueId':_0x3197b1['id'],'interface':util[_0xfbcc('0x60')]('%s/%s',_0xfbcc('0x61'),_0x30116e[_0xfbcc('0x2f')]),'penalty':0x0,'paused':_0x30116e[_0xfbcc('0x5e')],'createdAt':_0x30116e[_0xfbcc('0x5f')],'updatedAt':_0x30116e[_0xfbcc('0x62')]};})};}else{return{'count':0x0,'rows':[]};}})['then'](respondWithFilteredResult(_0x45ec01,_0x479a2d))[_0xfbcc('0x48')](handleError(_0x45ec01,null));};exports[_0xfbcc('0x63')]=function(_0x2efd37,_0xe8e81a,_0x495a0d){var _0x168e66={};var _0x5954e4={};var _0x2e6a15;var _0x371702;return db[_0xfbcc('0x4d')][_0xfbcc('0x64')]({'where':{'id':_0x2efd37['params']['id']}})[_0xfbcc('0x28')](handleEntityNotFound(_0xe8e81a,null))[_0xfbcc('0x28')](function(_0x2ba93c){if(_0x2ba93c){_0x2e6a15=_0x2ba93c;_0x5954e4[_0xfbcc('0x35')]=_[_0xfbcc('0x37')](db[_0xfbcc('0x65')]['rawAttributes']);_0x5954e4[_0xfbcc('0x36')]=_[_0xfbcc('0x37')](_0x2efd37['query']);_0x5954e4[_0xfbcc('0x38')]=_['intersection'](_0x5954e4[_0xfbcc('0x35')],_0x5954e4[_0xfbcc('0x36')]);_0x168e66[_0xfbcc('0x3b')]=_[_0xfbcc('0x39')](_0x5954e4[_0xfbcc('0x35')],qs[_0xfbcc('0x3a')](_0x2efd37[_0xfbcc('0x36')][_0xfbcc('0x3a')]));_0x168e66[_0xfbcc('0x3b')]=_0x168e66[_0xfbcc('0x3b')][_0xfbcc('0x4c')]?_0x168e66[_0xfbcc('0x3b')]:_0x5954e4[_0xfbcc('0x35')];_0x168e66['order']=qs[_0xfbcc('0x3e')](_0x2efd37['query'][_0xfbcc('0x3e')]);_0x168e66[_0xfbcc('0x3f')]=qs['filters'](_[_0xfbcc('0x40')](_0x2efd37['query'],_0x5954e4[_0xfbcc('0x38')]));if(_0x2efd37[_0xfbcc('0x36')]['filter']){_0x168e66[_0xfbcc('0x3f')]=_[_0xfbcc('0x41')](_0x168e66[_0xfbcc('0x3f')],{'$or':_[_0xfbcc('0x32')](_0x168e66[_0xfbcc('0x3b')],function(_0x11095b){var _0x49a93b={};_0x49a93b[_0x11095b]={'$like':'%'+_0x2efd37['query']['filter']+'%'};return _0x49a93b;})});}_0x168e66=_[_0xfbcc('0x41')]({},_0x168e66,_0x2efd37['options']);return _0x2e6a15['getTeams'](_0x168e66);}})[_0xfbcc('0x28')](function(_0x8daad5){if(_0x8daad5){_0x371702=_0x8daad5[_0xfbcc('0x4c')];if(!_0x2efd37[_0xfbcc('0x36')]['hasOwnProperty'](_0xfbcc('0x58'))){_0x168e66[_0xfbcc('0x21')]=qs['limit'](_0x2efd37[_0xfbcc('0x36')]['limit']);_0x168e66['offset']=qs[_0xfbcc('0x1f')](_0x2efd37[_0xfbcc('0x36')][_0xfbcc('0x1f')]);}return _0x2e6a15[_0xfbcc('0x63')](_0x168e66);}})[_0xfbcc('0x28')](function(_0xd18b5e){if(_0xd18b5e){return _0xd18b5e?{'count':_0x371702,'rows':_0xd18b5e}:null;}})[_0xfbcc('0x28')](respondWithResult(_0xe8e81a,null))[_0xfbcc('0x48')](handleError(_0xe8e81a,null));};exports[_0xfbcc('0x66')]=function(_0x54c24c,_0x52c823,_0x126085){var _0x13b221=_0x54c24c[_0xfbcc('0x50')][_0xfbcc('0x67')];return db['Team']['findAll']({'where':{'id':_0x13b221},'attributes':['id'],'include':[{'model':db[_0xfbcc('0x5b')],'as':_0xfbcc('0x68'),'attributes':['id',_0xfbcc('0x2f'),'online',_0xfbcc('0x69'),'interface'],'raw':!![]}]})['then'](function(_0x3e944c){if(_0x3e944c){var _0x52ff21=_[_0xfbcc('0x6a')](_0x3e944c,function(_0x323f6d){var _0x49f286=_0x323f6d[_0xfbcc('0x2a')]({'plain':!![]});return _0x49f286['Agents'];});return db[_0xfbcc('0x4d')][_0xfbcc('0x4e')]({'where':{'id':_0x54c24c[_0xfbcc('0x4a')]['id']}})['then'](function(_0x2f68d6){return db[_0xfbcc('0x6b')]['transaction'](function(_0x16302a){return _0x2f68d6[_0xfbcc('0x66')](_0x13b221,{'transaction':_0x16302a})['then'](function(){return BPromise['each'](_0x52ff21,function(_0x266fa2){return db['UserMailQueue']['findOrCreate']({'where':{'UserId':_0x266fa2['id'],'MailQueueId':_0x54c24c[_0xfbcc('0x4a')]['id']},'transaction':_0x16302a});});})[_0xfbcc('0x28')](function(){_0x52ff21[_0xfbcc('0x6c')](function(_0x489560){socket[_0xfbcc('0x6d')]('userMailQueue:save',{'UserId':_0x489560['id'],'MailQueueId':_0x2f68d6['id']});});});});});}})[_0xfbcc('0x28')](respondWithStatusCode(_0x52c823,null))[_0xfbcc('0x48')](handleError(_0x52c823,null));};exports[_0xfbcc('0x6e')]=function(_0x490c8c,_0x477a0b,_0x15ec98){return db[_0xfbcc('0x65')][_0xfbcc('0x46')]({'where':{'id':_0x490c8c[_0xfbcc('0x36')][_0xfbcc('0x67')]},'attributes':['id'],'include':[{'model':db[_0xfbcc('0x5b')],'as':_0xfbcc('0x68'),'attributes':['id'],'raw':!![]}]})[_0xfbcc('0x28')](handleEntityNotFound(_0x477a0b,null))[_0xfbcc('0x28')](function(_0x4ab631){var _0x59b80f=_['map'](_0x4ab631,'id');var _0x1dac6a=[];var _0x3adc2d=[];var _0x44f007=squel['select']();_0x44f007['field'](_0xfbcc('0x6f'))['from'](_0xfbcc('0x70'),'tq')[_0xfbcc('0x71')](_0xfbcc('0x72'),'ut',_0xfbcc('0x73'))[_0xfbcc('0x3f')](_0xfbcc('0x74'),_0x490c8c[_0xfbcc('0x4a')]['id']);for(var _0x27f4a0=0x0;_0x27f4a0<_0x4ab631[_0xfbcc('0x4c')];_0x27f4a0+=0x1){let _0x4f6f35=_0x4ab631[_0x27f4a0];for(var _0x4d86ad=0x0;_0x4d86ad<_0x4f6f35['Agents'][_0xfbcc('0x4c')];_0x4d86ad+=0x1){let _0x1c02dd=_0x4f6f35[_0xfbcc('0x68')][_0x4d86ad];var _0x330bde=_0x44f007[_0xfbcc('0x75')]();_0x330bde[_0xfbcc('0x3f')](_0xfbcc('0x76'),_0x1c02dd['id']);_0x3adc2d['push'](db[_0xfbcc('0x6b')][_0xfbcc('0x36')](_0x330bde[_0xfbcc('0x77')](),{'type':db['sequelize']['QueryTypes'][_0xfbcc('0x78')]})[_0xfbcc('0x28')](function(_0x176a6e){if(_0x176a6e[_0xfbcc('0x4c')]===0x1){return _0x1c02dd['id'];}else{var _0x3cb709=_['every'](_[_0xfbcc('0x32')](_0x176a6e,_0xfbcc('0x79')),function(_0x478a74){return _['includes'](_0x59b80f,_0x478a74);});if(_0x3cb709){return _0x1c02dd['id'];}}}));}}return BPromise[_0xfbcc('0x7a')](_0x3adc2d)[_0xfbcc('0x28')](function(_0x283fe6){_0x1dac6a=_(_0x1dac6a)[_0xfbcc('0x7b')](_0x283fe6)[_0xfbcc('0x7c')]()[_0xfbcc('0x7d')]();return db[_0xfbcc('0x4d')][_0xfbcc('0x4e')]({'where':{'id':_0x490c8c[_0xfbcc('0x4a')]['id']}})['then'](function(_0x297b2a){return db[_0xfbcc('0x6b')][_0xfbcc('0x7e')](function(_0x85d2a3){return _0x297b2a['removeTeams'](_0x490c8c[_0xfbcc('0x36')]['ids'],{'transaction':_0x85d2a3})['then'](function(){if(!_['isEmpty'](_0x1dac6a)){return _0x297b2a[_0xfbcc('0x7f')](_0x1dac6a,{'transaction':_0x85d2a3});}})[_0xfbcc('0x28')](function(){_0x1dac6a[_0xfbcc('0x6c')](function(_0x5c81e7){socket['emit'](_0xfbcc('0x80'),{'UserId':_0x5c81e7,'MailQueueId':_0x297b2a['id']});});});});});});})[_0xfbcc('0x28')](respondWithStatusCode(_0x477a0b,null))[_0xfbcc('0x48')](handleError(_0x477a0b,null));};exports[_0xfbcc('0x81')]=function(_0x1d82bb,_0x4c33b0,_0x4db2d5){return db['MailQueue'][_0xfbcc('0x4e')]({'where':{'id':_0x1d82bb['params']['id']}})[_0xfbcc('0x28')](handleEntityNotFound(_0x4c33b0,null))[_0xfbcc('0x28')](function(_0x22ba57){if(_0x22ba57){return _0x22ba57['addAgents'](_0x1d82bb[_0xfbcc('0x50')][_0xfbcc('0x67')],_[_0xfbcc('0x82')](_0x1d82bb[_0xfbcc('0x50')],[_0xfbcc('0x67'),'id'])||{})[_0xfbcc('0x83')](function(_0xf59f1e){for(var _0x5007e7=0x0;_0x5007e7<_0x1d82bb['body']['ids'][_0xfbcc('0x4c')];_0x5007e7+=0x1){socket[_0xfbcc('0x6d')](_0xfbcc('0x84'),{'UserId':Number(_0x1d82bb[_0xfbcc('0x50')][_0xfbcc('0x67')][_0x5007e7]),'MailQueueId':Number(_0x1d82bb[_0xfbcc('0x4a')]['id'])});}return _0xf59f1e;});}})[_0xfbcc('0x28')](respondWithResult(_0x4c33b0,null))[_0xfbcc('0x48')](handleError(_0x4c33b0,null));};exports['removeAgents']=function(_0x3be525,_0x15ca54,_0x1b61ab){return db[_0xfbcc('0x4d')][_0xfbcc('0x4e')]({'where':{'id':_0x3be525[_0xfbcc('0x4a')]['id']}})[_0xfbcc('0x28')](handleEntityNotFound(_0x15ca54,null))[_0xfbcc('0x28')](function(_0x2a4063){if(_0x2a4063){return _0x2a4063[_0xfbcc('0x7f')](_0x3be525[_0xfbcc('0x36')][_0xfbcc('0x67')])['then'](function(){if(_[_0xfbcc('0x85')](_0x3be525[_0xfbcc('0x36')][_0xfbcc('0x67')])){for(var _0x8ce0e6=0x0;_0x8ce0e6<_0x3be525[_0xfbcc('0x36')][_0xfbcc('0x67')][_0xfbcc('0x4c')];_0x8ce0e6+=0x1){socket[_0xfbcc('0x6d')](_0xfbcc('0x80'),{'UserId':Number(_0x3be525['query']['ids'][_0x8ce0e6]),'MailQueueId':Number(_0x3be525[_0xfbcc('0x4a')]['id'])});}}else{socket['emit']('userMailQueue:remove',{'UserId':Number(_0x3be525['query'][_0xfbcc('0x67')]),'MailQueueId':Number(_0x3be525[_0xfbcc('0x4a')]['id'])});}});}})[_0xfbcc('0x28')](respondWithStatusCode(_0x15ca54,null))[_0xfbcc('0x48')](handleError(_0x15ca54,null));};exports['getAgents']=function(_0x3ef7ed,_0x32846c,_0x21806b){var _0x262053={};var _0x387d19={};var _0xa96a7c;var _0x3566f2;return db[_0xfbcc('0x4d')][_0xfbcc('0x64')]({'where':{'id':_0x3ef7ed[_0xfbcc('0x4a')]['id']}})[_0xfbcc('0x28')](handleEntityNotFound(_0x32846c,null))[_0xfbcc('0x28')](function(_0x36c36e){if(_0x36c36e){_0xa96a7c=_0x36c36e;_0x387d19[_0xfbcc('0x35')]=_[_0xfbcc('0x37')](db['User']['rawAttributes']);_0x387d19['query']=_[_0xfbcc('0x37')](_0x3ef7ed[_0xfbcc('0x36')]);_0x387d19[_0xfbcc('0x38')]=_[_0xfbcc('0x39')](_0x387d19[_0xfbcc('0x35')],_0x387d19[_0xfbcc('0x36')]);_0x262053[_0xfbcc('0x3b')]=_[_0xfbcc('0x39')](_0x387d19[_0xfbcc('0x35')],qs[_0xfbcc('0x3a')](_0x3ef7ed['query'][_0xfbcc('0x3a')]));_0x262053[_0xfbcc('0x3b')]=_0x262053[_0xfbcc('0x3b')][_0xfbcc('0x4c')]?_0x262053[_0xfbcc('0x3b')]:_0x387d19[_0xfbcc('0x35')];_0x262053['order']=qs[_0xfbcc('0x3e')](_0x3ef7ed[_0xfbcc('0x36')][_0xfbcc('0x3e')]);_0x262053[_0xfbcc('0x3f')]=qs[_0xfbcc('0x38')](_[_0xfbcc('0x40')](_0x3ef7ed[_0xfbcc('0x36')],_0x387d19['filters']));if(_0x3ef7ed[_0xfbcc('0x36')][_0xfbcc('0x5a')]){_0x262053[_0xfbcc('0x3f')]=_[_0xfbcc('0x41')](_0x262053[_0xfbcc('0x3f')],{'$or':_[_0xfbcc('0x32')](_0x262053[_0xfbcc('0x3b')],function(_0x97e3e7){var _0x591e6c={};_0x591e6c[_0x97e3e7]={'$like':'%'+_0x3ef7ed[_0xfbcc('0x36')][_0xfbcc('0x5a')]+'%'};return _0x591e6c;})});}_0x262053=_[_0xfbcc('0x41')]({},_0x262053,_0x3ef7ed[_0xfbcc('0x43')]);return _0xa96a7c['getAgents'](_0x262053);}})[_0xfbcc('0x28')](function(_0x5df623){if(_0x5df623){_0x3566f2=_0x5df623['length'];if(!_0x3ef7ed[_0xfbcc('0x36')][_0xfbcc('0x3c')](_0xfbcc('0x58'))){_0x262053[_0xfbcc('0x21')]=qs[_0xfbcc('0x21')](_0x3ef7ed[_0xfbcc('0x36')]['limit']);_0x262053[_0xfbcc('0x1f')]=qs[_0xfbcc('0x1f')](_0x3ef7ed['query'][_0xfbcc('0x1f')]);}return _0xa96a7c[_0xfbcc('0x86')](_0x262053);}})[_0xfbcc('0x28')](function(_0x62da34){if(_0x62da34){return _0x62da34?{'count':_0x3566f2,'rows':_0x62da34}:null;}})[_0xfbcc('0x28')](respondWithResult(_0x32846c,null))[_0xfbcc('0x48')](handleError(_0x32846c,null));};