b433ed5b242a6951c65fc15d44af6fb099c90b8e
[motion2.git] / server / api / chatQueue / chatQueue.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 _0xab7e=['register','client','request','then','info','request\x20sent','debug','ChatQueue,\x20%s,\x20%s,\x20%s','stringify','error','code','ChatQueue,\x20%s,\x20%s','message','result','catch','status','json','undefined','limit','offset','count','set','Content-Range','reject','save','destroy','ChatQueues','UserProfileResource','end','sendStatus','name','send','index','map','rawAttributes','type','key','model','query','keys','attributes','fields','hasOwnProperty','sort','where','filters','pick','filter','merge','VIRTUAL','options','includeAll','include','ChatQueue','rows','show','params','length','find','create','body','user','get','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','describe','getMembers','findOne','intersection','nolimit','order','ChatQueueId','UserChatQueue','findAll','findAndCountAll','agent','chatPause','createdAt','format','SIP','updatedAt','Team','getTeams','addTeams','ids','Agents','online','voicePause','interface','flatMap','transaction','each','findOrCreate','forEach','userChatQueue:save','User','select','field','join','user_has_teams','ut.TeamId\x20=\x20tq.TeamId','ChatQueueId\x20=\x20?','ut.UserId\x20=\x20?','push','sequelize','toString','QueryTypes','every','TeamId','includes','all','union','compact','value','removeTeams','isEmpty','removeAgents','emit','userChatQueue:remove','addAgents','omit','spread','getAgents','eml-format','rimraf','zip-dir','request-promise','moment','bluebird','mustache','util','sox','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/license/util','../../mysqldb','defaults','redis','localhost','socket.io-emitter'];(function(_0x2f1114,_0x4561f1){var _0x2fe856=function(_0x8397f8){while(--_0x8397f8){_0x2f1114['push'](_0x2f1114['shift']());}};_0x2fe856(++_0x4561f1);}(_0xab7e,0x11b));var _0xeab7=function(_0x508376,_0x3aac03){_0x508376=_0x508376-0x0;var _0x1508d2=_0xab7e[_0x508376];return _0x1508d2;};'use strict';var emlformat=require(_0xeab7('0x0'));var rimraf=require(_0xeab7('0x1'));var zipdir=require(_0xeab7('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0xeab7('0x3'));var moment=require(_0xeab7('0x4'));var BPromise=require(_0xeab7('0x5'));var Mustache=require(_0xeab7('0x6'));var util=require(_0xeab7('0x7'));var path=require('path');var sox=require(_0xeab7('0x8'));var csv=require('to-csv');var ejs=require(_0xeab7('0x9'));var fs=require('fs');var fs_extra=require(_0xeab7('0xa'));var _=require(_0xeab7('0xb'));var squel=require(_0xeab7('0xc'));var crypto=require(_0xeab7('0xd'));var jsforce=require(_0xeab7('0xe'));var deskjs=require(_0xeab7('0xf'));var toCsv=require('to-csv');var querystring=require(_0xeab7('0x10'));var Papa=require(_0xeab7('0x11'));var Redis=require(_0xeab7('0x12'));var authService=require(_0xeab7('0x13'));var qs=require(_0xeab7('0x14'));var as=require(_0xeab7('0x15'));var hardwareService=require(_0xeab7('0x16'));var logger=require('../../config/logger')(_0xeab7('0x17'));var utils=require(_0xeab7('0x18'));var config=require('../../config/environment');var licenseUtil=require(_0xeab7('0x19'));var db=require(_0xeab7('0x1a'))['db'];config['redis']=_[_0xeab7('0x1b')](config[_0xeab7('0x1c')],{'host':_0xeab7('0x1d'),'port':0x18eb});var socket=require(_0xeab7('0x1e'))(new Redis(config['redis']));require('./chatQueue.socket')[_0xeab7('0x1f')](socket);var jayson=require('jayson/promise');var client=jayson[_0xeab7('0x20')]['http']({'port':0x232a});function respondWithRpcPromise(_0x3e342d,_0x4599fa,_0x4c43bc,_0x3cf400){return new BPromise(function(_0x51572f,_0xf82fff){var _0x225e8a=_0x3cf400||client;return _0x225e8a[_0xeab7('0x21')](_0x3e342d,_0x4c43bc)[_0xeab7('0x22')](function(_0x20537e){logger[_0xeab7('0x23')]('ChatQueue,\x20%s,\x20%s',_0x4599fa,_0xeab7('0x24'));logger[_0xeab7('0x25')](_0xeab7('0x26'),_0x4599fa,'request\x20sent',JSON[_0xeab7('0x27')](_0x20537e));if(_0x20537e[_0xeab7('0x28')]){if(_0x20537e[_0xeab7('0x28')][_0xeab7('0x29')]===0x1f4){logger[_0xeab7('0x28')](_0xeab7('0x2a'),_0x4599fa,_0x20537e[_0xeab7('0x28')]['message']);return _0xf82fff(_0x20537e[_0xeab7('0x28')][_0xeab7('0x2b')]);}logger[_0xeab7('0x28')]('ChatQueue,\x20%s,\x20%s',_0x4599fa,_0x20537e[_0xeab7('0x28')]['message']);return _0x51572f(_0x20537e['error']['message']);}else{logger['info'](_0xeab7('0x2a'),_0x4599fa,'request\x20sent');_0x51572f(_0x20537e[_0xeab7('0x2c')][_0xeab7('0x2b')]);}})[_0xeab7('0x2d')](function(_0x2b1158){logger['error'](_0xeab7('0x2a'),_0x4599fa,_0x2b1158);_0xf82fff(_0x2b1158);});});}function respondWithStatusCode(_0x23fc36,_0x45a5bd){_0x45a5bd=_0x45a5bd||0xcc;return function(_0x576b7c){if(_0x576b7c){return _0x23fc36['sendStatus'](_0x45a5bd);}return _0x23fc36[_0xeab7('0x2e')](_0x45a5bd)['end']();};}function respondWithResult(_0x2c36d0,_0x4b4344){_0x4b4344=_0x4b4344||0xc8;return function(_0x21a9fe){if(_0x21a9fe){return _0x2c36d0[_0xeab7('0x2e')](_0x4b4344)[_0xeab7('0x2f')](_0x21a9fe);}};}function respondWithFilteredResult(_0x4d68ae,_0x599845){return function(_0x1e7db1){if(_0x1e7db1){var _0x444c18=typeof _0x599845['offset']===_0xeab7('0x30')&&typeof _0x599845[_0xeab7('0x31')]===_0xeab7('0x30');var _0x37abb1=_0x1e7db1['count'];var _0x1fadc5=_0x444c18?0x0:_0x599845[_0xeab7('0x32')];var _0x5b8e4f=_0x444c18?_0x1e7db1[_0xeab7('0x33')]:_0x599845[_0xeab7('0x32')]+_0x599845[_0xeab7('0x31')];var _0x474d73;if(_0x5b8e4f>=_0x37abb1){_0x5b8e4f=_0x37abb1;_0x474d73=0xc8;}else{_0x474d73=0xce;}_0x4d68ae[_0xeab7('0x2e')](_0x474d73);return _0x4d68ae[_0xeab7('0x34')](_0xeab7('0x35'),_0x1fadc5+'-'+_0x5b8e4f+'/'+_0x37abb1)[_0xeab7('0x2f')](_0x1e7db1);}return null;};}function patchUpdates(_0x5921e9){return function(_0x27a4cc){try{jsonpatch['apply'](_0x27a4cc,_0x5921e9,!![]);}catch(_0x21ec98){return BPromise[_0xeab7('0x36')](_0x21ec98);}return _0x27a4cc[_0xeab7('0x37')]();};}function saveUpdates(_0x1a4fb3,_0x788a8d){return function(_0x1f75b7){if(_0x1f75b7){return _0x1f75b7['update'](_0x1a4fb3)[_0xeab7('0x22')](function(_0x48a514){return _0x48a514;});}return null;};}function removeEntity(_0x2582d9,_0x5c64f1){return function(_0x353b24){if(_0x353b24){return _0x353b24[_0xeab7('0x38')]()[_0xeab7('0x22')](function(){var _0x160d63=_0x353b24['get']({'plain':!![]});var _0x26daf6=_0xeab7('0x39');return db[_0xeab7('0x3a')]['destroy']({'where':{'type':_0x26daf6,'resourceId':_0x160d63['id']}})[_0xeab7('0x22')](function(){return _0x353b24;});})[_0xeab7('0x22')](function(){_0x2582d9[_0xeab7('0x2e')](0xcc)[_0xeab7('0x3b')]();});}};}function handleEntityNotFound(_0x3974be,_0x11fadd){return function(_0x406908){if(!_0x406908){_0x3974be[_0xeab7('0x3c')](0x194);}return _0x406908;};}function handleError(_0x26fbb1,_0xb05e1f){_0xb05e1f=_0xb05e1f||0x1f4;return function(_0x46ec58){logger[_0xeab7('0x28')](_0x46ec58['stack']);if(_0x46ec58[_0xeab7('0x3d')]){delete _0x46ec58[_0xeab7('0x3d')];}_0x26fbb1[_0xeab7('0x2e')](_0xb05e1f)[_0xeab7('0x3e')](_0x46ec58);};}exports[_0xeab7('0x3f')]=function(_0x3d3212,_0xb0ad77){var _0xa2adf8={},_0xc20c08={},_0x59fa8e={'count':0x0,'rows':[]};var _0x309259=_[_0xeab7('0x40')](db['ChatQueue'][_0xeab7('0x41')],function(_0x2b4155){return{'name':_0x2b4155['fieldName'],'type':_0x2b4155[_0xeab7('0x42')][_0xeab7('0x43')]};});_0xc20c08[_0xeab7('0x44')]=_['map'](_0x309259,_0xeab7('0x3d'));_0xc20c08[_0xeab7('0x45')]=_[_0xeab7('0x46')](_0x3d3212[_0xeab7('0x45')]);_0xc20c08['filters']=_['intersection'](_0xc20c08[_0xeab7('0x44')],_0xc20c08['query']);_0xa2adf8[_0xeab7('0x47')]=_['intersection'](_0xc20c08[_0xeab7('0x44')],qs[_0xeab7('0x48')](_0x3d3212[_0xeab7('0x45')][_0xeab7('0x48')]));_0xa2adf8['attributes']=_0xa2adf8[_0xeab7('0x47')]['length']?_0xa2adf8[_0xeab7('0x47')]:_0xc20c08[_0xeab7('0x44')];if(!_0x3d3212[_0xeab7('0x45')][_0xeab7('0x49')]('nolimit')){_0xa2adf8[_0xeab7('0x31')]=qs['limit'](_0x3d3212['query'][_0xeab7('0x31')]);_0xa2adf8['offset']=qs[_0xeab7('0x32')](_0x3d3212[_0xeab7('0x45')][_0xeab7('0x32')]);}_0xa2adf8['order']=qs[_0xeab7('0x4a')](_0x3d3212[_0xeab7('0x45')]['sort']);_0xa2adf8[_0xeab7('0x4b')]=qs[_0xeab7('0x4c')](_[_0xeab7('0x4d')](_0x3d3212[_0xeab7('0x45')],_0xc20c08[_0xeab7('0x4c')]),_0x309259);if(_0x3d3212['query'][_0xeab7('0x4e')]){_0xa2adf8[_0xeab7('0x4b')]=_[_0xeab7('0x4f')](_0xa2adf8[_0xeab7('0x4b')],{'$or':_[_0xeab7('0x40')](_0x309259,function(_0x2d524b){if(_0x2d524b[_0xeab7('0x42')]!==_0xeab7('0x50')){var _0x1e3ecb={};_0x1e3ecb[_0x2d524b[_0xeab7('0x3d')]]={'$like':'%'+_0x3d3212[_0xeab7('0x45')][_0xeab7('0x4e')]+'%'};return _0x1e3ecb;}})});}_0xa2adf8=_['merge']({},_0xa2adf8,_0x3d3212[_0xeab7('0x51')]);var _0x182f56={'where':_0xa2adf8[_0xeab7('0x4b')]};return db['ChatQueue']['count'](_0x182f56)[_0xeab7('0x22')](function(_0x537570){_0x59fa8e[_0xeab7('0x33')]=_0x537570;if(_0x3d3212[_0xeab7('0x45')][_0xeab7('0x52')]){_0xa2adf8[_0xeab7('0x53')]=[{'all':!![]}];}return db[_0xeab7('0x54')]['findAll'](_0xa2adf8);})[_0xeab7('0x22')](function(_0x49b56a){_0x59fa8e[_0xeab7('0x55')]=_0x49b56a;return _0x59fa8e;})['then'](respondWithFilteredResult(_0xb0ad77,_0xa2adf8))['catch'](handleError(_0xb0ad77,null));};exports[_0xeab7('0x56')]=function(_0x4d84d4,_0x48de53){var _0x73a9db={'raw':!![],'where':{'id':_0x4d84d4[_0xeab7('0x57')]['id']}},_0x407b55={};_0x407b55[_0xeab7('0x44')]=_[_0xeab7('0x46')](db[_0xeab7('0x54')]['rawAttributes']);_0x407b55[_0xeab7('0x45')]=_['keys'](_0x4d84d4[_0xeab7('0x45')]);_0x407b55[_0xeab7('0x4c')]=_['intersection'](_0x407b55[_0xeab7('0x44')],_0x407b55[_0xeab7('0x45')]);_0x73a9db[_0xeab7('0x47')]=_['intersection'](_0x407b55[_0xeab7('0x44')],qs[_0xeab7('0x48')](_0x4d84d4[_0xeab7('0x45')]['fields']));_0x73a9db['attributes']=_0x73a9db[_0xeab7('0x47')][_0xeab7('0x58')]?_0x73a9db[_0xeab7('0x47')]:_0x407b55[_0xeab7('0x44')];if(_0x4d84d4['query'][_0xeab7('0x52')]){_0x73a9db[_0xeab7('0x53')]=[{'all':!![]}];}_0x73a9db=_[_0xeab7('0x4f')]({},_0x73a9db,_0x4d84d4[_0xeab7('0x51')]);return db[_0xeab7('0x54')][_0xeab7('0x59')](_0x73a9db)[_0xeab7('0x22')](handleEntityNotFound(_0x48de53,null))[_0xeab7('0x22')](respondWithResult(_0x48de53,null))[_0xeab7('0x2d')](handleError(_0x48de53,null));};exports['create']=function(_0xc0d3fc,_0x2ae992){return db[_0xeab7('0x54')][_0xeab7('0x5a')](_0xc0d3fc[_0xeab7('0x5b')],{})[_0xeab7('0x22')](function(_0x8aded1){var _0x5b11e9=_0xc0d3fc[_0xeab7('0x5c')][_0xeab7('0x5d')]({'plain':!![]});if(!_0x5b11e9)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x5b11e9['role']===_0xeab7('0x5c')){var _0xf53092=_0x8aded1[_0xeab7('0x5d')]({'plain':!![]});var _0x497a9b='ChatQueues';return db[_0xeab7('0x5e')]['find']({'where':{'name':_0x497a9b,'userProfileId':_0x5b11e9[_0xeab7('0x5f')]},'raw':!![]})['then'](function(_0x46432d){if(_0x46432d&&_0x46432d[_0xeab7('0x60')]===0x0){return db[_0xeab7('0x3a')][_0xeab7('0x5a')]({'name':_0xf53092[_0xeab7('0x3d')],'resourceId':_0xf53092['id'],'type':_0x46432d[_0xeab7('0x3d')],'sectionId':_0x46432d['id']},{})[_0xeab7('0x22')](function(){return _0x8aded1;});}else{return _0x8aded1;}})['catch'](function(_0x167752){logger[_0xeab7('0x28')](_0xeab7('0x61'),_0x167752);throw _0x167752;});}return _0x8aded1;})[_0xeab7('0x22')](respondWithResult(_0x2ae992,0xc9))[_0xeab7('0x2d')](handleError(_0x2ae992,null));};exports[_0xeab7('0x62')]=function(_0x522568,_0x32d271){if(_0x522568[_0xeab7('0x5b')]['id']){delete _0x522568[_0xeab7('0x5b')]['id'];}return db['ChatQueue'][_0xeab7('0x59')]({'where':{'id':_0x522568['params']['id']}})[_0xeab7('0x22')](handleEntityNotFound(_0x32d271,null))[_0xeab7('0x22')](saveUpdates(_0x522568[_0xeab7('0x5b')],null))[_0xeab7('0x22')](respondWithResult(_0x32d271,null))[_0xeab7('0x2d')](handleError(_0x32d271,null));};exports[_0xeab7('0x38')]=function(_0x47f65d,_0x3abc92){return db[_0xeab7('0x54')]['find']({'where':{'id':_0x47f65d[_0xeab7('0x57')]['id']}})[_0xeab7('0x22')](handleEntityNotFound(_0x3abc92,null))[_0xeab7('0x22')](removeEntity(_0x3abc92,null))['catch'](handleError(_0x3abc92,null));};exports[_0xeab7('0x63')]=function(_0x4b9216,_0x4e1329){return db[_0xeab7('0x54')][_0xeab7('0x63')]()[_0xeab7('0x22')](respondWithResult(_0x4e1329,null))[_0xeab7('0x2d')](handleError(_0x4e1329,null));};exports[_0xeab7('0x64')]=function(_0x150527,_0xc4efbf,_0xb337e1){var _0x3adef5={'raw':!![],'where':{}},_0x1477fa={},_0x310862;return db[_0xeab7('0x54')][_0xeab7('0x65')]({'where':{'id':_0x150527[_0xeab7('0x57')]['id']}})[_0xeab7('0x22')](handleEntityNotFound(_0xc4efbf,null))[_0xeab7('0x22')](function(_0x3379e9){if(_0x3379e9){_0x310862=_0x3379e9[_0xeab7('0x5d')]({'plain':!![]});_0x1477fa[_0xeab7('0x44')]=_[_0xeab7('0x46')](db['UserChatQueue']['rawAttributes']);_0x1477fa[_0xeab7('0x45')]=_['keys'](_0x150527['query']);_0x1477fa[_0xeab7('0x4c')]=_[_0xeab7('0x66')](_0x1477fa[_0xeab7('0x44')],_0x1477fa[_0xeab7('0x45')]);_0x3adef5[_0xeab7('0x47')]=_[_0xeab7('0x66')](_0x1477fa[_0xeab7('0x44')],qs[_0xeab7('0x48')](_0x150527[_0xeab7('0x45')][_0xeab7('0x48')]));_0x3adef5[_0xeab7('0x47')]=_0x3adef5[_0xeab7('0x47')][_0xeab7('0x58')]?_0x3adef5[_0xeab7('0x47')]:_0x1477fa[_0xeab7('0x44')];if(!_0x150527[_0xeab7('0x45')][_0xeab7('0x49')](_0xeab7('0x67'))){_0x3adef5[_0xeab7('0x31')]=qs['limit'](_0x150527[_0xeab7('0x45')][_0xeab7('0x31')]);_0x3adef5[_0xeab7('0x32')]=qs[_0xeab7('0x32')](_0x150527[_0xeab7('0x45')][_0xeab7('0x32')]);}_0x3adef5[_0xeab7('0x68')]=qs[_0xeab7('0x4a')](_0x150527['query']['sort']);_0x3adef5['where']=qs['filters'](_[_0xeab7('0x4d')](_0x150527[_0xeab7('0x45')],_0x1477fa[_0xeab7('0x4c')]));_0x3adef5[_0xeab7('0x4b')][_0xeab7('0x69')]=_0x3379e9['id'];if(_0x150527[_0xeab7('0x45')][_0xeab7('0x4e')]){_0x3adef5[_0xeab7('0x4b')]=_[_0xeab7('0x4f')](_0x3adef5['where'],{'$or':_[_0xeab7('0x40')](_0x3adef5['attributes'],function(_0x4dd4f7){var _0x2a7877={};_0x2a7877[_0x4dd4f7]={'$like':'%'+_0x150527[_0xeab7('0x45')][_0xeab7('0x4e')]+'%'};return _0x2a7877;})});}_0x3adef5=_[_0xeab7('0x4f')]({},_0x3adef5,_0x150527[_0xeab7('0x51')]);return db[_0xeab7('0x6a')][_0xeab7('0x6b')](_0x3adef5);}})[_0xeab7('0x22')](function(_0x4a4e3e){if(_0x4a4e3e){return db['User'][_0xeab7('0x6c')]({'where':{'id':_[_0xeab7('0x40')](_0x4a4e3e,'UserId'),'role':_0xeab7('0x6d')},'attributes':['id',_0xeab7('0x3d'),_0xeab7('0x6e'),'updatedAt',_0xeab7('0x6f')]});}})['then'](function(_0x3dd5af){if(_0x3dd5af){return{'count':_0x3dd5af[_0xeab7('0x33')],'rows':_[_0xeab7('0x40')](_0x3dd5af[_0xeab7('0x55')],function(_0x27f5b2){return{'membername':_0x27f5b2[_0xeab7('0x3d')],'UserId':_0x27f5b2['id'],'queue_name':_0x310862[_0xeab7('0x3d')],'ChatQueueId':_0x310862['id'],'interface':util[_0xeab7('0x70')]('%s/%s',_0xeab7('0x71'),_0x27f5b2[_0xeab7('0x3d')]),'penalty':0x0,'paused':_0x27f5b2['chatPause'],'createdAt':_0x27f5b2[_0xeab7('0x6f')],'updatedAt':_0x27f5b2[_0xeab7('0x72')]};})};}else{return{'count':0x0,'rows':[]};}})['then'](respondWithFilteredResult(_0xc4efbf,_0x3adef5))[_0xeab7('0x2d')](handleError(_0xc4efbf,null));};exports['getTeams']=function(_0x114bfb,_0x4a4c3f,_0x11faae){var _0x526c93={};var _0x378dbc={};var _0x188c78;var _0x12e9d4;return db[_0xeab7('0x54')][_0xeab7('0x65')]({'where':{'id':_0x114bfb[_0xeab7('0x57')]['id']}})[_0xeab7('0x22')](handleEntityNotFound(_0x4a4c3f,null))[_0xeab7('0x22')](function(_0x12b76d){if(_0x12b76d){_0x188c78=_0x12b76d;_0x378dbc['model']=_[_0xeab7('0x46')](db[_0xeab7('0x73')]['rawAttributes']);_0x378dbc[_0xeab7('0x45')]=_[_0xeab7('0x46')](_0x114bfb[_0xeab7('0x45')]);_0x378dbc[_0xeab7('0x4c')]=_[_0xeab7('0x66')](_0x378dbc[_0xeab7('0x44')],_0x378dbc['query']);_0x526c93[_0xeab7('0x47')]=_['intersection'](_0x378dbc[_0xeab7('0x44')],qs[_0xeab7('0x48')](_0x114bfb[_0xeab7('0x45')][_0xeab7('0x48')]));_0x526c93[_0xeab7('0x47')]=_0x526c93[_0xeab7('0x47')][_0xeab7('0x58')]?_0x526c93[_0xeab7('0x47')]:_0x378dbc[_0xeab7('0x44')];_0x526c93[_0xeab7('0x68')]=qs['sort'](_0x114bfb['query'][_0xeab7('0x4a')]);_0x526c93['where']=qs[_0xeab7('0x4c')](_[_0xeab7('0x4d')](_0x114bfb[_0xeab7('0x45')],_0x378dbc[_0xeab7('0x4c')]));if(_0x114bfb[_0xeab7('0x45')][_0xeab7('0x4e')]){_0x526c93[_0xeab7('0x4b')]=_[_0xeab7('0x4f')](_0x526c93[_0xeab7('0x4b')],{'$or':_[_0xeab7('0x40')](_0x526c93['attributes'],function(_0x3cb3ef){var _0x47ee4b={};_0x47ee4b[_0x3cb3ef]={'$like':'%'+_0x114bfb[_0xeab7('0x45')][_0xeab7('0x4e')]+'%'};return _0x47ee4b;})});}_0x526c93=_[_0xeab7('0x4f')]({},_0x526c93,_0x114bfb['options']);return _0x188c78[_0xeab7('0x74')](_0x526c93);}})[_0xeab7('0x22')](function(_0x1e4fd6){if(_0x1e4fd6){_0x12e9d4=_0x1e4fd6['length'];if(!_0x114bfb[_0xeab7('0x45')][_0xeab7('0x49')](_0xeab7('0x67'))){_0x526c93['limit']=qs['limit'](_0x114bfb['query']['limit']);_0x526c93[_0xeab7('0x32')]=qs['offset'](_0x114bfb['query'][_0xeab7('0x32')]);}return _0x188c78[_0xeab7('0x74')](_0x526c93);}})[_0xeab7('0x22')](function(_0x1d093){if(_0x1d093){return _0x1d093?{'count':_0x12e9d4,'rows':_0x1d093}:null;}})[_0xeab7('0x22')](respondWithResult(_0x4a4c3f,null))[_0xeab7('0x2d')](handleError(_0x4a4c3f,null));};exports[_0xeab7('0x75')]=function(_0x5556d7,_0x4df002,_0x32d55e){var _0x113689=_0x5556d7[_0xeab7('0x5b')][_0xeab7('0x76')];return db['Team']['findAll']({'where':{'id':_0x113689},'attributes':['id'],'include':[{'model':db['User'],'as':_0xeab7('0x77'),'attributes':['id',_0xeab7('0x3d'),_0xeab7('0x78'),_0xeab7('0x79'),_0xeab7('0x7a')],'raw':!![]}]})[_0xeab7('0x22')](function(_0x4e6215){if(_0x4e6215){var _0x1e0ba1=_[_0xeab7('0x7b')](_0x4e6215,function(_0x460cfa){var _0x432c47=_0x460cfa['get']({'plain':!![]});return _0x432c47['Agents'];});return db['ChatQueue'][_0xeab7('0x59')]({'where':{'id':_0x5556d7[_0xeab7('0x57')]['id']}})[_0xeab7('0x22')](function(_0x46c9da){return db['sequelize'][_0xeab7('0x7c')](function(_0x5c0c9a){return _0x46c9da['addTeams'](_0x113689,{'transaction':_0x5c0c9a})[_0xeab7('0x22')](function(){return BPromise[_0xeab7('0x7d')](_0x1e0ba1,function(_0x3eadcb){return db[_0xeab7('0x6a')][_0xeab7('0x7e')]({'where':{'UserId':_0x3eadcb['id'],'ChatQueueId':_0x5556d7['params']['id']},'transaction':_0x5c0c9a});});})[_0xeab7('0x22')](function(){_0x1e0ba1[_0xeab7('0x7f')](function(_0x76ea82){socket['emit'](_0xeab7('0x80'),{'UserId':_0x76ea82['id'],'ChatQueueId':_0x46c9da['id']});});});});});}})[_0xeab7('0x22')](respondWithStatusCode(_0x4df002,null))[_0xeab7('0x2d')](handleError(_0x4df002,null));};exports['removeTeams']=function(_0x2edf44,_0x2eddaf,_0x39528f){return db[_0xeab7('0x73')][_0xeab7('0x6b')]({'where':{'id':_0x2edf44[_0xeab7('0x45')]['ids']},'attributes':['id'],'include':[{'model':db[_0xeab7('0x81')],'as':'Agents','attributes':['id'],'raw':!![]}]})['then'](handleEntityNotFound(_0x2eddaf,null))[_0xeab7('0x22')](function(_0x11e678){var _0x18193f=_[_0xeab7('0x40')](_0x11e678,'id');var _0x1fad7f=[];var _0x52e8eb=[];var _0x506547=squel[_0xeab7('0x82')]();_0x506547[_0xeab7('0x83')]('tq.TeamId')['from']('team_has_chat_queues','tq')[_0xeab7('0x84')](_0xeab7('0x85'),'ut',_0xeab7('0x86'))[_0xeab7('0x4b')](_0xeab7('0x87'),_0x2edf44[_0xeab7('0x57')]['id']);for(var _0x180094=0x0;_0x180094<_0x11e678[_0xeab7('0x58')];_0x180094+=0x1){let _0x536381=_0x11e678[_0x180094];for(var _0x43062a=0x0;_0x43062a<_0x536381[_0xeab7('0x77')][_0xeab7('0x58')];_0x43062a+=0x1){let _0x3692ea=_0x536381[_0xeab7('0x77')][_0x43062a];var _0x2547cf=_0x506547['clone']();_0x2547cf[_0xeab7('0x4b')](_0xeab7('0x88'),_0x3692ea['id']);_0x52e8eb[_0xeab7('0x89')](db[_0xeab7('0x8a')][_0xeab7('0x45')](_0x2547cf[_0xeab7('0x8b')](),{'type':db[_0xeab7('0x8a')][_0xeab7('0x8c')]['SELECT']})['then'](function(_0x394771){if(_0x394771[_0xeab7('0x58')]===0x1){return _0x3692ea['id'];}else{var _0x5145a3=_[_0xeab7('0x8d')](_['map'](_0x394771,_0xeab7('0x8e')),function(_0x2ef75f){return _[_0xeab7('0x8f')](_0x18193f,_0x2ef75f);});if(_0x5145a3){return _0x3692ea['id'];}}}));}}return BPromise[_0xeab7('0x90')](_0x52e8eb)[_0xeab7('0x22')](function(_0x4932b1){_0x1fad7f=_(_0x1fad7f)[_0xeab7('0x91')](_0x4932b1)[_0xeab7('0x92')]()[_0xeab7('0x93')]();return db[_0xeab7('0x54')][_0xeab7('0x59')]({'where':{'id':_0x2edf44[_0xeab7('0x57')]['id']}})[_0xeab7('0x22')](function(_0x1336ae){return db[_0xeab7('0x8a')][_0xeab7('0x7c')](function(_0xbee7dc){return _0x1336ae[_0xeab7('0x94')](_0x2edf44['query'][_0xeab7('0x76')],{'transaction':_0xbee7dc})[_0xeab7('0x22')](function(){if(!_[_0xeab7('0x95')](_0x1fad7f)){return _0x1336ae[_0xeab7('0x96')](_0x1fad7f,{'transaction':_0xbee7dc});}})[_0xeab7('0x22')](function(){_0x1fad7f[_0xeab7('0x7f')](function(_0xfdcc5){socket[_0xeab7('0x97')](_0xeab7('0x98'),{'UserId':_0xfdcc5,'ChatQueueId':_0x1336ae['id']});});});});});});})['then'](respondWithStatusCode(_0x2eddaf,null))[_0xeab7('0x2d')](handleError(_0x2eddaf,null));};exports['addAgents']=function(_0x423460,_0x5ad48a,_0x446fd7){return db[_0xeab7('0x54')][_0xeab7('0x59')]({'where':{'id':_0x423460[_0xeab7('0x57')]['id']}})[_0xeab7('0x22')](handleEntityNotFound(_0x5ad48a,null))[_0xeab7('0x22')](function(_0x8f1c7f){if(_0x8f1c7f){return _0x8f1c7f[_0xeab7('0x99')](_0x423460[_0xeab7('0x5b')][_0xeab7('0x76')],_[_0xeab7('0x9a')](_0x423460['body'],[_0xeab7('0x76'),'id'])||{})[_0xeab7('0x9b')](function(_0x231664){for(var _0x3f92f5=0x0;_0x3f92f5<_0x423460[_0xeab7('0x5b')][_0xeab7('0x76')]['length'];_0x3f92f5+=0x1){socket[_0xeab7('0x97')]('userChatQueue:save',{'UserId':Number(_0x423460[_0xeab7('0x5b')][_0xeab7('0x76')][_0x3f92f5]),'ChatQueueId':Number(_0x423460[_0xeab7('0x57')]['id'])});}return _0x231664;});}})['then'](respondWithResult(_0x5ad48a,null))['catch'](handleError(_0x5ad48a,null));};exports[_0xeab7('0x96')]=function(_0xe4d751,_0x282e8c,_0x8a2fc3){return db[_0xeab7('0x54')][_0xeab7('0x59')]({'where':{'id':_0xe4d751['params']['id']}})[_0xeab7('0x22')](handleEntityNotFound(_0x282e8c,null))[_0xeab7('0x22')](function(_0x45f80a){if(_0x45f80a){return _0x45f80a['removeAgents'](_0xe4d751[_0xeab7('0x45')][_0xeab7('0x76')])[_0xeab7('0x22')](function(){if(_['isArray'](_0xe4d751[_0xeab7('0x45')][_0xeab7('0x76')])){for(var _0x4c3ee4=0x0;_0x4c3ee4<_0xe4d751[_0xeab7('0x45')][_0xeab7('0x76')]['length'];_0x4c3ee4+=0x1){socket[_0xeab7('0x97')]('userChatQueue:remove',{'UserId':Number(_0xe4d751[_0xeab7('0x45')]['ids'][_0x4c3ee4]),'ChatQueueId':Number(_0xe4d751['params']['id'])});}}else{socket[_0xeab7('0x97')](_0xeab7('0x98'),{'UserId':Number(_0xe4d751['query']['ids']),'ChatQueueId':Number(_0xe4d751[_0xeab7('0x57')]['id'])});}});}})[_0xeab7('0x22')](respondWithStatusCode(_0x282e8c,null))[_0xeab7('0x2d')](handleError(_0x282e8c,null));};exports[_0xeab7('0x9c')]=function(_0xc6b693,_0xbc00dd,_0x453c13){var _0x2ba0da={};var _0xcb7b54={};var _0x1e6227;var _0x4025a4;return db[_0xeab7('0x54')][_0xeab7('0x65')]({'where':{'id':_0xc6b693[_0xeab7('0x57')]['id']}})[_0xeab7('0x22')](handleEntityNotFound(_0xbc00dd,null))['then'](function(_0x1efb23){if(_0x1efb23){_0x1e6227=_0x1efb23;_0xcb7b54[_0xeab7('0x44')]=_[_0xeab7('0x46')](db[_0xeab7('0x81')][_0xeab7('0x41')]);_0xcb7b54[_0xeab7('0x45')]=_[_0xeab7('0x46')](_0xc6b693['query']);_0xcb7b54[_0xeab7('0x4c')]=_['intersection'](_0xcb7b54['model'],_0xcb7b54[_0xeab7('0x45')]);_0x2ba0da[_0xeab7('0x47')]=_[_0xeab7('0x66')](_0xcb7b54[_0xeab7('0x44')],qs[_0xeab7('0x48')](_0xc6b693[_0xeab7('0x45')]['fields']));_0x2ba0da['attributes']=_0x2ba0da[_0xeab7('0x47')]['length']?_0x2ba0da['attributes']:_0xcb7b54[_0xeab7('0x44')];_0x2ba0da[_0xeab7('0x68')]=qs[_0xeab7('0x4a')](_0xc6b693[_0xeab7('0x45')][_0xeab7('0x4a')]);_0x2ba0da[_0xeab7('0x4b')]=qs[_0xeab7('0x4c')](_[_0xeab7('0x4d')](_0xc6b693[_0xeab7('0x45')],_0xcb7b54[_0xeab7('0x4c')]));if(_0xc6b693['query'][_0xeab7('0x4e')]){_0x2ba0da[_0xeab7('0x4b')]=_[_0xeab7('0x4f')](_0x2ba0da[_0xeab7('0x4b')],{'$or':_['map'](_0x2ba0da[_0xeab7('0x47')],function(_0x4f3909){var _0x14631a={};_0x14631a[_0x4f3909]={'$like':'%'+_0xc6b693[_0xeab7('0x45')][_0xeab7('0x4e')]+'%'};return _0x14631a;})});}_0x2ba0da=_[_0xeab7('0x4f')]({},_0x2ba0da,_0xc6b693[_0xeab7('0x51')]);return _0x1e6227[_0xeab7('0x9c')](_0x2ba0da);}})['then'](function(_0x54a75e){if(_0x54a75e){_0x4025a4=_0x54a75e[_0xeab7('0x58')];if(!_0xc6b693[_0xeab7('0x45')][_0xeab7('0x49')](_0xeab7('0x67'))){_0x2ba0da[_0xeab7('0x31')]=qs['limit'](_0xc6b693[_0xeab7('0x45')][_0xeab7('0x31')]);_0x2ba0da[_0xeab7('0x32')]=qs[_0xeab7('0x32')](_0xc6b693[_0xeab7('0x45')][_0xeab7('0x32')]);}return _0x1e6227[_0xeab7('0x9c')](_0x2ba0da);}})[_0xeab7('0x22')](function(_0x3dc44d){if(_0x3dc44d){return _0x3dc44d?{'count':_0x4025a4,'rows':_0x3dc44d}:null;}})[_0xeab7('0x22')](respondWithResult(_0xbc00dd,null))[_0xeab7('0x2d')](handleError(_0xbc00dd,null));};