Built motion from commit (unavailable).|2.5.31
[motion2.git] / server / api / whatsappQueue / whatsappQueue.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 _0xf3fb=['getMembers','findOne','keys','UserWhatsappQueue','WhatsappQueueId','agent','whatsappPause','updatedAt','format','SIP','createdAt','getTeams','Team','addTeams','ids','findAll','User','voicePause','interface','flatMap','Agents','sequelize','transaction','each','forEach','emit','userWhatsappQueue:save','removeTeams','select','field','tq.TeamId','team_has_whatsapp_queues','join','user_has_teams','clone','toString','QueryTypes','SELECT','every','TeamId','includes','all','compact','value','isEmpty','removeAgents','userWhatsappQueue:remove','addAgents','spread','getAgents','eml-format','zip-dir','fast-json-patch','moment','bluebird','mustache','util','sox','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/license/util','../../mysqldb','defaults','redis','socket.io-emitter','./whatsappQueue.socket','register','status','end','offset','limit','undefined','count','set','json','reject','update','then','destroy','get','error','stack','name','index','WhatsappQueue','rawAttributes','key','model','map','query','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','filters','pick','merge','filter','includeAll','rows','show','params','options','find','catch','create','body','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','user','WhatsappQueues','UserProfileSection','userProfileId','UserProfileResource','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','describe'];(function(_0x21a9d9,_0x4d5ac8){var _0x46adce=function(_0xfdd336){while(--_0xfdd336){_0x21a9d9['push'](_0x21a9d9['shift']());}};_0x46adce(++_0x4d5ac8);}(_0xf3fb,0x13e));var _0xbf3f=function(_0x43a251,_0x2ca1b8){_0x43a251=_0x43a251-0x0;var _0x15f782=_0xf3fb[_0x43a251];return _0x15f782;};'use strict';var emlformat=require(_0xbf3f('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xbf3f('0x1'));var jsonpatch=require(_0xbf3f('0x2'));var rp=require('request-promise');var moment=require(_0xbf3f('0x3'));var BPromise=require(_0xbf3f('0x4'));var Mustache=require(_0xbf3f('0x5'));var util=require(_0xbf3f('0x6'));var path=require('path');var sox=require(_0xbf3f('0x7'));var csv=require('to-csv');var ejs=require(_0xbf3f('0x8'));var fs=require('fs');var fs_extra=require(_0xbf3f('0x9'));var _=require(_0xbf3f('0xa'));var squel=require(_0xbf3f('0xb'));var crypto=require(_0xbf3f('0xc'));var jsforce=require(_0xbf3f('0xd'));var deskjs=require(_0xbf3f('0xe'));var toCsv=require('to-csv');var querystring=require(_0xbf3f('0xf'));var Papa=require(_0xbf3f('0x10'));var Redis=require('ioredis');var authService=require(_0xbf3f('0x11'));var qs=require(_0xbf3f('0x12'));var as=require(_0xbf3f('0x13'));var hardwareService=require(_0xbf3f('0x14'));var logger=require(_0xbf3f('0x15'))(_0xbf3f('0x16'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0xbf3f('0x17'));var db=require(_0xbf3f('0x18'))['db'];config['redis']=_[_0xbf3f('0x19')](config[_0xbf3f('0x1a')],{'host':'localhost','port':0x18eb});var socket=require(_0xbf3f('0x1b'))(new Redis(config[_0xbf3f('0x1a')]));require(_0xbf3f('0x1c'))[_0xbf3f('0x1d')](socket);function respondWithStatusCode(_0x925b51,_0x52a2c2){_0x52a2c2=_0x52a2c2||0xcc;return function(_0x50b08a){if(_0x50b08a){return _0x925b51['sendStatus'](_0x52a2c2);}return _0x925b51[_0xbf3f('0x1e')](_0x52a2c2)[_0xbf3f('0x1f')]();};}function respondWithResult(_0x23447f,_0x1879e1){_0x1879e1=_0x1879e1||0xc8;return function(_0x24b255){if(_0x24b255){return _0x23447f[_0xbf3f('0x1e')](_0x1879e1)['json'](_0x24b255);}};}function respondWithFilteredResult(_0x977f6f,_0x3dc20f){return function(_0x323e69){if(_0x323e69){var _0x499c2a=typeof _0x3dc20f[_0xbf3f('0x20')]==='undefined'&&typeof _0x3dc20f[_0xbf3f('0x21')]===_0xbf3f('0x22');var _0x43d200=_0x323e69[_0xbf3f('0x23')];var _0x5da1eb=_0x499c2a?0x0:_0x3dc20f['offset'];var _0x5a2508=_0x499c2a?_0x323e69[_0xbf3f('0x23')]:_0x3dc20f[_0xbf3f('0x20')]+_0x3dc20f[_0xbf3f('0x21')];var _0x1df70e;if(_0x5a2508>=_0x43d200){_0x5a2508=_0x43d200;_0x1df70e=0xc8;}else{_0x1df70e=0xce;}_0x977f6f[_0xbf3f('0x1e')](_0x1df70e);return _0x977f6f[_0xbf3f('0x24')]('Content-Range',_0x5da1eb+'-'+_0x5a2508+'/'+_0x43d200)[_0xbf3f('0x25')](_0x323e69);}return null;};}function patchUpdates(_0x5601ec){return function(_0x108c74){try{jsonpatch['apply'](_0x108c74,_0x5601ec,!![]);}catch(_0x1a6120){return BPromise[_0xbf3f('0x26')](_0x1a6120);}return _0x108c74['save']();};}function saveUpdates(_0x19d78f,_0xbc83da){return function(_0xf13616){if(_0xf13616){return _0xf13616[_0xbf3f('0x27')](_0x19d78f)[_0xbf3f('0x28')](function(_0x75896b){return _0x75896b;});}return null;};}function removeEntity(_0x5cd83d,_0x57d13f){return function(_0x341ee6){if(_0x341ee6){return _0x341ee6[_0xbf3f('0x29')]()['then'](function(){var _0x2d3e8f=_0x341ee6[_0xbf3f('0x2a')]({'plain':!![]});var _0xa48414='WhatsappQueues';return db['UserProfileResource'][_0xbf3f('0x29')]({'where':{'type':_0xa48414,'resourceId':_0x2d3e8f['id']}})[_0xbf3f('0x28')](function(){return _0x341ee6;});})[_0xbf3f('0x28')](function(){_0x5cd83d[_0xbf3f('0x1e')](0xcc)[_0xbf3f('0x1f')]();});}};}function handleEntityNotFound(_0x2cd256,_0x3b3215){return function(_0x48ad13){if(!_0x48ad13){_0x2cd256['sendStatus'](0x194);}return _0x48ad13;};}function handleError(_0x19aef8,_0x4a0866){_0x4a0866=_0x4a0866||0x1f4;return function(_0xe49d9a){logger[_0xbf3f('0x2b')](_0xe49d9a[_0xbf3f('0x2c')]);if(_0xe49d9a[_0xbf3f('0x2d')]){delete _0xe49d9a['name'];}_0x19aef8[_0xbf3f('0x1e')](_0x4a0866)['send'](_0xe49d9a);};}exports[_0xbf3f('0x2e')]=function(_0x2b9659,_0x1b2f33){var _0x55c191={},_0x47e127={},_0xd5322b={'count':0x0,'rows':[]};var _0x5430ce=_['map'](db[_0xbf3f('0x2f')][_0xbf3f('0x30')],function(_0x8ca4dd){return{'name':_0x8ca4dd['fieldName'],'type':_0x8ca4dd['type'][_0xbf3f('0x31')]};});_0x47e127[_0xbf3f('0x32')]=_[_0xbf3f('0x33')](_0x5430ce,_0xbf3f('0x2d'));_0x47e127[_0xbf3f('0x34')]=_['keys'](_0x2b9659[_0xbf3f('0x34')]);_0x47e127['filters']=_[_0xbf3f('0x35')](_0x47e127[_0xbf3f('0x32')],_0x47e127['query']);_0x55c191[_0xbf3f('0x36')]=_['intersection'](_0x47e127['model'],qs[_0xbf3f('0x37')](_0x2b9659[_0xbf3f('0x34')][_0xbf3f('0x37')]));_0x55c191[_0xbf3f('0x36')]=_0x55c191[_0xbf3f('0x36')][_0xbf3f('0x38')]?_0x55c191[_0xbf3f('0x36')]:_0x47e127[_0xbf3f('0x32')];if(!_0x2b9659['query'][_0xbf3f('0x39')](_0xbf3f('0x3a'))){_0x55c191[_0xbf3f('0x21')]=qs[_0xbf3f('0x21')](_0x2b9659['query']['limit']);_0x55c191['offset']=qs[_0xbf3f('0x20')](_0x2b9659[_0xbf3f('0x34')][_0xbf3f('0x20')]);}_0x55c191[_0xbf3f('0x3b')]=qs[_0xbf3f('0x3c')](_0x2b9659[_0xbf3f('0x34')][_0xbf3f('0x3c')]);_0x55c191[_0xbf3f('0x3d')]=qs[_0xbf3f('0x3e')](_[_0xbf3f('0x3f')](_0x2b9659[_0xbf3f('0x34')],_0x47e127[_0xbf3f('0x3e')]),_0x5430ce);if(_0x2b9659[_0xbf3f('0x34')]['filter']){_0x55c191[_0xbf3f('0x3d')]=_[_0xbf3f('0x40')](_0x55c191[_0xbf3f('0x3d')],{'$or':_[_0xbf3f('0x33')](_0x5430ce,function(_0xa249fe){if(_0xa249fe['type']!=='VIRTUAL'){var _0x1bc30b={};_0x1bc30b[_0xa249fe[_0xbf3f('0x2d')]]={'$like':'%'+_0x2b9659[_0xbf3f('0x34')][_0xbf3f('0x41')]+'%'};return _0x1bc30b;}})});}_0x55c191=_[_0xbf3f('0x40')]({},_0x55c191,_0x2b9659['options']);var _0x5153cb={'where':_0x55c191[_0xbf3f('0x3d')]};return db[_0xbf3f('0x2f')][_0xbf3f('0x23')](_0x5153cb)['then'](function(_0x363962){_0xd5322b[_0xbf3f('0x23')]=_0x363962;if(_0x2b9659[_0xbf3f('0x34')][_0xbf3f('0x42')]){_0x55c191['include']=[{'all':!![]}];}return db[_0xbf3f('0x2f')]['findAll'](_0x55c191);})[_0xbf3f('0x28')](function(_0x143d06){_0xd5322b[_0xbf3f('0x43')]=_0x143d06;return _0xd5322b;})[_0xbf3f('0x28')](respondWithFilteredResult(_0x1b2f33,_0x55c191))['catch'](handleError(_0x1b2f33,null));};exports[_0xbf3f('0x44')]=function(_0x4b6d05,_0x4c0183){var _0x524d61={'raw':!![],'where':{'id':_0x4b6d05[_0xbf3f('0x45')]['id']}},_0x4fdab6={};_0x4fdab6[_0xbf3f('0x32')]=_['keys'](db[_0xbf3f('0x2f')]['rawAttributes']);_0x4fdab6[_0xbf3f('0x34')]=_['keys'](_0x4b6d05[_0xbf3f('0x34')]);_0x4fdab6[_0xbf3f('0x3e')]=_[_0xbf3f('0x35')](_0x4fdab6[_0xbf3f('0x32')],_0x4fdab6[_0xbf3f('0x34')]);_0x524d61['attributes']=_[_0xbf3f('0x35')](_0x4fdab6[_0xbf3f('0x32')],qs[_0xbf3f('0x37')](_0x4b6d05['query'][_0xbf3f('0x37')]));_0x524d61[_0xbf3f('0x36')]=_0x524d61[_0xbf3f('0x36')][_0xbf3f('0x38')]?_0x524d61['attributes']:_0x4fdab6[_0xbf3f('0x32')];if(_0x4b6d05['query'][_0xbf3f('0x42')]){_0x524d61['include']=[{'all':!![]}];}_0x524d61=_[_0xbf3f('0x40')]({},_0x524d61,_0x4b6d05[_0xbf3f('0x46')]);return db[_0xbf3f('0x2f')][_0xbf3f('0x47')](_0x524d61)['then'](handleEntityNotFound(_0x4c0183,null))[_0xbf3f('0x28')](respondWithResult(_0x4c0183,null))[_0xbf3f('0x48')](handleError(_0x4c0183,null));};exports[_0xbf3f('0x49')]=function(_0x50565f,_0x7ea169){return db[_0xbf3f('0x2f')][_0xbf3f('0x49')](_0x50565f[_0xbf3f('0x4a')],{})[_0xbf3f('0x28')](function(_0x46e312){var _0x8af39f=_0x50565f['user'][_0xbf3f('0x2a')]({'plain':!![]});if(!_0x8af39f)throw new Error(_0xbf3f('0x4b'));if(_0x8af39f[_0xbf3f('0x4c')]===_0xbf3f('0x4d')){var _0x218f51=_0x46e312[_0xbf3f('0x2a')]({'plain':!![]});var _0x516c7d=_0xbf3f('0x4e');return db[_0xbf3f('0x4f')]['find']({'where':{'name':_0x516c7d,'userProfileId':_0x8af39f[_0xbf3f('0x50')]},'raw':!![]})[_0xbf3f('0x28')](function(_0x32594e){if(_0x32594e&&_0x32594e['autoAssociation']===0x0){return db[_0xbf3f('0x51')][_0xbf3f('0x49')]({'name':_0x218f51[_0xbf3f('0x2d')],'resourceId':_0x218f51['id'],'type':_0x32594e[_0xbf3f('0x2d')],'sectionId':_0x32594e['id']},{})['then'](function(){return _0x46e312;});}else{return _0x46e312;}})[_0xbf3f('0x48')](function(_0x17d30c){logger[_0xbf3f('0x2b')](_0xbf3f('0x52'),_0x17d30c);throw _0x17d30c;});}return _0x46e312;})[_0xbf3f('0x28')](respondWithResult(_0x7ea169,0xc9))[_0xbf3f('0x48')](handleError(_0x7ea169,null));};exports[_0xbf3f('0x27')]=function(_0x54171b,_0x3e5042){if(_0x54171b[_0xbf3f('0x4a')]['id']){delete _0x54171b[_0xbf3f('0x4a')]['id'];}return db['WhatsappQueue'][_0xbf3f('0x47')]({'where':{'id':_0x54171b['params']['id']}})[_0xbf3f('0x28')](handleEntityNotFound(_0x3e5042,null))[_0xbf3f('0x28')](saveUpdates(_0x54171b['body'],null))[_0xbf3f('0x28')](respondWithResult(_0x3e5042,null))['catch'](handleError(_0x3e5042,null));};exports['destroy']=function(_0xd3958b,_0x5cbe0f){return db[_0xbf3f('0x2f')][_0xbf3f('0x47')]({'where':{'id':_0xd3958b[_0xbf3f('0x45')]['id']}})['then'](handleEntityNotFound(_0x5cbe0f,null))[_0xbf3f('0x28')](removeEntity(_0x5cbe0f,null))['catch'](handleError(_0x5cbe0f,null));};exports[_0xbf3f('0x53')]=function(_0x330467,_0x28c564){return db[_0xbf3f('0x2f')]['describe']()['then'](respondWithResult(_0x28c564,null))['catch'](handleError(_0x28c564,null));};exports[_0xbf3f('0x54')]=function(_0xe5d771,_0x4496d8,_0x16d0bf){var _0x232cab={'raw':!![],'where':{}},_0x4f1c2c={},_0x349765;return db['WhatsappQueue'][_0xbf3f('0x55')]({'where':{'id':_0xe5d771[_0xbf3f('0x45')]['id']}})[_0xbf3f('0x28')](handleEntityNotFound(_0x4496d8,null))[_0xbf3f('0x28')](function(_0x40ae5d){if(_0x40ae5d){_0x349765=_0x40ae5d[_0xbf3f('0x2a')]({'plain':!![]});_0x4f1c2c[_0xbf3f('0x32')]=_[_0xbf3f('0x56')](db[_0xbf3f('0x57')]['rawAttributes']);_0x4f1c2c['query']=_[_0xbf3f('0x56')](_0xe5d771['query']);_0x4f1c2c['filters']=_['intersection'](_0x4f1c2c[_0xbf3f('0x32')],_0x4f1c2c['query']);_0x232cab[_0xbf3f('0x36')]=_[_0xbf3f('0x35')](_0x4f1c2c[_0xbf3f('0x32')],qs[_0xbf3f('0x37')](_0xe5d771[_0xbf3f('0x34')][_0xbf3f('0x37')]));_0x232cab[_0xbf3f('0x36')]=_0x232cab[_0xbf3f('0x36')]['length']?_0x232cab[_0xbf3f('0x36')]:_0x4f1c2c[_0xbf3f('0x32')];if(!_0xe5d771['query']['hasOwnProperty'](_0xbf3f('0x3a'))){_0x232cab[_0xbf3f('0x21')]=qs[_0xbf3f('0x21')](_0xe5d771[_0xbf3f('0x34')]['limit']);_0x232cab[_0xbf3f('0x20')]=qs['offset'](_0xe5d771[_0xbf3f('0x34')]['offset']);}_0x232cab[_0xbf3f('0x3b')]=qs[_0xbf3f('0x3c')](_0xe5d771['query'][_0xbf3f('0x3c')]);_0x232cab[_0xbf3f('0x3d')]=qs[_0xbf3f('0x3e')](_['pick'](_0xe5d771[_0xbf3f('0x34')],_0x4f1c2c[_0xbf3f('0x3e')]));_0x232cab['where'][_0xbf3f('0x58')]=_0x40ae5d['id'];if(_0xe5d771[_0xbf3f('0x34')]['filter']){_0x232cab[_0xbf3f('0x3d')]=_[_0xbf3f('0x40')](_0x232cab['where'],{'$or':_[_0xbf3f('0x33')](_0x232cab[_0xbf3f('0x36')],function(_0x556576){var _0x2893ba={};_0x2893ba[_0x556576]={'$like':'%'+_0xe5d771[_0xbf3f('0x34')][_0xbf3f('0x41')]+'%'};return _0x2893ba;})});}_0x232cab=_[_0xbf3f('0x40')]({},_0x232cab,_0xe5d771['options']);return db['UserWhatsappQueue']['findAll'](_0x232cab);}})[_0xbf3f('0x28')](function(_0xc57bce){if(_0xc57bce){return db['User']['findAndCountAll']({'where':{'id':_['map'](_0xc57bce,'UserId'),'role':_0xbf3f('0x59')},'attributes':['id',_0xbf3f('0x2d'),_0xbf3f('0x5a'),_0xbf3f('0x5b'),'createdAt']});}})['then'](function(_0x519723){if(_0x519723){return{'count':_0x519723['count'],'rows':_['map'](_0x519723['rows'],function(_0x160e3a){return{'membername':_0x160e3a['name'],'UserId':_0x160e3a['id'],'queue_name':_0x349765[_0xbf3f('0x2d')],'WhatsappQueueId':_0x349765['id'],'interface':util[_0xbf3f('0x5c')]('%s/%s',_0xbf3f('0x5d'),_0x160e3a[_0xbf3f('0x2d')]),'penalty':0x0,'paused':_0x160e3a[_0xbf3f('0x5a')],'createdAt':_0x160e3a[_0xbf3f('0x5e')],'updatedAt':_0x160e3a[_0xbf3f('0x5b')]};})};}else{return{'count':0x0,'rows':[]};}})[_0xbf3f('0x28')](respondWithFilteredResult(_0x4496d8,_0x232cab))[_0xbf3f('0x48')](handleError(_0x4496d8,null));};exports[_0xbf3f('0x5f')]=function(_0x374388,_0x17e467,_0x1dc916){var _0x4f79ca={};var _0x37136d={};var _0x7fc85a;var _0xe5eb33;return db['WhatsappQueue']['findOne']({'where':{'id':_0x374388[_0xbf3f('0x45')]['id']}})[_0xbf3f('0x28')](handleEntityNotFound(_0x17e467,null))[_0xbf3f('0x28')](function(_0xdb2ffa){if(_0xdb2ffa){_0x7fc85a=_0xdb2ffa;_0x37136d[_0xbf3f('0x32')]=_[_0xbf3f('0x56')](db[_0xbf3f('0x60')]['rawAttributes']);_0x37136d[_0xbf3f('0x34')]=_[_0xbf3f('0x56')](_0x374388['query']);_0x37136d[_0xbf3f('0x3e')]=_[_0xbf3f('0x35')](_0x37136d['model'],_0x37136d['query']);_0x4f79ca[_0xbf3f('0x36')]=_[_0xbf3f('0x35')](_0x37136d[_0xbf3f('0x32')],qs[_0xbf3f('0x37')](_0x374388[_0xbf3f('0x34')]['fields']));_0x4f79ca[_0xbf3f('0x36')]=_0x4f79ca[_0xbf3f('0x36')][_0xbf3f('0x38')]?_0x4f79ca[_0xbf3f('0x36')]:_0x37136d[_0xbf3f('0x32')];_0x4f79ca[_0xbf3f('0x3b')]=qs['sort'](_0x374388[_0xbf3f('0x34')][_0xbf3f('0x3c')]);_0x4f79ca[_0xbf3f('0x3d')]=qs[_0xbf3f('0x3e')](_[_0xbf3f('0x3f')](_0x374388['query'],_0x37136d['filters']));if(_0x374388['query']['filter']){_0x4f79ca['where']=_[_0xbf3f('0x40')](_0x4f79ca[_0xbf3f('0x3d')],{'$or':_[_0xbf3f('0x33')](_0x4f79ca[_0xbf3f('0x36')],function(_0x224b6a){var _0x50f36d={};_0x50f36d[_0x224b6a]={'$like':'%'+_0x374388[_0xbf3f('0x34')]['filter']+'%'};return _0x50f36d;})});}_0x4f79ca=_[_0xbf3f('0x40')]({},_0x4f79ca,_0x374388['options']);return _0x7fc85a['getTeams'](_0x4f79ca);}})['then'](function(_0x567937){if(_0x567937){_0xe5eb33=_0x567937[_0xbf3f('0x38')];if(!_0x374388[_0xbf3f('0x34')][_0xbf3f('0x39')](_0xbf3f('0x3a'))){_0x4f79ca[_0xbf3f('0x21')]=qs[_0xbf3f('0x21')](_0x374388['query']['limit']);_0x4f79ca[_0xbf3f('0x20')]=qs[_0xbf3f('0x20')](_0x374388[_0xbf3f('0x34')][_0xbf3f('0x20')]);}return _0x7fc85a[_0xbf3f('0x5f')](_0x4f79ca);}})['then'](function(_0x56d57b){if(_0x56d57b){return _0x56d57b?{'count':_0xe5eb33,'rows':_0x56d57b}:null;}})[_0xbf3f('0x28')](respondWithResult(_0x17e467,null))[_0xbf3f('0x48')](handleError(_0x17e467,null));};exports[_0xbf3f('0x61')]=function(_0x50b128,_0x45a610,_0x4c07f0){var _0x486232=_0x50b128[_0xbf3f('0x4a')][_0xbf3f('0x62')];return db[_0xbf3f('0x60')][_0xbf3f('0x63')]({'where':{'id':_0x486232},'attributes':['id'],'include':[{'model':db[_0xbf3f('0x64')],'as':'Agents','attributes':['id',_0xbf3f('0x2d'),'online',_0xbf3f('0x65'),_0xbf3f('0x66')],'raw':!![]}]})[_0xbf3f('0x28')](function(_0x5e1114){if(_0x5e1114){var _0x306d51=_[_0xbf3f('0x67')](_0x5e1114,function(_0x190a2a){var _0x5599e3=_0x190a2a[_0xbf3f('0x2a')]({'plain':!![]});return _0x5599e3[_0xbf3f('0x68')];});return db['WhatsappQueue'][_0xbf3f('0x47')]({'where':{'id':_0x50b128[_0xbf3f('0x45')]['id']}})[_0xbf3f('0x28')](function(_0x2bed50){return db[_0xbf3f('0x69')][_0xbf3f('0x6a')](function(_0x1f1cdd){return _0x2bed50['addTeams'](_0x486232,{'transaction':_0x1f1cdd})['then'](function(){return BPromise[_0xbf3f('0x6b')](_0x306d51,function(_0x2b6154){return db[_0xbf3f('0x57')]['findOrCreate']({'where':{'UserId':_0x2b6154['id'],'WhatsappQueueId':_0x50b128[_0xbf3f('0x45')]['id']},'transaction':_0x1f1cdd});});})['then'](function(){_0x306d51[_0xbf3f('0x6c')](function(_0x42d13c){socket[_0xbf3f('0x6d')](_0xbf3f('0x6e'),{'UserId':_0x42d13c['id'],'WhatsappQueueId':_0x2bed50['id']});});});});});}})[_0xbf3f('0x28')](respondWithStatusCode(_0x45a610,null))['catch'](handleError(_0x45a610,null));};exports[_0xbf3f('0x6f')]=function(_0xb820cc,_0x18b8c0,_0x1f3c18){return db[_0xbf3f('0x60')]['findAll']({'where':{'id':_0xb820cc['query'][_0xbf3f('0x62')]},'attributes':['id'],'include':[{'model':db[_0xbf3f('0x64')],'as':'Agents','attributes':['id'],'raw':!![]}]})[_0xbf3f('0x28')](handleEntityNotFound(_0x18b8c0,null))['then'](function(_0x40adb8){var _0x395ea6=_[_0xbf3f('0x33')](_0x40adb8,'id');var _0x7fc581=[];var _0x247a40=[];var _0x1253bf=squel[_0xbf3f('0x70')]();_0x1253bf[_0xbf3f('0x71')](_0xbf3f('0x72'))['from'](_0xbf3f('0x73'),'tq')[_0xbf3f('0x74')](_0xbf3f('0x75'),'ut','ut.TeamId\x20=\x20tq.TeamId')[_0xbf3f('0x3d')]('WhatsappQueueId\x20=\x20?',_0xb820cc[_0xbf3f('0x45')]['id']);for(var _0x337690=0x0;_0x337690<_0x40adb8[_0xbf3f('0x38')];_0x337690+=0x1){let _0x142359=_0x40adb8[_0x337690];for(var _0x4884ff=0x0;_0x4884ff<_0x142359[_0xbf3f('0x68')][_0xbf3f('0x38')];_0x4884ff+=0x1){let _0x4c72a8=_0x142359[_0xbf3f('0x68')][_0x4884ff];var _0x3e833d=_0x1253bf[_0xbf3f('0x76')]();_0x3e833d[_0xbf3f('0x3d')]('ut.UserId\x20=\x20?',_0x4c72a8['id']);_0x247a40['push'](db['sequelize'][_0xbf3f('0x34')](_0x3e833d[_0xbf3f('0x77')](),{'type':db[_0xbf3f('0x69')][_0xbf3f('0x78')][_0xbf3f('0x79')]})[_0xbf3f('0x28')](function(_0x199ec3){if(_0x199ec3[_0xbf3f('0x38')]===0x1){return _0x4c72a8['id'];}else{var _0x138a3f=_[_0xbf3f('0x7a')](_[_0xbf3f('0x33')](_0x199ec3,_0xbf3f('0x7b')),function(_0x3bf894){return _[_0xbf3f('0x7c')](_0x395ea6,_0x3bf894);});if(_0x138a3f){return _0x4c72a8['id'];}}}));}}return BPromise[_0xbf3f('0x7d')](_0x247a40)[_0xbf3f('0x28')](function(_0x3b83b0){_0x7fc581=_(_0x7fc581)['union'](_0x3b83b0)[_0xbf3f('0x7e')]()[_0xbf3f('0x7f')]();return db[_0xbf3f('0x2f')]['find']({'where':{'id':_0xb820cc[_0xbf3f('0x45')]['id']}})['then'](function(_0x4d6615){return db['sequelize'][_0xbf3f('0x6a')](function(_0x280633){return _0x4d6615['removeTeams'](_0xb820cc[_0xbf3f('0x34')][_0xbf3f('0x62')],{'transaction':_0x280633})[_0xbf3f('0x28')](function(){if(!_[_0xbf3f('0x80')](_0x7fc581)){return _0x4d6615[_0xbf3f('0x81')](_0x7fc581,{'transaction':_0x280633});}})[_0xbf3f('0x28')](function(){_0x7fc581[_0xbf3f('0x6c')](function(_0x2c4bc2){socket['emit'](_0xbf3f('0x82'),{'UserId':_0x2c4bc2,'WhatsappQueueId':_0x4d6615['id']});});});});});});})['then'](respondWithStatusCode(_0x18b8c0,null))[_0xbf3f('0x48')](handleError(_0x18b8c0,null));};exports[_0xbf3f('0x83')]=function(_0x1db4c8,_0xb99cd2,_0x26b600){return db[_0xbf3f('0x2f')][_0xbf3f('0x47')]({'where':{'id':_0x1db4c8['params']['id']}})['then'](handleEntityNotFound(_0xb99cd2,null))[_0xbf3f('0x28')](function(_0x3f54fc){if(_0x3f54fc){return _0x3f54fc[_0xbf3f('0x83')](_0x1db4c8['body']['ids'],_['omit'](_0x1db4c8[_0xbf3f('0x4a')],['ids','id'])||{})[_0xbf3f('0x84')](function(_0x392b61){for(var _0x12b1e8=0x0;_0x12b1e8<_0x1db4c8[_0xbf3f('0x4a')][_0xbf3f('0x62')]['length'];_0x12b1e8+=0x1){socket['emit'](_0xbf3f('0x6e'),{'UserId':_0x1db4c8[_0xbf3f('0x4a')]['ids'][_0x12b1e8],'WhatsappQueueId':_0x1db4c8[_0xbf3f('0x45')]['id']});}return _0x392b61;});}})[_0xbf3f('0x28')](respondWithResult(_0xb99cd2,null))[_0xbf3f('0x48')](handleError(_0xb99cd2,null));};exports['removeAgents']=function(_0x21ea3f,_0xe806c3,_0xc7f426){return db[_0xbf3f('0x2f')][_0xbf3f('0x47')]({'where':{'id':_0x21ea3f['params']['id']}})['then'](handleEntityNotFound(_0xe806c3,null))[_0xbf3f('0x28')](function(_0x53e395){if(_0x53e395){return _0x53e395[_0xbf3f('0x81')](_0x21ea3f[_0xbf3f('0x34')][_0xbf3f('0x62')])[_0xbf3f('0x28')](function(){if(_['isArray'](_0x21ea3f[_0xbf3f('0x34')][_0xbf3f('0x62')])){for(var _0xd8a660=0x0;_0xd8a660<_0x21ea3f[_0xbf3f('0x34')][_0xbf3f('0x62')][_0xbf3f('0x38')];_0xd8a660+=0x1){socket['emit'](_0xbf3f('0x82'),{'UserId':Number(_0x21ea3f[_0xbf3f('0x34')][_0xbf3f('0x62')][_0xd8a660]),'WhatsappQueueId':Number(_0x21ea3f['params']['id'])});}}else{socket[_0xbf3f('0x6d')](_0xbf3f('0x82'),{'UserId':Number(_0x21ea3f['query'][_0xbf3f('0x62')]),'WhatsappQueueId':Number(_0x21ea3f[_0xbf3f('0x45')]['id'])});}});}})[_0xbf3f('0x28')](respondWithStatusCode(_0xe806c3,null))[_0xbf3f('0x48')](handleError(_0xe806c3,null));};exports['getAgents']=function(_0x2f1d59,_0x3e72e6,_0x555ff8){var _0x31f82d={};var _0x588661={};var _0x47a331;var _0x1d4621;return db[_0xbf3f('0x2f')]['findOne']({'where':{'id':_0x2f1d59[_0xbf3f('0x45')]['id']}})[_0xbf3f('0x28')](handleEntityNotFound(_0x3e72e6,null))[_0xbf3f('0x28')](function(_0x354c92){if(_0x354c92){_0x47a331=_0x354c92;_0x588661[_0xbf3f('0x32')]=_[_0xbf3f('0x56')](db['User']['rawAttributes']);_0x588661['query']=_[_0xbf3f('0x56')](_0x2f1d59[_0xbf3f('0x34')]);_0x588661[_0xbf3f('0x3e')]=_['intersection'](_0x588661[_0xbf3f('0x32')],_0x588661[_0xbf3f('0x34')]);_0x31f82d[_0xbf3f('0x36')]=_[_0xbf3f('0x35')](_0x588661[_0xbf3f('0x32')],qs[_0xbf3f('0x37')](_0x2f1d59[_0xbf3f('0x34')][_0xbf3f('0x37')]));_0x31f82d[_0xbf3f('0x36')]=_0x31f82d['attributes']['length']?_0x31f82d['attributes']:_0x588661[_0xbf3f('0x32')];_0x31f82d['order']=qs[_0xbf3f('0x3c')](_0x2f1d59[_0xbf3f('0x34')][_0xbf3f('0x3c')]);_0x31f82d[_0xbf3f('0x3d')]=qs[_0xbf3f('0x3e')](_['pick'](_0x2f1d59[_0xbf3f('0x34')],_0x588661[_0xbf3f('0x3e')]));if(_0x2f1d59[_0xbf3f('0x34')][_0xbf3f('0x41')]){_0x31f82d['where']=_[_0xbf3f('0x40')](_0x31f82d[_0xbf3f('0x3d')],{'$or':_['map'](_0x31f82d[_0xbf3f('0x36')],function(_0x571255){var _0x16fb94={};_0x16fb94[_0x571255]={'$like':'%'+_0x2f1d59[_0xbf3f('0x34')][_0xbf3f('0x41')]+'%'};return _0x16fb94;})});}_0x31f82d=_[_0xbf3f('0x40')]({},_0x31f82d,_0x2f1d59[_0xbf3f('0x46')]);return _0x47a331['getAgents'](_0x31f82d);}})['then'](function(_0x4cdb39){if(_0x4cdb39){_0x1d4621=_0x4cdb39[_0xbf3f('0x38')];if(!_0x2f1d59[_0xbf3f('0x34')]['hasOwnProperty'](_0xbf3f('0x3a'))){_0x31f82d[_0xbf3f('0x21')]=qs[_0xbf3f('0x21')](_0x2f1d59[_0xbf3f('0x34')][_0xbf3f('0x21')]);_0x31f82d[_0xbf3f('0x20')]=qs[_0xbf3f('0x20')](_0x2f1d59[_0xbf3f('0x34')][_0xbf3f('0x20')]);}return _0x47a331[_0xbf3f('0x85')](_0x31f82d);}})['then'](function(_0x40a3a1){if(_0x40a3a1){return _0x40a3a1?{'count':_0x1d4621,'rows':_0x40a3a1}:null;}})['then'](respondWithResult(_0x3e72e6,null))['catch'](handleError(_0x3e72e6,null));};