922a412c11e326e3d759e6838d7b7318abf34866
[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 _0xbfd3=['UserId','agent','whatsappPause','updatedAt','createdAt','format','SIP','getTeams','Team','addTeams','ids','voicePause','interface','flatMap','Agents','transaction','each','userWhatsappQueue:save','removeTeams','select','field','tq.TeamId','from','team_has_whatsapp_queues','join','user_has_teams','ut.TeamId\x20=\x20tq.TeamId','WhatsappQueueId\x20=\x20?','clone','sequelize','toString','SELECT','TeamId','includes','all','union','compact','value','isEmpty','removeAgents','emit','userWhatsappQueue:remove','addAgents','omit','isArray','getAgents','html-pdf','eml-format','rimraf','zip-dir','fast-json-patch','moment','util','to-csv','ejs','lodash','squel','crypto','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','redis','defaults','socket.io-emitter','register','sendStatus','end','status','json','count','offset','limit','set','Content-Range','reject','save','update','destroy','then','WhatsappQueues','UserProfileResource','error','stack','name','send','index','map','WhatsappQueue','rawAttributes','fieldName','type','key','keys','query','filters','intersection','model','attributes','length','nolimit','order','sort','where','merge','filter','includeAll','catch','params','fields','include','find','create','body','user','get','role','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','describe','getMembers','findOne','UserWhatsappQueue','hasOwnProperty','pick','WhatsappQueueId','options','findAll','User','findAndCountAll'];(function(_0x4c321b,_0x2a2bf0){var _0x1fd775=function(_0x1a826c){while(--_0x1a826c){_0x4c321b['push'](_0x4c321b['shift']());}};_0x1fd775(++_0x2a2bf0);}(_0xbfd3,0x144));var _0x3bfd=function(_0x3857b2,_0x2d209d){_0x3857b2=_0x3857b2-0x0;var _0x49d6df=_0xbfd3[_0x3857b2];return _0x49d6df;};'use strict';var pdf=require(_0x3bfd('0x0'));var emlformat=require(_0x3bfd('0x1'));var rimraf=require(_0x3bfd('0x2'));var zipdir=require(_0x3bfd('0x3'));var jsonpatch=require(_0x3bfd('0x4'));var rp=require('request-promise');var moment=require(_0x3bfd('0x5'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0x3bfd('0x6'));var path=require('path');var sox=require('sox');var csv=require(_0x3bfd('0x7'));var ejs=require(_0x3bfd('0x8'));var fs=require('fs');var _=require(_0x3bfd('0x9'));var squel=require(_0x3bfd('0xa'));var crypto=require(_0x3bfd('0xb'));var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0x3bfd('0x7'));var querystring=require(_0x3bfd('0xc'));var Papa=require(_0x3bfd('0xd'));var Redis=require('ioredis');var authService=require(_0x3bfd('0xe'));var qs=require(_0x3bfd('0xf'));var as=require(_0x3bfd('0x10'));var hardwareService=require(_0x3bfd('0x11'));var logger=require(_0x3bfd('0x12'))(_0x3bfd('0x13'));var utils=require(_0x3bfd('0x14'));var config=require(_0x3bfd('0x15'));var db=require(_0x3bfd('0x16'))['db'];config[_0x3bfd('0x17')]=_[_0x3bfd('0x18')](config[_0x3bfd('0x17')],{'host':'localhost','port':0x18eb});var socket=require(_0x3bfd('0x19'))(new Redis(config[_0x3bfd('0x17')]));require('./whatsappQueue.socket')[_0x3bfd('0x1a')](socket);function respondWithStatusCode(_0x259f4c,_0x224734){_0x224734=_0x224734||0xcc;return function(_0x3b6bfb){if(_0x3b6bfb){return _0x259f4c[_0x3bfd('0x1b')](_0x224734);}return _0x259f4c['status'](_0x224734)[_0x3bfd('0x1c')]();};}function respondWithResult(_0x1f3ebf,_0x4f25f0){_0x4f25f0=_0x4f25f0||0xc8;return function(_0x18bba0){if(_0x18bba0){return _0x1f3ebf[_0x3bfd('0x1d')](_0x4f25f0)[_0x3bfd('0x1e')](_0x18bba0);}};}function respondWithFilteredResult(_0x70a22d,_0x3e30f9){return function(_0x4ec2b0){if(_0x4ec2b0){var _0xfe31ed=_0x4ec2b0[_0x3bfd('0x1f')],_0x488827=_0x3e30f9[_0x3bfd('0x20')],_0x2e6ace=_0x3e30f9[_0x3bfd('0x20')]+_0x3e30f9[_0x3bfd('0x21')],_0x3f45a8;if(_0x2e6ace>=_0xfe31ed){_0x2e6ace=_0xfe31ed;_0x3f45a8=0xc8;}else{_0x3f45a8=0xce;}_0x70a22d['status'](_0x3f45a8);return _0x70a22d[_0x3bfd('0x22')](_0x3bfd('0x23'),_0x488827+'-'+_0x2e6ace+'/'+_0xfe31ed)[_0x3bfd('0x1e')](_0x4ec2b0);}return null;};}function patchUpdates(_0x4a342e){return function(_0x48455f){try{jsonpatch['apply'](_0x48455f,_0x4a342e,!![]);}catch(_0x121342){return BPromise[_0x3bfd('0x24')](_0x121342);}return _0x48455f[_0x3bfd('0x25')]();};}function saveUpdates(_0x5480ee,_0x376091){return function(_0x248918){if(_0x248918){return _0x248918[_0x3bfd('0x26')](_0x5480ee)['then'](function(_0x3dcb1f){return _0x3dcb1f;});}return null;};}function removeEntity(_0xb8e84f,_0x28c081){return function(_0x4587a3){if(_0x4587a3){return _0x4587a3[_0x3bfd('0x27')]()[_0x3bfd('0x28')](function(){var _0x2752cf=_0x4587a3['get']({'plain':!![]});var _0x1a0875=_0x3bfd('0x29');return db[_0x3bfd('0x2a')]['destroy']({'where':{'type':_0x1a0875,'resourceId':_0x2752cf['id']}})[_0x3bfd('0x28')](function(){return _0x4587a3;});})[_0x3bfd('0x28')](function(){_0xb8e84f[_0x3bfd('0x1d')](0xcc)['end']();});}};}function handleEntityNotFound(_0x15cf39,_0x2f613f){return function(_0x9d3da2){if(!_0x9d3da2){_0x15cf39['sendStatus'](0x194);}return _0x9d3da2;};}function handleError(_0x39b7cf,_0xf509e5){_0xf509e5=_0xf509e5||0x1f4;return function(_0x32e329){logger[_0x3bfd('0x2b')](_0x32e329[_0x3bfd('0x2c')]);if(_0x32e329[_0x3bfd('0x2d')]){delete _0x32e329[_0x3bfd('0x2d')];}_0x39b7cf[_0x3bfd('0x1d')](_0xf509e5)[_0x3bfd('0x2e')](_0x32e329);};}exports[_0x3bfd('0x2f')]=function(_0x67af80,_0x54b82c){var _0x5e55d1={},_0x3a20e5={},_0x879dc2={'count':0x0,'rows':[]};var _0x2bd8cd=_[_0x3bfd('0x30')](db[_0x3bfd('0x31')][_0x3bfd('0x32')],function(_0x36cf1c){return{'name':_0x36cf1c[_0x3bfd('0x33')],'type':_0x36cf1c[_0x3bfd('0x34')][_0x3bfd('0x35')]};});_0x3a20e5['model']=_[_0x3bfd('0x30')](_0x2bd8cd,'name');_0x3a20e5['query']=_[_0x3bfd('0x36')](_0x67af80[_0x3bfd('0x37')]);_0x3a20e5[_0x3bfd('0x38')]=_[_0x3bfd('0x39')](_0x3a20e5[_0x3bfd('0x3a')],_0x3a20e5[_0x3bfd('0x37')]);_0x5e55d1[_0x3bfd('0x3b')]=_[_0x3bfd('0x39')](_0x3a20e5[_0x3bfd('0x3a')],qs['fields'](_0x67af80[_0x3bfd('0x37')]['fields']));_0x5e55d1[_0x3bfd('0x3b')]=_0x5e55d1[_0x3bfd('0x3b')][_0x3bfd('0x3c')]?_0x5e55d1[_0x3bfd('0x3b')]:_0x3a20e5[_0x3bfd('0x3a')];if(!_0x67af80[_0x3bfd('0x37')]['hasOwnProperty'](_0x3bfd('0x3d'))){_0x5e55d1[_0x3bfd('0x21')]=qs[_0x3bfd('0x21')](_0x67af80[_0x3bfd('0x37')][_0x3bfd('0x21')]);_0x5e55d1[_0x3bfd('0x20')]=qs[_0x3bfd('0x20')](_0x67af80[_0x3bfd('0x37')][_0x3bfd('0x20')]);}_0x5e55d1[_0x3bfd('0x3e')]=qs[_0x3bfd('0x3f')](_0x67af80[_0x3bfd('0x37')][_0x3bfd('0x3f')]);_0x5e55d1[_0x3bfd('0x40')]=qs[_0x3bfd('0x38')](_['pick'](_0x67af80[_0x3bfd('0x37')],_0x3a20e5['filters']),_0x2bd8cd);if(_0x67af80[_0x3bfd('0x37')]['filter']){_0x5e55d1['where']=_[_0x3bfd('0x41')](_0x5e55d1['where'],{'$or':_[_0x3bfd('0x30')](_0x2bd8cd,function(_0x136555){if(_0x136555[_0x3bfd('0x34')]!=='VIRTUAL'){var _0x54beab={};_0x54beab[_0x136555[_0x3bfd('0x2d')]]={'$like':'%'+_0x67af80['query'][_0x3bfd('0x42')]+'%'};return _0x54beab;}})});}_0x5e55d1=_[_0x3bfd('0x41')]({},_0x5e55d1,_0x67af80['options']);var _0x8265cd={'where':_0x5e55d1['where']};return db[_0x3bfd('0x31')][_0x3bfd('0x1f')](_0x8265cd)['then'](function(_0x5a9829){_0x879dc2['count']=_0x5a9829;if(_0x67af80['query'][_0x3bfd('0x43')]){_0x5e55d1['include']=[{'all':!![]}];}return db[_0x3bfd('0x31')]['findAll'](_0x5e55d1);})[_0x3bfd('0x28')](function(_0x4a7174){_0x879dc2['rows']=_0x4a7174;return _0x879dc2;})[_0x3bfd('0x28')](respondWithFilteredResult(_0x54b82c,_0x5e55d1))[_0x3bfd('0x44')](handleError(_0x54b82c,null));};exports['show']=function(_0x328ac0,_0x45bdb9){var _0x20c82f={'raw':!![],'where':{'id':_0x328ac0[_0x3bfd('0x45')]['id']}},_0x4f05b3={};_0x4f05b3['model']=_[_0x3bfd('0x36')](db[_0x3bfd('0x31')]['rawAttributes']);_0x4f05b3[_0x3bfd('0x37')]=_[_0x3bfd('0x36')](_0x328ac0[_0x3bfd('0x37')]);_0x4f05b3[_0x3bfd('0x38')]=_[_0x3bfd('0x39')](_0x4f05b3[_0x3bfd('0x3a')],_0x4f05b3[_0x3bfd('0x37')]);_0x20c82f[_0x3bfd('0x3b')]=_[_0x3bfd('0x39')](_0x4f05b3[_0x3bfd('0x3a')],qs[_0x3bfd('0x46')](_0x328ac0[_0x3bfd('0x37')][_0x3bfd('0x46')]));_0x20c82f[_0x3bfd('0x3b')]=_0x20c82f[_0x3bfd('0x3b')]['length']?_0x20c82f['attributes']:_0x4f05b3['model'];if(_0x328ac0['query'][_0x3bfd('0x43')]){_0x20c82f[_0x3bfd('0x47')]=[{'all':!![]}];}_0x20c82f=_[_0x3bfd('0x41')]({},_0x20c82f,_0x328ac0['options']);return db[_0x3bfd('0x31')][_0x3bfd('0x48')](_0x20c82f)[_0x3bfd('0x28')](handleEntityNotFound(_0x45bdb9,null))[_0x3bfd('0x28')](respondWithResult(_0x45bdb9,null))[_0x3bfd('0x44')](handleError(_0x45bdb9,null));};exports[_0x3bfd('0x49')]=function(_0x192482,_0x3667e9){return db[_0x3bfd('0x31')]['create'](_0x192482[_0x3bfd('0x4a')],{})[_0x3bfd('0x28')](function(_0xa88c53){var _0x340be0=_0x192482[_0x3bfd('0x4b')][_0x3bfd('0x4c')]({'plain':!![]});if(!_0x340be0)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x340be0[_0x3bfd('0x4d')]==='user'){var _0x5f1930=_0xa88c53['get']({'plain':!![]});var _0x5adb05=_0x3bfd('0x29');return db[_0x3bfd('0x4e')][_0x3bfd('0x48')]({'where':{'name':_0x5adb05,'userProfileId':_0x340be0[_0x3bfd('0x4f')]},'raw':!![]})[_0x3bfd('0x28')](function(_0xbfc350){if(_0xbfc350&&_0xbfc350[_0x3bfd('0x50')]===0x0){return db[_0x3bfd('0x2a')][_0x3bfd('0x49')]({'name':_0x5f1930[_0x3bfd('0x2d')],'resourceId':_0x5f1930['id'],'type':_0xbfc350[_0x3bfd('0x2d')],'sectionId':_0xbfc350['id']},{})['then'](function(){return _0xa88c53;});}else{return _0xa88c53;}})['catch'](function(_0x11be5e){logger[_0x3bfd('0x2b')](_0x3bfd('0x51'),_0x11be5e);throw _0x11be5e;});}return _0xa88c53;})[_0x3bfd('0x28')](respondWithResult(_0x3667e9,0xc9))[_0x3bfd('0x44')](handleError(_0x3667e9,null));};exports['update']=function(_0x2e27cd,_0x18744d){if(_0x2e27cd[_0x3bfd('0x4a')]['id']){delete _0x2e27cd['body']['id'];}return db[_0x3bfd('0x31')][_0x3bfd('0x48')]({'where':{'id':_0x2e27cd[_0x3bfd('0x45')]['id']}})[_0x3bfd('0x28')](handleEntityNotFound(_0x18744d,null))['then'](saveUpdates(_0x2e27cd['body'],null))[_0x3bfd('0x28')](respondWithResult(_0x18744d,null))['catch'](handleError(_0x18744d,null));};exports[_0x3bfd('0x27')]=function(_0x426edd,_0x2454cb){return db[_0x3bfd('0x31')][_0x3bfd('0x48')]({'where':{'id':_0x426edd[_0x3bfd('0x45')]['id']}})[_0x3bfd('0x28')](handleEntityNotFound(_0x2454cb,null))[_0x3bfd('0x28')](removeEntity(_0x2454cb,null))[_0x3bfd('0x44')](handleError(_0x2454cb,null));};exports[_0x3bfd('0x52')]=function(_0x122dc0,_0x22e833){return db[_0x3bfd('0x31')][_0x3bfd('0x52')]()[_0x3bfd('0x28')](respondWithResult(_0x22e833,null))[_0x3bfd('0x44')](handleError(_0x22e833,null));};exports[_0x3bfd('0x53')]=function(_0x5021bb,_0x35fa3e,_0x52dbca){var _0x5b7d71={'raw':!![],'where':{}},_0x4ad6f6={},_0x3cb4ea;return db['WhatsappQueue'][_0x3bfd('0x54')]({'where':{'id':_0x5021bb[_0x3bfd('0x45')]['id']}})[_0x3bfd('0x28')](handleEntityNotFound(_0x35fa3e,null))['then'](function(_0x3f149f){if(_0x3f149f){_0x3cb4ea=_0x3f149f['get']({'plain':!![]});_0x4ad6f6['model']=_[_0x3bfd('0x36')](db[_0x3bfd('0x55')][_0x3bfd('0x32')]);_0x4ad6f6[_0x3bfd('0x37')]=_['keys'](_0x5021bb[_0x3bfd('0x37')]);_0x4ad6f6[_0x3bfd('0x38')]=_[_0x3bfd('0x39')](_0x4ad6f6[_0x3bfd('0x3a')],_0x4ad6f6[_0x3bfd('0x37')]);_0x5b7d71[_0x3bfd('0x3b')]=_[_0x3bfd('0x39')](_0x4ad6f6[_0x3bfd('0x3a')],qs[_0x3bfd('0x46')](_0x5021bb['query']['fields']));_0x5b7d71[_0x3bfd('0x3b')]=_0x5b7d71[_0x3bfd('0x3b')][_0x3bfd('0x3c')]?_0x5b7d71[_0x3bfd('0x3b')]:_0x4ad6f6[_0x3bfd('0x3a')];if(!_0x5021bb['query'][_0x3bfd('0x56')](_0x3bfd('0x3d'))){_0x5b7d71['limit']=qs['limit'](_0x5021bb[_0x3bfd('0x37')][_0x3bfd('0x21')]);_0x5b7d71[_0x3bfd('0x20')]=qs[_0x3bfd('0x20')](_0x5021bb['query'][_0x3bfd('0x20')]);}_0x5b7d71[_0x3bfd('0x3e')]=qs[_0x3bfd('0x3f')](_0x5021bb[_0x3bfd('0x37')]['sort']);_0x5b7d71[_0x3bfd('0x40')]=qs[_0x3bfd('0x38')](_[_0x3bfd('0x57')](_0x5021bb[_0x3bfd('0x37')],_0x4ad6f6[_0x3bfd('0x38')]));_0x5b7d71[_0x3bfd('0x40')][_0x3bfd('0x58')]=_0x3f149f['id'];if(_0x5021bb[_0x3bfd('0x37')][_0x3bfd('0x42')]){_0x5b7d71[_0x3bfd('0x40')]=_['merge'](_0x5b7d71['where'],{'$or':_[_0x3bfd('0x30')](_0x5b7d71['attributes'],function(_0x21952a){var _0xa0520f={};_0xa0520f[_0x21952a]={'$like':'%'+_0x5021bb[_0x3bfd('0x37')][_0x3bfd('0x42')]+'%'};return _0xa0520f;})});}_0x5b7d71=_[_0x3bfd('0x41')]({},_0x5b7d71,_0x5021bb[_0x3bfd('0x59')]);return db[_0x3bfd('0x55')][_0x3bfd('0x5a')](_0x5b7d71);}})[_0x3bfd('0x28')](function(_0x3f71a4){if(_0x3f71a4){return db[_0x3bfd('0x5b')][_0x3bfd('0x5c')]({'where':{'id':_[_0x3bfd('0x30')](_0x3f71a4,_0x3bfd('0x5d')),'role':_0x3bfd('0x5e')},'attributes':['id',_0x3bfd('0x2d'),_0x3bfd('0x5f'),_0x3bfd('0x60'),_0x3bfd('0x61')]});}})[_0x3bfd('0x28')](function(_0x515c55){if(_0x515c55){return{'count':_0x515c55[_0x3bfd('0x1f')],'rows':_[_0x3bfd('0x30')](_0x515c55['rows'],function(_0x4a9327){return{'membername':_0x4a9327['name'],'UserId':_0x4a9327['id'],'queue_name':_0x3cb4ea[_0x3bfd('0x2d')],'WhatsappQueueId':_0x3cb4ea['id'],'interface':util[_0x3bfd('0x62')]('%s/%s',_0x3bfd('0x63'),_0x4a9327[_0x3bfd('0x2d')]),'penalty':0x0,'paused':_0x4a9327['whatsappPause'],'createdAt':_0x4a9327[_0x3bfd('0x61')],'updatedAt':_0x4a9327[_0x3bfd('0x60')]};})};}else{return{'count':0x0,'rows':[]};}})[_0x3bfd('0x28')](respondWithFilteredResult(_0x35fa3e,_0x5b7d71))['catch'](handleError(_0x35fa3e,null));};exports[_0x3bfd('0x64')]=function(_0x3cdd65,_0x4d2b23,_0x1f0375){var _0x5e0251={};var _0x179046={};var _0x5954ad;var _0x14e526;return db['WhatsappQueue'][_0x3bfd('0x54')]({'where':{'id':_0x3cdd65[_0x3bfd('0x45')]['id']}})[_0x3bfd('0x28')](handleEntityNotFound(_0x4d2b23,null))[_0x3bfd('0x28')](function(_0x3b17f4){if(_0x3b17f4){_0x5954ad=_0x3b17f4;_0x179046[_0x3bfd('0x3a')]=_['keys'](db[_0x3bfd('0x65')][_0x3bfd('0x32')]);_0x179046[_0x3bfd('0x37')]=_['keys'](_0x3cdd65[_0x3bfd('0x37')]);_0x179046[_0x3bfd('0x38')]=_[_0x3bfd('0x39')](_0x179046[_0x3bfd('0x3a')],_0x179046['query']);_0x5e0251[_0x3bfd('0x3b')]=_[_0x3bfd('0x39')](_0x179046[_0x3bfd('0x3a')],qs['fields'](_0x3cdd65[_0x3bfd('0x37')][_0x3bfd('0x46')]));_0x5e0251['attributes']=_0x5e0251[_0x3bfd('0x3b')][_0x3bfd('0x3c')]?_0x5e0251[_0x3bfd('0x3b')]:_0x179046['model'];_0x5e0251[_0x3bfd('0x3e')]=qs[_0x3bfd('0x3f')](_0x3cdd65[_0x3bfd('0x37')][_0x3bfd('0x3f')]);_0x5e0251[_0x3bfd('0x40')]=qs[_0x3bfd('0x38')](_[_0x3bfd('0x57')](_0x3cdd65[_0x3bfd('0x37')],_0x179046[_0x3bfd('0x38')]));if(_0x3cdd65[_0x3bfd('0x37')]['filter']){_0x5e0251['where']=_[_0x3bfd('0x41')](_0x5e0251[_0x3bfd('0x40')],{'$or':_['map'](_0x5e0251['attributes'],function(_0x3e9417){var _0x818e2c={};_0x818e2c[_0x3e9417]={'$like':'%'+_0x3cdd65[_0x3bfd('0x37')]['filter']+'%'};return _0x818e2c;})});}_0x5e0251=_[_0x3bfd('0x41')]({},_0x5e0251,_0x3cdd65[_0x3bfd('0x59')]);return _0x5954ad['getTeams'](_0x5e0251);}})[_0x3bfd('0x28')](function(_0x528d2b){if(_0x528d2b){_0x14e526=_0x528d2b[_0x3bfd('0x3c')];if(!_0x3cdd65[_0x3bfd('0x37')][_0x3bfd('0x56')]('nolimit')){_0x5e0251['limit']=qs[_0x3bfd('0x21')](_0x3cdd65['query'][_0x3bfd('0x21')]);_0x5e0251[_0x3bfd('0x20')]=qs[_0x3bfd('0x20')](_0x3cdd65[_0x3bfd('0x37')][_0x3bfd('0x20')]);}return _0x5954ad[_0x3bfd('0x64')](_0x5e0251);}})[_0x3bfd('0x28')](function(_0x39a6a2){if(_0x39a6a2){return _0x39a6a2?{'count':_0x14e526,'rows':_0x39a6a2}:null;}})[_0x3bfd('0x28')](respondWithResult(_0x4d2b23,null))['catch'](handleError(_0x4d2b23,null));};exports[_0x3bfd('0x66')]=function(_0x299825,_0x1f0c58,_0x48ed52){var _0x253774=_0x299825[_0x3bfd('0x4a')][_0x3bfd('0x67')];return db[_0x3bfd('0x65')]['findAll']({'where':{'id':_0x253774},'attributes':['id'],'include':[{'model':db[_0x3bfd('0x5b')],'as':'Agents','attributes':['id','name','online',_0x3bfd('0x68'),_0x3bfd('0x69')],'raw':!![]}]})[_0x3bfd('0x28')](function(_0x24f42a){if(_0x24f42a){var _0x105740=_[_0x3bfd('0x6a')](_0x24f42a,function(_0x128dcb){var _0x339cee=_0x128dcb[_0x3bfd('0x4c')]({'plain':!![]});return _0x339cee[_0x3bfd('0x6b')];});return db[_0x3bfd('0x31')][_0x3bfd('0x48')]({'where':{'id':_0x299825[_0x3bfd('0x45')]['id']}})[_0x3bfd('0x28')](function(_0x29e5e0){return db['sequelize'][_0x3bfd('0x6c')](function(_0x464ba7){return _0x29e5e0[_0x3bfd('0x66')](_0x253774,{'transaction':_0x464ba7})[_0x3bfd('0x28')](function(){return BPromise[_0x3bfd('0x6d')](_0x105740,function(_0x189c37){return db['UserWhatsappQueue']['findOrCreate']({'where':{'UserId':_0x189c37['id'],'WhatsappQueueId':_0x299825[_0x3bfd('0x45')]['id']},'transaction':_0x464ba7});});})[_0x3bfd('0x28')](function(){_0x105740['forEach'](function(_0x4be38e){socket['emit'](_0x3bfd('0x6e'),{'UserId':_0x4be38e['id'],'WhatsappQueueId':_0x29e5e0['id']});});});});});}})[_0x3bfd('0x28')](respondWithStatusCode(_0x1f0c58,null))['catch'](handleError(_0x1f0c58,null));};exports[_0x3bfd('0x6f')]=function(_0xd14f16,_0x4bd29c,_0x413a61){return db['Team'][_0x3bfd('0x5a')]({'where':{'id':_0xd14f16[_0x3bfd('0x37')][_0x3bfd('0x67')]},'attributes':['id'],'include':[{'model':db[_0x3bfd('0x5b')],'as':_0x3bfd('0x6b'),'attributes':['id'],'raw':!![]}]})[_0x3bfd('0x28')](handleEntityNotFound(_0x4bd29c,null))['then'](function(_0x42cbcb){var _0x2d9432=_[_0x3bfd('0x30')](_0x42cbcb,'id');var _0x2aec16=[];var _0x43a05a=[];var _0x2870ba=squel[_0x3bfd('0x70')]();_0x2870ba[_0x3bfd('0x71')](_0x3bfd('0x72'))[_0x3bfd('0x73')](_0x3bfd('0x74'),'tq')[_0x3bfd('0x75')](_0x3bfd('0x76'),'ut',_0x3bfd('0x77'))[_0x3bfd('0x40')](_0x3bfd('0x78'),_0xd14f16[_0x3bfd('0x45')]['id']);for(var _0x467be8=0x0;_0x467be8<_0x42cbcb[_0x3bfd('0x3c')];_0x467be8+=0x1){let _0x10bebe=_0x42cbcb[_0x467be8];for(var _0x585296=0x0;_0x585296<_0x10bebe[_0x3bfd('0x6b')][_0x3bfd('0x3c')];_0x585296+=0x1){let _0x165149=_0x10bebe[_0x3bfd('0x6b')][_0x585296];var _0x29d064=_0x2870ba[_0x3bfd('0x79')]();_0x29d064[_0x3bfd('0x40')]('ut.UserId\x20=\x20?',_0x165149['id']);_0x43a05a['push'](db[_0x3bfd('0x7a')][_0x3bfd('0x37')](_0x29d064[_0x3bfd('0x7b')](),{'type':db[_0x3bfd('0x7a')]['QueryTypes'][_0x3bfd('0x7c')]})[_0x3bfd('0x28')](function(_0x289afe){if(_0x289afe[_0x3bfd('0x3c')]===0x1){return _0x165149['id'];}else{var _0x1e3a01=_['every'](_[_0x3bfd('0x30')](_0x289afe,_0x3bfd('0x7d')),function(_0x23dc9a){return _[_0x3bfd('0x7e')](_0x2d9432,_0x23dc9a);});if(_0x1e3a01){return _0x165149['id'];}}}));}}return BPromise[_0x3bfd('0x7f')](_0x43a05a)[_0x3bfd('0x28')](function(_0x2b5a49){_0x2aec16=_(_0x2aec16)[_0x3bfd('0x80')](_0x2b5a49)[_0x3bfd('0x81')]()[_0x3bfd('0x82')]();return db[_0x3bfd('0x31')][_0x3bfd('0x48')]({'where':{'id':_0xd14f16[_0x3bfd('0x45')]['id']}})[_0x3bfd('0x28')](function(_0x44a9a1){return db[_0x3bfd('0x7a')]['transaction'](function(_0x1b77ed){return _0x44a9a1[_0x3bfd('0x6f')](_0xd14f16[_0x3bfd('0x37')][_0x3bfd('0x67')],{'transaction':_0x1b77ed})[_0x3bfd('0x28')](function(){if(!_[_0x3bfd('0x83')](_0x2aec16)){return _0x44a9a1[_0x3bfd('0x84')](_0x2aec16,{'transaction':_0x1b77ed});}})['then'](function(){_0x2aec16['forEach'](function(_0x293627){socket[_0x3bfd('0x85')](_0x3bfd('0x86'),{'UserId':_0x293627,'WhatsappQueueId':_0x44a9a1['id']});});});});});});})['then'](respondWithStatusCode(_0x4bd29c,null))[_0x3bfd('0x44')](handleError(_0x4bd29c,null));};exports[_0x3bfd('0x87')]=function(_0xedb91d,_0x8dc275,_0x5ccd34){return db['WhatsappQueue'][_0x3bfd('0x48')]({'where':{'id':_0xedb91d['params']['id']}})[_0x3bfd('0x28')](handleEntityNotFound(_0x8dc275,null))['then'](function(_0x339dfa){if(_0x339dfa){return _0x339dfa[_0x3bfd('0x87')](_0xedb91d[_0x3bfd('0x4a')][_0x3bfd('0x67')],_[_0x3bfd('0x88')](_0xedb91d[_0x3bfd('0x4a')],[_0x3bfd('0x67'),'id'])||{})['spread'](function(_0x1ccbc8){for(var _0xf164f1=0x0;_0xf164f1<_0xedb91d[_0x3bfd('0x4a')]['ids']['length'];_0xf164f1+=0x1){socket[_0x3bfd('0x85')](_0x3bfd('0x6e'),{'UserId':_0xedb91d[_0x3bfd('0x4a')][_0x3bfd('0x67')][_0xf164f1],'WhatsappQueueId':_0xedb91d[_0x3bfd('0x45')]['id']});}return _0x1ccbc8;});}})[_0x3bfd('0x28')](respondWithResult(_0x8dc275,null))[_0x3bfd('0x44')](handleError(_0x8dc275,null));};exports['removeAgents']=function(_0x3c5240,_0x4438a4,_0x484e2f){return db[_0x3bfd('0x31')][_0x3bfd('0x48')]({'where':{'id':_0x3c5240['params']['id']}})['then'](handleEntityNotFound(_0x4438a4,null))['then'](function(_0x166c99){if(_0x166c99){return _0x166c99['removeAgents'](_0x3c5240['query'][_0x3bfd('0x67')])['then'](function(){if(_[_0x3bfd('0x89')](_0x3c5240[_0x3bfd('0x37')][_0x3bfd('0x67')])){for(var _0x43298c=0x0;_0x43298c<_0x3c5240[_0x3bfd('0x37')][_0x3bfd('0x67')][_0x3bfd('0x3c')];_0x43298c+=0x1){socket[_0x3bfd('0x85')](_0x3bfd('0x86'),{'UserId':Number(_0x3c5240['query'][_0x3bfd('0x67')][_0x43298c]),'WhatsappQueueId':Number(_0x3c5240[_0x3bfd('0x45')]['id'])});}}else{socket[_0x3bfd('0x85')](_0x3bfd('0x86'),{'UserId':Number(_0x3c5240['query']['ids']),'WhatsappQueueId':Number(_0x3c5240[_0x3bfd('0x45')]['id'])});}});}})['then'](respondWithStatusCode(_0x4438a4,null))[_0x3bfd('0x44')](handleError(_0x4438a4,null));};exports[_0x3bfd('0x8a')]=function(_0x5efc4d,_0x5c0953,_0x391143){var _0x1f3279={};var _0x2a597a={};var _0x410c56;var _0x485136;return db['WhatsappQueue']['findOne']({'where':{'id':_0x5efc4d[_0x3bfd('0x45')]['id']}})[_0x3bfd('0x28')](handleEntityNotFound(_0x5c0953,null))[_0x3bfd('0x28')](function(_0x299ab5){if(_0x299ab5){_0x410c56=_0x299ab5;_0x2a597a[_0x3bfd('0x3a')]=_[_0x3bfd('0x36')](db['User'][_0x3bfd('0x32')]);_0x2a597a[_0x3bfd('0x37')]=_['keys'](_0x5efc4d[_0x3bfd('0x37')]);_0x2a597a['filters']=_[_0x3bfd('0x39')](_0x2a597a['model'],_0x2a597a[_0x3bfd('0x37')]);_0x1f3279['attributes']=_[_0x3bfd('0x39')](_0x2a597a['model'],qs[_0x3bfd('0x46')](_0x5efc4d[_0x3bfd('0x37')][_0x3bfd('0x46')]));_0x1f3279[_0x3bfd('0x3b')]=_0x1f3279[_0x3bfd('0x3b')][_0x3bfd('0x3c')]?_0x1f3279[_0x3bfd('0x3b')]:_0x2a597a[_0x3bfd('0x3a')];_0x1f3279[_0x3bfd('0x3e')]=qs[_0x3bfd('0x3f')](_0x5efc4d[_0x3bfd('0x37')][_0x3bfd('0x3f')]);_0x1f3279[_0x3bfd('0x40')]=qs[_0x3bfd('0x38')](_['pick'](_0x5efc4d[_0x3bfd('0x37')],_0x2a597a[_0x3bfd('0x38')]));if(_0x5efc4d['query']['filter']){_0x1f3279[_0x3bfd('0x40')]=_['merge'](_0x1f3279[_0x3bfd('0x40')],{'$or':_[_0x3bfd('0x30')](_0x1f3279[_0x3bfd('0x3b')],function(_0x259e5f){var _0x436e17={};_0x436e17[_0x259e5f]={'$like':'%'+_0x5efc4d[_0x3bfd('0x37')]['filter']+'%'};return _0x436e17;})});}_0x1f3279=_[_0x3bfd('0x41')]({},_0x1f3279,_0x5efc4d[_0x3bfd('0x59')]);return _0x410c56['getAgents'](_0x1f3279);}})[_0x3bfd('0x28')](function(_0x32a4e6){if(_0x32a4e6){_0x485136=_0x32a4e6[_0x3bfd('0x3c')];if(!_0x5efc4d[_0x3bfd('0x37')][_0x3bfd('0x56')](_0x3bfd('0x3d'))){_0x1f3279[_0x3bfd('0x21')]=qs[_0x3bfd('0x21')](_0x5efc4d['query'][_0x3bfd('0x21')]);_0x1f3279[_0x3bfd('0x20')]=qs[_0x3bfd('0x20')](_0x5efc4d['query']['offset']);}return _0x410c56[_0x3bfd('0x8a')](_0x1f3279);}})[_0x3bfd('0x28')](function(_0x1de869){if(_0x1de869){return _0x1de869?{'count':_0x485136,'rows':_0x1de869}:null;}})['then'](respondWithResult(_0x5c0953,null))[_0x3bfd('0x44')](handleError(_0x5c0953,null));};