a740c0ca30f5e77c28867b8b78fb792a7be48acc
[motion2.git] / server / api / smsQueue / smsQueue.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 _0x885a=['then','destroy','UserProfileResource','error','stack','name','index','map','SmsQueue','fieldName','key','query','filters','intersection','attributes','model','fields','hasOwnProperty','nolimit','sort','where','pick','filter','merge','type','VIRTUAL','options','count','includeAll','findAll','rows','catch','show','keys','find','create','user','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','SmsQueues','UserProfileSection','userProfileId','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','body','params','describe','findOne','UserSmsQueue','SmsQueueId','User','findAndCountAll','agent','smsPause','updatedAt','%s/%s','SIP','getTeams','Team','length','order','addTeams','ids','Agents','online','voicePause','flatMap','transaction','forEach','emit','removeTeams','field','tq.TeamId','from','team_has_sms_queues','join','user_has_teams','ut.TeamId\x20=\x20tq.TeamId','SmsQueueId\x20=\x20?','clone','ut.UserId\x20=\x20?','push','sequelize','QueryTypes','SELECT','TeamId','includes','all','union','compact','isEmpty','userSmsQueue:remove','addAgents','spread','userSmsQueue:save','removeAgents','isArray','getAgents','rawAttributes','eml-format','rimraf','zip-dir','fast-json-patch','moment','bluebird','mustache','util','path','sox','ejs','lodash','squel','jsforce','desk.js','to-csv','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','redis','defaults','socket.io-emitter','./smsQueue.socket','register','sendStatus','end','status','offset','limit','set','Content-Range','json','apply','save'];(function(_0x3e112d,_0x2bb132){var _0x531c31=function(_0x1954bc){while(--_0x1954bc){_0x3e112d['push'](_0x3e112d['shift']());}};_0x531c31(++_0x2bb132);}(_0x885a,0x180));var _0xa885=function(_0x27af45,_0x1b57aa){_0x27af45=_0x27af45-0x0;var _0x18df21=_0x885a[_0x27af45];return _0x18df21;};'use strict';var pdf=require('html-pdf');var emlformat=require(_0xa885('0x0'));var rimraf=require(_0xa885('0x1'));var zipdir=require(_0xa885('0x2'));var jsonpatch=require(_0xa885('0x3'));var rp=require('request-promise');var moment=require(_0xa885('0x4'));var BPromise=require(_0xa885('0x5'));var Mustache=require(_0xa885('0x6'));var util=require(_0xa885('0x7'));var path=require(_0xa885('0x8'));var sox=require(_0xa885('0x9'));var csv=require('to-csv');var ejs=require(_0xa885('0xa'));var fs=require('fs');var _=require(_0xa885('0xb'));var squel=require(_0xa885('0xc'));var crypto=require('crypto');var jsforce=require(_0xa885('0xd'));var deskjs=require(_0xa885('0xe'));var toCsv=require(_0xa885('0xf'));var querystring=require('querystring');var Papa=require(_0xa885('0x10'));var Redis=require(_0xa885('0x11'));var authService=require(_0xa885('0x12'));var qs=require(_0xa885('0x13'));var as=require(_0xa885('0x14'));var hardwareService=require(_0xa885('0x15'));var logger=require(_0xa885('0x16'))('api');var utils=require(_0xa885('0x17'));var config=require(_0xa885('0x18'));var licenseUtil=require(_0xa885('0x19'));var db=require(_0xa885('0x1a'))['db'];config[_0xa885('0x1b')]=_[_0xa885('0x1c')](config[_0xa885('0x1b')],{'host':'localhost','port':0x18eb});var socket=require(_0xa885('0x1d'))(new Redis(config[_0xa885('0x1b')]));require(_0xa885('0x1e'))[_0xa885('0x1f')](socket);function respondWithStatusCode(_0x33ce2e,_0x5bdb0c){_0x5bdb0c=_0x5bdb0c||0xcc;return function(_0x1c9687){if(_0x1c9687){return _0x33ce2e[_0xa885('0x20')](_0x5bdb0c);}return _0x33ce2e['status'](_0x5bdb0c)[_0xa885('0x21')]();};}function respondWithResult(_0x32fc86,_0x4e5ae5){_0x4e5ae5=_0x4e5ae5||0xc8;return function(_0x6c6a0b){if(_0x6c6a0b){return _0x32fc86[_0xa885('0x22')](_0x4e5ae5)['json'](_0x6c6a0b);}};}function respondWithFilteredResult(_0x20a206,_0x543e7a){return function(_0x2e9290){if(_0x2e9290){var _0x3960ea=_0x2e9290['count'],_0x8c26c=_0x543e7a[_0xa885('0x23')],_0x4e4d51=_0x543e7a[_0xa885('0x23')]+_0x543e7a[_0xa885('0x24')],_0x289b76;if(_0x4e4d51>=_0x3960ea){_0x4e4d51=_0x3960ea;_0x289b76=0xc8;}else{_0x289b76=0xce;}_0x20a206['status'](_0x289b76);return _0x20a206[_0xa885('0x25')](_0xa885('0x26'),_0x8c26c+'-'+_0x4e4d51+'/'+_0x3960ea)[_0xa885('0x27')](_0x2e9290);}return null;};}function patchUpdates(_0x4de770){return function(_0x273988){try{jsonpatch[_0xa885('0x28')](_0x273988,_0x4de770,!![]);}catch(_0x94fcac){return BPromise['reject'](_0x94fcac);}return _0x273988[_0xa885('0x29')]();};}function saveUpdates(_0x582b59,_0x20ec96){return function(_0x299553){if(_0x299553){return _0x299553['update'](_0x582b59)[_0xa885('0x2a')](function(_0x190a57){return _0x190a57;});}return null;};}function removeEntity(_0x21c98c,_0x554f01){return function(_0x35d4c4){if(_0x35d4c4){return _0x35d4c4[_0xa885('0x2b')]()['then'](function(){var _0x39ae58=_0x35d4c4['get']({'plain':!![]});var _0x15ff4d='SmsQueues';return db[_0xa885('0x2c')][_0xa885('0x2b')]({'where':{'type':_0x15ff4d,'resourceId':_0x39ae58['id']}})[_0xa885('0x2a')](function(){return _0x35d4c4;});})[_0xa885('0x2a')](function(){_0x21c98c[_0xa885('0x22')](0xcc)[_0xa885('0x21')]();});}};}function handleEntityNotFound(_0x1e13d5,_0x3f219d){return function(_0x55efb1){if(!_0x55efb1){_0x1e13d5['sendStatus'](0x194);}return _0x55efb1;};}function handleError(_0x5c4ca7,_0x4fc438){_0x4fc438=_0x4fc438||0x1f4;return function(_0x1c94ab){logger[_0xa885('0x2d')](_0x1c94ab[_0xa885('0x2e')]);if(_0x1c94ab[_0xa885('0x2f')]){delete _0x1c94ab[_0xa885('0x2f')];}_0x5c4ca7[_0xa885('0x22')](_0x4fc438)['send'](_0x1c94ab);};}exports[_0xa885('0x30')]=function(_0x47d01c,_0x1318ef){var _0x4a1e54={},_0x39990f={},_0x528a71={'count':0x0,'rows':[]};var _0x38f0ad=_[_0xa885('0x31')](db[_0xa885('0x32')]['rawAttributes'],function(_0x15f3a){return{'name':_0x15f3a[_0xa885('0x33')],'type':_0x15f3a['type'][_0xa885('0x34')]};});_0x39990f['model']=_[_0xa885('0x31')](_0x38f0ad,_0xa885('0x2f'));_0x39990f[_0xa885('0x35')]=_['keys'](_0x47d01c[_0xa885('0x35')]);_0x39990f[_0xa885('0x36')]=_[_0xa885('0x37')](_0x39990f['model'],_0x39990f[_0xa885('0x35')]);_0x4a1e54[_0xa885('0x38')]=_[_0xa885('0x37')](_0x39990f[_0xa885('0x39')],qs[_0xa885('0x3a')](_0x47d01c[_0xa885('0x35')][_0xa885('0x3a')]));_0x4a1e54[_0xa885('0x38')]=_0x4a1e54['attributes']['length']?_0x4a1e54[_0xa885('0x38')]:_0x39990f[_0xa885('0x39')];if(!_0x47d01c[_0xa885('0x35')][_0xa885('0x3b')](_0xa885('0x3c'))){_0x4a1e54[_0xa885('0x24')]=qs[_0xa885('0x24')](_0x47d01c[_0xa885('0x35')][_0xa885('0x24')]);_0x4a1e54['offset']=qs['offset'](_0x47d01c[_0xa885('0x35')]['offset']);}_0x4a1e54['order']=qs[_0xa885('0x3d')](_0x47d01c['query'][_0xa885('0x3d')]);_0x4a1e54[_0xa885('0x3e')]=qs[_0xa885('0x36')](_[_0xa885('0x3f')](_0x47d01c[_0xa885('0x35')],_0x39990f[_0xa885('0x36')]),_0x38f0ad);if(_0x47d01c[_0xa885('0x35')][_0xa885('0x40')]){_0x4a1e54[_0xa885('0x3e')]=_[_0xa885('0x41')](_0x4a1e54['where'],{'$or':_[_0xa885('0x31')](_0x38f0ad,function(_0x4012b3){if(_0x4012b3[_0xa885('0x42')]!==_0xa885('0x43')){var _0x129639={};_0x129639[_0x4012b3['name']]={'$like':'%'+_0x47d01c['query'][_0xa885('0x40')]+'%'};return _0x129639;}})});}_0x4a1e54=_[_0xa885('0x41')]({},_0x4a1e54,_0x47d01c[_0xa885('0x44')]);var _0x2a1e90={'where':_0x4a1e54[_0xa885('0x3e')]};return db['SmsQueue'][_0xa885('0x45')](_0x2a1e90)[_0xa885('0x2a')](function(_0x5dced9){_0x528a71['count']=_0x5dced9;if(_0x47d01c[_0xa885('0x35')][_0xa885('0x46')]){_0x4a1e54['include']=[{'all':!![]}];}return db['SmsQueue'][_0xa885('0x47')](_0x4a1e54);})[_0xa885('0x2a')](function(_0x2a3a3f){_0x528a71[_0xa885('0x48')]=_0x2a3a3f;return _0x528a71;})[_0xa885('0x2a')](respondWithFilteredResult(_0x1318ef,_0x4a1e54))[_0xa885('0x49')](handleError(_0x1318ef,null));};exports[_0xa885('0x4a')]=function(_0xb4660d,_0x44469c){var _0x3dcf63={'raw':!![],'where':{'id':_0xb4660d['params']['id']}},_0x2dca6f={};_0x2dca6f[_0xa885('0x39')]=_[_0xa885('0x4b')](db[_0xa885('0x32')]['rawAttributes']);_0x2dca6f[_0xa885('0x35')]=_[_0xa885('0x4b')](_0xb4660d[_0xa885('0x35')]);_0x2dca6f[_0xa885('0x36')]=_['intersection'](_0x2dca6f[_0xa885('0x39')],_0x2dca6f['query']);_0x3dcf63[_0xa885('0x38')]=_[_0xa885('0x37')](_0x2dca6f['model'],qs[_0xa885('0x3a')](_0xb4660d[_0xa885('0x35')][_0xa885('0x3a')]));_0x3dcf63[_0xa885('0x38')]=_0x3dcf63[_0xa885('0x38')]['length']?_0x3dcf63[_0xa885('0x38')]:_0x2dca6f[_0xa885('0x39')];if(_0xb4660d[_0xa885('0x35')]['includeAll']){_0x3dcf63['include']=[{'all':!![]}];}_0x3dcf63=_['merge']({},_0x3dcf63,_0xb4660d[_0xa885('0x44')]);return db[_0xa885('0x32')][_0xa885('0x4c')](_0x3dcf63)[_0xa885('0x2a')](handleEntityNotFound(_0x44469c,null))[_0xa885('0x2a')](respondWithResult(_0x44469c,null))[_0xa885('0x49')](handleError(_0x44469c,null));};exports[_0xa885('0x4d')]=function(_0x476532,_0x4397d6){return db[_0xa885('0x32')][_0xa885('0x4d')](_0x476532['body'],{})['then'](function(_0x5c6840){var _0x11a272=_0x476532[_0xa885('0x4e')][_0xa885('0x4f')]({'plain':!![]});if(!_0x11a272)throw new Error(_0xa885('0x50'));if(_0x11a272[_0xa885('0x51')]===_0xa885('0x4e')){var _0x35e931=_0x5c6840['get']({'plain':!![]});var _0x1b4a62=_0xa885('0x52');return db[_0xa885('0x53')][_0xa885('0x4c')]({'where':{'name':_0x1b4a62,'userProfileId':_0x11a272[_0xa885('0x54')]},'raw':!![]})[_0xa885('0x2a')](function(_0x159d12){if(_0x159d12&&_0x159d12['autoAssociation']===0x0){return db[_0xa885('0x2c')][_0xa885('0x4d')]({'name':_0x35e931[_0xa885('0x2f')],'resourceId':_0x35e931['id'],'type':_0x159d12['name'],'sectionId':_0x159d12['id']},{})[_0xa885('0x2a')](function(){return _0x5c6840;});}else{return _0x5c6840;}})[_0xa885('0x49')](function(_0x2be1c4){logger[_0xa885('0x2d')](_0xa885('0x55'),_0x2be1c4);throw _0x2be1c4;});}return _0x5c6840;})[_0xa885('0x2a')](respondWithResult(_0x4397d6,0xc9))[_0xa885('0x49')](handleError(_0x4397d6,null));};exports[_0xa885('0x56')]=function(_0x4bca45,_0x2bd2fa){if(_0x4bca45[_0xa885('0x57')]['id']){delete _0x4bca45[_0xa885('0x57')]['id'];}return db['SmsQueue'][_0xa885('0x4c')]({'where':{'id':_0x4bca45[_0xa885('0x58')]['id']}})[_0xa885('0x2a')](handleEntityNotFound(_0x2bd2fa,null))[_0xa885('0x2a')](saveUpdates(_0x4bca45[_0xa885('0x57')],null))[_0xa885('0x2a')](respondWithResult(_0x2bd2fa,null))['catch'](handleError(_0x2bd2fa,null));};exports[_0xa885('0x2b')]=function(_0x2800e8,_0x27d0b3){return db['SmsQueue'][_0xa885('0x4c')]({'where':{'id':_0x2800e8[_0xa885('0x58')]['id']}})[_0xa885('0x2a')](handleEntityNotFound(_0x27d0b3,null))[_0xa885('0x2a')](removeEntity(_0x27d0b3,null))[_0xa885('0x49')](handleError(_0x27d0b3,null));};exports[_0xa885('0x59')]=function(_0x16f162,_0x288bbc){return db[_0xa885('0x32')]['describe']()[_0xa885('0x2a')](respondWithResult(_0x288bbc,null))[_0xa885('0x49')](handleError(_0x288bbc,null));};exports['getMembers']=function(_0x4dc788,_0x1bfacb,_0x2965ef){var _0x487cc5={'raw':!![],'where':{}},_0x8aba4={},_0x27e8cd;return db[_0xa885('0x32')][_0xa885('0x5a')]({'where':{'id':_0x4dc788[_0xa885('0x58')]['id']}})[_0xa885('0x2a')](handleEntityNotFound(_0x1bfacb,null))[_0xa885('0x2a')](function(_0x5c0a94){if(_0x5c0a94){_0x27e8cd=_0x5c0a94[_0xa885('0x4f')]({'plain':!![]});_0x8aba4[_0xa885('0x39')]=_['keys'](db[_0xa885('0x5b')]['rawAttributes']);_0x8aba4[_0xa885('0x35')]=_['keys'](_0x4dc788[_0xa885('0x35')]);_0x8aba4[_0xa885('0x36')]=_[_0xa885('0x37')](_0x8aba4[_0xa885('0x39')],_0x8aba4['query']);_0x487cc5[_0xa885('0x38')]=_[_0xa885('0x37')](_0x8aba4[_0xa885('0x39')],qs['fields'](_0x4dc788[_0xa885('0x35')]['fields']));_0x487cc5[_0xa885('0x38')]=_0x487cc5[_0xa885('0x38')]['length']?_0x487cc5[_0xa885('0x38')]:_0x8aba4['model'];if(!_0x4dc788[_0xa885('0x35')][_0xa885('0x3b')]('nolimit')){_0x487cc5[_0xa885('0x24')]=qs[_0xa885('0x24')](_0x4dc788[_0xa885('0x35')][_0xa885('0x24')]);_0x487cc5['offset']=qs['offset'](_0x4dc788[_0xa885('0x35')][_0xa885('0x23')]);}_0x487cc5['order']=qs['sort'](_0x4dc788[_0xa885('0x35')][_0xa885('0x3d')]);_0x487cc5[_0xa885('0x3e')]=qs[_0xa885('0x36')](_[_0xa885('0x3f')](_0x4dc788[_0xa885('0x35')],_0x8aba4[_0xa885('0x36')]));_0x487cc5[_0xa885('0x3e')][_0xa885('0x5c')]=_0x5c0a94['id'];if(_0x4dc788[_0xa885('0x35')][_0xa885('0x40')]){_0x487cc5['where']=_[_0xa885('0x41')](_0x487cc5[_0xa885('0x3e')],{'$or':_[_0xa885('0x31')](_0x487cc5[_0xa885('0x38')],function(_0x1da286){var _0x5cc2cf={};_0x5cc2cf[_0x1da286]={'$like':'%'+_0x4dc788[_0xa885('0x35')]['filter']+'%'};return _0x5cc2cf;})});}_0x487cc5=_['merge']({},_0x487cc5,_0x4dc788[_0xa885('0x44')]);return db[_0xa885('0x5b')]['findAll'](_0x487cc5);}})['then'](function(_0xed114e){if(_0xed114e){return db[_0xa885('0x5d')][_0xa885('0x5e')]({'where':{'id':_['map'](_0xed114e,'UserId'),'role':_0xa885('0x5f')},'attributes':['id',_0xa885('0x2f'),_0xa885('0x60'),_0xa885('0x61'),'createdAt']});}})[_0xa885('0x2a')](function(_0x220192){if(_0x220192){return{'count':_0x220192[_0xa885('0x45')],'rows':_['map'](_0x220192['rows'],function(_0x277499){return{'membername':_0x277499[_0xa885('0x2f')],'UserId':_0x277499['id'],'queue_name':_0x27e8cd[_0xa885('0x2f')],'SmsQueueId':_0x27e8cd['id'],'interface':util['format'](_0xa885('0x62'),_0xa885('0x63'),_0x277499[_0xa885('0x2f')]),'penalty':0x0,'paused':_0x277499[_0xa885('0x60')],'createdAt':_0x277499['createdAt'],'updatedAt':_0x277499[_0xa885('0x61')]};})};}else{return{'count':0x0,'rows':[]};}})[_0xa885('0x2a')](respondWithFilteredResult(_0x1bfacb,_0x487cc5))['catch'](handleError(_0x1bfacb,null));};exports[_0xa885('0x64')]=function(_0x206b22,_0x5a7bde,_0x314d83){var _0x5f2c39={};var _0x44386f={};var _0xfb5b17;var _0x1cb988;return db[_0xa885('0x32')][_0xa885('0x5a')]({'where':{'id':_0x206b22[_0xa885('0x58')]['id']}})[_0xa885('0x2a')](handleEntityNotFound(_0x5a7bde,null))[_0xa885('0x2a')](function(_0x490d15){if(_0x490d15){_0xfb5b17=_0x490d15;_0x44386f['model']=_[_0xa885('0x4b')](db[_0xa885('0x65')]['rawAttributes']);_0x44386f[_0xa885('0x35')]=_[_0xa885('0x4b')](_0x206b22['query']);_0x44386f[_0xa885('0x36')]=_['intersection'](_0x44386f[_0xa885('0x39')],_0x44386f[_0xa885('0x35')]);_0x5f2c39[_0xa885('0x38')]=_[_0xa885('0x37')](_0x44386f['model'],qs['fields'](_0x206b22[_0xa885('0x35')][_0xa885('0x3a')]));_0x5f2c39[_0xa885('0x38')]=_0x5f2c39[_0xa885('0x38')][_0xa885('0x66')]?_0x5f2c39[_0xa885('0x38')]:_0x44386f[_0xa885('0x39')];_0x5f2c39[_0xa885('0x67')]=qs['sort'](_0x206b22[_0xa885('0x35')]['sort']);_0x5f2c39[_0xa885('0x3e')]=qs[_0xa885('0x36')](_[_0xa885('0x3f')](_0x206b22['query'],_0x44386f[_0xa885('0x36')]));if(_0x206b22[_0xa885('0x35')]['filter']){_0x5f2c39['where']=_[_0xa885('0x41')](_0x5f2c39[_0xa885('0x3e')],{'$or':_[_0xa885('0x31')](_0x5f2c39[_0xa885('0x38')],function(_0x243d5e){var _0x4bd60c={};_0x4bd60c[_0x243d5e]={'$like':'%'+_0x206b22['query'][_0xa885('0x40')]+'%'};return _0x4bd60c;})});}_0x5f2c39=_[_0xa885('0x41')]({},_0x5f2c39,_0x206b22[_0xa885('0x44')]);return _0xfb5b17[_0xa885('0x64')](_0x5f2c39);}})['then'](function(_0x4e4aa2){if(_0x4e4aa2){_0x1cb988=_0x4e4aa2['length'];if(!_0x206b22[_0xa885('0x35')][_0xa885('0x3b')](_0xa885('0x3c'))){_0x5f2c39[_0xa885('0x24')]=qs[_0xa885('0x24')](_0x206b22[_0xa885('0x35')][_0xa885('0x24')]);_0x5f2c39['offset']=qs['offset'](_0x206b22[_0xa885('0x35')]['offset']);}return _0xfb5b17[_0xa885('0x64')](_0x5f2c39);}})[_0xa885('0x2a')](function(_0xd7be43){if(_0xd7be43){return _0xd7be43?{'count':_0x1cb988,'rows':_0xd7be43}:null;}})[_0xa885('0x2a')](respondWithResult(_0x5a7bde,null))['catch'](handleError(_0x5a7bde,null));};exports[_0xa885('0x68')]=function(_0xd95575,_0x2ff728,_0x5a6f1f){var _0xe5881c=_0xd95575[_0xa885('0x57')][_0xa885('0x69')];return db['Team'][_0xa885('0x47')]({'where':{'id':_0xe5881c},'attributes':['id'],'include':[{'model':db[_0xa885('0x5d')],'as':_0xa885('0x6a'),'attributes':['id',_0xa885('0x2f'),_0xa885('0x6b'),_0xa885('0x6c'),'interface'],'raw':!![]}]})[_0xa885('0x2a')](function(_0x40a85d){if(_0x40a85d){var _0x3be76a=_[_0xa885('0x6d')](_0x40a85d,function(_0x5b7ab6){var _0x3c088d=_0x5b7ab6[_0xa885('0x4f')]({'plain':!![]});return _0x3c088d[_0xa885('0x6a')];});return db[_0xa885('0x32')][_0xa885('0x4c')]({'where':{'id':_0xd95575[_0xa885('0x58')]['id']}})[_0xa885('0x2a')](function(_0x2388b4){return db['sequelize'][_0xa885('0x6e')](function(_0x3dd30f){return _0x2388b4['addTeams'](_0xe5881c,{'transaction':_0x3dd30f})[_0xa885('0x2a')](function(){return BPromise['each'](_0x3be76a,function(_0x454841){return db[_0xa885('0x5b')]['findOrCreate']({'where':{'UserId':_0x454841['id'],'SmsQueueId':_0xd95575[_0xa885('0x58')]['id']},'transaction':_0x3dd30f});});})['then'](function(){_0x3be76a[_0xa885('0x6f')](function(_0x5a2122){socket[_0xa885('0x70')]('userSmsQueue:save',{'UserId':_0x5a2122['id'],'SmsQueueId':_0x2388b4['id']});});});});});}})[_0xa885('0x2a')](respondWithStatusCode(_0x2ff728,null))[_0xa885('0x49')](handleError(_0x2ff728,null));};exports[_0xa885('0x71')]=function(_0x3e63ba,_0x5408d6,_0x3ce976){return db[_0xa885('0x65')][_0xa885('0x47')]({'where':{'id':_0x3e63ba['query'][_0xa885('0x69')]},'attributes':['id'],'include':[{'model':db[_0xa885('0x5d')],'as':_0xa885('0x6a'),'attributes':['id'],'raw':!![]}]})['then'](handleEntityNotFound(_0x5408d6,null))[_0xa885('0x2a')](function(_0x1915bd){var _0x534151=_['map'](_0x1915bd,'id');var _0xf9e00f=[];var _0xa80b4d=[];var _0x40bfec=squel['select']();_0x40bfec[_0xa885('0x72')](_0xa885('0x73'))[_0xa885('0x74')](_0xa885('0x75'),'tq')[_0xa885('0x76')](_0xa885('0x77'),'ut',_0xa885('0x78'))[_0xa885('0x3e')](_0xa885('0x79'),_0x3e63ba[_0xa885('0x58')]['id']);for(var _0x5ecf82=0x0;_0x5ecf82<_0x1915bd['length'];_0x5ecf82+=0x1){let _0x3e9f5b=_0x1915bd[_0x5ecf82];for(var _0x5dc1d1=0x0;_0x5dc1d1<_0x3e9f5b[_0xa885('0x6a')][_0xa885('0x66')];_0x5dc1d1+=0x1){let _0x1d2de5=_0x3e9f5b['Agents'][_0x5dc1d1];var _0x43b75d=_0x40bfec[_0xa885('0x7a')]();_0x43b75d[_0xa885('0x3e')](_0xa885('0x7b'),_0x1d2de5['id']);_0xa80b4d[_0xa885('0x7c')](db[_0xa885('0x7d')][_0xa885('0x35')](_0x43b75d['toString'](),{'type':db[_0xa885('0x7d')][_0xa885('0x7e')][_0xa885('0x7f')]})[_0xa885('0x2a')](function(_0x5a2395){if(_0x5a2395[_0xa885('0x66')]===0x1){return _0x1d2de5['id'];}else{var _0x44b2d5=_['every'](_['map'](_0x5a2395,_0xa885('0x80')),function(_0x1fe9d3){return _[_0xa885('0x81')](_0x534151,_0x1fe9d3);});if(_0x44b2d5){return _0x1d2de5['id'];}}}));}}return BPromise[_0xa885('0x82')](_0xa80b4d)[_0xa885('0x2a')](function(_0x41f934){_0xf9e00f=_(_0xf9e00f)[_0xa885('0x83')](_0x41f934)[_0xa885('0x84')]()['value']();return db[_0xa885('0x32')]['find']({'where':{'id':_0x3e63ba[_0xa885('0x58')]['id']}})[_0xa885('0x2a')](function(_0x2b4916){return db[_0xa885('0x7d')]['transaction'](function(_0x12c2b2){return _0x2b4916[_0xa885('0x71')](_0x3e63ba[_0xa885('0x35')][_0xa885('0x69')],{'transaction':_0x12c2b2})['then'](function(){if(!_[_0xa885('0x85')](_0xf9e00f)){return _0x2b4916['removeAgents'](_0xf9e00f,{'transaction':_0x12c2b2});}})[_0xa885('0x2a')](function(){_0xf9e00f['forEach'](function(_0x43a66f){socket[_0xa885('0x70')](_0xa885('0x86'),{'UserId':_0x43a66f,'SmsQueueId':_0x2b4916['id']});});});});});});})['then'](respondWithStatusCode(_0x5408d6,null))[_0xa885('0x49')](handleError(_0x5408d6,null));};exports[_0xa885('0x87')]=function(_0x1f0884,_0x15c6a7,_0x9adf59){return db[_0xa885('0x32')][_0xa885('0x4c')]({'where':{'id':_0x1f0884[_0xa885('0x58')]['id']}})[_0xa885('0x2a')](handleEntityNotFound(_0x15c6a7,null))[_0xa885('0x2a')](function(_0x441940){if(_0x441940){return _0x441940[_0xa885('0x87')](_0x1f0884[_0xa885('0x57')][_0xa885('0x69')],_['omit'](_0x1f0884[_0xa885('0x57')],[_0xa885('0x69'),'id'])||{})[_0xa885('0x88')](function(_0x15ccce){for(var _0x4e99b0=0x0;_0x4e99b0<_0x1f0884[_0xa885('0x57')][_0xa885('0x69')][_0xa885('0x66')];_0x4e99b0+=0x1){socket[_0xa885('0x70')](_0xa885('0x89'),{'UserId':_0x1f0884[_0xa885('0x57')][_0xa885('0x69')][_0x4e99b0],'SmsQueueId':_0x1f0884[_0xa885('0x58')]['id']});}return _0x15ccce;});}})[_0xa885('0x2a')](respondWithResult(_0x15c6a7,null))['catch'](handleError(_0x15c6a7,null));};exports['removeAgents']=function(_0x43f8a2,_0x3a2bda,_0xb6f4b2){return db[_0xa885('0x32')][_0xa885('0x4c')]({'where':{'id':_0x43f8a2[_0xa885('0x58')]['id']}})[_0xa885('0x2a')](handleEntityNotFound(_0x3a2bda,null))[_0xa885('0x2a')](function(_0x1df8ce){if(_0x1df8ce){return _0x1df8ce[_0xa885('0x8a')](_0x43f8a2[_0xa885('0x35')]['ids'])[_0xa885('0x2a')](function(){if(_[_0xa885('0x8b')](_0x43f8a2[_0xa885('0x35')][_0xa885('0x69')])){for(var _0x2ec9fe=0x0;_0x2ec9fe<_0x43f8a2['query'][_0xa885('0x69')][_0xa885('0x66')];_0x2ec9fe+=0x1){socket[_0xa885('0x70')]('userSmsQueue:remove',{'UserId':Number(_0x43f8a2[_0xa885('0x35')][_0xa885('0x69')][_0x2ec9fe]),'SmsQueueId':Number(_0x43f8a2[_0xa885('0x58')]['id'])});}}else{socket[_0xa885('0x70')](_0xa885('0x86'),{'UserId':Number(_0x43f8a2[_0xa885('0x35')][_0xa885('0x69')]),'SmsQueueId':Number(_0x43f8a2['params']['id'])});}});}})[_0xa885('0x2a')](respondWithStatusCode(_0x3a2bda,null))[_0xa885('0x49')](handleError(_0x3a2bda,null));};exports[_0xa885('0x8c')]=function(_0x325225,_0x4cd913,_0x49d544){var _0x26630f={};var _0x2321c7={};var _0x1bee35;var _0x39411f;return db[_0xa885('0x32')]['findOne']({'where':{'id':_0x325225[_0xa885('0x58')]['id']}})[_0xa885('0x2a')](handleEntityNotFound(_0x4cd913,null))[_0xa885('0x2a')](function(_0x41a543){if(_0x41a543){_0x1bee35=_0x41a543;_0x2321c7[_0xa885('0x39')]=_[_0xa885('0x4b')](db['User'][_0xa885('0x8d')]);_0x2321c7['query']=_['keys'](_0x325225[_0xa885('0x35')]);_0x2321c7['filters']=_[_0xa885('0x37')](_0x2321c7['model'],_0x2321c7[_0xa885('0x35')]);_0x26630f[_0xa885('0x38')]=_[_0xa885('0x37')](_0x2321c7[_0xa885('0x39')],qs[_0xa885('0x3a')](_0x325225[_0xa885('0x35')][_0xa885('0x3a')]));_0x26630f[_0xa885('0x38')]=_0x26630f[_0xa885('0x38')][_0xa885('0x66')]?_0x26630f[_0xa885('0x38')]:_0x2321c7['model'];_0x26630f[_0xa885('0x67')]=qs[_0xa885('0x3d')](_0x325225[_0xa885('0x35')]['sort']);_0x26630f[_0xa885('0x3e')]=qs[_0xa885('0x36')](_[_0xa885('0x3f')](_0x325225[_0xa885('0x35')],_0x2321c7[_0xa885('0x36')]));if(_0x325225[_0xa885('0x35')]['filter']){_0x26630f[_0xa885('0x3e')]=_[_0xa885('0x41')](_0x26630f[_0xa885('0x3e')],{'$or':_[_0xa885('0x31')](_0x26630f['attributes'],function(_0x52c5fd){var _0x3545dc={};_0x3545dc[_0x52c5fd]={'$like':'%'+_0x325225[_0xa885('0x35')][_0xa885('0x40')]+'%'};return _0x3545dc;})});}_0x26630f=_['merge']({},_0x26630f,_0x325225['options']);return _0x1bee35[_0xa885('0x8c')](_0x26630f);}})[_0xa885('0x2a')](function(_0x42845e){if(_0x42845e){_0x39411f=_0x42845e[_0xa885('0x66')];if(!_0x325225['query'][_0xa885('0x3b')](_0xa885('0x3c'))){_0x26630f['limit']=qs['limit'](_0x325225[_0xa885('0x35')][_0xa885('0x24')]);_0x26630f['offset']=qs[_0xa885('0x23')](_0x325225[_0xa885('0x35')][_0xa885('0x23')]);}return _0x1bee35[_0xa885('0x8c')](_0x26630f);}})[_0xa885('0x2a')](function(_0x489469){if(_0x489469){return _0x489469?{'count':_0x39411f,'rows':_0x489469}:null;}})['then'](respondWithResult(_0x4cd913,null))[_0xa885('0x49')](handleError(_0x4cd913,null));};