158dadac9557bf92dafda102480580c7ba68494b
[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 _0xd413=['attributes','fields','hasOwnProperty','nolimit','order','sort','where','pick','filter','VIRTUAL','merge','options','includeAll','include','rows','catch','show','params','Pause','keys','length','find','create','body','role','user','SmsAccounts','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','describe','SmsAccountId','Disposition','getDispositions','findOne','findAll','removeDispositions','ids','addAnswer','CannedAnswer','getAnswers','removeAnswers','notify','sms','SmsApplication','Applications','account','applications','orderBy','priority','ValidationError','createdAt','updatedAt','difference','ListId','mobile','twilio','from','Body','messageId','skebby','sender','connectel','clicksend','message_id','plivo','From','Text','infobip','results','csc','originatingAddress','segmentedMessage','clickatell','fromNumber','text','replyMessageId','bandwidth','intelepeer','isNil','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','Sequelize','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','CmList','CmContact','omit','Contacts','SmsInteraction','spread','interaction','created','autoclose','push','unmanaged','*,*,*,*','SmsMessage','AttachmentId','UserId','User','unshift','agent','waitForTheAssignedAgent','Start','startRouting','entity\x20found','debug','SmsAccount,\x20%s,\x20%s,\x20%s,\x20%s','text/xml','addApplications','transaction','bulkCreate','findAndCountAll','getApplications','getInteractions','Contact','Owner','fullname','Tag','Tags','color','tag','List','DESC','out','Messages','dataValues','received\x20delivery\x20report\x20from','inspect','skebby_dispatch_id','statusInt','requestid','status_code','groupName','DELIVERED','redif','DELIVERED_TO_GATEWAY','message-delivered','ACCEPTD','statusGet','message-id','state','emit','userSmsAccount:save','removeAgents','isArray','userSmsAccount:remove','getAgents','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','to-csv','querystring','papaparse','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','redis','defaults','localhost','socket.io-emitter','./smsAccount.socket','register','jayson/promise','client','info','SmsAccount,\x20%s,\x20%s','request\x20sent','SmsAccount,\x20%s,\x20%s,\x20%s','stringify','error','code','message','result','status','end','offset','undefined','limit','count','Content-Range','json','apply','reject','update','destroy','get','UserProfileResource','then','sendStatus','stack','name','send','index','mandatoryDispositionPause','map','SmsAccount','rawAttributes','fieldName','type','key','query','filters','intersection','model'];(function(_0x17d9e3,_0x1f969c){var _0x14192d=function(_0x159745){while(--_0x159745){_0x17d9e3['push'](_0x17d9e3['shift']());}};_0x14192d(++_0x1f969c);}(_0xd413,0x173));var _0x3d41=function(_0x3b96cf,_0x1592f2){_0x3b96cf=_0x3b96cf-0x0;var _0x2e315a=_0xd413[_0x3b96cf];return _0x2e315a;};'use strict';var emlformat=require(_0x3d41('0x0'));var rimraf=require(_0x3d41('0x1'));var zipdir=require(_0x3d41('0x2'));var jsonpatch=require(_0x3d41('0x3'));var rp=require(_0x3d41('0x4'));var moment=require(_0x3d41('0x5'));var BPromise=require(_0x3d41('0x6'));var Mustache=require(_0x3d41('0x7'));var util=require(_0x3d41('0x8'));var path=require(_0x3d41('0x9'));var sox=require(_0x3d41('0xa'));var csv=require('to-csv');var ejs=require(_0x3d41('0xb'));var fs=require('fs');var fs_extra=require(_0x3d41('0xc'));var _=require(_0x3d41('0xd'));var squel=require(_0x3d41('0xe'));var crypto=require(_0x3d41('0xf'));var jsforce=require(_0x3d41('0x10'));var deskjs=require(_0x3d41('0x11'));var toCsv=require(_0x3d41('0x12'));var querystring=require(_0x3d41('0x13'));var Papa=require(_0x3d41('0x14'));var Redis=require('ioredis');var authService=require(_0x3d41('0x15'));var qs=require('../../components/parsers/qs');var as=require(_0x3d41('0x16'));var hardwareService=require(_0x3d41('0x17'));var logger=require(_0x3d41('0x18'))('api');var utils=require(_0x3d41('0x19'));var config=require(_0x3d41('0x1a'));var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];config[_0x3d41('0x1b')]=_[_0x3d41('0x1c')](config[_0x3d41('0x1b')],{'host':_0x3d41('0x1d'),'port':0x18eb});var socket=require(_0x3d41('0x1e'))(new Redis(config['redis']));require(_0x3d41('0x1f'))[_0x3d41('0x20')](socket);var jayson=require(_0x3d41('0x21'));var client=jayson[_0x3d41('0x22')]['http']({'port':0x232c});function respondWithRpcPromise(_0x5431d3,_0x5c249a,_0x2bf0dd,_0x2cb380){return new BPromise(function(_0x2bd680,_0x470f52){var _0x3200b2=_0x2cb380||client;return _0x3200b2['request'](_0x5431d3,_0x2bf0dd)['then'](function(_0x5402cc){logger[_0x3d41('0x23')](_0x3d41('0x24'),_0x5c249a,_0x3d41('0x25'));logger['debug'](_0x3d41('0x26'),_0x5c249a,'request\x20sent',JSON[_0x3d41('0x27')](_0x5402cc));if(_0x5402cc[_0x3d41('0x28')]){if(_0x5402cc[_0x3d41('0x28')][_0x3d41('0x29')]===0x1f4){logger['error'](_0x3d41('0x24'),_0x5c249a,_0x5402cc['error'][_0x3d41('0x2a')]);return _0x470f52(_0x5402cc['error']['message']);}logger[_0x3d41('0x28')]('SmsAccount,\x20%s,\x20%s',_0x5c249a,_0x5402cc['error'][_0x3d41('0x2a')]);return _0x2bd680(_0x5402cc[_0x3d41('0x28')]['message']);}else{logger[_0x3d41('0x23')]('SmsAccount,\x20%s,\x20%s',_0x5c249a,_0x3d41('0x25'));_0x2bd680(_0x5402cc[_0x3d41('0x2b')][_0x3d41('0x2a')]);}})['catch'](function(_0x527175){logger['error'](_0x3d41('0x24'),_0x5c249a,_0x527175);_0x470f52(_0x527175);});});}function respondWithStatusCode(_0x27efd1,_0x26699e){_0x26699e=_0x26699e||0xcc;return function(_0x5ae519){if(_0x5ae519){return _0x27efd1['sendStatus'](_0x26699e);}return _0x27efd1[_0x3d41('0x2c')](_0x26699e)[_0x3d41('0x2d')]();};}function respondWithResult(_0x4cccd9,_0x6ec7a4){_0x6ec7a4=_0x6ec7a4||0xc8;return function(_0x4d1154){if(_0x4d1154){return _0x4cccd9[_0x3d41('0x2c')](_0x6ec7a4)['json'](_0x4d1154);}};}function respondWithFilteredResult(_0x1a2438,_0x1d2495){return function(_0x6a67d){if(_0x6a67d){var _0x2b9a1e=typeof _0x1d2495[_0x3d41('0x2e')]===_0x3d41('0x2f')&&typeof _0x1d2495[_0x3d41('0x30')]===_0x3d41('0x2f');var _0x4e3dda=_0x6a67d['count'];var _0x575278=_0x2b9a1e?0x0:_0x1d2495[_0x3d41('0x2e')];var _0x332df5=_0x2b9a1e?_0x6a67d[_0x3d41('0x31')]:_0x1d2495[_0x3d41('0x2e')]+_0x1d2495[_0x3d41('0x30')];var _0x3369ba;if(_0x332df5>=_0x4e3dda){_0x332df5=_0x4e3dda;_0x3369ba=0xc8;}else{_0x3369ba=0xce;}_0x1a2438['status'](_0x3369ba);return _0x1a2438['set'](_0x3d41('0x32'),_0x575278+'-'+_0x332df5+'/'+_0x4e3dda)[_0x3d41('0x33')](_0x6a67d);}return null;};}function patchUpdates(_0x24a246){return function(_0x315eba){try{jsonpatch[_0x3d41('0x34')](_0x315eba,_0x24a246,!![]);}catch(_0x138380){return BPromise[_0x3d41('0x35')](_0x138380);}return _0x315eba['save']();};}function saveUpdates(_0x13b2e4,_0x5bf64a){return function(_0x17ea27){if(_0x17ea27){return _0x17ea27[_0x3d41('0x36')](_0x13b2e4)['then'](function(_0x1d5f68){return _0x1d5f68;});}return null;};}function removeEntity(_0x3117ea,_0x5f2586){return function(_0x493a0c){if(_0x493a0c){return _0x493a0c[_0x3d41('0x37')]()['then'](function(){var _0x21f356=_0x493a0c[_0x3d41('0x38')]({'plain':!![]});var _0x2dd0dd='SmsAccounts';return db[_0x3d41('0x39')][_0x3d41('0x37')]({'where':{'type':_0x2dd0dd,'resourceId':_0x21f356['id']}})[_0x3d41('0x3a')](function(){return _0x493a0c;});})[_0x3d41('0x3a')](function(){_0x3117ea['status'](0xcc)[_0x3d41('0x2d')]();});}};}function handleEntityNotFound(_0x4e8e39,_0x53a159){return function(_0xdaa90a){if(!_0xdaa90a){_0x4e8e39[_0x3d41('0x3b')](0x194);}return _0xdaa90a;};}function handleError(_0x49052,_0x85b038){_0x85b038=_0x85b038||0x1f4;return function(_0x100164){logger[_0x3d41('0x28')](_0x100164[_0x3d41('0x3c')]);if(_0x100164['name']){delete _0x100164[_0x3d41('0x3d')];}_0x49052[_0x3d41('0x2c')](_0x85b038)[_0x3d41('0x3e')](_0x100164);};}exports[_0x3d41('0x3f')]=function(_0x4addc5,_0x5ea94b){var _0x1a28d4={'include':[{'model':db['Pause'],'as':_0x3d41('0x40')}]},_0x492fd0={},_0x13ecbe={'count':0x0,'rows':[]};var _0x1c85d3=_[_0x3d41('0x41')](db[_0x3d41('0x42')][_0x3d41('0x43')],function(_0x14418b){return{'name':_0x14418b[_0x3d41('0x44')],'type':_0x14418b[_0x3d41('0x45')][_0x3d41('0x46')]};});_0x492fd0['model']=_[_0x3d41('0x41')](_0x1c85d3,_0x3d41('0x3d'));_0x492fd0[_0x3d41('0x47')]=_['keys'](_0x4addc5[_0x3d41('0x47')]);_0x492fd0[_0x3d41('0x48')]=_[_0x3d41('0x49')](_0x492fd0[_0x3d41('0x4a')],_0x492fd0[_0x3d41('0x47')]);_0x1a28d4[_0x3d41('0x4b')]=_[_0x3d41('0x49')](_0x492fd0[_0x3d41('0x4a')],qs['fields'](_0x4addc5['query'][_0x3d41('0x4c')]));_0x1a28d4[_0x3d41('0x4b')]=_0x1a28d4[_0x3d41('0x4b')]['length']?_0x1a28d4[_0x3d41('0x4b')]:_0x492fd0[_0x3d41('0x4a')];if(!_0x4addc5[_0x3d41('0x47')][_0x3d41('0x4d')](_0x3d41('0x4e'))){_0x1a28d4[_0x3d41('0x30')]=qs[_0x3d41('0x30')](_0x4addc5[_0x3d41('0x47')][_0x3d41('0x30')]);_0x1a28d4[_0x3d41('0x2e')]=qs['offset'](_0x4addc5[_0x3d41('0x47')]['offset']);}_0x1a28d4[_0x3d41('0x4f')]=qs[_0x3d41('0x50')](_0x4addc5['query'][_0x3d41('0x50')]);_0x1a28d4[_0x3d41('0x51')]=qs['filters'](_[_0x3d41('0x52')](_0x4addc5['query'],_0x492fd0['filters']),_0x1c85d3);if(_0x4addc5['query'][_0x3d41('0x53')]){_0x1a28d4['where']=_['merge'](_0x1a28d4[_0x3d41('0x51')],{'$or':_[_0x3d41('0x41')](_0x1c85d3,function(_0x3da6aa){if(_0x3da6aa['type']!==_0x3d41('0x54')){var _0x314256={};_0x314256[_0x3da6aa[_0x3d41('0x3d')]]={'$like':'%'+_0x4addc5[_0x3d41('0x47')][_0x3d41('0x53')]+'%'};return _0x314256;}})});}_0x1a28d4=_[_0x3d41('0x55')]({},_0x1a28d4,_0x4addc5[_0x3d41('0x56')]);var _0x32e91f={'where':_0x1a28d4[_0x3d41('0x51')]};return db[_0x3d41('0x42')][_0x3d41('0x31')](_0x32e91f)['then'](function(_0x27facc){_0x13ecbe[_0x3d41('0x31')]=_0x27facc;if(_0x4addc5['query'][_0x3d41('0x57')]){_0x1a28d4[_0x3d41('0x58')]=[{'all':!![]}];}return db[_0x3d41('0x42')]['findAll'](_0x1a28d4);})['then'](function(_0x4f5b2c){_0x13ecbe[_0x3d41('0x59')]=_0x4f5b2c;return _0x13ecbe;})[_0x3d41('0x3a')](respondWithFilteredResult(_0x5ea94b,_0x1a28d4))[_0x3d41('0x5a')](handleError(_0x5ea94b,null));};exports[_0x3d41('0x5b')]=function(_0x1b269e,_0x5555ef){var _0x5a8c20={'raw':![],'where':{'id':_0x1b269e[_0x3d41('0x5c')]['id']},'include':[{'model':db[_0x3d41('0x5d')],'as':'mandatoryDispositionPause'}]},_0x3e48d3={};_0x3e48d3[_0x3d41('0x4a')]=_[_0x3d41('0x5e')](db[_0x3d41('0x42')][_0x3d41('0x43')]);_0x3e48d3[_0x3d41('0x47')]=_[_0x3d41('0x5e')](_0x1b269e[_0x3d41('0x47')]);_0x3e48d3[_0x3d41('0x48')]=_[_0x3d41('0x49')](_0x3e48d3['model'],_0x3e48d3['query']);_0x5a8c20[_0x3d41('0x4b')]=_[_0x3d41('0x49')](_0x3e48d3[_0x3d41('0x4a')],qs[_0x3d41('0x4c')](_0x1b269e[_0x3d41('0x47')][_0x3d41('0x4c')]));_0x5a8c20[_0x3d41('0x4b')]=_0x5a8c20[_0x3d41('0x4b')][_0x3d41('0x5f')]?_0x5a8c20[_0x3d41('0x4b')]:_0x3e48d3[_0x3d41('0x4a')];if(_0x1b269e['query']['includeAll']){_0x5a8c20[_0x3d41('0x58')]=[{'all':!![]}];}_0x5a8c20=_['merge']({},_0x5a8c20,_0x1b269e[_0x3d41('0x56')]);return db['SmsAccount'][_0x3d41('0x60')](_0x5a8c20)[_0x3d41('0x3a')](handleEntityNotFound(_0x5555ef,null))['then'](respondWithResult(_0x5555ef,null))['catch'](handleError(_0x5555ef,null));};exports[_0x3d41('0x61')]=function(_0x5b07d2,_0x404ce2){return db[_0x3d41('0x42')][_0x3d41('0x61')](_0x5b07d2[_0x3d41('0x62')],{})[_0x3d41('0x3a')](function(_0x1cb176){var _0x42b803=_0x5b07d2['user']['get']({'plain':!![]});if(!_0x42b803)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x42b803[_0x3d41('0x63')]===_0x3d41('0x64')){var _0x372c09=_0x1cb176[_0x3d41('0x38')]({'plain':!![]});var _0x332266=_0x3d41('0x65');return db['UserProfileSection']['find']({'where':{'name':_0x332266,'userProfileId':_0x42b803['userProfileId']},'raw':!![]})[_0x3d41('0x3a')](function(_0x3326a2){if(_0x3326a2&&_0x3326a2['autoAssociation']===0x0){return db[_0x3d41('0x39')][_0x3d41('0x61')]({'name':_0x372c09[_0x3d41('0x3d')],'resourceId':_0x372c09['id'],'type':_0x3326a2['name'],'sectionId':_0x3326a2['id']},{})[_0x3d41('0x3a')](function(){return _0x1cb176;});}else{return _0x1cb176;}})[_0x3d41('0x5a')](function(_0x124403){logger['error'](_0x3d41('0x66'),_0x124403);throw _0x124403;});}return _0x1cb176;})[_0x3d41('0x3a')](respondWithResult(_0x404ce2,0xc9))[_0x3d41('0x5a')](handleError(_0x404ce2,null));};exports[_0x3d41('0x36')]=function(_0x175e8f,_0x5a5f1a){if(_0x175e8f[_0x3d41('0x62')]['id']){delete _0x175e8f[_0x3d41('0x62')]['id'];}return db['SmsAccount'][_0x3d41('0x60')]({'where':{'id':_0x175e8f[_0x3d41('0x5c')]['id']},'include':[{'model':db['Pause'],'as':_0x3d41('0x40')}]})['then'](handleEntityNotFound(_0x5a5f1a,null))[_0x3d41('0x3a')](saveUpdates(_0x175e8f[_0x3d41('0x62')],null))[_0x3d41('0x3a')](respondWithResult(_0x5a5f1a,null))[_0x3d41('0x5a')](handleError(_0x5a5f1a,null));};exports[_0x3d41('0x37')]=function(_0x12d638,_0x5d6df0){return db['SmsAccount']['find']({'where':{'id':_0x12d638['params']['id']}})[_0x3d41('0x3a')](handleEntityNotFound(_0x5d6df0,null))['then'](removeEntity(_0x5d6df0,null))['catch'](handleError(_0x5d6df0,null));};exports[_0x3d41('0x67')]=function(_0xef06b2,_0x4079a4){return db[_0x3d41('0x42')][_0x3d41('0x67')]()[_0x3d41('0x3a')](respondWithResult(_0x4079a4,null))[_0x3d41('0x5a')](handleError(_0x4079a4,null));};exports['addDisposition']=function(_0x5ecb06,_0xa19183,_0x18ad5b){if(_0x5ecb06[_0x3d41('0x62')]['id']){delete _0x5ecb06[_0x3d41('0x62')]['id'];}return db[_0x3d41('0x42')][_0x3d41('0x60')]({'where':{'id':_0x5ecb06[_0x3d41('0x5c')]['id']}})[_0x3d41('0x3a')](handleEntityNotFound(_0xa19183,null))[_0x3d41('0x3a')](function(_0x168b5a){if(_0x168b5a){_0x5ecb06[_0x3d41('0x62')][_0x3d41('0x68')]=_0x168b5a['id'];return db[_0x3d41('0x69')]['create'](_0x5ecb06[_0x3d41('0x62')]);}})['then'](respondWithResult(_0xa19183,null))[_0x3d41('0x5a')](handleError(_0xa19183,null));};exports[_0x3d41('0x6a')]=function(_0x496518,_0x765c74,_0x108f53){var _0x2f00fd={'raw':![],'where':{}};var _0x5f1a05={};var _0x1fadf4={'count':0x0,'rows':[]};return db[_0x3d41('0x42')][_0x3d41('0x6b')]({'where':{'id':_0x496518[_0x3d41('0x5c')]['id']}})[_0x3d41('0x3a')](handleEntityNotFound(_0x765c74,null))['then'](function(_0x415af6){if(_0x415af6){_0x5f1a05[_0x3d41('0x4a')]=_[_0x3d41('0x5e')](db[_0x3d41('0x69')][_0x3d41('0x43')]);_0x5f1a05[_0x3d41('0x47')]=_[_0x3d41('0x5e')](_0x496518['query']);_0x5f1a05[_0x3d41('0x48')]=_['intersection'](_0x5f1a05[_0x3d41('0x4a')],_0x5f1a05[_0x3d41('0x47')]);_0x2f00fd[_0x3d41('0x4b')]=_[_0x3d41('0x49')](_0x5f1a05['model'],qs[_0x3d41('0x4c')](_0x496518[_0x3d41('0x47')]['fields']));_0x2f00fd['attributes']=_0x2f00fd[_0x3d41('0x4b')]['length']?_0x2f00fd[_0x3d41('0x4b')]:_0x5f1a05[_0x3d41('0x4a')];if(!_0x496518[_0x3d41('0x47')][_0x3d41('0x4d')](_0x3d41('0x4e'))){_0x2f00fd['limit']=qs[_0x3d41('0x30')](_0x496518[_0x3d41('0x47')][_0x3d41('0x30')]);_0x2f00fd[_0x3d41('0x2e')]=qs[_0x3d41('0x2e')](_0x496518[_0x3d41('0x47')][_0x3d41('0x2e')]);}_0x2f00fd['order']=qs[_0x3d41('0x50')](_0x496518[_0x3d41('0x47')][_0x3d41('0x50')]);_0x2f00fd[_0x3d41('0x51')]=qs[_0x3d41('0x48')](_[_0x3d41('0x52')](_0x496518[_0x3d41('0x47')],_0x5f1a05['filters']));_0x2f00fd[_0x3d41('0x51')]['SmsAccountId']=_0x415af6['id'];if(_0x496518['query']['filter']){_0x2f00fd[_0x3d41('0x51')]=_[_0x3d41('0x55')](_0x2f00fd['where'],{'$or':_[_0x3d41('0x41')](_0x2f00fd['attributes'],function(_0x47377e){var _0xde60ed={};_0xde60ed[_0x47377e]={'$like':'%'+_0x496518[_0x3d41('0x47')]['filter']+'%'};return _0xde60ed;})});}_0x2f00fd=_['merge']({},_0x2f00fd,_0x496518[_0x3d41('0x56')]);return db[_0x3d41('0x69')]['count']({'where':_0x2f00fd['where']})['then'](function(_0x12c542){_0x1fadf4[_0x3d41('0x31')]=_0x12c542;if(_0x496518[_0x3d41('0x47')]['includeAll']){_0x2f00fd[_0x3d41('0x58')]=[{'all':!![]}];}return db[_0x3d41('0x69')][_0x3d41('0x6c')](_0x2f00fd);})[_0x3d41('0x3a')](function(_0x5b7391){_0x1fadf4[_0x3d41('0x59')]=_0x5b7391;return _0x1fadf4;});}})[_0x3d41('0x3a')](respondWithFilteredResult(_0x765c74,_0x2f00fd))['catch'](handleError(_0x765c74,null));};exports['removeDispositions']=function(_0x46dfa1,_0xbc7bb5,_0xfc9ace){return db[_0x3d41('0x42')][_0x3d41('0x60')]({'where':{'id':_0x46dfa1[_0x3d41('0x5c')]['id']}})['then'](handleEntityNotFound(_0xbc7bb5,null))[_0x3d41('0x3a')](function(_0x27fe36){if(_0x27fe36){return _0x27fe36[_0x3d41('0x6d')](_0x46dfa1[_0x3d41('0x47')][_0x3d41('0x6e')]);}})[_0x3d41('0x3a')](respondWithStatusCode(_0xbc7bb5,null))[_0x3d41('0x5a')](handleError(_0xbc7bb5,null));};exports[_0x3d41('0x6f')]=function(_0x481ec5,_0x3ad341,_0x82b542){if(_0x481ec5[_0x3d41('0x62')]['id']){delete _0x481ec5[_0x3d41('0x62')]['id'];}return db['SmsAccount']['find']({'where':{'id':_0x481ec5[_0x3d41('0x5c')]['id']}})['then'](handleEntityNotFound(_0x3ad341,null))['then'](function(_0x2a85c){if(_0x2a85c){_0x481ec5[_0x3d41('0x62')][_0x3d41('0x68')]=_0x2a85c['id'];return db[_0x3d41('0x70')]['create'](_0x481ec5[_0x3d41('0x62')]);}})[_0x3d41('0x3a')](respondWithResult(_0x3ad341,null))[_0x3d41('0x5a')](handleError(_0x3ad341,null));};exports[_0x3d41('0x71')]=function(_0x293a34,_0x2417a4,_0x2a2736){var _0x3f94a7={'raw':![],'where':{}};var _0x39c718={};var _0x290e42={'count':0x0,'rows':[]};return db[_0x3d41('0x42')][_0x3d41('0x6b')]({'where':{'id':_0x293a34[_0x3d41('0x5c')]['id']}})['then'](handleEntityNotFound(_0x2417a4,null))['then'](function(_0x4d1220){if(_0x4d1220){_0x39c718[_0x3d41('0x4a')]=_[_0x3d41('0x5e')](db[_0x3d41('0x70')][_0x3d41('0x43')]);_0x39c718[_0x3d41('0x47')]=_[_0x3d41('0x5e')](_0x293a34[_0x3d41('0x47')]);_0x39c718[_0x3d41('0x48')]=_['intersection'](_0x39c718[_0x3d41('0x4a')],_0x39c718['query']);_0x3f94a7[_0x3d41('0x4b')]=_[_0x3d41('0x49')](_0x39c718['model'],qs[_0x3d41('0x4c')](_0x293a34[_0x3d41('0x47')][_0x3d41('0x4c')]));_0x3f94a7[_0x3d41('0x4b')]=_0x3f94a7[_0x3d41('0x4b')][_0x3d41('0x5f')]?_0x3f94a7[_0x3d41('0x4b')]:_0x39c718['model'];if(!_0x293a34[_0x3d41('0x47')]['hasOwnProperty']('nolimit')){_0x3f94a7[_0x3d41('0x30')]=qs[_0x3d41('0x30')](_0x293a34[_0x3d41('0x47')][_0x3d41('0x30')]);_0x3f94a7[_0x3d41('0x2e')]=qs[_0x3d41('0x2e')](_0x293a34[_0x3d41('0x47')]['offset']);}_0x3f94a7['order']=qs[_0x3d41('0x50')](_0x293a34[_0x3d41('0x47')][_0x3d41('0x50')]);_0x3f94a7[_0x3d41('0x51')]=qs[_0x3d41('0x48')](_[_0x3d41('0x52')](_0x293a34[_0x3d41('0x47')],_0x39c718[_0x3d41('0x48')]));_0x3f94a7[_0x3d41('0x51')][_0x3d41('0x68')]=_0x4d1220['id'];if(_0x293a34[_0x3d41('0x47')][_0x3d41('0x53')]){_0x3f94a7[_0x3d41('0x51')]=_[_0x3d41('0x55')](_0x3f94a7[_0x3d41('0x51')],{'$or':_[_0x3d41('0x41')](_0x3f94a7['attributes'],function(_0x30d2c5){var _0x11d86e={};_0x11d86e[_0x30d2c5]={'$like':'%'+_0x293a34[_0x3d41('0x47')][_0x3d41('0x53')]+'%'};return _0x11d86e;})});}_0x3f94a7=_[_0x3d41('0x55')]({},_0x3f94a7,_0x293a34[_0x3d41('0x56')]);return db[_0x3d41('0x70')]['count']({'where':_0x3f94a7[_0x3d41('0x51')]})[_0x3d41('0x3a')](function(_0x1e5980){_0x290e42['count']=_0x1e5980;if(_0x293a34[_0x3d41('0x47')]['includeAll']){_0x3f94a7[_0x3d41('0x58')]=[{'all':!![]}];}return db[_0x3d41('0x70')][_0x3d41('0x6c')](_0x3f94a7);})['then'](function(_0x2c4f63){_0x290e42[_0x3d41('0x59')]=_0x2c4f63;return _0x290e42;});}})['then'](respondWithFilteredResult(_0x2417a4,_0x3f94a7))[_0x3d41('0x5a')](handleError(_0x2417a4,null));};exports[_0x3d41('0x72')]=function(_0x5a219b,_0x2fc98b,_0x1d21cc){return db[_0x3d41('0x42')][_0x3d41('0x60')]({'where':{'id':_0x5a219b[_0x3d41('0x5c')]['id']}})['then'](handleEntityNotFound(_0x2fc98b,null))[_0x3d41('0x3a')](function(_0x190721){if(_0x190721){return _0x190721['removeAnswers'](_0x5a219b[_0x3d41('0x47')][_0x3d41('0x6e')]);}})[_0x3d41('0x3a')](respondWithStatusCode(_0x2fc98b,null))[_0x3d41('0x5a')](handleError(_0x2fc98b,null));};exports[_0x3d41('0x73')]=function(_0x3e68f4,_0x1d6301,_0x57197c){var _0x2198ff={'channel':_0x3d41('0x74')};var _0x3b6492=[];var _0x40c5e7=[];var _0x5354df={};return db[_0x3d41('0x42')]['find']({'where':{'id':_0x3e68f4[_0x3d41('0x5c')]['id']},'include':[{'model':db[_0x3d41('0x75')],'as':_0x3d41('0x76')}]})[_0x3d41('0x3a')](handleEntityNotFound(_0x1d6301,null))[_0x3d41('0x3a')](function(_0x371d88){_0x2198ff[_0x3d41('0x77')]=_0x371d88;_0x2198ff[_0x3d41('0x78')]=_[_0x3d41('0x79')](_0x371d88[_0x3d41('0x76')],[_0x3d41('0x7a')],['asc']);if(_0x2198ff[_0x3d41('0x77')][_0x3d41('0x76')]){delete _0x2198ff[_0x3d41('0x77')][_0x3d41('0x76')];}return db['CmContact'][_0x3d41('0x67')]();})[_0x3d41('0x3a')](function(_0x4323e0){if(!_0x4323e0){throw new db['Sequelize'][(_0x3d41('0x7b'))]('no\x20available\x20attributes');}_0x3b6492=_['difference'](_[_0x3d41('0x5e')](_0x4323e0),[_0x3d41('0x7c'),_0x3d41('0x7d')]);_0x40c5e7=_[_0x3d41('0x7e')](_[_0x3d41('0x5e')](_0x4323e0),[_0x3d41('0x7c'),_0x3d41('0x7d'),'CompanyId',_0x3d41('0x7f')]);_0x2198ff[_0x3d41('0x62')]={'mapKey':_0x3d41('0x80')};if(_0x2198ff[_0x3d41('0x77')]['type']){switch(_0x2198ff[_0x3d41('0x77')][_0x3d41('0x45')]){case _0x3d41('0x81'):_0x2198ff['body'][_0x3d41('0x82')]=_0x3e68f4['body']['From'];_0x2198ff[_0x3d41('0x62')]['body']=_0x3e68f4[_0x3d41('0x62')][_0x3d41('0x83')];_0x2198ff['body'][_0x3d41('0x84')]=_0x3e68f4[_0x3d41('0x62')]['MessageSid'];break;case _0x3d41('0x85'):_0x2198ff[_0x3d41('0x62')][_0x3d41('0x82')]='+'+_0x3e68f4['body'][_0x3d41('0x86')];_0x2198ff['body']['body']=_0x3e68f4[_0x3d41('0x62')]['text'];break;case _0x3d41('0x87'):_0x2198ff['body'][_0x3d41('0x82')]=_0x3e68f4[_0x3d41('0x62')][_0x3d41('0x82')];_0x2198ff[_0x3d41('0x62')]['body']=_0x3e68f4[_0x3d41('0x62')]['message'];break;case _0x3d41('0x88'):_0x2198ff[_0x3d41('0x62')][_0x3d41('0x82')]=_0x3e68f4[_0x3d41('0x62')][_0x3d41('0x82')];_0x2198ff[_0x3d41('0x62')][_0x3d41('0x62')]=_0x3e68f4[_0x3d41('0x62')][_0x3d41('0x62')];_0x2198ff[_0x3d41('0x62')]['messageId']=_0x3e68f4['body'][_0x3d41('0x89')];break;case _0x3d41('0x8a'):_0x2198ff[_0x3d41('0x62')][_0x3d41('0x82')]=_0x3e68f4[_0x3d41('0x62')][_0x3d41('0x8b')];_0x2198ff['body'][_0x3d41('0x62')]=_0x3e68f4[_0x3d41('0x62')][_0x3d41('0x8c')];_0x2198ff[_0x3d41('0x62')]['messageId']=_0x3e68f4['body']['MessageUUID'];break;case _0x3d41('0x8d'):_0x2198ff[_0x3d41('0x62')]['from']='+'+_0x3e68f4[_0x3d41('0x62')][_0x3d41('0x8e')][0x0][_0x3d41('0x82')];_0x2198ff['body'][_0x3d41('0x62')]=_0x3e68f4['body'][_0x3d41('0x8e')][0x0]['text'];_0x2198ff[_0x3d41('0x62')]['messageId']=_0x3e68f4[_0x3d41('0x62')][_0x3d41('0x8e')][0x0][_0x3d41('0x84')];break;case _0x3d41('0x8f'):_0x2198ff[_0x3d41('0x62')]['from']=_0x3e68f4[_0x3d41('0x62')][_0x3d41('0x90')];_0x2198ff[_0x3d41('0x62')][_0x3d41('0x62')]=_0x3e68f4[_0x3d41('0x62')][_0x3d41('0x91')]['message'];break;case _0x3d41('0x92'):_0x2198ff[_0x3d41('0x62')]['from']=_0x3e68f4[_0x3d41('0x62')][_0x3d41('0x93')];_0x2198ff['body'][_0x3d41('0x62')]=_0x3e68f4[_0x3d41('0x62')][_0x3d41('0x94')];_0x2198ff[_0x3d41('0x62')][_0x3d41('0x84')]=_0x3e68f4['body'][_0x3d41('0x95')];break;case _0x3d41('0x96'):_0x2198ff[_0x3d41('0x62')]['messageId']=_0x3e68f4[_0x3d41('0x62')][0x0][_0x3d41('0x2a')]['id'];_0x2198ff[_0x3d41('0x62')][_0x3d41('0x82')]=_0x3e68f4['body'][0x0]['message'][_0x3d41('0x82')];_0x2198ff[_0x3d41('0x62')][_0x3d41('0x62')]=_0x3e68f4['body'][0x0][_0x3d41('0x2a')]['text'];break;case _0x3d41('0x97'):_0x2198ff[_0x3d41('0x62')][_0x3d41('0x82')]=_0x3e68f4[_0x3d41('0x62')][_0x3d41('0x82')];_0x2198ff['body'][_0x3d41('0x62')]=_0x3e68f4[_0x3d41('0x62')]['message'];_0x2198ff[_0x3d41('0x62')][_0x3d41('0x84')]=_0x3e68f4['body']['refid'];break;}}if(_0x2198ff[_0x3d41('0x62')]['id']){delete _0x2198ff[_0x3d41('0x62')]['id'];}if(_[_0x3d41('0x98')](_0x2198ff[_0x3d41('0x62')][_0x3d41('0x82')])){throw new db['Sequelize'][(_0x3d41('0x7b'))]('from\x20is\x20mandatory');}if(_[_0x3d41('0x98')](_0x2198ff[_0x3d41('0x62')][_0x3d41('0x62')])||_0x2198ff['body'][_0x3d41('0x62')]===''){throw new db['Sequelize'][(_0x3d41('0x7b'))](_0x3d41('0x99'));}if(_[_0x3d41('0x98')](_0x2198ff[_0x3d41('0x62')][_0x3d41('0x9a')])){throw new db[(_0x3d41('0x9b'))]['ValidationError'](_0x3d41('0x9c')+_0x40c5e7);}if(!_[_0x3d41('0x9d')](_0x40c5e7,_0x2198ff[_0x3d41('0x62')][_0x3d41('0x9a')])){throw new db[(_0x3d41('0x9b'))]['ValidationError'](_0x3d41('0x9e')+_0x40c5e7);}_0x5354df[_0x2198ff[_0x3d41('0x62')]['mapKey']]=_0x2198ff[_0x3d41('0x62')]['from'];return db[_0x3d41('0x9f')][_0x3d41('0x60')]({'where':{'id':_0x2198ff['account'][_0x3d41('0x7f')]},'include':[{'model':db[_0x3d41('0xa0')],'as':'Contacts','where':_0x5354df,'limit':0x1,'order':[[_0x3d41('0x7d'),'DESC']]}]});})['then'](handleEntityNotFound(_0x1d6301,null))[_0x3d41('0x3a')](function(_0x49bd23){if(_0x49bd23){_0x2198ff['list']=_[_0x3d41('0xa1')](_0x49bd23,['Contacts']);if(_0x49bd23['Contacts'][_0x3d41('0x5f')]){return _0x49bd23[_0x3d41('0xa2')][0x0];}var _0x3b6f30=_[_0x3d41('0x1c')](_0x2198ff[_0x3d41('0x62')],{'firstName':_0x2198ff[_0x3d41('0x62')][_0x3d41('0x82')],'ListId':_0x49bd23['id']});_0x3b6f30[_0x2198ff[_0x3d41('0x62')][_0x3d41('0x9a')]]=_0x2198ff[_0x3d41('0x62')][_0x3d41('0x82')];return db['CmContact'][_0x3d41('0x61')](_0x3b6f30,{'fields':_0x3b6492,'raw':!![]});}})[_0x3d41('0x3a')](handleEntityNotFound(_0x1d6301,null))[_0x3d41('0x3a')](function(_0x27f967){if(_0x27f967){_0x2198ff['contact']=_0x27f967;var _0x1b4631={'ContactId':_0x27f967['id'],'phone':_0x2198ff[_0x3d41('0x62')][_0x3d41('0x82')],'SmsAccountId':_0x3e68f4[_0x3d41('0x5c')]['id']};var _0x28008a={'ContactId':_0x27f967['id'],'SmsAccountId':_0x3e68f4['params']['id'],'closed':![]};return db[_0x3d41('0xa3')][_0x3d41('0x60')]({'where':_0x28008a})['then'](function(_0x3ea60c){if(_0x3ea60c){return[_0x3ea60c,![]];}return db['SmsInteraction'][_0x3d41('0x61')](_0x1b4631)[_0x3d41('0x3a')](function(_0x4e619c){return[_0x4e619c,!![]];});});}})[_0x3d41('0xa4')](function(_0x39c4cd,_0x5bb21b){_0x2198ff['interaction']=_0x39c4cd[_0x3d41('0x38')]({'plain':!![]});_0x2198ff[_0x3d41('0xa5')][_0x3d41('0xa6')]=![];if(_0x5bb21b){if(_0x2198ff[_0x3d41('0x77')][_0x3d41('0xa7')]){_0x2198ff['applications'][_0x3d41('0xa8')]({'id':0x0,'priority':_0x2198ff[_0x3d41('0x78')][_0x3d41('0x5f')]+0x1,'app':'close','appdata':_0x3d41('0xa9'),'interval':_0x3d41('0xaa')});}_0x2198ff[_0x3d41('0xa5')]['created']=!![];}return db[_0x3d41('0xab')][_0x3d41('0x61')]({'phone':_0x2198ff[_0x3d41('0x62')][_0x3d41('0x82')],'messageId':_0x2198ff[_0x3d41('0x62')][_0x3d41('0x84')],'body':_0x2198ff['body'][_0x3d41('0x62')],'SmsAccountId':_0x3e68f4[_0x3d41('0x5c')]['id'],'SmsInteractionId':_0x39c4cd['id'],'direction':'in','ContactId':_0x39c4cd['ContactId'],'AttachmentId':_0x2198ff[_0x3d41('0x62')][_0x3d41('0xac')]});})[_0x3d41('0x3a')](function(_0x1a3bd2){_0x2198ff[_0x3d41('0x2a')]=_0x1a3bd2;if(_0x2198ff[_0x3d41('0xa5')][_0x3d41('0xad')]){return db[_0x3d41('0xae')][_0x3d41('0x60')]({'attributes':['id','name'],'where':{'id':_0x2198ff[_0x3d41('0xa5')]['UserId']}})[_0x3d41('0x3a')](function(_0x1c8ab6){if(_0x1c8ab6){_0x2198ff[_0x3d41('0x78')][_0x3d41('0xaf')]({'id':0x0,'priority':0x0,'app':_0x3d41('0xb0'),'appdata':_0x1c8ab6[_0x3d41('0x3d')]+','+(_0x2198ff[_0x3d41('0x77')][_0x3d41('0xb1')]||0xa),'interval':'*,*,*,*'});}return respondWithRpcPromise(_0x3d41('0xb2'),_0x3d41('0xb3'),_0x2198ff);});}return respondWithRpcPromise(_0x3d41('0xb2'),_0x3d41('0xb3'),_0x2198ff);})['then'](function(_0x32433d){if(_0x32433d){var _0x148e2c=0xc8;var _0x13e7b9='receiveMessage';logger['info'](_0x3d41('0x26'),_0x13e7b9,_0x148e2c,_0x3d41('0xb4'));logger[_0x3d41('0xb5')](_0x3d41('0xb6'),_0x13e7b9,_0x148e2c,_0x3d41('0xb4'),JSON[_0x3d41('0x27')](_0x32433d));if(_0x2198ff['account']['type']===_0x3d41('0x81')){return _0x1d6301[_0x3d41('0x2c')](_0x148e2c)['set']('Content-Type',_0x3d41('0xb7'))[_0x3d41('0x3e')]('<?xml\x20version=\x221.0\x22\x20encoding=\x22UTF-8\x22?>\x0a<Response></Response>');}else{return _0x1d6301['status'](_0x148e2c)[_0x3d41('0x33')](_0x32433d);}}})[_0x3d41('0x5a')](handleError(_0x1d6301,null));};exports[_0x3d41('0xb8')]=function(_0x258ccd,_0x49fbcf,_0x4cbcd9){return db[_0x3d41('0x42')]['findOne']({'where':{'id':_0x258ccd[_0x3d41('0x5c')]['id']}})[_0x3d41('0x3a')](handleEntityNotFound(_0x49fbcf,null))[_0x3d41('0x3a')](function(_0xdec521){if(_0xdec521){return db['sequelize'][_0x3d41('0xb9')](function(_0x36ea78){return db[_0x3d41('0x75')]['destroy']({'where':{'SmsAccountId':_0x258ccd['params']['id']},'transaction':_0x36ea78})[_0x3d41('0x3a')](function(_0x5c37b9){var _0x386f47=_['map'](_0x258ccd[_0x3d41('0x62')],function(_0x3c1dc0){_0x3c1dc0[_0x3d41('0x68')]=_0x258ccd[_0x3d41('0x5c')]['id'];return _0x3c1dc0;});return db['SmsApplication'][_0x3d41('0xba')](_0x386f47,{'transaction':_0x36ea78});});})[_0x3d41('0x3a')](function(){return db[_0x3d41('0x75')][_0x3d41('0xbb')]({'where':{'SmsAccountId':_0x258ccd[_0x3d41('0x5c')]['id']},'order':'priority'});});}})[_0x3d41('0x3a')](respondWithResult(_0x49fbcf,null))[_0x3d41('0x5a')](handleError(_0x49fbcf,null));};exports['getApplications']=function(_0x1bce9f,_0x222e7e,_0x59f503){var _0x141fcd={};var _0x573264={};var _0x301599;var _0x58f945;return db[_0x3d41('0x42')][_0x3d41('0x6b')]({'where':{'id':_0x1bce9f['params']['id']}})[_0x3d41('0x3a')](handleEntityNotFound(_0x222e7e,null))[_0x3d41('0x3a')](function(_0x1e5801){if(_0x1e5801){_0x301599=_0x1e5801;_0x573264[_0x3d41('0x4a')]=_[_0x3d41('0x5e')](db[_0x3d41('0x75')]['rawAttributes']);_0x573264[_0x3d41('0x47')]=_[_0x3d41('0x5e')](_0x1bce9f[_0x3d41('0x47')]);_0x573264[_0x3d41('0x48')]=_[_0x3d41('0x49')](_0x573264[_0x3d41('0x4a')],_0x573264[_0x3d41('0x47')]);_0x141fcd[_0x3d41('0x4b')]=_['intersection'](_0x573264[_0x3d41('0x4a')],qs['fields'](_0x1bce9f[_0x3d41('0x47')][_0x3d41('0x4c')]));_0x141fcd[_0x3d41('0x4b')]=_0x141fcd['attributes']['length']?_0x141fcd[_0x3d41('0x4b')]:_0x573264[_0x3d41('0x4a')];_0x141fcd['order']=qs[_0x3d41('0x50')](_0x1bce9f[_0x3d41('0x47')][_0x3d41('0x50')]);_0x141fcd[_0x3d41('0x51')]=qs[_0x3d41('0x48')](_['pick'](_0x1bce9f['query'],_0x573264['filters']));if(_0x1bce9f[_0x3d41('0x47')][_0x3d41('0x53')]){_0x141fcd[_0x3d41('0x51')]=_[_0x3d41('0x55')](_0x141fcd[_0x3d41('0x51')],{'$or':_[_0x3d41('0x41')](_0x141fcd[_0x3d41('0x4b')],function(_0x44bc0c){var _0x44b3cf={};_0x44b3cf[_0x44bc0c]={'$like':'%'+_0x1bce9f['query'][_0x3d41('0x53')]+'%'};return _0x44b3cf;})});}_0x141fcd=_[_0x3d41('0x55')]({},_0x141fcd,_0x1bce9f[_0x3d41('0x56')]);return _0x301599[_0x3d41('0xbc')](_0x141fcd);}})[_0x3d41('0x3a')](function(_0x5c8f72){if(_0x5c8f72){_0x58f945=_0x5c8f72[_0x3d41('0x5f')];if(!_0x1bce9f[_0x3d41('0x47')][_0x3d41('0x4d')](_0x3d41('0x4e'))){_0x141fcd[_0x3d41('0x30')]=qs[_0x3d41('0x30')](_0x1bce9f[_0x3d41('0x47')]['limit']);_0x141fcd[_0x3d41('0x2e')]=qs[_0x3d41('0x2e')](_0x1bce9f[_0x3d41('0x47')][_0x3d41('0x2e')]);}return _0x301599[_0x3d41('0xbc')](_0x141fcd);}})[_0x3d41('0x3a')](function(_0xf9d969){if(_0xf9d969){return _0xf9d969?{'count':_0x58f945,'rows':_0xf9d969}:null;}})[_0x3d41('0x3a')](respondWithResult(_0x222e7e,null))[_0x3d41('0x5a')](handleError(_0x222e7e,null));};exports[_0x3d41('0xbd')]=function(_0x9e0074,_0xf95431,_0x3756a9){var _0x3538a6={'raw':![],'where':{}};var _0x428476={};var _0x48af61={'count':0x0,'rows':[]};return db['SmsAccount'][_0x3d41('0x6b')]({'where':{'id':_0x9e0074['params']['id']}})[_0x3d41('0x3a')](handleEntityNotFound(_0xf95431,null))[_0x3d41('0x3a')](function(_0x32098e){if(_0x32098e){_0x428476['model']=_[_0x3d41('0x5e')](db['SmsInteraction'][_0x3d41('0x43')]);_0x428476[_0x3d41('0x47')]=_[_0x3d41('0x5e')](_0x9e0074[_0x3d41('0x47')]);_0x428476[_0x3d41('0x48')]=_['intersection'](_0x428476[_0x3d41('0x4a')],_0x428476[_0x3d41('0x47')]);_0x3538a6[_0x3d41('0x4b')]=_[_0x3d41('0x49')](_0x428476['model'],qs['fields'](_0x9e0074[_0x3d41('0x47')][_0x3d41('0x4c')]));_0x3538a6[_0x3d41('0x4b')]=_0x3538a6[_0x3d41('0x4b')]['length']?_0x3538a6[_0x3d41('0x4b')]:_0x428476[_0x3d41('0x4a')];if(!_0x9e0074[_0x3d41('0x47')]['hasOwnProperty'](_0x3d41('0x4e'))){_0x3538a6['limit']=qs[_0x3d41('0x30')](_0x9e0074[_0x3d41('0x47')][_0x3d41('0x30')]);_0x3538a6[_0x3d41('0x2e')]=qs['offset'](_0x9e0074[_0x3d41('0x47')][_0x3d41('0x2e')]);}_0x3538a6[_0x3d41('0x4f')]=qs[_0x3d41('0x50')](_0x9e0074[_0x3d41('0x47')]['sort']);_0x3538a6[_0x3d41('0x51')]=qs[_0x3d41('0x48')](_[_0x3d41('0x52')](_0x9e0074[_0x3d41('0x47')],_0x428476[_0x3d41('0x48')]));_0x3538a6[_0x3d41('0x51')]['SmsAccountId']=_0x32098e['id'];if(_0x9e0074[_0x3d41('0x47')][_0x3d41('0x53')]){_0x3538a6[_0x3d41('0x51')]=_[_0x3d41('0x55')](_0x3538a6['where'],{'$or':_[_0x3d41('0x41')](_0x3538a6[_0x3d41('0x4b')],function(_0x3239ad){var _0x610c86={};_0x610c86[_0x3239ad]={'$like':'%'+_0x9e0074[_0x3d41('0x47')][_0x3d41('0x53')]+'%'};return _0x610c86;})});}_0x3538a6=_[_0x3d41('0x55')]({},_0x3538a6,_0x9e0074['options']);return db[_0x3d41('0xa3')][_0x3d41('0x31')]({'where':_0x3538a6['where']})[_0x3d41('0x3a')](function(_0x2bf53e){_0x48af61[_0x3d41('0x31')]=_0x2bf53e;if(_0x9e0074[_0x3d41('0x47')][_0x3d41('0x57')]){_0x3538a6[_0x3d41('0x58')]=[{'model':db['CmContact'],'as':_0x3d41('0xbe'),'required':![]},{'model':db[_0x3d41('0xae')],'as':_0x3d41('0xbf'),'attributes':[_0x3d41('0x3d'),_0x3d41('0xc0'),'internal'],'required':![]},{'model':db[_0x3d41('0xc1')],'as':_0x3d41('0xc2'),'attributes':['id',_0x3d41('0x3d'),_0x3d41('0xc3')],'where':_0x9e0074[_0x3d41('0x47')][_0x3d41('0xc4')]?{'id':_0x9e0074[_0x3d41('0x47')][_0x3d41('0xc4')]}:undefined,'required':_0x9e0074[_0x3d41('0x47')][_0x3d41('0xc4')]?!![]:![]}];}return db[_0x3d41('0xa3')][_0x3d41('0x6c')](_0x3538a6);})[_0x3d41('0x3a')](function(_0x59ff99){_0x48af61[_0x3d41('0x59')]=_0x59ff99;return _0x48af61;});}})[_0x3d41('0x3a')](respondWithFilteredResult(_0xf95431,_0x3538a6))[_0x3d41('0x5a')](handleError(_0xf95431,null));};exports[_0x3d41('0x3e')]=function(_0xa9b9d8,_0x4c94ca,_0x649c78){var _0x5b93a0,_0x729064,_0x225074;if(_[_0x3d41('0x98')](_0xa9b9d8['body'][_0x3d41('0x62')])||_0xa9b9d8['body'][_0x3d41('0x62')]===''){throw new db[(_0x3d41('0x9b'))][(_0x3d41('0x7b'))](_0x3d41('0x99'));}if(_[_0x3d41('0x98')](_0xa9b9d8[_0x3d41('0x62')]['to'])){throw new db[(_0x3d41('0x9b'))][(_0x3d41('0x7b'))]('to\x20is\x20mandatory');}_0x5b93a0=_0xa9b9d8[_0x3d41('0x62')]['to'];return db[_0x3d41('0x42')][_0x3d41('0x60')]({'where':{'id':_0xa9b9d8[_0x3d41('0x5c')]['id']},'include':[{'model':db[_0x3d41('0x9f')],'as':_0x3d41('0xc5'),'include':[{'model':db['CmContact'],'as':_0x3d41('0xa2'),'where':{'mobile':_0x5b93a0},'limit':0x1,'order':[['updatedAt',_0x3d41('0xc6')]]}]}]})[_0x3d41('0x3a')](handleEntityNotFound(_0x4c94ca,null))[_0x3d41('0x3a')](function(_0x3471f3){if(_0x3471f3&&_0x3471f3[_0x3d41('0xc5')]){_0x729064=_0x3471f3['get']({'plain':!![]});if(_0x729064[_0x3d41('0xc5')]&&_0x729064[_0x3d41('0xc5')][_0x3d41('0xa2')][_0x3d41('0x5f')]){return _0x729064[_0x3d41('0xc5')][_0x3d41('0xa2')][0x0];}return db['CmContact'][_0x3d41('0x61')](_[_0x3d41('0x1c')](_0xa9b9d8[_0x3d41('0x62')],{'firstName':_0x5b93a0,'mobile':_0x5b93a0,'phone':_0x5b93a0,'ListId':_0x729064['ListId']}));}})[_0x3d41('0x3a')](handleEntityNotFound(_0x4c94ca,null))['then'](function(_0x215719){if(_0x215719){_0x225074=_0x215719;return db[_0x3d41('0xa3')][_0x3d41('0x60')]({'where':{'ContactId':_0x225074['id'],'closed':![],'SmsAccountId':_0x729064['id']}})[_0x3d41('0x3a')](function(_0x2815ea){if(_0x2815ea){return[_0x2815ea,![]];}return db[_0x3d41('0xa3')]['create']({'UserId':_0xa9b9d8[_0x3d41('0x64')]['id'],'ContactId':_0x225074['id'],'SmsAccountId':_0x729064['id'],'phone':_0x5b93a0,'firstMsgDirection':_0x3d41('0xc7'),'Messages':[_[_0x3d41('0x55')](_0xa9b9d8[_0x3d41('0x62')],{'read':![],'body':_0xa9b9d8['body']['body'],'phone':_0x5b93a0,'SmsAccountId':_0x729064['id'],'UserId':_0xa9b9d8[_0x3d41('0x64')]['id'],'ContactId':_0x225074['id']})]},{'include':[{'model':db[_0x3d41('0xab')],'as':_0x3d41('0xc8')}]})[_0x3d41('0x3a')](function(_0x336709){return[_0x336709,!![]];});});}})['spread'](function(_0x40f34e,_0x14c6dd){if(_0x14c6dd){return _0x40f34e;}return db[_0x3d41('0xab')][_0x3d41('0x61')](_['merge'](_0xa9b9d8[_0x3d41('0x62')],{'read':![],'body':_0xa9b9d8[_0x3d41('0x62')]['body'],'phone':_0x5b93a0,'SmsAccountId':_0x729064['id'],'SmsInteractionId':_0x40f34e['id'],'UserId':_0xa9b9d8[_0x3d41('0x64')]['id'],'ContactId':_0x225074['id']}))[_0x3d41('0x3a')](function(_0x176118){_0x40f34e['dataValues']['Messages']=[];_0x40f34e[_0x3d41('0xc9')]['Messages'][_0x3d41('0xa8')](_0x176118[_0x3d41('0xc9')]);return _0x40f34e;});})['then'](respondWithResult(_0x4c94ca,null))[_0x3d41('0x5a')](handleError(_0x4c94ca,null));};exports['status']=function(_0x3ee260,_0x4531f2,_0x1d13e8){if(_0x3ee260[_0x3d41('0x62')]['id']){delete _0x3ee260[_0x3d41('0x62')]['id'];}var _0x224821,_0x545e57,_0x552d27;return db[_0x3d41('0x42')][_0x3d41('0x60')]({'where':{'id':_0x3ee260[_0x3d41('0x5c')]['id']}})['then'](handleEntityNotFound(_0x4531f2,null))['then'](function(_0x33d5c9){if(_0x33d5c9){_0x552d27=_0x33d5c9;logger[_0x3d41('0xb5')](_0x3d41('0x26'),_0x3d41('0x2c'),_0x3d41('0xca'),_0x33d5c9[_0x3d41('0x45')],util[_0x3d41('0xcb')](_0x3ee260['body'],{'showHidden':![],'depth':null}));switch(_0x33d5c9['type']){case'skebby':if(_0x3ee260[_0x3d41('0x62')][_0x3d41('0xcc')]&&_0x3ee260['body'][_0x3d41('0x2c')]){_0x545e57=_0x3ee260[_0x3d41('0x62')]['status'];return db[_0x3d41('0xab')][_0x3d41('0x60')]({'where':{'messageId':_0x3ee260[_0x3d41('0x62')][_0x3d41('0xcc')]}});}break;case _0x3d41('0x87'):if(_0x3ee260['body']['requestid']&&_0x3ee260[_0x3d41('0x62')]['statusInt']){_0x545e57=_0x3ee260['body'][_0x3d41('0xcd')];return db['SmsMessage'][_0x3d41('0x60')]({'where':{'messageId':_0x3ee260['body'][_0x3d41('0xce')]}});}break;case _0x3d41('0x88'):if(_0x3ee260[_0x3d41('0x62')][_0x3d41('0x89')]&&_0x3ee260[_0x3d41('0x62')]['status_code']){_0x545e57=_0x3ee260['body'][_0x3d41('0xcf')];return db[_0x3d41('0xab')][_0x3d41('0x60')]({'where':{'messageId':_0x3ee260[_0x3d41('0x62')][_0x3d41('0x89')]}});}break;case _0x3d41('0x8d'):var _0x459bc7=_0x3ee260['body'][_0x3d41('0x8e')][0x0];if(_0x459bc7[_0x3d41('0x84')]&&_0x459bc7['status'][_0x3d41('0xd0')]===_0x3d41('0xd1')){_0x545e57=_0x459bc7[_0x3d41('0x2c')][_0x3d41('0xd0')];return db[_0x3d41('0xab')][_0x3d41('0x60')]({'where':{'messageId':_0x459bc7[_0x3d41('0x84')]}});}break;case _0x3d41('0x92'):if(_0x3ee260['body'][_0x3d41('0x84')]&&_0x3ee260[_0x3d41('0x62')][_0x3d41('0x2c')]){_0x545e57=_0x3ee260[_0x3d41('0x62')][_0x3d41('0x2c')];return db['SmsMessage'][_0x3d41('0x60')]({'where':{'messageId':_0x3ee260[_0x3d41('0x62')][_0x3d41('0x84')]}});}break;case _0x3d41('0x96'):if(_0x3ee260[_0x3d41('0x62')][0x0]['message']['id']){_0x545e57=_0x3ee260[_0x3d41('0x62')][0x0][_0x3d41('0x45')];return db[_0x3d41('0xab')]['find']({'where':{'messageId':_0x3ee260[_0x3d41('0x62')][0x0][_0x3d41('0x2a')]['id']}});}break;case _0x3d41('0x97'):if(_0x3ee260[_0x3d41('0x62')][_0x3d41('0xd2')]&&_0x3ee260['body'][_0x3d41('0x2c')]){_0x545e57=_0x3ee260[_0x3d41('0x62')][_0x3d41('0x2c')];return db['SmsMessage'][_0x3d41('0x60')]({'where':{'messageId':_0x3ee260[_0x3d41('0x62')][_0x3d41('0xd2')]}});}break;}}})[_0x3d41('0x3a')](handleEntityNotFound(_0x4531f2,null))[_0x3d41('0x3a')](function(_0xed37de){if(_0xed37de){_0x224821=_0xed37de;switch(_0x552d27['type']){case'skebby':if(_0x545e57===_0x3d41('0xd1')){return _0x224821[_0x3d41('0x36')]({'read':!![]});}else{logger[_0x3d41('0x28')]('SmsAccount,\x20%s,\x20%s,\x20%s','status',_0x3d41('0x85'),util['inspect'](_0x3ee260['body'],{'showHidden':![],'depth':null}));}break;case _0x3d41('0x87'):if(_0x545e57==='1'){return _0x224821[_0x3d41('0x36')]({'read':!![]});}else{logger['error'](_0x3d41('0x26'),_0x3d41('0x2c'),_0x3d41('0x85'),util[_0x3d41('0xcb')](_0x3ee260[_0x3d41('0x62')],{'showHidden':![],'depth':null}));}break;case'clicksend':if(!_0x3ee260[_0x3d41('0x62')]['error_code']&&_0x545e57>=0xc8&&_0x545e57<=0xce){return _0x224821[_0x3d41('0x36')]({'read':!![]});}else{logger[_0x3d41('0x28')](_0x3d41('0x26'),_0x3d41('0x2c'),_0x3d41('0x88'),util[_0x3d41('0xcb')](_0x3ee260[_0x3d41('0x62')],{'showHidden':![],'depth':null}));}break;case _0x3d41('0x8d'):var _0x589001=_0x3ee260['body'][_0x3d41('0x8e')][0x0];if(_0x589001[_0x3d41('0x2c')][_0x3d41('0xd0')]===_0x3d41('0xd1')){return _0x224821[_0x3d41('0x36')]({'read':!![]});}else{logger['error'](_0x3d41('0x26'),_0x3d41('0x2c'),'infobip',util['inspect'](_0x3ee260[_0x3d41('0x62')],{'showHidden':![],'depth':null}));}break;case _0x3d41('0x92'):if(_0x545e57==_0x3d41('0xd3')||_0x545e57=='RECEIVED_BY_RECIPIENT'){return _0x224821['update']({'read':!![]});}else{logger[_0x3d41('0x28')]('SmsAccount,\x20%s,\x20%s,\x20%s',_0x3d41('0x2c'),_0x3d41('0x92'),util[_0x3d41('0xcb')](_0x3ee260[_0x3d41('0x62')],{'showHidden':![],'depth':null}));}break;case _0x3d41('0x96'):if(_0x545e57==_0x3d41('0xd4')){return _0x224821['update']({'read':!![]});}else{logger[_0x3d41('0x28')]('SmsAccount,\x20%s,\x20%s,\x20%s','status',_0x3d41('0x96'),util[_0x3d41('0xcb')](_0x3ee260[_0x3d41('0x62')],{'showHidden':![],'depth':null}));}break;case'intelepeer':if(_0x545e57==='DELIVRD'||_0x545e57===_0x3d41('0xd5')){return _0x224821[_0x3d41('0x36')]({'read':!![]});}else{logger['error'](_0x3d41('0x26'),_0x3d41('0x2c'),_0x3d41('0x97'),util[_0x3d41('0xcb')](_0x3ee260[_0x3d41('0x62')],{'showHidden':![],'depth':null}));}break;}return _0x224821;}})[_0x3d41('0x3a')](respondWithResult(_0x4531f2,null))[_0x3d41('0x5a')](handleError(_0x4531f2,null));};exports[_0x3d41('0xd6')]=function(_0x4a39ed,_0x46e74b,_0x399f77){var _0x4cac12,_0x1744ad,_0x11594e;return db[_0x3d41('0x42')][_0x3d41('0x60')]({'where':{'id':_0x4a39ed[_0x3d41('0x5c')]['id']}})[_0x3d41('0x3a')](handleEntityNotFound(_0x46e74b,null))[_0x3d41('0x3a')](function(_0x4533cb){if(_0x4533cb){_0x11594e=_0x4533cb;logger[_0x3d41('0xb5')](_0x3d41('0x26'),_0x3d41('0x2c'),_0x3d41('0xca'),_0x4533cb[_0x3d41('0x45')],util['inspect'](_0x4a39ed[_0x3d41('0x47')],{'showHidden':![],'depth':null}));switch(_0x4533cb[_0x3d41('0x45')]){case _0x3d41('0x8f'):if(_0x4a39ed[_0x3d41('0x47')][_0x3d41('0xd7')]&&_0x4a39ed['query'][_0x3d41('0xd8')]){_0x1744ad=_0x4a39ed[_0x3d41('0x47')][_0x3d41('0xd8')];return db[_0x3d41('0xab')]['find']({'where':{'messageId':_0x4a39ed[_0x3d41('0x47')]['message-id']}});}break;}}})[_0x3d41('0x3a')](handleEntityNotFound(_0x46e74b,null))[_0x3d41('0x3a')](function(_0x45b513){if(_0x45b513){_0x4cac12=_0x45b513;switch(_0x11594e[_0x3d41('0x45')]){case'csc':if(_0x1744ad==='2'||_0x1744ad==='6'){return _0x4cac12[_0x3d41('0x36')]({'read':!![]});}else{logger[_0x3d41('0x28')]('SmsAccount,\x20%s,\x20%s,\x20%s',_0x3d41('0x2c'),'csc',util[_0x3d41('0xcb')](_0x4a39ed[_0x3d41('0x47')],{'showHidden':![],'depth':null}));};break;}return _0x4cac12;}})[_0x3d41('0x3a')](respondWithResult(_0x46e74b,null))[_0x3d41('0x5a')](handleError(_0x46e74b,null));};exports['addAgents']=function(_0x5e608d,_0x4653f0,_0x257b49){return db[_0x3d41('0x42')][_0x3d41('0x60')]({'where':{'id':_0x5e608d[_0x3d41('0x5c')]['id']}})[_0x3d41('0x3a')](handleEntityNotFound(_0x4653f0,null))[_0x3d41('0x3a')](function(_0x177a44){if(_0x177a44){return _0x177a44['addAgents'](_0x5e608d[_0x3d41('0x62')][_0x3d41('0x6e')],_['omit'](_0x5e608d[_0x3d41('0x62')],['ids','id'])||{})[_0x3d41('0xa4')](function(_0x2194a0){for(var _0x525658=0x0;_0x525658<_0x5e608d[_0x3d41('0x62')][_0x3d41('0x6e')][_0x3d41('0x5f')];_0x525658+=0x1){socket[_0x3d41('0xd9')](_0x3d41('0xda'),{'UserId':Number(_0x5e608d['body']['ids'][_0x525658]),'SmsAccountId':Number(_0x5e608d[_0x3d41('0x5c')]['id'])});}return _0x2194a0;});}})['then'](respondWithResult(_0x4653f0,null))[_0x3d41('0x5a')](handleError(_0x4653f0,null));};exports[_0x3d41('0xdb')]=function(_0xdd6b36,_0x104e01,_0x22fde7){return db[_0x3d41('0x42')][_0x3d41('0x60')]({'where':{'id':_0xdd6b36[_0x3d41('0x5c')]['id']}})['then'](handleEntityNotFound(_0x104e01,null))[_0x3d41('0x3a')](function(_0x21249e){if(_0x21249e){return _0x21249e[_0x3d41('0xdb')](_0xdd6b36[_0x3d41('0x47')][_0x3d41('0x6e')])[_0x3d41('0x3a')](function(){if(_[_0x3d41('0xdc')](_0xdd6b36[_0x3d41('0x47')]['ids'])){for(var _0x20e624=0x0;_0x20e624<_0xdd6b36[_0x3d41('0x47')][_0x3d41('0x6e')]['length'];_0x20e624+=0x1){socket['emit']('userSmsAccount:remove',{'UserId':Number(_0xdd6b36[_0x3d41('0x47')]['ids'][_0x20e624]),'SmsAccountId':Number(_0xdd6b36[_0x3d41('0x5c')]['id'])});}}else{socket['emit'](_0x3d41('0xdd'),{'UserId':Number(_0xdd6b36[_0x3d41('0x47')][_0x3d41('0x6e')]),'SmsAccountId':Number(_0xdd6b36[_0x3d41('0x5c')]['id'])});}});}})[_0x3d41('0x3a')](respondWithStatusCode(_0x104e01,null))['catch'](handleError(_0x104e01,null));};exports['getAgents']=function(_0x21e690,_0x33d674,_0x5e3548){var _0x44a291={};var _0x10aa03={};var _0x454a61;var _0x5f466b;return db['SmsAccount'][_0x3d41('0x6b')]({'where':{'id':_0x21e690[_0x3d41('0x5c')]['id']}})[_0x3d41('0x3a')](handleEntityNotFound(_0x33d674,null))['then'](function(_0x2f5c7b){if(_0x2f5c7b){_0x454a61=_0x2f5c7b;_0x10aa03[_0x3d41('0x4a')]=_[_0x3d41('0x5e')](db[_0x3d41('0xae')][_0x3d41('0x43')]);_0x10aa03[_0x3d41('0x47')]=_[_0x3d41('0x5e')](_0x21e690[_0x3d41('0x47')]);_0x10aa03[_0x3d41('0x48')]=_[_0x3d41('0x49')](_0x10aa03[_0x3d41('0x4a')],_0x10aa03[_0x3d41('0x47')]);_0x44a291[_0x3d41('0x4b')]=_[_0x3d41('0x49')](_0x10aa03[_0x3d41('0x4a')],qs[_0x3d41('0x4c')](_0x21e690[_0x3d41('0x47')]['fields']));_0x44a291[_0x3d41('0x4b')]=_0x44a291[_0x3d41('0x4b')][_0x3d41('0x5f')]?_0x44a291['attributes']:_0x10aa03['model'];_0x44a291[_0x3d41('0x4f')]=qs[_0x3d41('0x50')](_0x21e690['query'][_0x3d41('0x50')]);_0x44a291[_0x3d41('0x51')]=qs[_0x3d41('0x48')](_[_0x3d41('0x52')](_0x21e690[_0x3d41('0x47')],_0x10aa03[_0x3d41('0x48')]));if(_0x21e690[_0x3d41('0x47')][_0x3d41('0x53')]){_0x44a291[_0x3d41('0x51')]=_[_0x3d41('0x55')](_0x44a291['where'],{'$or':_[_0x3d41('0x41')](_0x44a291[_0x3d41('0x4b')],function(_0x4ed9d6){var _0x17f956={};_0x17f956[_0x4ed9d6]={'$like':'%'+_0x21e690['query'][_0x3d41('0x53')]+'%'};return _0x17f956;})});}_0x44a291=_[_0x3d41('0x55')]({},_0x44a291,_0x21e690['options']);return _0x454a61[_0x3d41('0xde')](_0x44a291);}})['then'](function(_0xdb1775){if(_0xdb1775){_0x5f466b=_0xdb1775['length'];if(!_0x21e690['query'][_0x3d41('0x4d')](_0x3d41('0x4e'))){_0x44a291[_0x3d41('0x30')]=qs['limit'](_0x21e690[_0x3d41('0x47')][_0x3d41('0x30')]);_0x44a291[_0x3d41('0x2e')]=qs[_0x3d41('0x2e')](_0x21e690[_0x3d41('0x47')][_0x3d41('0x2e')]);}return _0x454a61[_0x3d41('0xde')](_0x44a291);}})[_0x3d41('0x3a')](function(_0x49fa9a){if(_0x49fa9a){return _0x49fa9a?{'count':_0x5f466b,'rows':_0x49fa9a}:null;}})['then'](respondWithResult(_0x33d674,null))[_0x3d41('0x5a')](handleError(_0x33d674,null));};