Built motion from commit efa66e19.|2.6.22
[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 _0x86de=['findOne','rawAttributes','removeDispositions','ids','addAnswer','getAnswers','CannedAnswer','removeAnswers','notify','sms','SmsApplication','account','applications','orderBy','Applications','priority','ValidationError','difference','createdAt','updatedAt','ListId','mobile','twilio','from','Body','messageId','MessageSid','text','connectel','clicksend','plivo','From','Text','MessageUUID','infobip','results','csc','fromNumber','replyMessageId','bandwidth','intelepeer','refid','isNil','Sequelize','from\x20is\x20mandatory','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','spread','interaction','created','push','unmanaged','*,*,*,*','SmsMessage','ContactId','UserId','User','unshift','agent','waitForTheAssignedAgent','queueId','SmsQueue','queue','waitForTheAssignedQueue','Start','startRouting','receiveMessage','SmsAccount,\x20%s,\x20%s,\x20%s','entity\x20found','SmsAccount,\x20%s,\x20%s,\x20%s,\x20%s','set','Content-Type','text/xml','sequelize','transaction','Transaction','ISOLATION_LEVELS','bulkCreate','findAndCountAll','finally','getApplications','SmsInteraction','CmContact','Contact','Owner','Tag','Tags','color','tag','to\x20is\x20mandatory','List','out','Messages','dataValues','received\x20delivery\x20report\x20from','inspect','skebby','requestid','statusInt','message_id','status_code','clickatell','redif','update','error_code','DELIVERED','DELIVERED_TO_GATEWAY','RECEIVED_BY_RECIPIENT','message-delivered','DELIVRD','ACCEPTD','statusGet','state','message-id','addAgents','emit','userSmsAccount:save','removeAgents','isArray','userSmsAccount:remove','getAgents','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','path','to-csv','ejs','fs-extra','lodash','crypto','jsforce','desk.js','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','redis','defaults','localhost','socket.io-emitter','register','jayson/promise','client','http','request','info','request\x20sent','debug','stringify','error','code','message','SmsAccount,\x20%s,\x20%s','catch','sendStatus','status','end','json','undefined','limit','count','offset','Content-Range','save','then','destroy','get','SmsAccounts','UserProfileResource','stack','name','send','Pause','mandatoryDispositionPause','map','SmsAccount','model','query','keys','filters','fields','attributes','length','hasOwnProperty','nolimit','order','sort','pick','filter','where','type','VIRTUAL','merge','options','includeAll','include','findAll','rows','params','intersection','create','body','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','user','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','find','describe','SmsAccountId','Disposition','getDispositions'];(function(_0x51c819,_0xd812db){var _0x15c5d2=function(_0x2977f0){while(--_0x2977f0){_0x51c819['push'](_0x51c819['shift']());}};_0x15c5d2(++_0xd812db);}(_0x86de,0x81));var _0xe86d=function(_0x321bb2,_0x202794){_0x321bb2=_0x321bb2-0x0;var _0x9c12ed=_0x86de[_0x321bb2];return _0x9c12ed;};'use strict';var emlformat=require(_0xe86d('0x0'));var rimraf=require(_0xe86d('0x1'));var zipdir=require(_0xe86d('0x2'));var jsonpatch=require(_0xe86d('0x3'));var rp=require(_0xe86d('0x4'));var moment=require(_0xe86d('0x5'));var BPromise=require(_0xe86d('0x6'));var Mustache=require(_0xe86d('0x7'));var util=require('util');var path=require(_0xe86d('0x8'));var sox=require('sox');var csv=require(_0xe86d('0x9'));var ejs=require(_0xe86d('0xa'));var fs=require('fs');var fs_extra=require(_0xe86d('0xb'));var _=require(_0xe86d('0xc'));var squel=require('squel');var crypto=require(_0xe86d('0xd'));var jsforce=require(_0xe86d('0xe'));var deskjs=require(_0xe86d('0xf'));var toCsv=require(_0xe86d('0x9'));var querystring=require('querystring');var Papa=require('papaparse');var Redis=require(_0xe86d('0x10'));var authService=require(_0xe86d('0x11'));var qs=require('../../components/parsers/qs');var as=require(_0xe86d('0x12'));var hardwareService=require(_0xe86d('0x13'));var logger=require(_0xe86d('0x14'))(_0xe86d('0x15'));var utils=require(_0xe86d('0x16'));var config=require(_0xe86d('0x17'));var licenseUtil=require(_0xe86d('0x18'));var db=require('../../mysqldb')['db'];config[_0xe86d('0x19')]=_[_0xe86d('0x1a')](config[_0xe86d('0x19')],{'host':_0xe86d('0x1b'),'port':0x18eb});var socket=require(_0xe86d('0x1c'))(new Redis(config['redis']));require('./smsAccount.socket')[_0xe86d('0x1d')](socket);var jayson=require(_0xe86d('0x1e'));var client=jayson[_0xe86d('0x1f')][_0xe86d('0x20')]({'port':0x232c});function respondWithRpcPromise(_0x1b87f8,_0x19a869,_0x3c74a2,_0x37d641){return new BPromise(function(_0x34d05a,_0x45261e){var _0x35df76=_0x37d641||client;return _0x35df76[_0xe86d('0x21')](_0x1b87f8,_0x3c74a2)['then'](function(_0xfd78b2){logger[_0xe86d('0x22')]('SmsAccount,\x20%s,\x20%s',_0x19a869,_0xe86d('0x23'));logger[_0xe86d('0x24')]('SmsAccount,\x20%s,\x20%s,\x20%s',_0x19a869,_0xe86d('0x23'),JSON[_0xe86d('0x25')](_0xfd78b2));if(_0xfd78b2['error']){if(_0xfd78b2[_0xe86d('0x26')][_0xe86d('0x27')]===0x1f4){logger['error']('SmsAccount,\x20%s,\x20%s',_0x19a869,_0xfd78b2[_0xe86d('0x26')]['message']);return _0x45261e(_0xfd78b2['error'][_0xe86d('0x28')]);}logger['error'](_0xe86d('0x29'),_0x19a869,_0xfd78b2[_0xe86d('0x26')][_0xe86d('0x28')]);return _0x34d05a(_0xfd78b2[_0xe86d('0x26')]['message']);}else{logger[_0xe86d('0x22')](_0xe86d('0x29'),_0x19a869,_0xe86d('0x23'));_0x34d05a(_0xfd78b2['result'][_0xe86d('0x28')]);}})[_0xe86d('0x2a')](function(_0x2d5635){logger[_0xe86d('0x26')](_0xe86d('0x29'),_0x19a869,_0x2d5635);_0x45261e(_0x2d5635);});});}function respondWithStatusCode(_0x577ff9,_0x271b06){_0x271b06=_0x271b06||0xcc;return function(_0x2d36cc){if(_0x2d36cc){return _0x577ff9[_0xe86d('0x2b')](_0x271b06);}return _0x577ff9[_0xe86d('0x2c')](_0x271b06)[_0xe86d('0x2d')]();};}function respondWithResult(_0x30c17c,_0x3826c2){_0x3826c2=_0x3826c2||0xc8;return function(_0x59229d){if(_0x59229d){return _0x30c17c[_0xe86d('0x2c')](_0x3826c2)[_0xe86d('0x2e')](_0x59229d);}};}function respondWithFilteredResult(_0x480175,_0x533013){return function(_0x56e088){if(_0x56e088){var _0x4f3254=typeof _0x533013['offset']===_0xe86d('0x2f')&&typeof _0x533013[_0xe86d('0x30')]===_0xe86d('0x2f');var _0x32ca07=_0x56e088[_0xe86d('0x31')];var _0x349efa=_0x4f3254?0x0:_0x533013[_0xe86d('0x32')];var _0x595778=_0x4f3254?_0x56e088[_0xe86d('0x31')]:_0x533013[_0xe86d('0x32')]+_0x533013['limit'];var _0x5a834e;if(_0x595778>=_0x32ca07){_0x595778=_0x32ca07;_0x5a834e=0xc8;}else{_0x5a834e=0xce;}_0x480175[_0xe86d('0x2c')](_0x5a834e);return _0x480175['set'](_0xe86d('0x33'),_0x349efa+'-'+_0x595778+'/'+_0x32ca07)[_0xe86d('0x2e')](_0x56e088);}return null;};}function patchUpdates(_0x5891bf){return function(_0x1df063){try{jsonpatch['apply'](_0x1df063,_0x5891bf,!![]);}catch(_0x6b7bcb){return BPromise['reject'](_0x6b7bcb);}return _0x1df063[_0xe86d('0x34')]();};}function saveUpdates(_0x25df32,_0x4759f7){return function(_0x25969b){if(_0x25969b){return _0x25969b['update'](_0x25df32)[_0xe86d('0x35')](function(_0x539436){return _0x539436;});}return null;};}function removeEntity(_0x485ecd,_0x4d2323){return function(_0xa8f90f){if(_0xa8f90f){return _0xa8f90f[_0xe86d('0x36')]()[_0xe86d('0x35')](function(){var _0x32b814=_0xa8f90f[_0xe86d('0x37')]({'plain':!![]});var _0x4b5dd5=_0xe86d('0x38');return db[_0xe86d('0x39')]['destroy']({'where':{'type':_0x4b5dd5,'resourceId':_0x32b814['id']}})[_0xe86d('0x35')](function(){return _0xa8f90f;});})[_0xe86d('0x35')](function(){_0x485ecd[_0xe86d('0x2c')](0xcc)[_0xe86d('0x2d')]();});}};}function handleEntityNotFound(_0x2a06d5,_0xd1ebe0){return function(_0x16005d){if(!_0x16005d){_0x2a06d5[_0xe86d('0x2b')](0x194);}return _0x16005d;};}function handleError(_0x4bced7,_0x28d190){_0x28d190=_0x28d190||0x1f4;return function(_0x4870dc){logger[_0xe86d('0x26')](_0x4870dc[_0xe86d('0x3a')]);if(_0x4870dc[_0xe86d('0x3b')]){delete _0x4870dc[_0xe86d('0x3b')];}_0x4bced7['status'](_0x28d190)[_0xe86d('0x3c')](_0x4870dc);};}exports['index']=function(_0x37a452,_0x3fc59a){var _0x44a343={'include':[{'model':db[_0xe86d('0x3d')],'as':_0xe86d('0x3e')}]},_0x5dfe2b={},_0x1ad59a={'count':0x0,'rows':[]};var _0x2e7c0a=_[_0xe86d('0x3f')](db[_0xe86d('0x40')]['rawAttributes'],function(_0x1a16a1){return{'name':_0x1a16a1['fieldName'],'type':_0x1a16a1['type']['key']};});_0x5dfe2b[_0xe86d('0x41')]=_['map'](_0x2e7c0a,_0xe86d('0x3b'));_0x5dfe2b[_0xe86d('0x42')]=_[_0xe86d('0x43')](_0x37a452['query']);_0x5dfe2b[_0xe86d('0x44')]=_['intersection'](_0x5dfe2b[_0xe86d('0x41')],_0x5dfe2b[_0xe86d('0x42')]);_0x44a343['attributes']=_['intersection'](_0x5dfe2b[_0xe86d('0x41')],qs[_0xe86d('0x45')](_0x37a452['query']['fields']));_0x44a343[_0xe86d('0x46')]=_0x44a343[_0xe86d('0x46')][_0xe86d('0x47')]?_0x44a343['attributes']:_0x5dfe2b[_0xe86d('0x41')];if(!_0x37a452[_0xe86d('0x42')][_0xe86d('0x48')](_0xe86d('0x49'))){_0x44a343['limit']=qs[_0xe86d('0x30')](_0x37a452['query'][_0xe86d('0x30')]);_0x44a343['offset']=qs[_0xe86d('0x32')](_0x37a452[_0xe86d('0x42')][_0xe86d('0x32')]);}_0x44a343[_0xe86d('0x4a')]=qs['sort'](_0x37a452[_0xe86d('0x42')][_0xe86d('0x4b')]);_0x44a343['where']=qs[_0xe86d('0x44')](_[_0xe86d('0x4c')](_0x37a452[_0xe86d('0x42')],_0x5dfe2b[_0xe86d('0x44')]),_0x2e7c0a);if(_0x37a452[_0xe86d('0x42')][_0xe86d('0x4d')]){_0x44a343[_0xe86d('0x4e')]=_['merge'](_0x44a343[_0xe86d('0x4e')],{'$or':_[_0xe86d('0x3f')](_0x2e7c0a,function(_0x4374c1){if(_0x4374c1[_0xe86d('0x4f')]!==_0xe86d('0x50')){var _0x54bb41={};_0x54bb41[_0x4374c1['name']]={'$like':'%'+_0x37a452['query'][_0xe86d('0x4d')]+'%'};return _0x54bb41;}})});}_0x44a343=_[_0xe86d('0x51')]({},_0x44a343,_0x37a452[_0xe86d('0x52')]);var _0xb7f7c9={'where':_0x44a343[_0xe86d('0x4e')]};return db[_0xe86d('0x40')]['count'](_0xb7f7c9)[_0xe86d('0x35')](function(_0x29c0f4){_0x1ad59a[_0xe86d('0x31')]=_0x29c0f4;if(_0x37a452[_0xe86d('0x42')][_0xe86d('0x53')]){_0x44a343[_0xe86d('0x54')]=[{'all':!![]}];}return db[_0xe86d('0x40')][_0xe86d('0x55')](_0x44a343);})[_0xe86d('0x35')](function(_0x936295){_0x1ad59a[_0xe86d('0x56')]=_0x936295;return _0x1ad59a;})[_0xe86d('0x35')](respondWithFilteredResult(_0x3fc59a,_0x44a343))['catch'](handleError(_0x3fc59a,null));};exports['show']=function(_0x36a481,_0x177e89){var _0x2149b9={'raw':![],'where':{'id':_0x36a481[_0xe86d('0x57')]['id']},'include':[{'model':db['Pause'],'as':_0xe86d('0x3e')}]},_0xd6d2be={};_0xd6d2be[_0xe86d('0x41')]=_[_0xe86d('0x43')](db[_0xe86d('0x40')]['rawAttributes']);_0xd6d2be[_0xe86d('0x42')]=_[_0xe86d('0x43')](_0x36a481[_0xe86d('0x42')]);_0xd6d2be['filters']=_[_0xe86d('0x58')](_0xd6d2be[_0xe86d('0x41')],_0xd6d2be[_0xe86d('0x42')]);_0x2149b9[_0xe86d('0x46')]=_[_0xe86d('0x58')](_0xd6d2be[_0xe86d('0x41')],qs[_0xe86d('0x45')](_0x36a481[_0xe86d('0x42')]['fields']));_0x2149b9['attributes']=_0x2149b9[_0xe86d('0x46')][_0xe86d('0x47')]?_0x2149b9[_0xe86d('0x46')]:_0xd6d2be[_0xe86d('0x41')];if(_0x36a481[_0xe86d('0x42')][_0xe86d('0x53')]){_0x2149b9[_0xe86d('0x54')]=[{'all':!![]}];}_0x2149b9=_[_0xe86d('0x51')]({},_0x2149b9,_0x36a481[_0xe86d('0x52')]);return db['SmsAccount']['find'](_0x2149b9)[_0xe86d('0x35')](handleEntityNotFound(_0x177e89,null))['then'](respondWithResult(_0x177e89,null))['catch'](handleError(_0x177e89,null));};exports[_0xe86d('0x59')]=function(_0xa694fd,_0x16e600){return db['SmsAccount']['create'](_0xa694fd[_0xe86d('0x5a')],{})['then'](function(_0x16aae2){var _0x5cf3be=_0xa694fd['user'][_0xe86d('0x37')]({'plain':!![]});if(!_0x5cf3be)throw new Error(_0xe86d('0x5b'));if(_0x5cf3be[_0xe86d('0x5c')]===_0xe86d('0x5d')){var _0x5dfc92=_0x16aae2['get']({'plain':!![]});var _0x1357cc='SmsAccounts';return db['UserProfileSection']['find']({'where':{'name':_0x1357cc,'userProfileId':_0x5cf3be[_0xe86d('0x5e')]},'raw':!![]})['then'](function(_0x43b15f){if(_0x43b15f&&_0x43b15f[_0xe86d('0x5f')]===0x0){return db[_0xe86d('0x39')][_0xe86d('0x59')]({'name':_0x5dfc92[_0xe86d('0x3b')],'resourceId':_0x5dfc92['id'],'type':_0x43b15f['name'],'sectionId':_0x43b15f['id']},{})[_0xe86d('0x35')](function(){return _0x16aae2;});}else{return _0x16aae2;}})[_0xe86d('0x2a')](function(_0xcdaaac){logger[_0xe86d('0x26')](_0xe86d('0x60'),_0xcdaaac);throw _0xcdaaac;});}return _0x16aae2;})[_0xe86d('0x35')](respondWithResult(_0x16e600,0xc9))[_0xe86d('0x2a')](handleError(_0x16e600,null));};exports['update']=function(_0x39cc5c,_0xcaa6dc){if(_0x39cc5c[_0xe86d('0x5a')]['id']){delete _0x39cc5c[_0xe86d('0x5a')]['id'];}return db[_0xe86d('0x40')][_0xe86d('0x61')]({'where':{'id':_0x39cc5c[_0xe86d('0x57')]['id']},'include':[{'model':db['Pause'],'as':_0xe86d('0x3e')}]})[_0xe86d('0x35')](handleEntityNotFound(_0xcaa6dc,null))['then'](saveUpdates(_0x39cc5c['body'],null))[_0xe86d('0x35')](respondWithResult(_0xcaa6dc,null))['catch'](handleError(_0xcaa6dc,null));};exports['destroy']=function(_0x4f03a1,_0x389d18){return db[_0xe86d('0x40')][_0xe86d('0x61')]({'where':{'id':_0x4f03a1[_0xe86d('0x57')]['id']}})[_0xe86d('0x35')](handleEntityNotFound(_0x389d18,null))[_0xe86d('0x35')](removeEntity(_0x389d18,null))['catch'](handleError(_0x389d18,null));};exports[_0xe86d('0x62')]=function(_0x5d7af8,_0xcc3272){return db[_0xe86d('0x40')][_0xe86d('0x62')]()[_0xe86d('0x35')](respondWithResult(_0xcc3272,null))[_0xe86d('0x2a')](handleError(_0xcc3272,null));};exports['addDisposition']=function(_0x18d039,_0x3f7ab2,_0xfab3da){if(_0x18d039[_0xe86d('0x5a')]['id']){delete _0x18d039[_0xe86d('0x5a')]['id'];}return db['SmsAccount'][_0xe86d('0x61')]({'where':{'id':_0x18d039[_0xe86d('0x57')]['id']}})[_0xe86d('0x35')](handleEntityNotFound(_0x3f7ab2,null))[_0xe86d('0x35')](function(_0x158799){if(_0x158799){_0x18d039[_0xe86d('0x5a')][_0xe86d('0x63')]=_0x158799['id'];return db[_0xe86d('0x64')]['create'](_0x18d039[_0xe86d('0x5a')]);}})[_0xe86d('0x35')](respondWithResult(_0x3f7ab2,null))[_0xe86d('0x2a')](handleError(_0x3f7ab2,null));};exports[_0xe86d('0x65')]=function(_0x317107,_0xd742e4,_0x5364e1){var _0x2fc972={'raw':![],'where':{}};var _0x500e06={};var _0x35f450={'count':0x0,'rows':[]};return db['SmsAccount'][_0xe86d('0x66')]({'where':{'id':_0x317107[_0xe86d('0x57')]['id']}})[_0xe86d('0x35')](handleEntityNotFound(_0xd742e4,null))['then'](function(_0x8d3d55){if(_0x8d3d55){_0x500e06[_0xe86d('0x41')]=_[_0xe86d('0x43')](db[_0xe86d('0x64')][_0xe86d('0x67')]);_0x500e06[_0xe86d('0x42')]=_[_0xe86d('0x43')](_0x317107['query']);_0x500e06[_0xe86d('0x44')]=_['intersection'](_0x500e06['model'],_0x500e06[_0xe86d('0x42')]);_0x2fc972[_0xe86d('0x46')]=_['intersection'](_0x500e06['model'],qs[_0xe86d('0x45')](_0x317107['query'][_0xe86d('0x45')]));_0x2fc972[_0xe86d('0x46')]=_0x2fc972[_0xe86d('0x46')][_0xe86d('0x47')]?_0x2fc972[_0xe86d('0x46')]:_0x500e06[_0xe86d('0x41')];if(!_0x317107[_0xe86d('0x42')][_0xe86d('0x48')](_0xe86d('0x49'))){_0x2fc972['limit']=qs[_0xe86d('0x30')](_0x317107[_0xe86d('0x42')][_0xe86d('0x30')]);_0x2fc972[_0xe86d('0x32')]=qs[_0xe86d('0x32')](_0x317107[_0xe86d('0x42')][_0xe86d('0x32')]);}_0x2fc972[_0xe86d('0x4a')]=qs[_0xe86d('0x4b')](_0x317107[_0xe86d('0x42')]['sort']);_0x2fc972['where']=qs[_0xe86d('0x44')](_['pick'](_0x317107[_0xe86d('0x42')],_0x500e06[_0xe86d('0x44')]));_0x2fc972[_0xe86d('0x4e')]['SmsAccountId']=_0x8d3d55['id'];if(_0x317107[_0xe86d('0x42')][_0xe86d('0x4d')]){_0x2fc972[_0xe86d('0x4e')]=_['merge'](_0x2fc972['where'],{'$or':_[_0xe86d('0x3f')](_0x2fc972['attributes'],function(_0x585c67){var _0x25283d={};_0x25283d[_0x585c67]={'$like':'%'+_0x317107[_0xe86d('0x42')][_0xe86d('0x4d')]+'%'};return _0x25283d;})});}_0x2fc972=_[_0xe86d('0x51')]({},_0x2fc972,_0x317107['options']);return db[_0xe86d('0x64')][_0xe86d('0x31')]({'where':_0x2fc972[_0xe86d('0x4e')]})[_0xe86d('0x35')](function(_0x308774){_0x35f450[_0xe86d('0x31')]=_0x308774;if(_0x317107[_0xe86d('0x42')][_0xe86d('0x53')]){_0x2fc972[_0xe86d('0x54')]=[{'all':!![]}];}return db[_0xe86d('0x64')][_0xe86d('0x55')](_0x2fc972);})[_0xe86d('0x35')](function(_0x2d524a){_0x35f450[_0xe86d('0x56')]=_0x2d524a;return _0x35f450;});}})[_0xe86d('0x35')](respondWithFilteredResult(_0xd742e4,_0x2fc972))[_0xe86d('0x2a')](handleError(_0xd742e4,null));};exports[_0xe86d('0x68')]=function(_0x5dd727,_0x589cd5,_0x4bfb25){return db[_0xe86d('0x40')][_0xe86d('0x61')]({'where':{'id':_0x5dd727[_0xe86d('0x57')]['id']}})[_0xe86d('0x35')](handleEntityNotFound(_0x589cd5,null))[_0xe86d('0x35')](function(_0x175556){if(_0x175556){return _0x175556[_0xe86d('0x68')](_0x5dd727[_0xe86d('0x42')][_0xe86d('0x69')]);}})['then'](respondWithStatusCode(_0x589cd5,null))[_0xe86d('0x2a')](handleError(_0x589cd5,null));};exports[_0xe86d('0x6a')]=function(_0x458d2f,_0x3514a0,_0x2b42cc){if(_0x458d2f[_0xe86d('0x5a')]['id']){delete _0x458d2f[_0xe86d('0x5a')]['id'];}return db[_0xe86d('0x40')][_0xe86d('0x61')]({'where':{'id':_0x458d2f[_0xe86d('0x57')]['id']}})['then'](handleEntityNotFound(_0x3514a0,null))[_0xe86d('0x35')](function(_0x3911e6){if(_0x3911e6){_0x458d2f[_0xe86d('0x5a')][_0xe86d('0x63')]=_0x3911e6['id'];return db['CannedAnswer'][_0xe86d('0x59')](_0x458d2f[_0xe86d('0x5a')]);}})[_0xe86d('0x35')](respondWithResult(_0x3514a0,null))[_0xe86d('0x2a')](handleError(_0x3514a0,null));};exports[_0xe86d('0x6b')]=function(_0xc2d066,_0x16c3f5,_0x4767e5){var _0x21d8e6={'raw':![],'where':{}};var _0x529f2f={};var _0x34201c={'count':0x0,'rows':[]};return db[_0xe86d('0x40')]['findOne']({'where':{'id':_0xc2d066[_0xe86d('0x57')]['id']}})[_0xe86d('0x35')](handleEntityNotFound(_0x16c3f5,null))[_0xe86d('0x35')](function(_0x499aec){if(_0x499aec){_0x529f2f['model']=_[_0xe86d('0x43')](db[_0xe86d('0x6c')]['rawAttributes']);_0x529f2f[_0xe86d('0x42')]=_[_0xe86d('0x43')](_0xc2d066[_0xe86d('0x42')]);_0x529f2f[_0xe86d('0x44')]=_[_0xe86d('0x58')](_0x529f2f['model'],_0x529f2f[_0xe86d('0x42')]);_0x21d8e6[_0xe86d('0x46')]=_[_0xe86d('0x58')](_0x529f2f[_0xe86d('0x41')],qs['fields'](_0xc2d066['query'][_0xe86d('0x45')]));_0x21d8e6[_0xe86d('0x46')]=_0x21d8e6['attributes']['length']?_0x21d8e6['attributes']:_0x529f2f[_0xe86d('0x41')];if(!_0xc2d066[_0xe86d('0x42')][_0xe86d('0x48')](_0xe86d('0x49'))){_0x21d8e6[_0xe86d('0x30')]=qs[_0xe86d('0x30')](_0xc2d066['query'][_0xe86d('0x30')]);_0x21d8e6[_0xe86d('0x32')]=qs[_0xe86d('0x32')](_0xc2d066[_0xe86d('0x42')][_0xe86d('0x32')]);}_0x21d8e6['order']=qs['sort'](_0xc2d066['query'][_0xe86d('0x4b')]);_0x21d8e6[_0xe86d('0x4e')]=qs[_0xe86d('0x44')](_['pick'](_0xc2d066['query'],_0x529f2f[_0xe86d('0x44')]));_0x21d8e6[_0xe86d('0x4e')][_0xe86d('0x63')]=_0x499aec['id'];if(_0xc2d066[_0xe86d('0x42')][_0xe86d('0x4d')]){_0x21d8e6[_0xe86d('0x4e')]=_[_0xe86d('0x51')](_0x21d8e6[_0xe86d('0x4e')],{'$or':_['map'](_0x21d8e6[_0xe86d('0x46')],function(_0x1985ea){var _0x277ccd={};_0x277ccd[_0x1985ea]={'$like':'%'+_0xc2d066['query'][_0xe86d('0x4d')]+'%'};return _0x277ccd;})});}_0x21d8e6=_[_0xe86d('0x51')]({},_0x21d8e6,_0xc2d066['options']);return db[_0xe86d('0x6c')]['count']({'where':_0x21d8e6[_0xe86d('0x4e')]})[_0xe86d('0x35')](function(_0x10d80b){_0x34201c[_0xe86d('0x31')]=_0x10d80b;if(_0xc2d066['query'][_0xe86d('0x53')]){_0x21d8e6['include']=[{'all':!![]}];}return db[_0xe86d('0x6c')]['findAll'](_0x21d8e6);})[_0xe86d('0x35')](function(_0x3c5627){_0x34201c['rows']=_0x3c5627;return _0x34201c;});}})['then'](respondWithFilteredResult(_0x16c3f5,_0x21d8e6))['catch'](handleError(_0x16c3f5,null));};exports[_0xe86d('0x6d')]=function(_0x1cf3cd,_0x14a054,_0x574792){return db[_0xe86d('0x40')][_0xe86d('0x61')]({'where':{'id':_0x1cf3cd[_0xe86d('0x57')]['id']}})[_0xe86d('0x35')](handleEntityNotFound(_0x14a054,null))[_0xe86d('0x35')](function(_0x2f23c3){if(_0x2f23c3){return _0x2f23c3['removeAnswers'](_0x1cf3cd['query']['ids']);}})[_0xe86d('0x35')](respondWithStatusCode(_0x14a054,null))['catch'](handleError(_0x14a054,null));};exports[_0xe86d('0x6e')]=function(_0x5749f8,_0x153e52,_0x3ad2db){var _0x498b13={'channel':_0xe86d('0x6f')};var _0x9caa8e=[];var _0x6d0ae3=[];var _0x5405c3={};return db[_0xe86d('0x40')]['find']({'where':{'id':_0x5749f8[_0xe86d('0x57')]['id']},'include':[{'model':db[_0xe86d('0x70')],'as':'Applications'}]})['then'](handleEntityNotFound(_0x153e52,null))['then'](function(_0x1357a8){_0x498b13[_0xe86d('0x71')]=_0x1357a8;_0x498b13[_0xe86d('0x72')]=_[_0xe86d('0x73')](_0x1357a8[_0xe86d('0x74')],[_0xe86d('0x75')],['asc']);if(_0x498b13[_0xe86d('0x71')]['Applications']){delete _0x498b13[_0xe86d('0x71')][_0xe86d('0x74')];}return db['CmContact'][_0xe86d('0x62')]();})[_0xe86d('0x35')](function(_0x177eab){if(!_0x177eab){throw new db['Sequelize'][(_0xe86d('0x76'))]('no\x20available\x20attributes');}_0x9caa8e=_[_0xe86d('0x77')](_[_0xe86d('0x43')](_0x177eab),[_0xe86d('0x78'),_0xe86d('0x79')]);_0x6d0ae3=_[_0xe86d('0x77')](_[_0xe86d('0x43')](_0x177eab),[_0xe86d('0x78'),_0xe86d('0x79'),'CompanyId',_0xe86d('0x7a')]);_0x498b13['body']={'mapKey':_0xe86d('0x7b')};if(_0x498b13[_0xe86d('0x71')]['type']){switch(_0x498b13[_0xe86d('0x71')][_0xe86d('0x4f')]){case _0xe86d('0x7c'):_0x498b13[_0xe86d('0x5a')][_0xe86d('0x7d')]=_0x5749f8[_0xe86d('0x5a')]['From'];_0x498b13[_0xe86d('0x5a')][_0xe86d('0x5a')]=_0x5749f8[_0xe86d('0x5a')][_0xe86d('0x7e')];_0x498b13[_0xe86d('0x5a')][_0xe86d('0x7f')]=_0x5749f8[_0xe86d('0x5a')][_0xe86d('0x80')];break;case'skebby':_0x498b13['body'][_0xe86d('0x7d')]='+'+_0x5749f8[_0xe86d('0x5a')]['sender'];_0x498b13['body'][_0xe86d('0x5a')]=_0x5749f8[_0xe86d('0x5a')][_0xe86d('0x81')];break;case _0xe86d('0x82'):_0x498b13[_0xe86d('0x5a')][_0xe86d('0x7d')]=_0x5749f8[_0xe86d('0x5a')][_0xe86d('0x7d')];_0x498b13[_0xe86d('0x5a')][_0xe86d('0x5a')]=_0x5749f8['body'][_0xe86d('0x28')];break;case _0xe86d('0x83'):_0x498b13[_0xe86d('0x5a')][_0xe86d('0x7d')]=_0x5749f8['body'][_0xe86d('0x7d')];_0x498b13[_0xe86d('0x5a')][_0xe86d('0x5a')]=_0x5749f8[_0xe86d('0x5a')][_0xe86d('0x5a')];_0x498b13[_0xe86d('0x5a')][_0xe86d('0x7f')]=_0x5749f8[_0xe86d('0x5a')]['message_id'];break;case _0xe86d('0x84'):_0x498b13[_0xe86d('0x5a')][_0xe86d('0x7d')]=_0x5749f8[_0xe86d('0x5a')][_0xe86d('0x85')];_0x498b13[_0xe86d('0x5a')][_0xe86d('0x5a')]=_0x5749f8['body'][_0xe86d('0x86')];_0x498b13[_0xe86d('0x5a')]['messageId']=_0x5749f8[_0xe86d('0x5a')][_0xe86d('0x87')];break;case _0xe86d('0x88'):_0x498b13[_0xe86d('0x5a')][_0xe86d('0x7d')]='+'+_0x5749f8['body']['results'][0x0][_0xe86d('0x7d')];_0x498b13[_0xe86d('0x5a')]['body']=_0x5749f8[_0xe86d('0x5a')][_0xe86d('0x89')][0x0][_0xe86d('0x81')];_0x498b13[_0xe86d('0x5a')]['messageId']=_0x5749f8[_0xe86d('0x5a')][_0xe86d('0x89')][0x0][_0xe86d('0x7f')];break;case _0xe86d('0x8a'):_0x498b13[_0xe86d('0x5a')][_0xe86d('0x7d')]=_0x5749f8['body']['originatingAddress'];_0x498b13['body']['body']=_0x5749f8[_0xe86d('0x5a')]['segmentedMessage'][_0xe86d('0x28')];break;case'clickatell':_0x498b13[_0xe86d('0x5a')][_0xe86d('0x7d')]=_0x5749f8[_0xe86d('0x5a')][_0xe86d('0x8b')];_0x498b13[_0xe86d('0x5a')][_0xe86d('0x5a')]=_0x5749f8[_0xe86d('0x5a')][_0xe86d('0x81')];_0x498b13[_0xe86d('0x5a')][_0xe86d('0x7f')]=_0x5749f8[_0xe86d('0x5a')][_0xe86d('0x8c')];break;case _0xe86d('0x8d'):_0x498b13[_0xe86d('0x5a')][_0xe86d('0x7f')]=_0x5749f8[_0xe86d('0x5a')][0x0]['message']['id'];_0x498b13[_0xe86d('0x5a')]['from']=_0x5749f8[_0xe86d('0x5a')][0x0][_0xe86d('0x28')][_0xe86d('0x7d')];_0x498b13[_0xe86d('0x5a')][_0xe86d('0x5a')]=_0x5749f8[_0xe86d('0x5a')][0x0][_0xe86d('0x28')][_0xe86d('0x81')];break;case _0xe86d('0x8e'):_0x498b13[_0xe86d('0x5a')]['from']=_0x5749f8[_0xe86d('0x5a')]['from'];_0x498b13['body']['body']=_0x5749f8[_0xe86d('0x5a')][_0xe86d('0x28')];_0x498b13['body']['messageId']=_0x5749f8[_0xe86d('0x5a')][_0xe86d('0x8f')];break;}}if(_0x498b13[_0xe86d('0x5a')]['id']){delete _0x498b13['body']['id'];}if(_[_0xe86d('0x90')](_0x498b13[_0xe86d('0x5a')]['from'])){throw new db[(_0xe86d('0x91'))][(_0xe86d('0x76'))](_0xe86d('0x92'));}if(_['isNil'](_0x498b13[_0xe86d('0x5a')][_0xe86d('0x5a')])||_0x498b13[_0xe86d('0x5a')]['body']===''){throw new db[(_0xe86d('0x91'))][(_0xe86d('0x76'))](_0xe86d('0x93'));}if(_['isNil'](_0x498b13[_0xe86d('0x5a')][_0xe86d('0x94')])){throw new db[(_0xe86d('0x91'))][(_0xe86d('0x76'))](_0xe86d('0x95')+_0x6d0ae3);}if(!_[_0xe86d('0x96')](_0x6d0ae3,_0x498b13[_0xe86d('0x5a')][_0xe86d('0x94')])){throw new db[(_0xe86d('0x91'))][(_0xe86d('0x76'))](_0xe86d('0x97')+_0x6d0ae3);}_0x5405c3[_0x498b13[_0xe86d('0x5a')][_0xe86d('0x94')]]=_0x498b13[_0xe86d('0x5a')][_0xe86d('0x7d')];return db[_0xe86d('0x98')]['find']({'where':{'id':_0x498b13[_0xe86d('0x71')]['ListId']},'include':[{'model':db['CmContact'],'as':_0xe86d('0x99'),'where':_0x5405c3,'limit':0x1,'order':[['updatedAt',_0xe86d('0x9a')]]}]});})[_0xe86d('0x35')](handleEntityNotFound(_0x153e52,null))[_0xe86d('0x35')](function(_0x188cb1){if(_0x188cb1){_0x498b13[_0xe86d('0x9b')]=_[_0xe86d('0x9c')](_0x188cb1,[_0xe86d('0x99')]);if(_0x188cb1['Contacts'][_0xe86d('0x47')]){return _0x188cb1[_0xe86d('0x99')][0x0];}var _0x40e178=_[_0xe86d('0x1a')](_0x498b13['body'],{'firstName':_0x498b13[_0xe86d('0x5a')][_0xe86d('0x7d')],'ListId':_0x188cb1['id']});_0x40e178[_0x498b13[_0xe86d('0x5a')]['mapKey']]=_0x498b13[_0xe86d('0x5a')][_0xe86d('0x7d')];return db['CmContact'][_0xe86d('0x59')](_0x40e178,{'fields':_0x9caa8e,'raw':!![]});}})[_0xe86d('0x35')](handleEntityNotFound(_0x153e52,null))[_0xe86d('0x35')](function(_0x58fc65){if(_0x58fc65){_0x498b13['contact']=_0x58fc65;var _0x155061={'ContactId':_0x58fc65['id'],'phone':_0x498b13[_0xe86d('0x5a')]['from'],'SmsAccountId':_0x5749f8[_0xe86d('0x57')]['id']};var _0x5609bb={'ContactId':_0x58fc65['id'],'SmsAccountId':_0x5749f8[_0xe86d('0x57')]['id'],'closed':![]};return db['SmsInteraction']['find']({'where':_0x5609bb})[_0xe86d('0x35')](function(_0xcad5ef){if(_0xcad5ef){return[_0xcad5ef,![]];}return db['SmsInteraction'][_0xe86d('0x59')](_0x155061)[_0xe86d('0x35')](function(_0x1fa25c){return[_0x1fa25c,!![]];});});}})[_0xe86d('0x9d')](function(_0x3fb63f,_0x5ec0b1){_0x498b13[_0xe86d('0x9e')]=_0x3fb63f[_0xe86d('0x37')]({'plain':!![]});_0x498b13[_0xe86d('0x9e')][_0xe86d('0x9f')]=![];if(_0x5ec0b1){if(_0x498b13['account']['autoclose']){_0x498b13[_0xe86d('0x72')][_0xe86d('0xa0')]({'id':0x0,'priority':_0x498b13[_0xe86d('0x72')][_0xe86d('0x47')]+0x1,'app':'close','appdata':_0xe86d('0xa1'),'interval':_0xe86d('0xa2')});}_0x498b13[_0xe86d('0x9e')]['created']=!![];}return db[_0xe86d('0xa3')][_0xe86d('0x59')]({'phone':_0x498b13[_0xe86d('0x5a')][_0xe86d('0x7d')],'messageId':_0x498b13['body']['messageId'],'body':_0x498b13[_0xe86d('0x5a')]['body'],'SmsAccountId':_0x5749f8['params']['id'],'SmsInteractionId':_0x3fb63f['id'],'direction':'in','ContactId':_0x3fb63f[_0xe86d('0xa4')],'AttachmentId':_0x498b13[_0xe86d('0x5a')]['AttachmentId']});})[_0xe86d('0x35')](function(_0x5245e8){_0x498b13[_0xe86d('0x28')]=_0x5245e8;if(!_0x498b13['interaction'][_0xe86d('0xa5')])return;return db[_0xe86d('0xa6')][_0xe86d('0x61')]({'attributes':['id',_0xe86d('0x3b')],'where':{'id':_0x498b13[_0xe86d('0x9e')][_0xe86d('0xa5')]}});})[_0xe86d('0x35')](function(_0x489ef9){if(_0x489ef9){_0x498b13[_0xe86d('0x72')][_0xe86d('0xa7')]({'id':0x0,'priority':0x0,'app':_0xe86d('0xa8'),'appdata':_0x489ef9['name']+','+(_0x498b13[_0xe86d('0x71')][_0xe86d('0xa9')]||0xa),'interval':_0xe86d('0xa2')});}if(!_0x498b13[_0xe86d('0x9e')][_0xe86d('0xaa')])return;return db[_0xe86d('0xab')][_0xe86d('0x61')]({'attributes':['id',_0xe86d('0x3b')],'where':{'id':_0x498b13[_0xe86d('0x9e')][_0xe86d('0xaa')]}});})[_0xe86d('0x35')](function(_0x30beb7){if(_0x30beb7){_0x498b13[_0xe86d('0x72')][_0xe86d('0xa7')]({'id':0x0,'priority':0x1,'app':_0xe86d('0xac'),'appdata':_0x30beb7['name']+','+(_0x498b13[_0xe86d('0x71')][_0xe86d('0xad')]||0x12c),'interval':_0xe86d('0xa2')});}return respondWithRpcPromise(_0xe86d('0xae'),_0xe86d('0xaf'),_0x498b13);})[_0xe86d('0x35')](function(_0x24d5dd){if(_0x24d5dd){var _0xe802b3=0xc8;var _0x44579d=_0xe86d('0xb0');logger[_0xe86d('0x22')](_0xe86d('0xb1'),_0x44579d,_0xe802b3,_0xe86d('0xb2'));logger[_0xe86d('0x24')](_0xe86d('0xb3'),_0x44579d,_0xe802b3,'entity\x20found',JSON['stringify'](_0x24d5dd));if(_0x498b13[_0xe86d('0x71')][_0xe86d('0x4f')]==='twilio'){return _0x153e52[_0xe86d('0x2c')](_0xe802b3)[_0xe86d('0xb4')](_0xe86d('0xb5'),_0xe86d('0xb6'))[_0xe86d('0x3c')]('<?xml\x20version=\x221.0\x22\x20encoding=\x22UTF-8\x22?>\x0a<Response></Response>');}else{return _0x153e52[_0xe86d('0x2c')](_0xe802b3)[_0xe86d('0x2e')](_0x24d5dd);}}})[_0xe86d('0x2a')](handleError(_0x153e52,null));};exports['addApplications']=function(_0x3a07bd,_0x1805e2){var _0x1f0bbd=_0x3a07bd['params']['id'];var _0x129268=_0x3a07bd['body'];var _0xca350a=0xc8;var _0x3dc9ab=null;return db[_0xe86d('0xb7')][_0xe86d('0xb8')]({'isolationLevel':db['sequelize'][_0xe86d('0xb9')][_0xe86d('0xba')]['READ_COMMITTED']},function(_0x406604){return db[_0xe86d('0x40')][_0xe86d('0x66')]({'where':{'id':_0x1f0bbd},'transaction':_0x406604})['then'](function(_0x438242){if(_0x438242){return db[_0xe86d('0x70')][_0xe86d('0x36')]({'where':{'SmsAccountId':_0x1f0bbd},'transaction':_0x406604})[_0xe86d('0x35')](function(){var _0x384e9b=_[_0xe86d('0x3f')](_0x129268,function(_0x470dca){_0x470dca['SmsAccountId']=_0x1f0bbd;return _0x470dca;});return db[_0xe86d('0x70')][_0xe86d('0xbb')](_0x384e9b,{'transaction':_0x406604});});}else{_0xca350a=0x194;_0x3dc9ab=[];}});})['then'](function(){if(_0xca350a!==0x194){return db[_0xe86d('0x70')][_0xe86d('0xbc')]({'where':{'SmsAccountId':_0x1f0bbd},'order':_0xe86d('0x75')})[_0xe86d('0x35')](function(_0xef01b0){_0x3dc9ab=_0xef01b0;});}})[_0xe86d('0x2a')](function(_0x534e9c){_0xca350a=0x1f4;logger['error'](_0x534e9c['stack']);if(_0x534e9c[_0xe86d('0x3b')]){delete _0x534e9c['name'];}_0x3dc9ab=_0x534e9c;})[_0xe86d('0xbd')](function(){if(_0x3dc9ab===null){_0x1805e2[_0xe86d('0x2b')](_0xca350a);}else{if(_0xca350a===0x1f4){_0x1805e2[_0xe86d('0x2c')](_0xca350a)[_0xe86d('0x3c')](_0x3dc9ab);}else{_0x1805e2['status'](_0xca350a)['json'](_0x3dc9ab);}}});};exports[_0xe86d('0xbe')]=function(_0x5a4f2e,_0x26b0c3,_0x3df4bd){var _0x499cdb={};var _0x1716fb={};var _0x997173;var _0x3ee5dc;return db[_0xe86d('0x40')][_0xe86d('0x66')]({'where':{'id':_0x5a4f2e[_0xe86d('0x57')]['id']}})[_0xe86d('0x35')](handleEntityNotFound(_0x26b0c3,null))[_0xe86d('0x35')](function(_0x2b7d96){if(_0x2b7d96){_0x997173=_0x2b7d96;_0x1716fb[_0xe86d('0x41')]=_[_0xe86d('0x43')](db[_0xe86d('0x70')][_0xe86d('0x67')]);_0x1716fb[_0xe86d('0x42')]=_['keys'](_0x5a4f2e[_0xe86d('0x42')]);_0x1716fb[_0xe86d('0x44')]=_[_0xe86d('0x58')](_0x1716fb[_0xe86d('0x41')],_0x1716fb[_0xe86d('0x42')]);_0x499cdb[_0xe86d('0x46')]=_['intersection'](_0x1716fb['model'],qs[_0xe86d('0x45')](_0x5a4f2e[_0xe86d('0x42')]['fields']));_0x499cdb[_0xe86d('0x46')]=_0x499cdb[_0xe86d('0x46')][_0xe86d('0x47')]?_0x499cdb[_0xe86d('0x46')]:_0x1716fb['model'];_0x499cdb[_0xe86d('0x4a')]=qs[_0xe86d('0x4b')](_0x5a4f2e[_0xe86d('0x42')][_0xe86d('0x4b')]);_0x499cdb[_0xe86d('0x4e')]=qs[_0xe86d('0x44')](_[_0xe86d('0x4c')](_0x5a4f2e[_0xe86d('0x42')],_0x1716fb[_0xe86d('0x44')]));if(_0x5a4f2e[_0xe86d('0x42')][_0xe86d('0x4d')]){_0x499cdb['where']=_['merge'](_0x499cdb['where'],{'$or':_['map'](_0x499cdb[_0xe86d('0x46')],function(_0x172271){var _0x4a16b7={};_0x4a16b7[_0x172271]={'$like':'%'+_0x5a4f2e['query'][_0xe86d('0x4d')]+'%'};return _0x4a16b7;})});}_0x499cdb=_[_0xe86d('0x51')]({},_0x499cdb,_0x5a4f2e['options']);return _0x997173[_0xe86d('0xbe')](_0x499cdb);}})[_0xe86d('0x35')](function(_0x3fbf78){if(_0x3fbf78){_0x3ee5dc=_0x3fbf78['length'];if(!_0x5a4f2e[_0xe86d('0x42')][_0xe86d('0x48')](_0xe86d('0x49'))){_0x499cdb[_0xe86d('0x30')]=qs[_0xe86d('0x30')](_0x5a4f2e[_0xe86d('0x42')]['limit']);_0x499cdb[_0xe86d('0x32')]=qs[_0xe86d('0x32')](_0x5a4f2e[_0xe86d('0x42')][_0xe86d('0x32')]);}return _0x997173[_0xe86d('0xbe')](_0x499cdb);}})['then'](function(_0x1b154c){if(_0x1b154c){return _0x1b154c?{'count':_0x3ee5dc,'rows':_0x1b154c}:null;}})[_0xe86d('0x35')](respondWithResult(_0x26b0c3,null))[_0xe86d('0x2a')](handleError(_0x26b0c3,null));};exports['getInteractions']=function(_0x49eb8f,_0x32de42,_0x39553a){var _0x33d281={'raw':![],'where':{}};var _0x9fedcc={};var _0x51e5d7={'count':0x0,'rows':[]};return db[_0xe86d('0x40')]['findOne']({'where':{'id':_0x49eb8f[_0xe86d('0x57')]['id']}})['then'](handleEntityNotFound(_0x32de42,null))[_0xe86d('0x35')](function(_0x24bd56){if(_0x24bd56){_0x9fedcc[_0xe86d('0x41')]=_[_0xe86d('0x43')](db['SmsInteraction'][_0xe86d('0x67')]);_0x9fedcc[_0xe86d('0x42')]=_[_0xe86d('0x43')](_0x49eb8f['query']);_0x9fedcc['filters']=_[_0xe86d('0x58')](_0x9fedcc['model'],_0x9fedcc[_0xe86d('0x42')]);_0x33d281[_0xe86d('0x46')]=_[_0xe86d('0x58')](_0x9fedcc[_0xe86d('0x41')],qs['fields'](_0x49eb8f[_0xe86d('0x42')][_0xe86d('0x45')]));_0x33d281[_0xe86d('0x46')]=_0x33d281[_0xe86d('0x46')]['length']?_0x33d281[_0xe86d('0x46')]:_0x9fedcc[_0xe86d('0x41')];if(!_0x49eb8f['query']['hasOwnProperty']('nolimit')){_0x33d281[_0xe86d('0x30')]=qs['limit'](_0x49eb8f['query'][_0xe86d('0x30')]);_0x33d281[_0xe86d('0x32')]=qs[_0xe86d('0x32')](_0x49eb8f[_0xe86d('0x42')]['offset']);}_0x33d281[_0xe86d('0x4a')]=qs['sort'](_0x49eb8f[_0xe86d('0x42')][_0xe86d('0x4b')]);_0x33d281[_0xe86d('0x4e')]=qs[_0xe86d('0x44')](_[_0xe86d('0x4c')](_0x49eb8f['query'],_0x9fedcc[_0xe86d('0x44')]));_0x33d281['where'][_0xe86d('0x63')]=_0x24bd56['id'];if(_0x49eb8f['query']['filter']){_0x33d281[_0xe86d('0x4e')]=_[_0xe86d('0x51')](_0x33d281[_0xe86d('0x4e')],{'$or':_[_0xe86d('0x3f')](_0x33d281['attributes'],function(_0x38c88a){var _0xb6ae86={};_0xb6ae86[_0x38c88a]={'$like':'%'+_0x49eb8f[_0xe86d('0x42')][_0xe86d('0x4d')]+'%'};return _0xb6ae86;})});}_0x33d281=_[_0xe86d('0x51')]({},_0x33d281,_0x49eb8f[_0xe86d('0x52')]);return db[_0xe86d('0xbf')]['count']({'where':_0x33d281['where']})[_0xe86d('0x35')](function(_0x568e0a){_0x51e5d7[_0xe86d('0x31')]=_0x568e0a;if(_0x49eb8f[_0xe86d('0x42')][_0xe86d('0x53')]){_0x33d281[_0xe86d('0x54')]=[{'model':db[_0xe86d('0xc0')],'as':_0xe86d('0xc1'),'required':![]},{'model':db[_0xe86d('0xa6')],'as':_0xe86d('0xc2'),'attributes':[_0xe86d('0x3b'),'fullname','internal'],'required':![]},{'model':db[_0xe86d('0xc3')],'as':_0xe86d('0xc4'),'attributes':['id',_0xe86d('0x3b'),_0xe86d('0xc5')],'where':_0x49eb8f[_0xe86d('0x42')][_0xe86d('0xc6')]?{'id':_0x49eb8f[_0xe86d('0x42')][_0xe86d('0xc6')]}:undefined,'required':_0x49eb8f[_0xe86d('0x42')][_0xe86d('0xc6')]?!![]:![]}];}return db['SmsInteraction'][_0xe86d('0x55')](_0x33d281);})['then'](function(_0x561e55){_0x51e5d7[_0xe86d('0x56')]=_0x561e55;return _0x51e5d7;});}})[_0xe86d('0x35')](respondWithFilteredResult(_0x32de42,_0x33d281))[_0xe86d('0x2a')](handleError(_0x32de42,null));};exports[_0xe86d('0x3c')]=function(_0x4ce97e,_0x441ec1,_0x52d837){var _0x5b7232,_0xfc4f10,_0x31e05a;if(_[_0xe86d('0x90')](_0x4ce97e[_0xe86d('0x5a')][_0xe86d('0x5a')])||_0x4ce97e['body'][_0xe86d('0x5a')]===''){throw new db[(_0xe86d('0x91'))][(_0xe86d('0x76'))]('body\x20is\x20mandatory\x20and\x20not\x20empty');}if(_[_0xe86d('0x90')](_0x4ce97e[_0xe86d('0x5a')]['to'])){throw new db[(_0xe86d('0x91'))][(_0xe86d('0x76'))](_0xe86d('0xc7'));}_0x5b7232=_0x4ce97e[_0xe86d('0x5a')]['to'];return db[_0xe86d('0x40')][_0xe86d('0x61')]({'where':{'id':_0x4ce97e[_0xe86d('0x57')]['id']},'include':[{'model':db[_0xe86d('0x98')],'as':'List','include':[{'model':db[_0xe86d('0xc0')],'as':'Contacts','where':{'mobile':_0x5b7232},'limit':0x1,'order':[['updatedAt',_0xe86d('0x9a')]]}]}]})[_0xe86d('0x35')](handleEntityNotFound(_0x441ec1,null))[_0xe86d('0x35')](function(_0x49e708){if(_0x49e708&&_0x49e708[_0xe86d('0xc8')]){_0xfc4f10=_0x49e708['get']({'plain':!![]});if(_0xfc4f10[_0xe86d('0xc8')]&&_0xfc4f10['List'][_0xe86d('0x99')]['length']){return _0xfc4f10[_0xe86d('0xc8')][_0xe86d('0x99')][0x0];}return db['CmContact']['create'](_[_0xe86d('0x1a')](_0x4ce97e[_0xe86d('0x5a')],{'firstName':_0x5b7232,'mobile':_0x5b7232,'phone':_0x5b7232,'ListId':_0xfc4f10[_0xe86d('0x7a')]}));}})[_0xe86d('0x35')](handleEntityNotFound(_0x441ec1,null))[_0xe86d('0x35')](function(_0x11dcac){if(_0x11dcac){_0x31e05a=_0x11dcac;return db['SmsInteraction']['find']({'where':{'ContactId':_0x31e05a['id'],'closed':![],'SmsAccountId':_0xfc4f10['id']}})[_0xe86d('0x35')](function(_0x2f5b83){if(_0x2f5b83){return[_0x2f5b83,![]];}return db[_0xe86d('0xbf')][_0xe86d('0x59')]({'UserId':_0x4ce97e['user']['id'],'ContactId':_0x31e05a['id'],'SmsAccountId':_0xfc4f10['id'],'phone':_0x5b7232,'firstMsgDirection':_0xe86d('0xc9'),'Messages':[_['merge'](_0x4ce97e['body'],{'read':![],'body':_0x4ce97e[_0xe86d('0x5a')][_0xe86d('0x5a')],'phone':_0x5b7232,'SmsAccountId':_0xfc4f10['id'],'UserId':_0x4ce97e['user']['id'],'ContactId':_0x31e05a['id']})]},{'include':[{'model':db[_0xe86d('0xa3')],'as':_0xe86d('0xca')}]})['then'](function(_0x1a3450){return[_0x1a3450,!![]];});});}})['spread'](function(_0x1439c0,_0x3b3542){if(_0x3b3542){return _0x1439c0;}return db['SmsMessage']['create'](_['merge'](_0x4ce97e[_0xe86d('0x5a')],{'read':![],'body':_0x4ce97e[_0xe86d('0x5a')][_0xe86d('0x5a')],'phone':_0x5b7232,'SmsAccountId':_0xfc4f10['id'],'SmsInteractionId':_0x1439c0['id'],'UserId':_0x4ce97e[_0xe86d('0x5d')]['id'],'ContactId':_0x31e05a['id']}))[_0xe86d('0x35')](function(_0x447498){_0x1439c0[_0xe86d('0xcb')][_0xe86d('0xca')]=[];_0x1439c0[_0xe86d('0xcb')][_0xe86d('0xca')][_0xe86d('0xa0')](_0x447498[_0xe86d('0xcb')]);return _0x1439c0;});})['then'](respondWithResult(_0x441ec1,null))[_0xe86d('0x2a')](handleError(_0x441ec1,null));};exports['status']=function(_0x5ca81f,_0x1f5c4e,_0x4f4df8){if(_0x5ca81f[_0xe86d('0x5a')]['id']){delete _0x5ca81f[_0xe86d('0x5a')]['id'];}var _0x15cac5,_0x427f0c,_0x58fbf8;return db[_0xe86d('0x40')][_0xe86d('0x61')]({'where':{'id':_0x5ca81f[_0xe86d('0x57')]['id']}})[_0xe86d('0x35')](handleEntityNotFound(_0x1f5c4e,null))[_0xe86d('0x35')](function(_0x126aa4){if(_0x126aa4){_0x58fbf8=_0x126aa4;logger[_0xe86d('0x24')](_0xe86d('0xb1'),'status',_0xe86d('0xcc'),_0x126aa4[_0xe86d('0x4f')],util[_0xe86d('0xcd')](_0x5ca81f['body'],{'showHidden':![],'depth':null}));switch(_0x126aa4['type']){case _0xe86d('0xce'):if(_0x5ca81f[_0xe86d('0x5a')]['skebby_dispatch_id']&&_0x5ca81f[_0xe86d('0x5a')][_0xe86d('0x2c')]){_0x427f0c=_0x5ca81f[_0xe86d('0x5a')][_0xe86d('0x2c')];return db[_0xe86d('0xa3')][_0xe86d('0x61')]({'where':{'messageId':_0x5ca81f[_0xe86d('0x5a')]['skebby_dispatch_id']}});}break;case'connectel':if(_0x5ca81f['body'][_0xe86d('0xcf')]&&_0x5ca81f[_0xe86d('0x5a')][_0xe86d('0xd0')]){_0x427f0c=_0x5ca81f[_0xe86d('0x5a')][_0xe86d('0xd0')];return db['SmsMessage'][_0xe86d('0x61')]({'where':{'messageId':_0x5ca81f[_0xe86d('0x5a')]['requestid']}});}break;case _0xe86d('0x83'):if(_0x5ca81f['body'][_0xe86d('0xd1')]&&_0x5ca81f[_0xe86d('0x5a')][_0xe86d('0xd2')]){_0x427f0c=_0x5ca81f[_0xe86d('0x5a')][_0xe86d('0xd2')];return db[_0xe86d('0xa3')]['find']({'where':{'messageId':_0x5ca81f[_0xe86d('0x5a')]['message_id']}});}break;case _0xe86d('0x88'):var _0x1834c3=_0x5ca81f[_0xe86d('0x5a')][_0xe86d('0x89')][0x0];if(_0x1834c3[_0xe86d('0x7f')]&&_0x1834c3[_0xe86d('0x2c')]['groupName']==='DELIVERED'){_0x427f0c=_0x1834c3[_0xe86d('0x2c')]['groupName'];return db[_0xe86d('0xa3')][_0xe86d('0x61')]({'where':{'messageId':_0x1834c3['messageId']}});}break;case _0xe86d('0xd3'):if(_0x5ca81f['body'][_0xe86d('0x7f')]&&_0x5ca81f[_0xe86d('0x5a')][_0xe86d('0x2c')]){_0x427f0c=_0x5ca81f['body']['status'];return db[_0xe86d('0xa3')][_0xe86d('0x61')]({'where':{'messageId':_0x5ca81f[_0xe86d('0x5a')][_0xe86d('0x7f')]}});}break;case _0xe86d('0x8d'):if(_0x5ca81f['body'][0x0][_0xe86d('0x28')]['id']){_0x427f0c=_0x5ca81f['body'][0x0][_0xe86d('0x4f')];return db[_0xe86d('0xa3')][_0xe86d('0x61')]({'where':{'messageId':_0x5ca81f['body'][0x0][_0xe86d('0x28')]['id']}});}break;case _0xe86d('0x8e'):if(_0x5ca81f['body'][_0xe86d('0xd4')]&&_0x5ca81f['body'][_0xe86d('0x2c')]){_0x427f0c=_0x5ca81f[_0xe86d('0x5a')]['status'];return db[_0xe86d('0xa3')][_0xe86d('0x61')]({'where':{'messageId':_0x5ca81f[_0xe86d('0x5a')][_0xe86d('0xd4')]}});}break;}}})[_0xe86d('0x35')](handleEntityNotFound(_0x1f5c4e,null))['then'](function(_0x5f795a){if(_0x5f795a){_0x15cac5=_0x5f795a;switch(_0x58fbf8[_0xe86d('0x4f')]){case _0xe86d('0xce'):if(_0x427f0c==='DELIVERED'){return _0x15cac5[_0xe86d('0xd5')]({'read':!![]});}else{logger['error'](_0xe86d('0xb1'),_0xe86d('0x2c'),'skebby',util[_0xe86d('0xcd')](_0x5ca81f['body'],{'showHidden':![],'depth':null}));}break;case _0xe86d('0x82'):if(_0x427f0c==='1'){return _0x15cac5['update']({'read':!![]});}else{logger[_0xe86d('0x26')]('SmsAccount,\x20%s,\x20%s,\x20%s',_0xe86d('0x2c'),_0xe86d('0xce'),util[_0xe86d('0xcd')](_0x5ca81f[_0xe86d('0x5a')],{'showHidden':![],'depth':null}));}break;case _0xe86d('0x83'):if(!_0x5ca81f[_0xe86d('0x5a')][_0xe86d('0xd6')]&&_0x427f0c>=0xc8&&_0x427f0c<=0xce){return _0x15cac5[_0xe86d('0xd5')]({'read':!![]});}else{logger[_0xe86d('0x26')](_0xe86d('0xb1'),'status',_0xe86d('0x83'),util[_0xe86d('0xcd')](_0x5ca81f[_0xe86d('0x5a')],{'showHidden':![],'depth':null}));}break;case _0xe86d('0x88'):var _0x1fa654=_0x5ca81f[_0xe86d('0x5a')]['results'][0x0];if(_0x1fa654['status']['groupName']===_0xe86d('0xd7')){return _0x15cac5[_0xe86d('0xd5')]({'read':!![]});}else{logger[_0xe86d('0x26')]('SmsAccount,\x20%s,\x20%s,\x20%s','status',_0xe86d('0x88'),util[_0xe86d('0xcd')](_0x5ca81f[_0xe86d('0x5a')],{'showHidden':![],'depth':null}));}break;case _0xe86d('0xd3'):if(_0x427f0c==_0xe86d('0xd8')||_0x427f0c==_0xe86d('0xd9')){return _0x15cac5[_0xe86d('0xd5')]({'read':!![]});}else{logger['error'](_0xe86d('0xb1'),'status',_0xe86d('0xd3'),util[_0xe86d('0xcd')](_0x5ca81f[_0xe86d('0x5a')],{'showHidden':![],'depth':null}));}break;case _0xe86d('0x8d'):if(_0x427f0c==_0xe86d('0xda')){return _0x15cac5[_0xe86d('0xd5')]({'read':!![]});}else{logger[_0xe86d('0x26')](_0xe86d('0xb1'),_0xe86d('0x2c'),'bandwidth',util[_0xe86d('0xcd')](_0x5ca81f[_0xe86d('0x5a')],{'showHidden':![],'depth':null}));}break;case _0xe86d('0x8e'):if(_0x427f0c===_0xe86d('0xdb')||_0x427f0c===_0xe86d('0xdc')){return _0x15cac5['update']({'read':!![]});}else{logger[_0xe86d('0x26')](_0xe86d('0xb1'),'status',_0xe86d('0x8e'),util['inspect'](_0x5ca81f[_0xe86d('0x5a')],{'showHidden':![],'depth':null}));}break;}return _0x15cac5;}})[_0xe86d('0x35')](respondWithResult(_0x1f5c4e,null))['catch'](handleError(_0x1f5c4e,null));};exports[_0xe86d('0xdd')]=function(_0x2aef1a,_0x592592,_0x3be17c){var _0x3468b4,_0x3d33d7,_0x482d8b;return db[_0xe86d('0x40')]['find']({'where':{'id':_0x2aef1a[_0xe86d('0x57')]['id']}})['then'](handleEntityNotFound(_0x592592,null))[_0xe86d('0x35')](function(_0x5af297){if(_0x5af297){_0x482d8b=_0x5af297;logger[_0xe86d('0x24')](_0xe86d('0xb1'),_0xe86d('0x2c'),_0xe86d('0xcc'),_0x5af297['type'],util[_0xe86d('0xcd')](_0x2aef1a[_0xe86d('0x42')],{'showHidden':![],'depth':null}));switch(_0x5af297[_0xe86d('0x4f')]){case _0xe86d('0x8a'):if(_0x2aef1a[_0xe86d('0x42')]['message-id']&&_0x2aef1a['query']['state']){_0x3d33d7=_0x2aef1a[_0xe86d('0x42')][_0xe86d('0xde')];return db[_0xe86d('0xa3')][_0xe86d('0x61')]({'where':{'messageId':_0x2aef1a[_0xe86d('0x42')][_0xe86d('0xdf')]}});}break;}}})[_0xe86d('0x35')](handleEntityNotFound(_0x592592,null))[_0xe86d('0x35')](function(_0x2ccef3){if(_0x2ccef3){_0x3468b4=_0x2ccef3;switch(_0x482d8b[_0xe86d('0x4f')]){case _0xe86d('0x8a'):if(_0x3d33d7==='2'||_0x3d33d7==='6'){return _0x3468b4[_0xe86d('0xd5')]({'read':!![]});}else{logger[_0xe86d('0x26')](_0xe86d('0xb1'),'status',_0xe86d('0x8a'),util[_0xe86d('0xcd')](_0x2aef1a['query'],{'showHidden':![],'depth':null}));};break;}return _0x3468b4;}})[_0xe86d('0x35')](respondWithResult(_0x592592,null))[_0xe86d('0x2a')](handleError(_0x592592,null));};exports['addAgents']=function(_0x32c4a2,_0x29fc4d,_0x5e559a){return db[_0xe86d('0x40')][_0xe86d('0x61')]({'where':{'id':_0x32c4a2[_0xe86d('0x57')]['id']}})[_0xe86d('0x35')](handleEntityNotFound(_0x29fc4d,null))[_0xe86d('0x35')](function(_0x2cb0c0){if(_0x2cb0c0){return _0x2cb0c0[_0xe86d('0xe0')](_0x32c4a2[_0xe86d('0x5a')]['ids'],_[_0xe86d('0x9c')](_0x32c4a2[_0xe86d('0x5a')],[_0xe86d('0x69'),'id'])||{})[_0xe86d('0x9d')](function(_0x5a0903){for(var _0x2d52dd=0x0;_0x2d52dd<_0x32c4a2[_0xe86d('0x5a')]['ids'][_0xe86d('0x47')];_0x2d52dd+=0x1){socket[_0xe86d('0xe1')](_0xe86d('0xe2'),{'UserId':Number(_0x32c4a2['body'][_0xe86d('0x69')][_0x2d52dd]),'SmsAccountId':Number(_0x32c4a2['params']['id'])});}return _0x5a0903;});}})[_0xe86d('0x35')](respondWithResult(_0x29fc4d,null))[_0xe86d('0x2a')](handleError(_0x29fc4d,null));};exports['removeAgents']=function(_0x6a1867,_0x4deb5a,_0x459b10){return db[_0xe86d('0x40')]['find']({'where':{'id':_0x6a1867[_0xe86d('0x57')]['id']}})[_0xe86d('0x35')](handleEntityNotFound(_0x4deb5a,null))[_0xe86d('0x35')](function(_0x2e66fb){if(_0x2e66fb){return _0x2e66fb[_0xe86d('0xe3')](_0x6a1867['query'][_0xe86d('0x69')])[_0xe86d('0x35')](function(){if(_[_0xe86d('0xe4')](_0x6a1867['query'][_0xe86d('0x69')])){for(var _0xf40478=0x0;_0xf40478<_0x6a1867['query'][_0xe86d('0x69')][_0xe86d('0x47')];_0xf40478+=0x1){socket['emit'](_0xe86d('0xe5'),{'UserId':Number(_0x6a1867[_0xe86d('0x42')]['ids'][_0xf40478]),'SmsAccountId':Number(_0x6a1867[_0xe86d('0x57')]['id'])});}}else{socket[_0xe86d('0xe1')](_0xe86d('0xe5'),{'UserId':Number(_0x6a1867['query']['ids']),'SmsAccountId':Number(_0x6a1867[_0xe86d('0x57')]['id'])});}});}})['then'](respondWithStatusCode(_0x4deb5a,null))[_0xe86d('0x2a')](handleError(_0x4deb5a,null));};exports[_0xe86d('0xe6')]=function(_0x33c8af,_0x58440c,_0x56ba4c){var _0x26edb0={};var _0x22dc37={};var _0x4b31ed;var _0x10dbb3;return db[_0xe86d('0x40')][_0xe86d('0x66')]({'where':{'id':_0x33c8af[_0xe86d('0x57')]['id']}})[_0xe86d('0x35')](handleEntityNotFound(_0x58440c,null))['then'](function(_0x4901ab){if(_0x4901ab){_0x4b31ed=_0x4901ab;_0x22dc37[_0xe86d('0x41')]=_[_0xe86d('0x43')](db[_0xe86d('0xa6')][_0xe86d('0x67')]);_0x22dc37[_0xe86d('0x42')]=_[_0xe86d('0x43')](_0x33c8af[_0xe86d('0x42')]);_0x22dc37[_0xe86d('0x44')]=_['intersection'](_0x22dc37[_0xe86d('0x41')],_0x22dc37[_0xe86d('0x42')]);_0x26edb0[_0xe86d('0x46')]=_[_0xe86d('0x58')](_0x22dc37['model'],qs[_0xe86d('0x45')](_0x33c8af[_0xe86d('0x42')][_0xe86d('0x45')]));_0x26edb0[_0xe86d('0x46')]=_0x26edb0[_0xe86d('0x46')]['length']?_0x26edb0[_0xe86d('0x46')]:_0x22dc37['model'];_0x26edb0[_0xe86d('0x4a')]=qs[_0xe86d('0x4b')](_0x33c8af[_0xe86d('0x42')][_0xe86d('0x4b')]);_0x26edb0['where']=qs[_0xe86d('0x44')](_[_0xe86d('0x4c')](_0x33c8af[_0xe86d('0x42')],_0x22dc37['filters']));if(_0x33c8af[_0xe86d('0x42')][_0xe86d('0x4d')]){_0x26edb0[_0xe86d('0x4e')]=_[_0xe86d('0x51')](_0x26edb0[_0xe86d('0x4e')],{'$or':_[_0xe86d('0x3f')](_0x26edb0[_0xe86d('0x46')],function(_0x44a90b){var _0x5bc76b={};_0x5bc76b[_0x44a90b]={'$like':'%'+_0x33c8af[_0xe86d('0x42')][_0xe86d('0x4d')]+'%'};return _0x5bc76b;})});}_0x26edb0=_['merge']({},_0x26edb0,_0x33c8af[_0xe86d('0x52')]);return _0x4b31ed['getAgents'](_0x26edb0);}})[_0xe86d('0x35')](function(_0x1c4283){if(_0x1c4283){_0x10dbb3=_0x1c4283[_0xe86d('0x47')];if(!_0x33c8af['query'][_0xe86d('0x48')](_0xe86d('0x49'))){_0x26edb0[_0xe86d('0x30')]=qs['limit'](_0x33c8af[_0xe86d('0x42')][_0xe86d('0x30')]);_0x26edb0[_0xe86d('0x32')]=qs['offset'](_0x33c8af[_0xe86d('0x42')][_0xe86d('0x32')]);}return _0x4b31ed[_0xe86d('0xe6')](_0x26edb0);}})[_0xe86d('0x35')](function(_0x4b661d){if(_0x4b661d){return _0x4b661d?{'count':_0x10dbb3,'rows':_0x4b661d}:null;}})['then'](respondWithResult(_0x58440c,null))[_0xe86d('0x2a')](handleError(_0x58440c,null));};