Built motion from commit (unavailable).|2.5.31
[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 _0x6f77=['index','map','rawAttributes','fieldName','type','model','keys','query','intersection','attributes','fields','hasOwnProperty','order','sort','where','filters','pick','filter','merge','MailQueue','includeAll','findAll','catch','show','length','include','options','find','create','body','user','role','UserProfileSection','userProfileId','autoAssociation','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','params','describe','UserMailQueue','MailQueueId','User','findAndCountAll','UserId','mailPause','createdAt','rows','%s/%s','Team','getTeams','addTeams','Agents','online','voicePause','interface','flatMap','sequelize','transaction','each','emit','userMailQueue:save','removeTeams','ids','select','tq.TeamId','team_has_mail_queues','join','user_has_teams','ut.TeamId\x20=\x20tq.TeamId','MailQueueId\x20=\x20?','push','toString','QueryTypes','SELECT','every','TeamId','includes','all','union','compact','isEmpty','removeAgents','forEach','userMailQueue:remove','addAgents','omit','isArray','getAgents','findOne','eml-format','rimraf','zip-dir','bluebird','mustache','util','path','sox','to-csv','fs-extra','lodash','squel','crypto','jsforce','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','redis','defaults','socket.io-emitter','register','sendStatus','status','end','json','offset','undefined','count','limit','set','apply','reject','save','update','then','destroy','get','MailQueues','UserProfileResource','stack','name','send'];(function(_0x1b349a,_0x22222c){var _0x5d1f7c=function(_0x368d7c){while(--_0x368d7c){_0x1b349a['push'](_0x1b349a['shift']());}};_0x5d1f7c(++_0x22222c);}(_0x6f77,0xe4));var _0x76f7=function(_0x5933fb,_0x9115ad){_0x5933fb=_0x5933fb-0x0;var _0x44d988=_0x6f77[_0x5933fb];return _0x44d988;};'use strict';var emlformat=require(_0x76f7('0x0'));var rimraf=require(_0x76f7('0x1'));var zipdir=require(_0x76f7('0x2'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0x76f7('0x3'));var Mustache=require(_0x76f7('0x4'));var util=require(_0x76f7('0x5'));var path=require(_0x76f7('0x6'));var sox=require(_0x76f7('0x7'));var csv=require(_0x76f7('0x8'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x76f7('0x9'));var _=require(_0x76f7('0xa'));var squel=require(_0x76f7('0xb'));var crypto=require(_0x76f7('0xc'));var jsforce=require(_0x76f7('0xd'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0x76f7('0xe'));var Papa=require(_0x76f7('0xf'));var Redis=require(_0x76f7('0x10'));var authService=require(_0x76f7('0x11'));var qs=require(_0x76f7('0x12'));var as=require(_0x76f7('0x13'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x76f7('0x14'))('api');var utils=require(_0x76f7('0x15'));var config=require(_0x76f7('0x16'));var licenseUtil=require(_0x76f7('0x17'));var db=require(_0x76f7('0x18'))['db'];config[_0x76f7('0x19')]=_[_0x76f7('0x1a')](config[_0x76f7('0x19')],{'host':'localhost','port':0x18eb});var socket=require(_0x76f7('0x1b'))(new Redis(config[_0x76f7('0x19')]));require('./mailQueue.socket')[_0x76f7('0x1c')](socket);function respondWithStatusCode(_0x316a0b,_0xb033aa){_0xb033aa=_0xb033aa||0xcc;return function(_0x4ba47f){if(_0x4ba47f){return _0x316a0b[_0x76f7('0x1d')](_0xb033aa);}return _0x316a0b[_0x76f7('0x1e')](_0xb033aa)[_0x76f7('0x1f')]();};}function respondWithResult(_0x41e1c2,_0x304dc0){_0x304dc0=_0x304dc0||0xc8;return function(_0x1d4914){if(_0x1d4914){return _0x41e1c2[_0x76f7('0x1e')](_0x304dc0)[_0x76f7('0x20')](_0x1d4914);}};}function respondWithFilteredResult(_0x15456d,_0x41e8f9){return function(_0x156c6c){if(_0x156c6c){var _0x186f77=typeof _0x41e8f9[_0x76f7('0x21')]===_0x76f7('0x22')&&typeof _0x41e8f9['limit']===_0x76f7('0x22');var _0x48aa8a=_0x156c6c[_0x76f7('0x23')];var _0x2ea896=_0x186f77?0x0:_0x41e8f9['offset'];var _0x2018b1=_0x186f77?_0x156c6c['count']:_0x41e8f9[_0x76f7('0x21')]+_0x41e8f9[_0x76f7('0x24')];var _0x465535;if(_0x2018b1>=_0x48aa8a){_0x2018b1=_0x48aa8a;_0x465535=0xc8;}else{_0x465535=0xce;}_0x15456d['status'](_0x465535);return _0x15456d[_0x76f7('0x25')]('Content-Range',_0x2ea896+'-'+_0x2018b1+'/'+_0x48aa8a)[_0x76f7('0x20')](_0x156c6c);}return null;};}function patchUpdates(_0x13260a){return function(_0x288259){try{jsonpatch[_0x76f7('0x26')](_0x288259,_0x13260a,!![]);}catch(_0x35a372){return BPromise[_0x76f7('0x27')](_0x35a372);}return _0x288259[_0x76f7('0x28')]();};}function saveUpdates(_0x587bda,_0x310232){return function(_0x20189d){if(_0x20189d){return _0x20189d[_0x76f7('0x29')](_0x587bda)[_0x76f7('0x2a')](function(_0x21526a){return _0x21526a;});}return null;};}function removeEntity(_0x497893,_0xcd749d){return function(_0x507460){if(_0x507460){return _0x507460[_0x76f7('0x2b')]()[_0x76f7('0x2a')](function(){var _0x318813=_0x507460[_0x76f7('0x2c')]({'plain':!![]});var _0x4e1f68=_0x76f7('0x2d');return db[_0x76f7('0x2e')][_0x76f7('0x2b')]({'where':{'type':_0x4e1f68,'resourceId':_0x318813['id']}})[_0x76f7('0x2a')](function(){return _0x507460;});})[_0x76f7('0x2a')](function(){_0x497893['status'](0xcc)[_0x76f7('0x1f')]();});}};}function handleEntityNotFound(_0x3a9bdf,_0x525ab1){return function(_0x3cef32){if(!_0x3cef32){_0x3a9bdf[_0x76f7('0x1d')](0x194);}return _0x3cef32;};}function handleError(_0x326760,_0x166faf){_0x166faf=_0x166faf||0x1f4;return function(_0x578c2b){logger['error'](_0x578c2b[_0x76f7('0x2f')]);if(_0x578c2b[_0x76f7('0x30')]){delete _0x578c2b[_0x76f7('0x30')];}_0x326760[_0x76f7('0x1e')](_0x166faf)[_0x76f7('0x31')](_0x578c2b);};}exports[_0x76f7('0x32')]=function(_0xc38461,_0x259787){var _0x56e640={},_0x4956f2={},_0x13c19b={'count':0x0,'rows':[]};var _0x23fa1c=_[_0x76f7('0x33')](db['MailQueue'][_0x76f7('0x34')],function(_0x1f7b03){return{'name':_0x1f7b03[_0x76f7('0x35')],'type':_0x1f7b03[_0x76f7('0x36')]['key']};});_0x4956f2[_0x76f7('0x37')]=_[_0x76f7('0x33')](_0x23fa1c,'name');_0x4956f2['query']=_[_0x76f7('0x38')](_0xc38461[_0x76f7('0x39')]);_0x4956f2['filters']=_[_0x76f7('0x3a')](_0x4956f2[_0x76f7('0x37')],_0x4956f2[_0x76f7('0x39')]);_0x56e640[_0x76f7('0x3b')]=_['intersection'](_0x4956f2[_0x76f7('0x37')],qs[_0x76f7('0x3c')](_0xc38461['query']['fields']));_0x56e640[_0x76f7('0x3b')]=_0x56e640[_0x76f7('0x3b')]['length']?_0x56e640[_0x76f7('0x3b')]:_0x4956f2['model'];if(!_0xc38461[_0x76f7('0x39')][_0x76f7('0x3d')]('nolimit')){_0x56e640[_0x76f7('0x24')]=qs[_0x76f7('0x24')](_0xc38461[_0x76f7('0x39')][_0x76f7('0x24')]);_0x56e640['offset']=qs[_0x76f7('0x21')](_0xc38461[_0x76f7('0x39')][_0x76f7('0x21')]);}_0x56e640[_0x76f7('0x3e')]=qs[_0x76f7('0x3f')](_0xc38461[_0x76f7('0x39')][_0x76f7('0x3f')]);_0x56e640[_0x76f7('0x40')]=qs[_0x76f7('0x41')](_[_0x76f7('0x42')](_0xc38461[_0x76f7('0x39')],_0x4956f2['filters']),_0x23fa1c);if(_0xc38461['query'][_0x76f7('0x43')]){_0x56e640[_0x76f7('0x40')]=_[_0x76f7('0x44')](_0x56e640[_0x76f7('0x40')],{'$or':_[_0x76f7('0x33')](_0x23fa1c,function(_0x316865){if(_0x316865[_0x76f7('0x36')]!=='VIRTUAL'){var _0x3a7206={};_0x3a7206[_0x316865[_0x76f7('0x30')]]={'$like':'%'+_0xc38461[_0x76f7('0x39')][_0x76f7('0x43')]+'%'};return _0x3a7206;}})});}_0x56e640=_[_0x76f7('0x44')]({},_0x56e640,_0xc38461['options']);var _0x240695={'where':_0x56e640[_0x76f7('0x40')]};return db[_0x76f7('0x45')][_0x76f7('0x23')](_0x240695)[_0x76f7('0x2a')](function(_0x1832eb){_0x13c19b[_0x76f7('0x23')]=_0x1832eb;if(_0xc38461[_0x76f7('0x39')][_0x76f7('0x46')]){_0x56e640['include']=[{'all':!![]}];}return db[_0x76f7('0x45')][_0x76f7('0x47')](_0x56e640);})['then'](function(_0x12a2ff){_0x13c19b['rows']=_0x12a2ff;return _0x13c19b;})[_0x76f7('0x2a')](respondWithFilteredResult(_0x259787,_0x56e640))[_0x76f7('0x48')](handleError(_0x259787,null));};exports[_0x76f7('0x49')]=function(_0x4cdac4,_0x544b04){var _0x414707={'raw':!![],'where':{'id':_0x4cdac4['params']['id']}},_0x1077b7={};_0x1077b7[_0x76f7('0x37')]=_[_0x76f7('0x38')](db[_0x76f7('0x45')]['rawAttributes']);_0x1077b7[_0x76f7('0x39')]=_['keys'](_0x4cdac4[_0x76f7('0x39')]);_0x1077b7[_0x76f7('0x41')]=_[_0x76f7('0x3a')](_0x1077b7[_0x76f7('0x37')],_0x1077b7[_0x76f7('0x39')]);_0x414707[_0x76f7('0x3b')]=_[_0x76f7('0x3a')](_0x1077b7['model'],qs[_0x76f7('0x3c')](_0x4cdac4['query']['fields']));_0x414707[_0x76f7('0x3b')]=_0x414707[_0x76f7('0x3b')][_0x76f7('0x4a')]?_0x414707['attributes']:_0x1077b7[_0x76f7('0x37')];if(_0x4cdac4[_0x76f7('0x39')][_0x76f7('0x46')]){_0x414707[_0x76f7('0x4b')]=[{'all':!![]}];}_0x414707=_['merge']({},_0x414707,_0x4cdac4[_0x76f7('0x4c')]);return db[_0x76f7('0x45')][_0x76f7('0x4d')](_0x414707)[_0x76f7('0x2a')](handleEntityNotFound(_0x544b04,null))[_0x76f7('0x2a')](respondWithResult(_0x544b04,null))[_0x76f7('0x48')](handleError(_0x544b04,null));};exports['create']=function(_0xc5e15e,_0x52ad3a){return db[_0x76f7('0x45')][_0x76f7('0x4e')](_0xc5e15e[_0x76f7('0x4f')],{})[_0x76f7('0x2a')](function(_0x516cca){var _0x33f047=_0xc5e15e[_0x76f7('0x50')][_0x76f7('0x2c')]({'plain':!![]});if(!_0x33f047)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x33f047[_0x76f7('0x51')]===_0x76f7('0x50')){var _0x46dfb2=_0x516cca[_0x76f7('0x2c')]({'plain':!![]});var _0x5465eb=_0x76f7('0x2d');return db[_0x76f7('0x52')]['find']({'where':{'name':_0x5465eb,'userProfileId':_0x33f047[_0x76f7('0x53')]},'raw':!![]})[_0x76f7('0x2a')](function(_0x2d02f5){if(_0x2d02f5&&_0x2d02f5[_0x76f7('0x54')]===0x0){return db[_0x76f7('0x2e')][_0x76f7('0x4e')]({'name':_0x46dfb2['name'],'resourceId':_0x46dfb2['id'],'type':_0x2d02f5[_0x76f7('0x30')],'sectionId':_0x2d02f5['id']},{})['then'](function(){return _0x516cca;});}else{return _0x516cca;}})[_0x76f7('0x48')](function(_0x3269f9){logger[_0x76f7('0x55')](_0x76f7('0x56'),_0x3269f9);throw _0x3269f9;});}return _0x516cca;})['then'](respondWithResult(_0x52ad3a,0xc9))[_0x76f7('0x48')](handleError(_0x52ad3a,null));};exports[_0x76f7('0x29')]=function(_0x5b097e,_0x1f48e9){if(_0x5b097e[_0x76f7('0x4f')]['id']){delete _0x5b097e[_0x76f7('0x4f')]['id'];}return db['MailQueue'][_0x76f7('0x4d')]({'where':{'id':_0x5b097e[_0x76f7('0x57')]['id']}})['then'](handleEntityNotFound(_0x1f48e9,null))[_0x76f7('0x2a')](saveUpdates(_0x5b097e['body'],null))[_0x76f7('0x2a')](respondWithResult(_0x1f48e9,null))[_0x76f7('0x48')](handleError(_0x1f48e9,null));};exports['destroy']=function(_0x2738ba,_0x365573){return db['MailQueue']['find']({'where':{'id':_0x2738ba[_0x76f7('0x57')]['id']}})[_0x76f7('0x2a')](handleEntityNotFound(_0x365573,null))[_0x76f7('0x2a')](removeEntity(_0x365573,null))[_0x76f7('0x48')](handleError(_0x365573,null));};exports[_0x76f7('0x58')]=function(_0x455b96,_0x8b6f6c){return db[_0x76f7('0x45')][_0x76f7('0x58')]()[_0x76f7('0x2a')](respondWithResult(_0x8b6f6c,null))[_0x76f7('0x48')](handleError(_0x8b6f6c,null));};exports['getMembers']=function(_0xee5ad1,_0x325cc1,_0x75bacb){var _0x106770={'raw':!![],'where':{}},_0x539051={},_0x41bde8;return db[_0x76f7('0x45')]['findOne']({'where':{'id':_0xee5ad1['params']['id']}})[_0x76f7('0x2a')](handleEntityNotFound(_0x325cc1,null))[_0x76f7('0x2a')](function(_0x363e1b){if(_0x363e1b){_0x41bde8=_0x363e1b[_0x76f7('0x2c')]({'plain':!![]});_0x539051['model']=_[_0x76f7('0x38')](db[_0x76f7('0x59')]['rawAttributes']);_0x539051['query']=_[_0x76f7('0x38')](_0xee5ad1[_0x76f7('0x39')]);_0x539051[_0x76f7('0x41')]=_[_0x76f7('0x3a')](_0x539051['model'],_0x539051[_0x76f7('0x39')]);_0x106770[_0x76f7('0x3b')]=_['intersection'](_0x539051[_0x76f7('0x37')],qs[_0x76f7('0x3c')](_0xee5ad1[_0x76f7('0x39')][_0x76f7('0x3c')]));_0x106770['attributes']=_0x106770['attributes']['length']?_0x106770[_0x76f7('0x3b')]:_0x539051[_0x76f7('0x37')];if(!_0xee5ad1[_0x76f7('0x39')][_0x76f7('0x3d')]('nolimit')){_0x106770[_0x76f7('0x24')]=qs[_0x76f7('0x24')](_0xee5ad1[_0x76f7('0x39')][_0x76f7('0x24')]);_0x106770[_0x76f7('0x21')]=qs[_0x76f7('0x21')](_0xee5ad1[_0x76f7('0x39')][_0x76f7('0x21')]);}_0x106770[_0x76f7('0x3e')]=qs['sort'](_0xee5ad1['query']['sort']);_0x106770[_0x76f7('0x40')]=qs[_0x76f7('0x41')](_[_0x76f7('0x42')](_0xee5ad1[_0x76f7('0x39')],_0x539051[_0x76f7('0x41')]));_0x106770[_0x76f7('0x40')][_0x76f7('0x5a')]=_0x363e1b['id'];if(_0xee5ad1[_0x76f7('0x39')][_0x76f7('0x43')]){_0x106770[_0x76f7('0x40')]=_['merge'](_0x106770[_0x76f7('0x40')],{'$or':_[_0x76f7('0x33')](_0x106770[_0x76f7('0x3b')],function(_0x307270){var _0xcbbc40={};_0xcbbc40[_0x307270]={'$like':'%'+_0xee5ad1[_0x76f7('0x39')][_0x76f7('0x43')]+'%'};return _0xcbbc40;})});}_0x106770=_[_0x76f7('0x44')]({},_0x106770,_0xee5ad1[_0x76f7('0x4c')]);return db[_0x76f7('0x59')][_0x76f7('0x47')](_0x106770);}})[_0x76f7('0x2a')](function(_0x54d99b){if(_0x54d99b){return db[_0x76f7('0x5b')][_0x76f7('0x5c')]({'where':{'id':_[_0x76f7('0x33')](_0x54d99b,_0x76f7('0x5d')),'role':'agent'},'attributes':['id',_0x76f7('0x30'),_0x76f7('0x5e'),'updatedAt',_0x76f7('0x5f')]});}})[_0x76f7('0x2a')](function(_0x5e1d43){if(_0x5e1d43){return{'count':_0x5e1d43['count'],'rows':_['map'](_0x5e1d43[_0x76f7('0x60')],function(_0x2e8c1a){return{'membername':_0x2e8c1a[_0x76f7('0x30')],'UserId':_0x2e8c1a['id'],'queue_name':_0x41bde8[_0x76f7('0x30')],'MailQueueId':_0x41bde8['id'],'interface':util['format'](_0x76f7('0x61'),'SIP',_0x2e8c1a[_0x76f7('0x30')]),'penalty':0x0,'paused':_0x2e8c1a[_0x76f7('0x5e')],'createdAt':_0x2e8c1a[_0x76f7('0x5f')],'updatedAt':_0x2e8c1a['updatedAt']};})};}else{return{'count':0x0,'rows':[]};}})[_0x76f7('0x2a')](respondWithFilteredResult(_0x325cc1,_0x106770))[_0x76f7('0x48')](handleError(_0x325cc1,null));};exports['getTeams']=function(_0x294e11,_0x30e89d,_0x5f440c){var _0x1a9adf={};var _0x3800c0={};var _0x1cea28;var _0x31b84c;return db['MailQueue']['findOne']({'where':{'id':_0x294e11[_0x76f7('0x57')]['id']}})[_0x76f7('0x2a')](handleEntityNotFound(_0x30e89d,null))[_0x76f7('0x2a')](function(_0xf67da){if(_0xf67da){_0x1cea28=_0xf67da;_0x3800c0[_0x76f7('0x37')]=_[_0x76f7('0x38')](db[_0x76f7('0x62')][_0x76f7('0x34')]);_0x3800c0['query']=_[_0x76f7('0x38')](_0x294e11['query']);_0x3800c0[_0x76f7('0x41')]=_[_0x76f7('0x3a')](_0x3800c0[_0x76f7('0x37')],_0x3800c0[_0x76f7('0x39')]);_0x1a9adf[_0x76f7('0x3b')]=_[_0x76f7('0x3a')](_0x3800c0[_0x76f7('0x37')],qs[_0x76f7('0x3c')](_0x294e11[_0x76f7('0x39')][_0x76f7('0x3c')]));_0x1a9adf['attributes']=_0x1a9adf[_0x76f7('0x3b')][_0x76f7('0x4a')]?_0x1a9adf['attributes']:_0x3800c0[_0x76f7('0x37')];_0x1a9adf[_0x76f7('0x3e')]=qs['sort'](_0x294e11['query'][_0x76f7('0x3f')]);_0x1a9adf['where']=qs['filters'](_['pick'](_0x294e11['query'],_0x3800c0['filters']));if(_0x294e11[_0x76f7('0x39')][_0x76f7('0x43')]){_0x1a9adf[_0x76f7('0x40')]=_[_0x76f7('0x44')](_0x1a9adf[_0x76f7('0x40')],{'$or':_[_0x76f7('0x33')](_0x1a9adf[_0x76f7('0x3b')],function(_0x1a37b0){var _0x554fd7={};_0x554fd7[_0x1a37b0]={'$like':'%'+_0x294e11[_0x76f7('0x39')]['filter']+'%'};return _0x554fd7;})});}_0x1a9adf=_[_0x76f7('0x44')]({},_0x1a9adf,_0x294e11['options']);return _0x1cea28[_0x76f7('0x63')](_0x1a9adf);}})[_0x76f7('0x2a')](function(_0xca904c){if(_0xca904c){_0x31b84c=_0xca904c[_0x76f7('0x4a')];if(!_0x294e11[_0x76f7('0x39')][_0x76f7('0x3d')]('nolimit')){_0x1a9adf[_0x76f7('0x24')]=qs['limit'](_0x294e11['query'][_0x76f7('0x24')]);_0x1a9adf[_0x76f7('0x21')]=qs['offset'](_0x294e11[_0x76f7('0x39')][_0x76f7('0x21')]);}return _0x1cea28['getTeams'](_0x1a9adf);}})[_0x76f7('0x2a')](function(_0x2c1615){if(_0x2c1615){return _0x2c1615?{'count':_0x31b84c,'rows':_0x2c1615}:null;}})[_0x76f7('0x2a')](respondWithResult(_0x30e89d,null))[_0x76f7('0x48')](handleError(_0x30e89d,null));};exports[_0x76f7('0x64')]=function(_0x524381,_0x259ca4,_0x2bfbf0){var _0xa439f5=_0x524381[_0x76f7('0x4f')]['ids'];return db[_0x76f7('0x62')]['findAll']({'where':{'id':_0xa439f5},'attributes':['id'],'include':[{'model':db[_0x76f7('0x5b')],'as':_0x76f7('0x65'),'attributes':['id',_0x76f7('0x30'),_0x76f7('0x66'),_0x76f7('0x67'),_0x76f7('0x68')],'raw':!![]}]})[_0x76f7('0x2a')](function(_0x13ba1b){if(_0x13ba1b){var _0x3a7ae9=_[_0x76f7('0x69')](_0x13ba1b,function(_0x3ebe7f){var _0x5b97f7=_0x3ebe7f[_0x76f7('0x2c')]({'plain':!![]});return _0x5b97f7[_0x76f7('0x65')];});return db[_0x76f7('0x45')][_0x76f7('0x4d')]({'where':{'id':_0x524381[_0x76f7('0x57')]['id']}})[_0x76f7('0x2a')](function(_0x24a08d){return db[_0x76f7('0x6a')][_0x76f7('0x6b')](function(_0xced0ff){return _0x24a08d[_0x76f7('0x64')](_0xa439f5,{'transaction':_0xced0ff})[_0x76f7('0x2a')](function(){return BPromise[_0x76f7('0x6c')](_0x3a7ae9,function(_0x3af2a7){return db[_0x76f7('0x59')]['findOrCreate']({'where':{'UserId':_0x3af2a7['id'],'MailQueueId':_0x524381['params']['id']},'transaction':_0xced0ff});});})[_0x76f7('0x2a')](function(){_0x3a7ae9['forEach'](function(_0x542330){socket[_0x76f7('0x6d')](_0x76f7('0x6e'),{'UserId':_0x542330['id'],'MailQueueId':_0x24a08d['id']});});});});});}})[_0x76f7('0x2a')](respondWithStatusCode(_0x259ca4,null))[_0x76f7('0x48')](handleError(_0x259ca4,null));};exports[_0x76f7('0x6f')]=function(_0x18ae7e,_0x280384,_0x4013c7){return db[_0x76f7('0x62')][_0x76f7('0x47')]({'where':{'id':_0x18ae7e[_0x76f7('0x39')][_0x76f7('0x70')]},'attributes':['id'],'include':[{'model':db['User'],'as':'Agents','attributes':['id'],'raw':!![]}]})[_0x76f7('0x2a')](handleEntityNotFound(_0x280384,null))[_0x76f7('0x2a')](function(_0x2ff420){var _0x279bbb=_[_0x76f7('0x33')](_0x2ff420,'id');var _0xcc9d43=[];var _0x340f82=[];var _0x5b4812=squel[_0x76f7('0x71')]();_0x5b4812['field'](_0x76f7('0x72'))['from'](_0x76f7('0x73'),'tq')[_0x76f7('0x74')](_0x76f7('0x75'),'ut',_0x76f7('0x76'))['where'](_0x76f7('0x77'),_0x18ae7e[_0x76f7('0x57')]['id']);for(var _0x44ae5c=0x0;_0x44ae5c<_0x2ff420[_0x76f7('0x4a')];_0x44ae5c+=0x1){let _0x23e887=_0x2ff420[_0x44ae5c];for(var _0x1e87c6=0x0;_0x1e87c6<_0x23e887[_0x76f7('0x65')][_0x76f7('0x4a')];_0x1e87c6+=0x1){let _0x4268b9=_0x23e887[_0x76f7('0x65')][_0x1e87c6];var _0x885059=_0x5b4812['clone']();_0x885059[_0x76f7('0x40')]('ut.UserId\x20=\x20?',_0x4268b9['id']);_0x340f82[_0x76f7('0x78')](db[_0x76f7('0x6a')][_0x76f7('0x39')](_0x885059[_0x76f7('0x79')](),{'type':db['sequelize'][_0x76f7('0x7a')][_0x76f7('0x7b')]})['then'](function(_0x3489c3){if(_0x3489c3['length']===0x1){return _0x4268b9['id'];}else{var _0x3aa0ff=_[_0x76f7('0x7c')](_['map'](_0x3489c3,_0x76f7('0x7d')),function(_0x382ceb){return _[_0x76f7('0x7e')](_0x279bbb,_0x382ceb);});if(_0x3aa0ff){return _0x4268b9['id'];}}}));}}return BPromise[_0x76f7('0x7f')](_0x340f82)[_0x76f7('0x2a')](function(_0x3dc688){_0xcc9d43=_(_0xcc9d43)[_0x76f7('0x80')](_0x3dc688)[_0x76f7('0x81')]()['value']();return db[_0x76f7('0x45')][_0x76f7('0x4d')]({'where':{'id':_0x18ae7e[_0x76f7('0x57')]['id']}})[_0x76f7('0x2a')](function(_0x18a2d4){return db[_0x76f7('0x6a')][_0x76f7('0x6b')](function(_0x43a291){return _0x18a2d4[_0x76f7('0x6f')](_0x18ae7e[_0x76f7('0x39')][_0x76f7('0x70')],{'transaction':_0x43a291})[_0x76f7('0x2a')](function(){if(!_[_0x76f7('0x82')](_0xcc9d43)){return _0x18a2d4[_0x76f7('0x83')](_0xcc9d43,{'transaction':_0x43a291});}})['then'](function(){_0xcc9d43[_0x76f7('0x84')](function(_0xa7940){socket['emit'](_0x76f7('0x85'),{'UserId':_0xa7940,'MailQueueId':_0x18a2d4['id']});});});});});});})[_0x76f7('0x2a')](respondWithStatusCode(_0x280384,null))['catch'](handleError(_0x280384,null));};exports['addAgents']=function(_0x23f165,_0x4e44b2,_0x97a943){return db[_0x76f7('0x45')][_0x76f7('0x4d')]({'where':{'id':_0x23f165[_0x76f7('0x57')]['id']}})[_0x76f7('0x2a')](handleEntityNotFound(_0x4e44b2,null))[_0x76f7('0x2a')](function(_0x313c12){if(_0x313c12){return _0x313c12[_0x76f7('0x86')](_0x23f165[_0x76f7('0x4f')][_0x76f7('0x70')],_[_0x76f7('0x87')](_0x23f165[_0x76f7('0x4f')],[_0x76f7('0x70'),'id'])||{})['spread'](function(_0x2e7b90){for(var _0x228907=0x0;_0x228907<_0x23f165[_0x76f7('0x4f')][_0x76f7('0x70')]['length'];_0x228907+=0x1){socket[_0x76f7('0x6d')](_0x76f7('0x6e'),{'UserId':_0x23f165[_0x76f7('0x4f')][_0x76f7('0x70')][_0x228907],'MailQueueId':_0x23f165[_0x76f7('0x57')]['id']});}return _0x2e7b90;});}})['then'](respondWithResult(_0x4e44b2,null))[_0x76f7('0x48')](handleError(_0x4e44b2,null));};exports[_0x76f7('0x83')]=function(_0x20136f,_0x100482,_0xb60e65){return db[_0x76f7('0x45')][_0x76f7('0x4d')]({'where':{'id':_0x20136f[_0x76f7('0x57')]['id']}})[_0x76f7('0x2a')](handleEntityNotFound(_0x100482,null))[_0x76f7('0x2a')](function(_0x191dc7){if(_0x191dc7){return _0x191dc7['removeAgents'](_0x20136f['query']['ids'])[_0x76f7('0x2a')](function(){if(_[_0x76f7('0x88')](_0x20136f[_0x76f7('0x39')]['ids'])){for(var _0x3287e0=0x0;_0x3287e0<_0x20136f[_0x76f7('0x39')][_0x76f7('0x70')]['length'];_0x3287e0+=0x1){socket[_0x76f7('0x6d')](_0x76f7('0x85'),{'UserId':Number(_0x20136f[_0x76f7('0x39')][_0x76f7('0x70')][_0x3287e0]),'MailQueueId':Number(_0x20136f[_0x76f7('0x57')]['id'])});}}else{socket[_0x76f7('0x6d')](_0x76f7('0x85'),{'UserId':Number(_0x20136f[_0x76f7('0x39')]['ids']),'MailQueueId':Number(_0x20136f[_0x76f7('0x57')]['id'])});}});}})['then'](respondWithStatusCode(_0x100482,null))['catch'](handleError(_0x100482,null));};exports[_0x76f7('0x89')]=function(_0x565e78,_0x8e4c9f,_0x223ece){var _0x370ca7={};var _0x2b49f6={};var _0x5c5c71;var _0x375e5e;return db[_0x76f7('0x45')][_0x76f7('0x8a')]({'where':{'id':_0x565e78[_0x76f7('0x57')]['id']}})['then'](handleEntityNotFound(_0x8e4c9f,null))['then'](function(_0x6f61b8){if(_0x6f61b8){_0x5c5c71=_0x6f61b8;_0x2b49f6[_0x76f7('0x37')]=_[_0x76f7('0x38')](db['User'][_0x76f7('0x34')]);_0x2b49f6[_0x76f7('0x39')]=_['keys'](_0x565e78[_0x76f7('0x39')]);_0x2b49f6[_0x76f7('0x41')]=_[_0x76f7('0x3a')](_0x2b49f6['model'],_0x2b49f6[_0x76f7('0x39')]);_0x370ca7['attributes']=_['intersection'](_0x2b49f6[_0x76f7('0x37')],qs[_0x76f7('0x3c')](_0x565e78[_0x76f7('0x39')][_0x76f7('0x3c')]));_0x370ca7[_0x76f7('0x3b')]=_0x370ca7['attributes'][_0x76f7('0x4a')]?_0x370ca7[_0x76f7('0x3b')]:_0x2b49f6[_0x76f7('0x37')];_0x370ca7[_0x76f7('0x3e')]=qs[_0x76f7('0x3f')](_0x565e78[_0x76f7('0x39')][_0x76f7('0x3f')]);_0x370ca7[_0x76f7('0x40')]=qs['filters'](_[_0x76f7('0x42')](_0x565e78[_0x76f7('0x39')],_0x2b49f6[_0x76f7('0x41')]));if(_0x565e78[_0x76f7('0x39')][_0x76f7('0x43')]){_0x370ca7[_0x76f7('0x40')]=_['merge'](_0x370ca7[_0x76f7('0x40')],{'$or':_[_0x76f7('0x33')](_0x370ca7[_0x76f7('0x3b')],function(_0x65597c){var _0x5d9e4c={};_0x5d9e4c[_0x65597c]={'$like':'%'+_0x565e78[_0x76f7('0x39')]['filter']+'%'};return _0x5d9e4c;})});}_0x370ca7=_[_0x76f7('0x44')]({},_0x370ca7,_0x565e78[_0x76f7('0x4c')]);return _0x5c5c71['getAgents'](_0x370ca7);}})[_0x76f7('0x2a')](function(_0x5f06a3){if(_0x5f06a3){_0x375e5e=_0x5f06a3[_0x76f7('0x4a')];if(!_0x565e78[_0x76f7('0x39')][_0x76f7('0x3d')]('nolimit')){_0x370ca7['limit']=qs[_0x76f7('0x24')](_0x565e78['query'][_0x76f7('0x24')]);_0x370ca7[_0x76f7('0x21')]=qs[_0x76f7('0x21')](_0x565e78[_0x76f7('0x39')][_0x76f7('0x21')]);}return _0x5c5c71[_0x76f7('0x89')](_0x370ca7);}})[_0x76f7('0x2a')](function(_0x54c04a){if(_0x54c04a){return _0x54c04a?{'count':_0x375e5e,'rows':_0x54c04a}:null;}})[_0x76f7('0x2a')](respondWithResult(_0x8e4c9f,null))[_0x76f7('0x48')](handleError(_0x8e4c9f,null));};