788fc9cde46595332eb8ba5db1af04d8b900d060
[motion2.git] / server / api / smsAccount / smsAccount.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 _0x426f=['util','sox','ejs','fs-extra','lodash','crypto','jsforce','desk.js','to-csv','querystring','papaparse','ioredis','../../components/auth/service','../../config/license/hardware','../../config/utils','../../config/environment','../../config/license/util','defaults','redis','localhost','socket.io-emitter','./smsAccount.socket','register','jayson/promise','client','request','info','SmsAccount,\x20%s,\x20%s','request\x20sent','debug','SmsAccount,\x20%s,\x20%s,\x20%s','error','message','result','catch','sendStatus','status','end','json','offset','limit','undefined','count','set','apply','reject','save','update','then','get','SmsAccounts','UserProfileResource','destroy','stack','name','send','index','mandatoryDispositionPause','map','rawAttributes','fieldName','type','model','query','keys','intersection','fields','attributes','length','hasOwnProperty','nolimit','order','sort','where','filters','pick','filter','merge','VIRTUAL','options','SmsAccount','includeAll','include','findAll','rows','show','params','Pause','create','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','find','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','describe','addDisposition','SmsAccountId','Disposition','getDispositions','findOne','removeDispositions','ids','addAnswer','getAnswers','CannedAnswer','removeAnswers','notify','sms','SmsApplication','Applications','account','applications','priority','asc','CmContact','Sequelize','ValidationError','no\x20available\x20attributes','difference','createdAt','updatedAt','CompanyId','ListId','mobile','from','Body','messageId','MessageSid','skebby','sender','text','connectel','clicksend','message_id','plivo','Text','MessageUUID','infobip','results','segmentedMessage','clickatell','fromNumber','replyMessageId','bandwidth','intelepeer','isNil','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','CmList','Contacts','DESC','list','omit','contact','SmsInteraction','spread','interaction','created','autoclose','close','unmanaged','*,*,*,*','SmsMessage','ContactId','AttachmentId','UserId','User','unshift','agent','queueId','queue','waitForTheAssignedQueue','Start','startRouting','receiveMessage','entity\x20found','SmsAccount,\x20%s,\x20%s,\x20%s,\x20%s','twilio','Content-Type','<?xml\x20version=\x221.0\x22\x20encoding=\x22UTF-8\x22?>\x0a<Response></Response>','addApplications','sequelize','ISOLATION_LEVELS','READ_COMMITTED','bulkCreate','findAndCountAll','finally','getApplications','getInteractions','fullname','internal','Tag','Tags','tag','to\x20is\x20mandatory','List','out','Messages','dataValues','received\x20delivery\x20report\x20from','inspect','requestid','statusInt','status_code','groupName','redif','DELIVERED','error_code','RECEIVED_BY_RECIPIENT','DELIVRD','ACCEPTD','csc','message-id','state','addAgents','userSmsAccount:save','removeAgents','isArray','emit','userSmsAccount:remove','getAgents','eml-format','zip-dir','fast-json-patch','moment','bluebird','mustache'];(function(_0x363d41,_0x28d349){var _0x458f3=function(_0x3f4fe7){while(--_0x3f4fe7){_0x363d41['push'](_0x363d41['shift']());}};_0x458f3(++_0x28d349);}(_0x426f,0xe4));var _0xf426=function(_0x47b58b,_0x41a189){_0x47b58b=_0x47b58b-0x0;var _0x46bebe=_0x426f[_0x47b58b];return _0x46bebe;};'use strict';var emlformat=require(_0xf426('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xf426('0x1'));var jsonpatch=require(_0xf426('0x2'));var rp=require('request-promise');var moment=require(_0xf426('0x3'));var BPromise=require(_0xf426('0x4'));var Mustache=require(_0xf426('0x5'));var util=require(_0xf426('0x6'));var path=require('path');var sox=require(_0xf426('0x7'));var csv=require('to-csv');var ejs=require(_0xf426('0x8'));var fs=require('fs');var fs_extra=require(_0xf426('0x9'));var _=require(_0xf426('0xa'));var squel=require('squel');var crypto=require(_0xf426('0xb'));var jsforce=require(_0xf426('0xc'));var deskjs=require(_0xf426('0xd'));var toCsv=require(_0xf426('0xe'));var querystring=require(_0xf426('0xf'));var Papa=require(_0xf426('0x10'));var Redis=require(_0xf426('0x11'));var authService=require(_0xf426('0x12'));var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xf426('0x13'));var logger=require('../../config/logger')('api');var utils=require(_0xf426('0x14'));var config=require(_0xf426('0x15'));var licenseUtil=require(_0xf426('0x16'));var db=require('../../mysqldb')['db'];config['redis']=_[_0xf426('0x17')](config[_0xf426('0x18')],{'host':_0xf426('0x19'),'port':0x18eb});var socket=require(_0xf426('0x1a'))(new Redis(config[_0xf426('0x18')]));require(_0xf426('0x1b'))[_0xf426('0x1c')](socket);var jayson=require(_0xf426('0x1d'));var client=jayson[_0xf426('0x1e')]['http']({'port':0x232c});function respondWithRpcPromise(_0x1bf5ae,_0x30b228,_0x4105b5,_0x3661af){return new BPromise(function(_0x7983fd,_0x3c0fca){var _0x5ca83e=_0x3661af||client;return _0x5ca83e[_0xf426('0x1f')](_0x1bf5ae,_0x4105b5)['then'](function(_0x19e948){logger[_0xf426('0x20')](_0xf426('0x21'),_0x30b228,_0xf426('0x22'));logger[_0xf426('0x23')](_0xf426('0x24'),_0x30b228,_0xf426('0x22'),JSON['stringify'](_0x19e948));if(_0x19e948[_0xf426('0x25')]){if(_0x19e948[_0xf426('0x25')]['code']===0x1f4){logger['error'](_0xf426('0x21'),_0x30b228,_0x19e948[_0xf426('0x25')][_0xf426('0x26')]);return _0x3c0fca(_0x19e948[_0xf426('0x25')][_0xf426('0x26')]);}logger[_0xf426('0x25')]('SmsAccount,\x20%s,\x20%s',_0x30b228,_0x19e948[_0xf426('0x25')][_0xf426('0x26')]);return _0x7983fd(_0x19e948['error'][_0xf426('0x26')]);}else{logger[_0xf426('0x20')](_0xf426('0x21'),_0x30b228,_0xf426('0x22'));_0x7983fd(_0x19e948[_0xf426('0x27')][_0xf426('0x26')]);}})[_0xf426('0x28')](function(_0x7e794a){logger['error']('SmsAccount,\x20%s,\x20%s',_0x30b228,_0x7e794a);_0x3c0fca(_0x7e794a);});});}function respondWithStatusCode(_0xa93cc7,_0x58e8b1){_0x58e8b1=_0x58e8b1||0xcc;return function(_0x2aec20){if(_0x2aec20){return _0xa93cc7[_0xf426('0x29')](_0x58e8b1);}return _0xa93cc7[_0xf426('0x2a')](_0x58e8b1)[_0xf426('0x2b')]();};}function respondWithResult(_0xb20a18,_0x246e21){_0x246e21=_0x246e21||0xc8;return function(_0x57a06f){if(_0x57a06f){return _0xb20a18['status'](_0x246e21)[_0xf426('0x2c')](_0x57a06f);}};}function respondWithFilteredResult(_0x33856a,_0x4f1523){return function(_0x198a48){if(_0x198a48){var _0x5e4e58=typeof _0x4f1523[_0xf426('0x2d')]==='undefined'&&typeof _0x4f1523[_0xf426('0x2e')]===_0xf426('0x2f');var _0x217a7b=_0x198a48['count'];var _0x172bca=_0x5e4e58?0x0:_0x4f1523[_0xf426('0x2d')];var _0x2c2d6e=_0x5e4e58?_0x198a48[_0xf426('0x30')]:_0x4f1523['offset']+_0x4f1523[_0xf426('0x2e')];var _0x255282;if(_0x2c2d6e>=_0x217a7b){_0x2c2d6e=_0x217a7b;_0x255282=0xc8;}else{_0x255282=0xce;}_0x33856a[_0xf426('0x2a')](_0x255282);return _0x33856a[_0xf426('0x31')]('Content-Range',_0x172bca+'-'+_0x2c2d6e+'/'+_0x217a7b)['json'](_0x198a48);}return null;};}function patchUpdates(_0x4b1df6){return function(_0x5cf540){try{jsonpatch[_0xf426('0x32')](_0x5cf540,_0x4b1df6,!![]);}catch(_0x494c72){return BPromise[_0xf426('0x33')](_0x494c72);}return _0x5cf540[_0xf426('0x34')]();};}function saveUpdates(_0x910ba2,_0x1608fe){return function(_0x4760fa){if(_0x4760fa){return _0x4760fa[_0xf426('0x35')](_0x910ba2)[_0xf426('0x36')](function(_0x5ad876){return _0x5ad876;});}return null;};}function removeEntity(_0x4e6ea2,_0x5e6046){return function(_0x4fb0da){if(_0x4fb0da){return _0x4fb0da['destroy']()[_0xf426('0x36')](function(){var _0x2bba79=_0x4fb0da[_0xf426('0x37')]({'plain':!![]});var _0x515646=_0xf426('0x38');return db[_0xf426('0x39')][_0xf426('0x3a')]({'where':{'type':_0x515646,'resourceId':_0x2bba79['id']}})[_0xf426('0x36')](function(){return _0x4fb0da;});})[_0xf426('0x36')](function(){_0x4e6ea2[_0xf426('0x2a')](0xcc)[_0xf426('0x2b')]();});}};}function handleEntityNotFound(_0x4a24cf,_0x591bc5){return function(_0x1c1eb0){if(!_0x1c1eb0){_0x4a24cf[_0xf426('0x29')](0x194);}return _0x1c1eb0;};}function handleError(_0x5169b5,_0x2a2e7c){_0x2a2e7c=_0x2a2e7c||0x1f4;return function(_0x58e41c){logger['error'](_0x58e41c[_0xf426('0x3b')]);if(_0x58e41c[_0xf426('0x3c')]){delete _0x58e41c[_0xf426('0x3c')];}_0x5169b5[_0xf426('0x2a')](_0x2a2e7c)[_0xf426('0x3d')](_0x58e41c);};}exports[_0xf426('0x3e')]=function(_0x266f49,_0x3eb9f2){var _0x44843c={'include':[{'model':db['Pause'],'as':_0xf426('0x3f')}]},_0x22b14c={},_0x47cd97={'count':0x0,'rows':[]};var _0x45011e=_[_0xf426('0x40')](db['SmsAccount'][_0xf426('0x41')],function(_0x311cc5){return{'name':_0x311cc5[_0xf426('0x42')],'type':_0x311cc5[_0xf426('0x43')]['key']};});_0x22b14c[_0xf426('0x44')]=_[_0xf426('0x40')](_0x45011e,'name');_0x22b14c[_0xf426('0x45')]=_[_0xf426('0x46')](_0x266f49[_0xf426('0x45')]);_0x22b14c['filters']=_[_0xf426('0x47')](_0x22b14c[_0xf426('0x44')],_0x22b14c['query']);_0x44843c['attributes']=_['intersection'](_0x22b14c['model'],qs[_0xf426('0x48')](_0x266f49['query']['fields']));_0x44843c[_0xf426('0x49')]=_0x44843c[_0xf426('0x49')][_0xf426('0x4a')]?_0x44843c[_0xf426('0x49')]:_0x22b14c[_0xf426('0x44')];if(!_0x266f49['query'][_0xf426('0x4b')](_0xf426('0x4c'))){_0x44843c[_0xf426('0x2e')]=qs[_0xf426('0x2e')](_0x266f49[_0xf426('0x45')]['limit']);_0x44843c[_0xf426('0x2d')]=qs['offset'](_0x266f49[_0xf426('0x45')][_0xf426('0x2d')]);}_0x44843c[_0xf426('0x4d')]=qs['sort'](_0x266f49['query'][_0xf426('0x4e')]);_0x44843c[_0xf426('0x4f')]=qs[_0xf426('0x50')](_[_0xf426('0x51')](_0x266f49[_0xf426('0x45')],_0x22b14c[_0xf426('0x50')]),_0x45011e);if(_0x266f49[_0xf426('0x45')][_0xf426('0x52')]){_0x44843c['where']=_[_0xf426('0x53')](_0x44843c[_0xf426('0x4f')],{'$or':_[_0xf426('0x40')](_0x45011e,function(_0x1c93c3){if(_0x1c93c3[_0xf426('0x43')]!==_0xf426('0x54')){var _0x57e3cc={};_0x57e3cc[_0x1c93c3['name']]={'$like':'%'+_0x266f49[_0xf426('0x45')][_0xf426('0x52')]+'%'};return _0x57e3cc;}})});}_0x44843c=_[_0xf426('0x53')]({},_0x44843c,_0x266f49[_0xf426('0x55')]);var _0x3bd881={'where':_0x44843c[_0xf426('0x4f')]};return db[_0xf426('0x56')]['count'](_0x3bd881)[_0xf426('0x36')](function(_0x519814){_0x47cd97[_0xf426('0x30')]=_0x519814;if(_0x266f49[_0xf426('0x45')][_0xf426('0x57')]){_0x44843c[_0xf426('0x58')]=[{'all':!![]}];}return db[_0xf426('0x56')][_0xf426('0x59')](_0x44843c);})['then'](function(_0x1f547a){_0x47cd97[_0xf426('0x5a')]=_0x1f547a;return _0x47cd97;})[_0xf426('0x36')](respondWithFilteredResult(_0x3eb9f2,_0x44843c))[_0xf426('0x28')](handleError(_0x3eb9f2,null));};exports[_0xf426('0x5b')]=function(_0x1c39d7,_0x55c069){var _0x2df20f={'raw':![],'where':{'id':_0x1c39d7[_0xf426('0x5c')]['id']},'include':[{'model':db[_0xf426('0x5d')],'as':_0xf426('0x3f')}]},_0x490c76={};_0x490c76[_0xf426('0x44')]=_[_0xf426('0x46')](db[_0xf426('0x56')]['rawAttributes']);_0x490c76['query']=_['keys'](_0x1c39d7[_0xf426('0x45')]);_0x490c76[_0xf426('0x50')]=_[_0xf426('0x47')](_0x490c76[_0xf426('0x44')],_0x490c76[_0xf426('0x45')]);_0x2df20f['attributes']=_[_0xf426('0x47')](_0x490c76[_0xf426('0x44')],qs['fields'](_0x1c39d7['query'][_0xf426('0x48')]));_0x2df20f[_0xf426('0x49')]=_0x2df20f['attributes'][_0xf426('0x4a')]?_0x2df20f[_0xf426('0x49')]:_0x490c76['model'];if(_0x1c39d7[_0xf426('0x45')][_0xf426('0x57')]){_0x2df20f['include']=[{'all':!![]}];}_0x2df20f=_[_0xf426('0x53')]({},_0x2df20f,_0x1c39d7[_0xf426('0x55')]);return db[_0xf426('0x56')]['find'](_0x2df20f)[_0xf426('0x36')](handleEntityNotFound(_0x55c069,null))[_0xf426('0x36')](respondWithResult(_0x55c069,null))[_0xf426('0x28')](handleError(_0x55c069,null));};exports[_0xf426('0x5e')]=function(_0x20a7be,_0x34435d){return db['SmsAccount']['create'](_0x20a7be['body'],{})[_0xf426('0x36')](function(_0x3e6520){var _0x4076ca=_0x20a7be[_0xf426('0x5f')]['get']({'plain':!![]});if(!_0x4076ca)throw new Error(_0xf426('0x60'));if(_0x4076ca[_0xf426('0x61')]===_0xf426('0x5f')){var _0x39491d=_0x3e6520[_0xf426('0x37')]({'plain':!![]});var _0x2af811=_0xf426('0x38');return db[_0xf426('0x62')][_0xf426('0x63')]({'where':{'name':_0x2af811,'userProfileId':_0x4076ca[_0xf426('0x64')]},'raw':!![]})[_0xf426('0x36')](function(_0x3f046f){if(_0x3f046f&&_0x3f046f[_0xf426('0x65')]===0x0){return db[_0xf426('0x39')][_0xf426('0x5e')]({'name':_0x39491d[_0xf426('0x3c')],'resourceId':_0x39491d['id'],'type':_0x3f046f[_0xf426('0x3c')],'sectionId':_0x3f046f['id']},{})[_0xf426('0x36')](function(){return _0x3e6520;});}else{return _0x3e6520;}})['catch'](function(_0x558a38){logger[_0xf426('0x25')](_0xf426('0x66'),_0x558a38);throw _0x558a38;});}return _0x3e6520;})[_0xf426('0x36')](respondWithResult(_0x34435d,0xc9))[_0xf426('0x28')](handleError(_0x34435d,null));};exports[_0xf426('0x35')]=function(_0x5bdc09,_0x249f6c){if(_0x5bdc09[_0xf426('0x67')]['id']){delete _0x5bdc09['body']['id'];}return db[_0xf426('0x56')][_0xf426('0x63')]({'where':{'id':_0x5bdc09[_0xf426('0x5c')]['id']},'include':[{'model':db['Pause'],'as':_0xf426('0x3f')}]})[_0xf426('0x36')](handleEntityNotFound(_0x249f6c,null))['then'](saveUpdates(_0x5bdc09[_0xf426('0x67')],null))[_0xf426('0x36')](respondWithResult(_0x249f6c,null))[_0xf426('0x28')](handleError(_0x249f6c,null));};exports[_0xf426('0x3a')]=function(_0x3c305c,_0x5dffb8){return db[_0xf426('0x56')]['find']({'where':{'id':_0x3c305c['params']['id']}})[_0xf426('0x36')](handleEntityNotFound(_0x5dffb8,null))['then'](removeEntity(_0x5dffb8,null))[_0xf426('0x28')](handleError(_0x5dffb8,null));};exports[_0xf426('0x68')]=function(_0x1cf023,_0x2bb3d0){return db[_0xf426('0x56')][_0xf426('0x68')]()[_0xf426('0x36')](respondWithResult(_0x2bb3d0,null))[_0xf426('0x28')](handleError(_0x2bb3d0,null));};exports[_0xf426('0x69')]=function(_0x26c250,_0xae8271,_0xd73052){if(_0x26c250[_0xf426('0x67')]['id']){delete _0x26c250[_0xf426('0x67')]['id'];}return db[_0xf426('0x56')]['find']({'where':{'id':_0x26c250[_0xf426('0x5c')]['id']}})['then'](handleEntityNotFound(_0xae8271,null))[_0xf426('0x36')](function(_0x57d2c3){if(_0x57d2c3){_0x26c250[_0xf426('0x67')][_0xf426('0x6a')]=_0x57d2c3['id'];return db[_0xf426('0x6b')][_0xf426('0x5e')](_0x26c250['body']);}})['then'](respondWithResult(_0xae8271,null))[_0xf426('0x28')](handleError(_0xae8271,null));};exports[_0xf426('0x6c')]=function(_0x3ee62c,_0x5c2b33,_0x1b4ac5){var _0x2b77ca={'raw':![],'where':{}};var _0x74225e={};var _0x21c616={'count':0x0,'rows':[]};return db[_0xf426('0x56')][_0xf426('0x6d')]({'where':{'id':_0x3ee62c['params']['id']}})[_0xf426('0x36')](handleEntityNotFound(_0x5c2b33,null))['then'](function(_0x2e9336){if(_0x2e9336){_0x74225e['model']=_[_0xf426('0x46')](db['Disposition'][_0xf426('0x41')]);_0x74225e['query']=_['keys'](_0x3ee62c[_0xf426('0x45')]);_0x74225e[_0xf426('0x50')]=_[_0xf426('0x47')](_0x74225e[_0xf426('0x44')],_0x74225e[_0xf426('0x45')]);_0x2b77ca[_0xf426('0x49')]=_['intersection'](_0x74225e[_0xf426('0x44')],qs[_0xf426('0x48')](_0x3ee62c['query'][_0xf426('0x48')]));_0x2b77ca[_0xf426('0x49')]=_0x2b77ca[_0xf426('0x49')][_0xf426('0x4a')]?_0x2b77ca[_0xf426('0x49')]:_0x74225e['model'];if(!_0x3ee62c[_0xf426('0x45')]['hasOwnProperty']('nolimit')){_0x2b77ca['limit']=qs[_0xf426('0x2e')](_0x3ee62c[_0xf426('0x45')]['limit']);_0x2b77ca[_0xf426('0x2d')]=qs[_0xf426('0x2d')](_0x3ee62c[_0xf426('0x45')][_0xf426('0x2d')]);}_0x2b77ca['order']=qs[_0xf426('0x4e')](_0x3ee62c[_0xf426('0x45')][_0xf426('0x4e')]);_0x2b77ca['where']=qs[_0xf426('0x50')](_[_0xf426('0x51')](_0x3ee62c[_0xf426('0x45')],_0x74225e[_0xf426('0x50')]));_0x2b77ca[_0xf426('0x4f')][_0xf426('0x6a')]=_0x2e9336['id'];if(_0x3ee62c['query']['filter']){_0x2b77ca[_0xf426('0x4f')]=_['merge'](_0x2b77ca['where'],{'$or':_[_0xf426('0x40')](_0x2b77ca[_0xf426('0x49')],function(_0x314edc){var _0x125747={};_0x125747[_0x314edc]={'$like':'%'+_0x3ee62c[_0xf426('0x45')][_0xf426('0x52')]+'%'};return _0x125747;})});}_0x2b77ca=_[_0xf426('0x53')]({},_0x2b77ca,_0x3ee62c[_0xf426('0x55')]);return db[_0xf426('0x6b')][_0xf426('0x30')]({'where':_0x2b77ca['where']})['then'](function(_0x229061){_0x21c616[_0xf426('0x30')]=_0x229061;if(_0x3ee62c[_0xf426('0x45')][_0xf426('0x57')]){_0x2b77ca[_0xf426('0x58')]=[{'all':!![]}];}return db[_0xf426('0x6b')][_0xf426('0x59')](_0x2b77ca);})[_0xf426('0x36')](function(_0x5c185f){_0x21c616['rows']=_0x5c185f;return _0x21c616;});}})['then'](respondWithFilteredResult(_0x5c2b33,_0x2b77ca))[_0xf426('0x28')](handleError(_0x5c2b33,null));};exports[_0xf426('0x6e')]=function(_0xce285e,_0x3b98f6,_0x2d7160){return db[_0xf426('0x56')][_0xf426('0x63')]({'where':{'id':_0xce285e[_0xf426('0x5c')]['id']}})[_0xf426('0x36')](handleEntityNotFound(_0x3b98f6,null))[_0xf426('0x36')](function(_0x29c4ec){if(_0x29c4ec){return _0x29c4ec[_0xf426('0x6e')](_0xce285e[_0xf426('0x45')][_0xf426('0x6f')]);}})[_0xf426('0x36')](respondWithStatusCode(_0x3b98f6,null))[_0xf426('0x28')](handleError(_0x3b98f6,null));};exports[_0xf426('0x70')]=function(_0x3f0a25,_0x49a207,_0x5e16c4){if(_0x3f0a25['body']['id']){delete _0x3f0a25['body']['id'];}return db['SmsAccount'][_0xf426('0x63')]({'where':{'id':_0x3f0a25['params']['id']}})[_0xf426('0x36')](handleEntityNotFound(_0x49a207,null))[_0xf426('0x36')](function(_0x53777b){if(_0x53777b){_0x3f0a25[_0xf426('0x67')][_0xf426('0x6a')]=_0x53777b['id'];return db['CannedAnswer'][_0xf426('0x5e')](_0x3f0a25[_0xf426('0x67')]);}})[_0xf426('0x36')](respondWithResult(_0x49a207,null))['catch'](handleError(_0x49a207,null));};exports[_0xf426('0x71')]=function(_0xd70915,_0x28518c,_0x14f423){var _0x3e53b2={'raw':![],'where':{}};var _0x9406f5={};var _0x1e45c3={'count':0x0,'rows':[]};return db[_0xf426('0x56')][_0xf426('0x6d')]({'where':{'id':_0xd70915[_0xf426('0x5c')]['id']}})[_0xf426('0x36')](handleEntityNotFound(_0x28518c,null))[_0xf426('0x36')](function(_0x3fa66e){if(_0x3fa66e){_0x9406f5[_0xf426('0x44')]=_[_0xf426('0x46')](db[_0xf426('0x72')]['rawAttributes']);_0x9406f5[_0xf426('0x45')]=_[_0xf426('0x46')](_0xd70915['query']);_0x9406f5[_0xf426('0x50')]=_[_0xf426('0x47')](_0x9406f5['model'],_0x9406f5[_0xf426('0x45')]);_0x3e53b2[_0xf426('0x49')]=_[_0xf426('0x47')](_0x9406f5[_0xf426('0x44')],qs[_0xf426('0x48')](_0xd70915[_0xf426('0x45')][_0xf426('0x48')]));_0x3e53b2['attributes']=_0x3e53b2[_0xf426('0x49')]['length']?_0x3e53b2[_0xf426('0x49')]:_0x9406f5['model'];if(!_0xd70915[_0xf426('0x45')][_0xf426('0x4b')](_0xf426('0x4c'))){_0x3e53b2[_0xf426('0x2e')]=qs[_0xf426('0x2e')](_0xd70915[_0xf426('0x45')][_0xf426('0x2e')]);_0x3e53b2['offset']=qs[_0xf426('0x2d')](_0xd70915[_0xf426('0x45')][_0xf426('0x2d')]);}_0x3e53b2[_0xf426('0x4d')]=qs[_0xf426('0x4e')](_0xd70915[_0xf426('0x45')][_0xf426('0x4e')]);_0x3e53b2['where']=qs['filters'](_['pick'](_0xd70915[_0xf426('0x45')],_0x9406f5[_0xf426('0x50')]));_0x3e53b2[_0xf426('0x4f')][_0xf426('0x6a')]=_0x3fa66e['id'];if(_0xd70915[_0xf426('0x45')]['filter']){_0x3e53b2['where']=_[_0xf426('0x53')](_0x3e53b2['where'],{'$or':_['map'](_0x3e53b2[_0xf426('0x49')],function(_0x2b3837){var _0x15280e={};_0x15280e[_0x2b3837]={'$like':'%'+_0xd70915[_0xf426('0x45')][_0xf426('0x52')]+'%'};return _0x15280e;})});}_0x3e53b2=_[_0xf426('0x53')]({},_0x3e53b2,_0xd70915[_0xf426('0x55')]);return db[_0xf426('0x72')]['count']({'where':_0x3e53b2['where']})[_0xf426('0x36')](function(_0x4a6934){_0x1e45c3[_0xf426('0x30')]=_0x4a6934;if(_0xd70915[_0xf426('0x45')]['includeAll']){_0x3e53b2[_0xf426('0x58')]=[{'all':!![]}];}return db[_0xf426('0x72')]['findAll'](_0x3e53b2);})[_0xf426('0x36')](function(_0x5dd8cc){_0x1e45c3[_0xf426('0x5a')]=_0x5dd8cc;return _0x1e45c3;});}})[_0xf426('0x36')](respondWithFilteredResult(_0x28518c,_0x3e53b2))[_0xf426('0x28')](handleError(_0x28518c,null));};exports[_0xf426('0x73')]=function(_0x1e8713,_0x56981e,_0x114b0b){return db[_0xf426('0x56')][_0xf426('0x63')]({'where':{'id':_0x1e8713[_0xf426('0x5c')]['id']}})[_0xf426('0x36')](handleEntityNotFound(_0x56981e,null))['then'](function(_0x1c79e8){if(_0x1c79e8){return _0x1c79e8[_0xf426('0x73')](_0x1e8713[_0xf426('0x45')][_0xf426('0x6f')]);}})[_0xf426('0x36')](respondWithStatusCode(_0x56981e,null))[_0xf426('0x28')](handleError(_0x56981e,null));};exports[_0xf426('0x74')]=function(_0x11f032,_0x1abb20,_0x2c5230){var _0x49af93={'channel':_0xf426('0x75')};var _0x123d31=[];var _0x272cf3=[];var _0x144e8e={};return db[_0xf426('0x56')][_0xf426('0x63')]({'where':{'id':_0x11f032[_0xf426('0x5c')]['id']},'include':[{'model':db[_0xf426('0x76')],'as':_0xf426('0x77')}]})[_0xf426('0x36')](handleEntityNotFound(_0x1abb20,null))[_0xf426('0x36')](function(_0x2beddf){_0x49af93[_0xf426('0x78')]=_0x2beddf;_0x49af93[_0xf426('0x79')]=_['orderBy'](_0x2beddf[_0xf426('0x77')],[_0xf426('0x7a')],[_0xf426('0x7b')]);if(_0x49af93[_0xf426('0x78')][_0xf426('0x77')]){delete _0x49af93['account'][_0xf426('0x77')];}return db[_0xf426('0x7c')]['describe']();})[_0xf426('0x36')](function(_0x20d84e){if(!_0x20d84e){throw new db[(_0xf426('0x7d'))][(_0xf426('0x7e'))](_0xf426('0x7f'));}_0x123d31=_[_0xf426('0x80')](_[_0xf426('0x46')](_0x20d84e),[_0xf426('0x81'),_0xf426('0x82')]);_0x272cf3=_[_0xf426('0x80')](_['keys'](_0x20d84e),[_0xf426('0x81'),_0xf426('0x82'),_0xf426('0x83'),_0xf426('0x84')]);_0x49af93[_0xf426('0x67')]={'mapKey':_0xf426('0x85')};if(_0x49af93[_0xf426('0x78')][_0xf426('0x43')]){switch(_0x49af93['account'][_0xf426('0x43')]){case'twilio':_0x49af93[_0xf426('0x67')][_0xf426('0x86')]=_0x11f032[_0xf426('0x67')]['From'];_0x49af93[_0xf426('0x67')][_0xf426('0x67')]=_0x11f032[_0xf426('0x67')][_0xf426('0x87')];_0x49af93['body'][_0xf426('0x88')]=_0x11f032[_0xf426('0x67')][_0xf426('0x89')];break;case _0xf426('0x8a'):_0x49af93['body']['from']='+'+_0x11f032['body'][_0xf426('0x8b')];_0x49af93[_0xf426('0x67')]['body']=_0x11f032[_0xf426('0x67')][_0xf426('0x8c')];break;case _0xf426('0x8d'):_0x49af93['body']['from']=_0x11f032[_0xf426('0x67')][_0xf426('0x86')];_0x49af93[_0xf426('0x67')][_0xf426('0x67')]=_0x11f032['body'][_0xf426('0x26')];break;case _0xf426('0x8e'):_0x49af93[_0xf426('0x67')]['from']=_0x11f032['body'][_0xf426('0x86')];_0x49af93[_0xf426('0x67')][_0xf426('0x67')]=_0x11f032[_0xf426('0x67')]['body'];_0x49af93['body'][_0xf426('0x88')]=_0x11f032[_0xf426('0x67')][_0xf426('0x8f')];break;case _0xf426('0x90'):_0x49af93[_0xf426('0x67')]['from']=_0x11f032[_0xf426('0x67')]['From'];_0x49af93['body'][_0xf426('0x67')]=_0x11f032[_0xf426('0x67')][_0xf426('0x91')];_0x49af93[_0xf426('0x67')][_0xf426('0x88')]=_0x11f032[_0xf426('0x67')][_0xf426('0x92')];break;case _0xf426('0x93'):_0x49af93[_0xf426('0x67')]['from']='+'+_0x11f032[_0xf426('0x67')][_0xf426('0x94')][0x0][_0xf426('0x86')];_0x49af93[_0xf426('0x67')][_0xf426('0x67')]=_0x11f032[_0xf426('0x67')][_0xf426('0x94')][0x0]['text'];_0x49af93[_0xf426('0x67')]['messageId']=_0x11f032[_0xf426('0x67')][_0xf426('0x94')][0x0][_0xf426('0x88')];break;case'csc':_0x49af93[_0xf426('0x67')][_0xf426('0x86')]=_0x11f032[_0xf426('0x67')]['originatingAddress'];_0x49af93['body'][_0xf426('0x67')]=_0x11f032[_0xf426('0x67')][_0xf426('0x95')][_0xf426('0x26')];break;case _0xf426('0x96'):_0x49af93[_0xf426('0x67')]['from']=_0x11f032[_0xf426('0x67')][_0xf426('0x97')];_0x49af93[_0xf426('0x67')]['body']=_0x11f032[_0xf426('0x67')][_0xf426('0x8c')];_0x49af93[_0xf426('0x67')][_0xf426('0x88')]=_0x11f032[_0xf426('0x67')][_0xf426('0x98')];break;case _0xf426('0x99'):_0x49af93[_0xf426('0x67')][_0xf426('0x88')]=_0x11f032[_0xf426('0x67')][0x0][_0xf426('0x26')]['id'];_0x49af93[_0xf426('0x67')]['from']=_0x11f032[_0xf426('0x67')][0x0][_0xf426('0x26')]['from'];_0x49af93[_0xf426('0x67')][_0xf426('0x67')]=_0x11f032[_0xf426('0x67')][0x0][_0xf426('0x26')][_0xf426('0x8c')];break;case _0xf426('0x9a'):_0x49af93[_0xf426('0x67')][_0xf426('0x86')]=_0x11f032[_0xf426('0x67')][_0xf426('0x86')];_0x49af93['body'][_0xf426('0x67')]=_0x11f032[_0xf426('0x67')][_0xf426('0x26')];_0x49af93[_0xf426('0x67')][_0xf426('0x88')]=_0x11f032[_0xf426('0x67')]['refid'];break;}}if(_0x49af93['body']['id']){delete _0x49af93[_0xf426('0x67')]['id'];}if(_[_0xf426('0x9b')](_0x49af93[_0xf426('0x67')][_0xf426('0x86')])){throw new db['Sequelize'][(_0xf426('0x7e'))]('from\x20is\x20mandatory');}if(_[_0xf426('0x9b')](_0x49af93[_0xf426('0x67')][_0xf426('0x67')])||_0x49af93[_0xf426('0x67')][_0xf426('0x67')]===''){throw new db[(_0xf426('0x7d'))][(_0xf426('0x7e'))](_0xf426('0x9c'));}if(_[_0xf426('0x9b')](_0x49af93[_0xf426('0x67')][_0xf426('0x9d')])){throw new db['Sequelize']['ValidationError'](_0xf426('0x9e')+_0x272cf3);}if(!_[_0xf426('0x9f')](_0x272cf3,_0x49af93[_0xf426('0x67')]['mapKey'])){throw new db['Sequelize'][(_0xf426('0x7e'))](_0xf426('0xa0')+_0x272cf3);}_0x144e8e[_0x49af93['body'][_0xf426('0x9d')]]=_0x49af93[_0xf426('0x67')]['from'];return db[_0xf426('0xa1')][_0xf426('0x63')]({'where':{'id':_0x49af93[_0xf426('0x78')]['ListId']},'include':[{'model':db[_0xf426('0x7c')],'as':_0xf426('0xa2'),'where':_0x144e8e,'limit':0x1,'order':[['updatedAt',_0xf426('0xa3')]]}]});})['then'](handleEntityNotFound(_0x1abb20,null))[_0xf426('0x36')](function(_0x411f58){if(_0x411f58){_0x49af93[_0xf426('0xa4')]=_[_0xf426('0xa5')](_0x411f58,[_0xf426('0xa2')]);if(_0x411f58['Contacts'][_0xf426('0x4a')]){return _0x411f58[_0xf426('0xa2')][0x0];}var _0x3dfdae=_[_0xf426('0x17')](_0x49af93['body'],{'firstName':_0x49af93[_0xf426('0x67')][_0xf426('0x86')],'ListId':_0x411f58['id']});_0x3dfdae[_0x49af93[_0xf426('0x67')][_0xf426('0x9d')]]=_0x49af93[_0xf426('0x67')][_0xf426('0x86')];return db[_0xf426('0x7c')]['create'](_0x3dfdae,{'fields':_0x123d31,'raw':!![]});}})['then'](handleEntityNotFound(_0x1abb20,null))[_0xf426('0x36')](function(_0x464fb8){if(_0x464fb8){_0x49af93[_0xf426('0xa6')]=_0x464fb8;var _0x103b5c={'ContactId':_0x464fb8['id'],'phone':_0x49af93[_0xf426('0x67')][_0xf426('0x86')],'SmsAccountId':_0x11f032['params']['id']};var _0xb662aa={'ContactId':_0x464fb8['id'],'SmsAccountId':_0x11f032[_0xf426('0x5c')]['id'],'closed':![]};return db[_0xf426('0xa7')][_0xf426('0x63')]({'where':_0xb662aa})[_0xf426('0x36')](function(_0xc3dd4c){if(_0xc3dd4c){return[_0xc3dd4c,![]];}return db[_0xf426('0xa7')][_0xf426('0x5e')](_0x103b5c)[_0xf426('0x36')](function(_0x5a7948){return[_0x5a7948,!![]];});});}})[_0xf426('0xa8')](function(_0x23c566,_0x502b5f){_0x49af93[_0xf426('0xa9')]=_0x23c566[_0xf426('0x37')]({'plain':!![]});_0x49af93[_0xf426('0xa9')][_0xf426('0xaa')]=![];if(_0x502b5f){if(_0x49af93['account'][_0xf426('0xab')]){_0x49af93['applications']['push']({'id':0x0,'priority':_0x49af93[_0xf426('0x79')][_0xf426('0x4a')]+0x1,'app':_0xf426('0xac'),'appdata':_0xf426('0xad'),'interval':_0xf426('0xae')});}_0x49af93[_0xf426('0xa9')][_0xf426('0xaa')]=!![];}return db[_0xf426('0xaf')]['create']({'phone':_0x49af93[_0xf426('0x67')][_0xf426('0x86')],'messageId':_0x49af93[_0xf426('0x67')]['messageId'],'body':_0x49af93[_0xf426('0x67')][_0xf426('0x67')],'SmsAccountId':_0x11f032['params']['id'],'SmsInteractionId':_0x23c566['id'],'direction':'in','ContactId':_0x23c566[_0xf426('0xb0')],'AttachmentId':_0x49af93[_0xf426('0x67')][_0xf426('0xb1')]});})[_0xf426('0x36')](function(_0x4fdc01){_0x49af93[_0xf426('0x26')]=_0x4fdc01;if(!_0x49af93[_0xf426('0xa9')][_0xf426('0xb2')])return;return db[_0xf426('0xb3')]['find']({'attributes':['id','name'],'where':{'id':_0x49af93['interaction']['UserId']}});})['then'](function(_0x4ec556){if(_0x4ec556){_0x49af93[_0xf426('0x79')][_0xf426('0xb4')]({'id':0x0,'priority':0x0,'app':_0xf426('0xb5'),'appdata':_0x4ec556[_0xf426('0x3c')]+','+(_0x49af93['account']['waitForTheAssignedAgent']||0xa),'interval':'*,*,*,*'});}if(!_0x49af93[_0xf426('0xa9')][_0xf426('0xb6')])return;return db['SmsQueue'][_0xf426('0x63')]({'attributes':['id',_0xf426('0x3c')],'where':{'id':_0x49af93['interaction'][_0xf426('0xb6')]}});})[_0xf426('0x36')](function(_0x4be20f){if(_0x4be20f){_0x49af93['applications'][_0xf426('0xb4')]({'id':0x0,'priority':0x1,'app':_0xf426('0xb7'),'appdata':_0x4be20f[_0xf426('0x3c')]+','+(_0x49af93[_0xf426('0x78')][_0xf426('0xb8')]||0x12c),'interval':_0xf426('0xae')});}return respondWithRpcPromise(_0xf426('0xb9'),_0xf426('0xba'),_0x49af93);})['then'](function(_0xd06925){if(_0xd06925){var _0xe36f77=0xc8;var _0x4da258=_0xf426('0xbb');logger[_0xf426('0x20')](_0xf426('0x24'),_0x4da258,_0xe36f77,_0xf426('0xbc'));logger['debug'](_0xf426('0xbd'),_0x4da258,_0xe36f77,_0xf426('0xbc'),JSON['stringify'](_0xd06925));if(_0x49af93[_0xf426('0x78')][_0xf426('0x43')]===_0xf426('0xbe')){return _0x1abb20[_0xf426('0x2a')](_0xe36f77)[_0xf426('0x31')](_0xf426('0xbf'),'text/xml')[_0xf426('0x3d')](_0xf426('0xc0'));}else{return _0x1abb20[_0xf426('0x2a')](_0xe36f77)[_0xf426('0x2c')](_0xd06925);}}})[_0xf426('0x28')](handleError(_0x1abb20,null));};exports[_0xf426('0xc1')]=function(_0x21a561,_0x45165e){var _0x5791d0=_0x21a561[_0xf426('0x5c')]['id'];var _0x2a6739=_0x21a561[_0xf426('0x67')];var _0x98a0ab=0xc8;var _0x5edf10=null;return db['sequelize']['transaction']({'isolationLevel':db[_0xf426('0xc2')]['Transaction'][_0xf426('0xc3')][_0xf426('0xc4')]},function(_0xc8ed00){return db[_0xf426('0x56')][_0xf426('0x6d')]({'where':{'id':_0x5791d0},'transaction':_0xc8ed00})[_0xf426('0x36')](function(_0x207f20){if(_0x207f20){return db[_0xf426('0x76')][_0xf426('0x3a')]({'where':{'SmsAccountId':_0x5791d0},'transaction':_0xc8ed00})[_0xf426('0x36')](function(){var _0x1f7036=_[_0xf426('0x40')](_0x2a6739,function(_0x3910d6){_0x3910d6[_0xf426('0x6a')]=_0x5791d0;return _0x3910d6;});return db[_0xf426('0x76')][_0xf426('0xc5')](_0x1f7036,{'transaction':_0xc8ed00});});}else{_0x98a0ab=0x194;_0x5edf10=[];}});})['then'](function(){if(_0x98a0ab!==0x194){return db[_0xf426('0x76')][_0xf426('0xc6')]({'where':{'SmsAccountId':_0x5791d0},'order':_0xf426('0x7a')})[_0xf426('0x36')](function(_0x45bfa5){_0x5edf10=_0x45bfa5;});}})[_0xf426('0x28')](function(_0xfdf01f){_0x98a0ab=0x1f4;logger[_0xf426('0x25')](_0xfdf01f[_0xf426('0x3b')]);if(_0xfdf01f[_0xf426('0x3c')]){delete _0xfdf01f['name'];}_0x5edf10=_0xfdf01f;})[_0xf426('0xc7')](function(){if(_0x5edf10===null){_0x45165e[_0xf426('0x29')](_0x98a0ab);}else{if(_0x98a0ab===0x1f4){_0x45165e[_0xf426('0x2a')](_0x98a0ab)[_0xf426('0x3d')](_0x5edf10);}else{_0x45165e['status'](_0x98a0ab)[_0xf426('0x2c')](_0x5edf10);}}});};exports['getApplications']=function(_0x546e60,_0xa72dfc,_0x42094d){var _0x4cf700={};var _0x125779={};var _0x33631a;var _0x52aac1;return db[_0xf426('0x56')][_0xf426('0x6d')]({'where':{'id':_0x546e60[_0xf426('0x5c')]['id']}})[_0xf426('0x36')](handleEntityNotFound(_0xa72dfc,null))['then'](function(_0x403d6a){if(_0x403d6a){_0x33631a=_0x403d6a;_0x125779[_0xf426('0x44')]=_['keys'](db[_0xf426('0x76')][_0xf426('0x41')]);_0x125779[_0xf426('0x45')]=_['keys'](_0x546e60[_0xf426('0x45')]);_0x125779['filters']=_[_0xf426('0x47')](_0x125779[_0xf426('0x44')],_0x125779[_0xf426('0x45')]);_0x4cf700[_0xf426('0x49')]=_[_0xf426('0x47')](_0x125779[_0xf426('0x44')],qs['fields'](_0x546e60[_0xf426('0x45')][_0xf426('0x48')]));_0x4cf700[_0xf426('0x49')]=_0x4cf700['attributes'][_0xf426('0x4a')]?_0x4cf700[_0xf426('0x49')]:_0x125779[_0xf426('0x44')];_0x4cf700[_0xf426('0x4d')]=qs[_0xf426('0x4e')](_0x546e60['query'][_0xf426('0x4e')]);_0x4cf700['where']=qs[_0xf426('0x50')](_['pick'](_0x546e60[_0xf426('0x45')],_0x125779[_0xf426('0x50')]));if(_0x546e60[_0xf426('0x45')][_0xf426('0x52')]){_0x4cf700['where']=_[_0xf426('0x53')](_0x4cf700['where'],{'$or':_['map'](_0x4cf700[_0xf426('0x49')],function(_0x3a9e83){var _0x3b24a2={};_0x3b24a2[_0x3a9e83]={'$like':'%'+_0x546e60[_0xf426('0x45')][_0xf426('0x52')]+'%'};return _0x3b24a2;})});}_0x4cf700=_[_0xf426('0x53')]({},_0x4cf700,_0x546e60['options']);return _0x33631a['getApplications'](_0x4cf700);}})['then'](function(_0x163e5d){if(_0x163e5d){_0x52aac1=_0x163e5d[_0xf426('0x4a')];if(!_0x546e60[_0xf426('0x45')][_0xf426('0x4b')](_0xf426('0x4c'))){_0x4cf700[_0xf426('0x2e')]=qs[_0xf426('0x2e')](_0x546e60['query'][_0xf426('0x2e')]);_0x4cf700[_0xf426('0x2d')]=qs[_0xf426('0x2d')](_0x546e60[_0xf426('0x45')][_0xf426('0x2d')]);}return _0x33631a[_0xf426('0xc8')](_0x4cf700);}})['then'](function(_0x1ffe2d){if(_0x1ffe2d){return _0x1ffe2d?{'count':_0x52aac1,'rows':_0x1ffe2d}:null;}})[_0xf426('0x36')](respondWithResult(_0xa72dfc,null))[_0xf426('0x28')](handleError(_0xa72dfc,null));};exports[_0xf426('0xc9')]=function(_0x3f356e,_0x1d5391,_0x21181f){var _0x50c0e8={'raw':![],'where':{}};var _0x6b3a43={};var _0x182da7={'count':0x0,'rows':[]};return db[_0xf426('0x56')][_0xf426('0x6d')]({'where':{'id':_0x3f356e[_0xf426('0x5c')]['id']}})[_0xf426('0x36')](handleEntityNotFound(_0x1d5391,null))['then'](function(_0x39391a){if(_0x39391a){_0x6b3a43[_0xf426('0x44')]=_[_0xf426('0x46')](db[_0xf426('0xa7')][_0xf426('0x41')]);_0x6b3a43[_0xf426('0x45')]=_[_0xf426('0x46')](_0x3f356e[_0xf426('0x45')]);_0x6b3a43['filters']=_[_0xf426('0x47')](_0x6b3a43[_0xf426('0x44')],_0x6b3a43[_0xf426('0x45')]);_0x50c0e8[_0xf426('0x49')]=_['intersection'](_0x6b3a43[_0xf426('0x44')],qs[_0xf426('0x48')](_0x3f356e[_0xf426('0x45')]['fields']));_0x50c0e8[_0xf426('0x49')]=_0x50c0e8[_0xf426('0x49')][_0xf426('0x4a')]?_0x50c0e8[_0xf426('0x49')]:_0x6b3a43[_0xf426('0x44')];if(!_0x3f356e[_0xf426('0x45')][_0xf426('0x4b')](_0xf426('0x4c'))){_0x50c0e8[_0xf426('0x2e')]=qs[_0xf426('0x2e')](_0x3f356e[_0xf426('0x45')][_0xf426('0x2e')]);_0x50c0e8[_0xf426('0x2d')]=qs[_0xf426('0x2d')](_0x3f356e['query'][_0xf426('0x2d')]);}_0x50c0e8[_0xf426('0x4d')]=qs[_0xf426('0x4e')](_0x3f356e[_0xf426('0x45')]['sort']);_0x50c0e8[_0xf426('0x4f')]=qs[_0xf426('0x50')](_[_0xf426('0x51')](_0x3f356e[_0xf426('0x45')],_0x6b3a43[_0xf426('0x50')]));_0x50c0e8[_0xf426('0x4f')][_0xf426('0x6a')]=_0x39391a['id'];if(_0x3f356e[_0xf426('0x45')][_0xf426('0x52')]){_0x50c0e8[_0xf426('0x4f')]=_[_0xf426('0x53')](_0x50c0e8[_0xf426('0x4f')],{'$or':_[_0xf426('0x40')](_0x50c0e8[_0xf426('0x49')],function(_0x24c3a4){var _0x303c62={};_0x303c62[_0x24c3a4]={'$like':'%'+_0x3f356e[_0xf426('0x45')][_0xf426('0x52')]+'%'};return _0x303c62;})});}_0x50c0e8=_[_0xf426('0x53')]({},_0x50c0e8,_0x3f356e[_0xf426('0x55')]);return db['SmsInteraction']['count']({'where':_0x50c0e8['where']})[_0xf426('0x36')](function(_0x4fd1dc){_0x182da7[_0xf426('0x30')]=_0x4fd1dc;if(_0x3f356e[_0xf426('0x45')][_0xf426('0x57')]){_0x50c0e8[_0xf426('0x58')]=[{'model':db[_0xf426('0x7c')],'as':'Contact','required':![]},{'model':db[_0xf426('0xb3')],'as':'Owner','attributes':[_0xf426('0x3c'),_0xf426('0xca'),_0xf426('0xcb')],'required':![]},{'model':db[_0xf426('0xcc')],'as':_0xf426('0xcd'),'attributes':['id',_0xf426('0x3c'),'color'],'where':_0x3f356e[_0xf426('0x45')]['tag']?{'id':_0x3f356e['query'][_0xf426('0xce')]}:undefined,'required':_0x3f356e[_0xf426('0x45')][_0xf426('0xce')]?!![]:![]}];}return db[_0xf426('0xa7')][_0xf426('0x59')](_0x50c0e8);})['then'](function(_0x2e73bf){_0x182da7[_0xf426('0x5a')]=_0x2e73bf;return _0x182da7;});}})[_0xf426('0x36')](respondWithFilteredResult(_0x1d5391,_0x50c0e8))[_0xf426('0x28')](handleError(_0x1d5391,null));};exports[_0xf426('0x3d')]=function(_0x24cd97,_0x5d30df,_0x50d191){var _0x13d693,_0x1cc29d,_0x471f8e;if(_[_0xf426('0x9b')](_0x24cd97[_0xf426('0x67')][_0xf426('0x67')])||_0x24cd97[_0xf426('0x67')][_0xf426('0x67')]===''){throw new db[(_0xf426('0x7d'))][(_0xf426('0x7e'))](_0xf426('0x9c'));}if(_[_0xf426('0x9b')](_0x24cd97[_0xf426('0x67')]['to'])){throw new db[(_0xf426('0x7d'))][(_0xf426('0x7e'))](_0xf426('0xcf'));}_0x13d693=_0x24cd97[_0xf426('0x67')]['to'];return db[_0xf426('0x56')][_0xf426('0x63')]({'where':{'id':_0x24cd97[_0xf426('0x5c')]['id']},'include':[{'model':db['CmList'],'as':'List','include':[{'model':db[_0xf426('0x7c')],'as':'Contacts','where':{'mobile':_0x13d693},'limit':0x1,'order':[[_0xf426('0x82'),_0xf426('0xa3')]]}]}]})['then'](handleEntityNotFound(_0x5d30df,null))[_0xf426('0x36')](function(_0x4ab81e){if(_0x4ab81e&&_0x4ab81e[_0xf426('0xd0')]){_0x1cc29d=_0x4ab81e[_0xf426('0x37')]({'plain':!![]});if(_0x1cc29d[_0xf426('0xd0')]&&_0x1cc29d[_0xf426('0xd0')]['Contacts'][_0xf426('0x4a')]){return _0x1cc29d[_0xf426('0xd0')]['Contacts'][0x0];}return db['CmContact']['create'](_[_0xf426('0x17')](_0x24cd97[_0xf426('0x67')],{'firstName':_0x13d693,'mobile':_0x13d693,'phone':_0x13d693,'ListId':_0x1cc29d[_0xf426('0x84')]}));}})[_0xf426('0x36')](handleEntityNotFound(_0x5d30df,null))[_0xf426('0x36')](function(_0x164da1){if(_0x164da1){_0x471f8e=_0x164da1;return db['SmsInteraction'][_0xf426('0x63')]({'where':{'ContactId':_0x471f8e['id'],'closed':![],'SmsAccountId':_0x1cc29d['id']}})['then'](function(_0x40f20a){if(_0x40f20a){return[_0x40f20a,![]];}return db['SmsInteraction'][_0xf426('0x5e')]({'UserId':_0x24cd97[_0xf426('0x5f')]['id'],'ContactId':_0x471f8e['id'],'SmsAccountId':_0x1cc29d['id'],'phone':_0x13d693,'firstMsgDirection':_0xf426('0xd1'),'Messages':[_[_0xf426('0x53')](_0x24cd97[_0xf426('0x67')],{'read':![],'body':_0x24cd97[_0xf426('0x67')][_0xf426('0x67')],'phone':_0x13d693,'SmsAccountId':_0x1cc29d['id'],'UserId':_0x24cd97[_0xf426('0x5f')]['id'],'ContactId':_0x471f8e['id']})]},{'include':[{'model':db[_0xf426('0xaf')],'as':_0xf426('0xd2')}]})['then'](function(_0x1c1486){return[_0x1c1486,!![]];});});}})['spread'](function(_0x53359b,_0x16c726){if(_0x16c726){return _0x53359b;}return db['SmsMessage'][_0xf426('0x5e')](_['merge'](_0x24cd97[_0xf426('0x67')],{'read':![],'body':_0x24cd97[_0xf426('0x67')][_0xf426('0x67')],'phone':_0x13d693,'SmsAccountId':_0x1cc29d['id'],'SmsInteractionId':_0x53359b['id'],'UserId':_0x24cd97[_0xf426('0x5f')]['id'],'ContactId':_0x471f8e['id']}))[_0xf426('0x36')](function(_0x22010c){_0x53359b[_0xf426('0xd3')][_0xf426('0xd2')]=[];_0x53359b[_0xf426('0xd3')][_0xf426('0xd2')]['push'](_0x22010c[_0xf426('0xd3')]);return _0x53359b;});})[_0xf426('0x36')](respondWithResult(_0x5d30df,null))['catch'](handleError(_0x5d30df,null));};exports[_0xf426('0x2a')]=function(_0x4691a2,_0x293771,_0x10d61f){if(_0x4691a2['body']['id']){delete _0x4691a2[_0xf426('0x67')]['id'];}var _0x3ab24d,_0x445cf6,_0xb451b9;return db['SmsAccount'][_0xf426('0x63')]({'where':{'id':_0x4691a2[_0xf426('0x5c')]['id']}})['then'](handleEntityNotFound(_0x293771,null))[_0xf426('0x36')](function(_0x28929b){if(_0x28929b){_0xb451b9=_0x28929b;logger[_0xf426('0x23')](_0xf426('0x24'),_0xf426('0x2a'),_0xf426('0xd4'),_0x28929b[_0xf426('0x43')],util[_0xf426('0xd5')](_0x4691a2['body'],{'showHidden':![],'depth':null}));switch(_0x28929b[_0xf426('0x43')]){case'skebby':if(_0x4691a2[_0xf426('0x67')]['skebby_dispatch_id']&&_0x4691a2['body'][_0xf426('0x2a')]){_0x445cf6=_0x4691a2['body']['status'];return db[_0xf426('0xaf')][_0xf426('0x63')]({'where':{'messageId':_0x4691a2[_0xf426('0x67')]['skebby_dispatch_id']}});}break;case _0xf426('0x8d'):if(_0x4691a2[_0xf426('0x67')][_0xf426('0xd6')]&&_0x4691a2[_0xf426('0x67')]['statusInt']){_0x445cf6=_0x4691a2[_0xf426('0x67')][_0xf426('0xd7')];return db[_0xf426('0xaf')]['find']({'where':{'messageId':_0x4691a2[_0xf426('0x67')]['requestid']}});}break;case'clicksend':if(_0x4691a2['body'][_0xf426('0x8f')]&&_0x4691a2[_0xf426('0x67')][_0xf426('0xd8')]){_0x445cf6=_0x4691a2['body'][_0xf426('0xd8')];return db[_0xf426('0xaf')][_0xf426('0x63')]({'where':{'messageId':_0x4691a2[_0xf426('0x67')][_0xf426('0x8f')]}});}break;case'infobip':var _0x15b0a9=_0x4691a2[_0xf426('0x67')][_0xf426('0x94')][0x0];if(_0x15b0a9[_0xf426('0x88')]&&_0x15b0a9['status']['groupName']==='DELIVERED'){_0x445cf6=_0x15b0a9[_0xf426('0x2a')][_0xf426('0xd9')];return db['SmsMessage'][_0xf426('0x63')]({'where':{'messageId':_0x15b0a9[_0xf426('0x88')]}});}break;case _0xf426('0x96'):if(_0x4691a2[_0xf426('0x67')][_0xf426('0x88')]&&_0x4691a2[_0xf426('0x67')][_0xf426('0x2a')]){_0x445cf6=_0x4691a2['body']['status'];return db['SmsMessage'][_0xf426('0x63')]({'where':{'messageId':_0x4691a2[_0xf426('0x67')]['messageId']}});}break;case _0xf426('0x99'):if(_0x4691a2[_0xf426('0x67')][0x0][_0xf426('0x26')]['id']){_0x445cf6=_0x4691a2[_0xf426('0x67')][0x0]['type'];return db[_0xf426('0xaf')]['find']({'where':{'messageId':_0x4691a2['body'][0x0][_0xf426('0x26')]['id']}});}break;case _0xf426('0x9a'):if(_0x4691a2[_0xf426('0x67')][_0xf426('0xda')]&&_0x4691a2[_0xf426('0x67')]['status']){_0x445cf6=_0x4691a2[_0xf426('0x67')]['status'];return db[_0xf426('0xaf')][_0xf426('0x63')]({'where':{'messageId':_0x4691a2[_0xf426('0x67')]['redif']}});}break;}}})['then'](handleEntityNotFound(_0x293771,null))[_0xf426('0x36')](function(_0x326114){if(_0x326114){_0x3ab24d=_0x326114;switch(_0xb451b9[_0xf426('0x43')]){case'skebby':if(_0x445cf6===_0xf426('0xdb')){return _0x3ab24d[_0xf426('0x35')]({'read':!![]});}else{logger[_0xf426('0x25')](_0xf426('0x24'),_0xf426('0x2a'),_0xf426('0x8a'),util['inspect'](_0x4691a2[_0xf426('0x67')],{'showHidden':![],'depth':null}));}break;case'connectel':if(_0x445cf6==='1'){return _0x3ab24d[_0xf426('0x35')]({'read':!![]});}else{logger[_0xf426('0x25')]('SmsAccount,\x20%s,\x20%s,\x20%s',_0xf426('0x2a'),_0xf426('0x8a'),util[_0xf426('0xd5')](_0x4691a2[_0xf426('0x67')],{'showHidden':![],'depth':null}));}break;case _0xf426('0x8e'):if(!_0x4691a2['body'][_0xf426('0xdc')]&&_0x445cf6>=0xc8&&_0x445cf6<=0xce){return _0x3ab24d[_0xf426('0x35')]({'read':!![]});}else{logger[_0xf426('0x25')](_0xf426('0x24'),_0xf426('0x2a'),_0xf426('0x8e'),util[_0xf426('0xd5')](_0x4691a2[_0xf426('0x67')],{'showHidden':![],'depth':null}));}break;case _0xf426('0x93'):var _0x537649=_0x4691a2[_0xf426('0x67')][_0xf426('0x94')][0x0];if(_0x537649[_0xf426('0x2a')]['groupName']===_0xf426('0xdb')){return _0x3ab24d[_0xf426('0x35')]({'read':!![]});}else{logger[_0xf426('0x25')](_0xf426('0x24'),'status',_0xf426('0x93'),util[_0xf426('0xd5')](_0x4691a2[_0xf426('0x67')],{'showHidden':![],'depth':null}));}break;case _0xf426('0x96'):if(_0x445cf6=='DELIVERED_TO_GATEWAY'||_0x445cf6==_0xf426('0xdd')){return _0x3ab24d[_0xf426('0x35')]({'read':!![]});}else{logger[_0xf426('0x25')](_0xf426('0x24'),_0xf426('0x2a'),_0xf426('0x96'),util['inspect'](_0x4691a2[_0xf426('0x67')],{'showHidden':![],'depth':null}));}break;case _0xf426('0x99'):if(_0x445cf6=='message-delivered'){return _0x3ab24d[_0xf426('0x35')]({'read':!![]});}else{logger['error'](_0xf426('0x24'),_0xf426('0x2a'),'bandwidth',util[_0xf426('0xd5')](_0x4691a2['body'],{'showHidden':![],'depth':null}));}break;case _0xf426('0x9a'):if(_0x445cf6===_0xf426('0xde')||_0x445cf6===_0xf426('0xdf')){return _0x3ab24d[_0xf426('0x35')]({'read':!![]});}else{logger[_0xf426('0x25')](_0xf426('0x24'),_0xf426('0x2a'),_0xf426('0x9a'),util[_0xf426('0xd5')](_0x4691a2[_0xf426('0x67')],{'showHidden':![],'depth':null}));}break;}return _0x3ab24d;}})[_0xf426('0x36')](respondWithResult(_0x293771,null))[_0xf426('0x28')](handleError(_0x293771,null));};exports['statusGet']=function(_0x5b4aa1,_0x49ed72,_0x13262b){var _0x24ed2e,_0x208ff1,_0x5d3d67;return db[_0xf426('0x56')][_0xf426('0x63')]({'where':{'id':_0x5b4aa1[_0xf426('0x5c')]['id']}})['then'](handleEntityNotFound(_0x49ed72,null))[_0xf426('0x36')](function(_0xdc2b8f){if(_0xdc2b8f){_0x5d3d67=_0xdc2b8f;logger[_0xf426('0x23')](_0xf426('0x24'),_0xf426('0x2a'),_0xf426('0xd4'),_0xdc2b8f[_0xf426('0x43')],util[_0xf426('0xd5')](_0x5b4aa1[_0xf426('0x45')],{'showHidden':![],'depth':null}));switch(_0xdc2b8f[_0xf426('0x43')]){case _0xf426('0xe0'):if(_0x5b4aa1[_0xf426('0x45')][_0xf426('0xe1')]&&_0x5b4aa1[_0xf426('0x45')]['state']){_0x208ff1=_0x5b4aa1['query'][_0xf426('0xe2')];return db['SmsMessage'][_0xf426('0x63')]({'where':{'messageId':_0x5b4aa1[_0xf426('0x45')]['message-id']}});}break;}}})['then'](handleEntityNotFound(_0x49ed72,null))[_0xf426('0x36')](function(_0x18e9c8){if(_0x18e9c8){_0x24ed2e=_0x18e9c8;switch(_0x5d3d67['type']){case'csc':if(_0x208ff1==='2'||_0x208ff1==='6'){return _0x24ed2e[_0xf426('0x35')]({'read':!![]});}else{logger['error'](_0xf426('0x24'),_0xf426('0x2a'),'csc',util[_0xf426('0xd5')](_0x5b4aa1[_0xf426('0x45')],{'showHidden':![],'depth':null}));};break;}return _0x24ed2e;}})[_0xf426('0x36')](respondWithResult(_0x49ed72,null))[_0xf426('0x28')](handleError(_0x49ed72,null));};exports[_0xf426('0xe3')]=function(_0xdb7a76,_0x428641,_0x118d1c){return db[_0xf426('0x56')][_0xf426('0x63')]({'where':{'id':_0xdb7a76[_0xf426('0x5c')]['id']}})[_0xf426('0x36')](handleEntityNotFound(_0x428641,null))[_0xf426('0x36')](function(_0x1d4f18){if(_0x1d4f18){return _0x1d4f18['addAgents'](_0xdb7a76[_0xf426('0x67')][_0xf426('0x6f')],_[_0xf426('0xa5')](_0xdb7a76[_0xf426('0x67')],[_0xf426('0x6f'),'id'])||{})[_0xf426('0xa8')](function(_0x373e2a){for(var _0x131cd8=0x0;_0x131cd8<_0xdb7a76[_0xf426('0x67')]['ids'][_0xf426('0x4a')];_0x131cd8+=0x1){socket['emit'](_0xf426('0xe4'),{'UserId':Number(_0xdb7a76[_0xf426('0x67')][_0xf426('0x6f')][_0x131cd8]),'SmsAccountId':Number(_0xdb7a76[_0xf426('0x5c')]['id'])});}return _0x373e2a;});}})[_0xf426('0x36')](respondWithResult(_0x428641,null))[_0xf426('0x28')](handleError(_0x428641,null));};exports[_0xf426('0xe5')]=function(_0x49b3c0,_0x4ee135,_0x306b52){return db[_0xf426('0x56')][_0xf426('0x63')]({'where':{'id':_0x49b3c0[_0xf426('0x5c')]['id']}})[_0xf426('0x36')](handleEntityNotFound(_0x4ee135,null))[_0xf426('0x36')](function(_0x2c54b2){if(_0x2c54b2){return _0x2c54b2[_0xf426('0xe5')](_0x49b3c0[_0xf426('0x45')][_0xf426('0x6f')])[_0xf426('0x36')](function(){if(_[_0xf426('0xe6')](_0x49b3c0[_0xf426('0x45')][_0xf426('0x6f')])){for(var _0x4907dd=0x0;_0x4907dd<_0x49b3c0['query'][_0xf426('0x6f')][_0xf426('0x4a')];_0x4907dd+=0x1){socket[_0xf426('0xe7')](_0xf426('0xe8'),{'UserId':Number(_0x49b3c0[_0xf426('0x45')]['ids'][_0x4907dd]),'SmsAccountId':Number(_0x49b3c0[_0xf426('0x5c')]['id'])});}}else{socket[_0xf426('0xe7')]('userSmsAccount:remove',{'UserId':Number(_0x49b3c0['query'][_0xf426('0x6f')]),'SmsAccountId':Number(_0x49b3c0[_0xf426('0x5c')]['id'])});}});}})['then'](respondWithStatusCode(_0x4ee135,null))[_0xf426('0x28')](handleError(_0x4ee135,null));};exports[_0xf426('0xe9')]=function(_0x3fc88f,_0x4bb87f,_0x502596){var _0x2be833={};var _0x194bc5={};var _0xd7c261;var _0x4f56c6;return db[_0xf426('0x56')][_0xf426('0x6d')]({'where':{'id':_0x3fc88f[_0xf426('0x5c')]['id']}})[_0xf426('0x36')](handleEntityNotFound(_0x4bb87f,null))[_0xf426('0x36')](function(_0x33a383){if(_0x33a383){_0xd7c261=_0x33a383;_0x194bc5[_0xf426('0x44')]=_[_0xf426('0x46')](db[_0xf426('0xb3')]['rawAttributes']);_0x194bc5[_0xf426('0x45')]=_['keys'](_0x3fc88f['query']);_0x194bc5['filters']=_[_0xf426('0x47')](_0x194bc5[_0xf426('0x44')],_0x194bc5[_0xf426('0x45')]);_0x2be833[_0xf426('0x49')]=_['intersection'](_0x194bc5[_0xf426('0x44')],qs[_0xf426('0x48')](_0x3fc88f[_0xf426('0x45')][_0xf426('0x48')]));_0x2be833['attributes']=_0x2be833[_0xf426('0x49')][_0xf426('0x4a')]?_0x2be833[_0xf426('0x49')]:_0x194bc5[_0xf426('0x44')];_0x2be833[_0xf426('0x4d')]=qs[_0xf426('0x4e')](_0x3fc88f[_0xf426('0x45')]['sort']);_0x2be833[_0xf426('0x4f')]=qs[_0xf426('0x50')](_[_0xf426('0x51')](_0x3fc88f[_0xf426('0x45')],_0x194bc5[_0xf426('0x50')]));if(_0x3fc88f[_0xf426('0x45')][_0xf426('0x52')]){_0x2be833[_0xf426('0x4f')]=_[_0xf426('0x53')](_0x2be833[_0xf426('0x4f')],{'$or':_[_0xf426('0x40')](_0x2be833[_0xf426('0x49')],function(_0x432183){var _0x43edbd={};_0x43edbd[_0x432183]={'$like':'%'+_0x3fc88f[_0xf426('0x45')][_0xf426('0x52')]+'%'};return _0x43edbd;})});}_0x2be833=_['merge']({},_0x2be833,_0x3fc88f[_0xf426('0x55')]);return _0xd7c261[_0xf426('0xe9')](_0x2be833);}})[_0xf426('0x36')](function(_0x1171e2){if(_0x1171e2){_0x4f56c6=_0x1171e2[_0xf426('0x4a')];if(!_0x3fc88f[_0xf426('0x45')][_0xf426('0x4b')](_0xf426('0x4c'))){_0x2be833[_0xf426('0x2e')]=qs[_0xf426('0x2e')](_0x3fc88f['query'][_0xf426('0x2e')]);_0x2be833['offset']=qs['offset'](_0x3fc88f[_0xf426('0x45')][_0xf426('0x2d')]);}return _0xd7c261[_0xf426('0xe9')](_0x2be833);}})[_0xf426('0x36')](function(_0x43b7a2){if(_0x43b7a2){return _0x43b7a2?{'count':_0x4f56c6,'rows':_0x43b7a2}:null;}})['then'](respondWithResult(_0x4bb87f,null))[_0xf426('0x28')](handleError(_0x4bb87f,null));};