05d73c04f1e980b0838d22516ef7c26dc58dd155
[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 _0x71cf=['DELIVERED','error_code','RECEIVED_BY_RECIPIENT','message-delivered','DELIVRD','ACCEPTD','state','addAgents','emit','removeAgents','userSmsAccount:remove','getAgents','eml-format','zip-dir','request-promise','moment','bluebird','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','redis','defaults','localhost','./smsAccount.socket','register','jayson/promise','http','request','then','info','SmsAccount,\x20%s,\x20%s','request\x20sent','debug','error','code','message','catch','sendStatus','status','json','undefined','limit','offset','set','apply','reject','save','destroy','get','SmsAccounts','UserProfileResource','end','stack','name','send','Pause','mandatoryDispositionPause','SmsAccount','fieldName','type','key','model','map','keys','query','intersection','attributes','fields','hasOwnProperty','nolimit','sort','where','filters','filter','merge','VIRTUAL','options','count','includeAll','include','findAll','rows','params','rawAttributes','create','body','user','UserProfileSection','find','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','describe','addDisposition','SmsAccountId','Disposition','getDispositions','length','removeDispositions','ids','addAnswer','findOne','order','pick','CannedAnswer','removeAnswers','notify','sms','SmsApplication','Applications','account','applications','orderBy','priority','asc','CmContact','Sequelize','ValidationError','difference','updatedAt','createdAt','CompanyId','mobile','twilio','from','From','messageId','MessageSid','skebby','sender','text','connectel','message_id','plivo','Text','MessageUUID','results','csc','originatingAddress','segmentedMessage','clickatell','fromNumber','intelepeer','refid','isNil','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','ListId','Contacts','DESC','omit','SmsInteraction','interaction','autoclose','push','close','unmanaged','*,*,*,*','created','ContactId','AttachmentId','UserId','unshift','agent','Start','startRouting','receiveMessage','SmsAccount,\x20%s,\x20%s,\x20%s','SmsAccount,\x20%s,\x20%s,\x20%s,\x20%s','entity\x20found','stringify','Content-Type','text/xml','<?xml\x20version=\x221.0\x22\x20encoding=\x22UTF-8\x22?>\x0a<Response></Response>','sequelize','transaction','Transaction','ISOLATION_LEVELS','findAndCountAll','finally','getApplications','Contact','User','Owner','fullname','internal','Tag','Tags','color','tag','CmList','List','out','Messages','spread','dataValues','received\x20delivery\x20report\x20from','inspect','skebby_dispatch_id','statusInt','SmsMessage','requestid','clicksend','status_code','infobip','groupName','bandwidth','redif'];(function(_0x4bd06e,_0x593701){var _0x85ebb=function(_0x26a7f2){while(--_0x26a7f2){_0x4bd06e['push'](_0x4bd06e['shift']());}};_0x85ebb(++_0x593701);}(_0x71cf,0x1d8));var _0xf71c=function(_0x416cdc,_0x213b87){_0x416cdc=_0x416cdc-0x0;var _0x1931ce=_0x71cf[_0x416cdc];return _0x1931ce;};'use strict';var emlformat=require(_0xf71c('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xf71c('0x1'));var jsonpatch=require('fast-json-patch');var rp=require(_0xf71c('0x2'));var moment=require(_0xf71c('0x3'));var BPromise=require(_0xf71c('0x4'));var Mustache=require('mustache');var util=require(_0xf71c('0x5'));var path=require(_0xf71c('0x6'));var sox=require(_0xf71c('0x7'));var csv=require(_0xf71c('0x8'));var ejs=require(_0xf71c('0x9'));var fs=require('fs');var fs_extra=require(_0xf71c('0xa'));var _=require(_0xf71c('0xb'));var squel=require(_0xf71c('0xc'));var crypto=require(_0xf71c('0xd'));var jsforce=require(_0xf71c('0xe'));var deskjs=require(_0xf71c('0xf'));var toCsv=require(_0xf71c('0x8'));var querystring=require(_0xf71c('0x10'));var Papa=require(_0xf71c('0x11'));var Redis=require(_0xf71c('0x12'));var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require(_0xf71c('0x13'));var hardwareService=require(_0xf71c('0x14'));var logger=require(_0xf71c('0x15'))(_0xf71c('0x16'));var utils=require('../../config/utils');var config=require(_0xf71c('0x17'));var licenseUtil=require(_0xf71c('0x18'));var db=require(_0xf71c('0x19'))['db'];config[_0xf71c('0x1a')]=_[_0xf71c('0x1b')](config[_0xf71c('0x1a')],{'host':_0xf71c('0x1c'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0xf71c('0x1a')]));require(_0xf71c('0x1d'))[_0xf71c('0x1e')](socket);var jayson=require(_0xf71c('0x1f'));var client=jayson['client'][_0xf71c('0x20')]({'port':0x232c});function respondWithRpcPromise(_0xc216ad,_0x411974,_0x545d08,_0x1bcabb){return new BPromise(function(_0x429bdf,_0xc01e86){var _0x952ca3=_0x1bcabb||client;return _0x952ca3[_0xf71c('0x21')](_0xc216ad,_0x545d08)[_0xf71c('0x22')](function(_0x4d1e87){logger[_0xf71c('0x23')](_0xf71c('0x24'),_0x411974,_0xf71c('0x25'));logger[_0xf71c('0x26')]('SmsAccount,\x20%s,\x20%s,\x20%s',_0x411974,_0xf71c('0x25'),JSON['stringify'](_0x4d1e87));if(_0x4d1e87[_0xf71c('0x27')]){if(_0x4d1e87[_0xf71c('0x27')][_0xf71c('0x28')]===0x1f4){logger[_0xf71c('0x27')](_0xf71c('0x24'),_0x411974,_0x4d1e87[_0xf71c('0x27')][_0xf71c('0x29')]);return _0xc01e86(_0x4d1e87[_0xf71c('0x27')][_0xf71c('0x29')]);}logger[_0xf71c('0x27')](_0xf71c('0x24'),_0x411974,_0x4d1e87[_0xf71c('0x27')][_0xf71c('0x29')]);return _0x429bdf(_0x4d1e87[_0xf71c('0x27')][_0xf71c('0x29')]);}else{logger[_0xf71c('0x23')](_0xf71c('0x24'),_0x411974,'request\x20sent');_0x429bdf(_0x4d1e87['result']['message']);}})[_0xf71c('0x2a')](function(_0x1bbc93){logger['error'](_0xf71c('0x24'),_0x411974,_0x1bbc93);_0xc01e86(_0x1bbc93);});});}function respondWithStatusCode(_0x31b8e6,_0x4e21a3){_0x4e21a3=_0x4e21a3||0xcc;return function(_0x1f9f13){if(_0x1f9f13){return _0x31b8e6[_0xf71c('0x2b')](_0x4e21a3);}return _0x31b8e6[_0xf71c('0x2c')](_0x4e21a3)['end']();};}function respondWithResult(_0x81c5b7,_0x5d65d1){_0x5d65d1=_0x5d65d1||0xc8;return function(_0xf33226){if(_0xf33226){return _0x81c5b7[_0xf71c('0x2c')](_0x5d65d1)[_0xf71c('0x2d')](_0xf33226);}};}function respondWithFilteredResult(_0x549ca7,_0x1cc5b7){return function(_0x1a3c14){if(_0x1a3c14){var _0x5923cb=typeof _0x1cc5b7['offset']===_0xf71c('0x2e')&&typeof _0x1cc5b7[_0xf71c('0x2f')]===_0xf71c('0x2e');var _0x3ecba7=_0x1a3c14['count'];var _0x410210=_0x5923cb?0x0:_0x1cc5b7['offset'];var _0x267028=_0x5923cb?_0x1a3c14['count']:_0x1cc5b7[_0xf71c('0x30')]+_0x1cc5b7[_0xf71c('0x2f')];var _0x472ff1;if(_0x267028>=_0x3ecba7){_0x267028=_0x3ecba7;_0x472ff1=0xc8;}else{_0x472ff1=0xce;}_0x549ca7[_0xf71c('0x2c')](_0x472ff1);return _0x549ca7[_0xf71c('0x31')]('Content-Range',_0x410210+'-'+_0x267028+'/'+_0x3ecba7)[_0xf71c('0x2d')](_0x1a3c14);}return null;};}function patchUpdates(_0x4141d3){return function(_0x120e60){try{jsonpatch[_0xf71c('0x32')](_0x120e60,_0x4141d3,!![]);}catch(_0x15dd94){return BPromise[_0xf71c('0x33')](_0x15dd94);}return _0x120e60[_0xf71c('0x34')]();};}function saveUpdates(_0x19a625,_0x42980d){return function(_0x22fe20){if(_0x22fe20){return _0x22fe20['update'](_0x19a625)[_0xf71c('0x22')](function(_0x102b2f){return _0x102b2f;});}return null;};}function removeEntity(_0x47b080,_0x1a0d12){return function(_0x20a7e8){if(_0x20a7e8){return _0x20a7e8[_0xf71c('0x35')]()[_0xf71c('0x22')](function(){var _0x53cdf0=_0x20a7e8[_0xf71c('0x36')]({'plain':!![]});var _0x16a031=_0xf71c('0x37');return db[_0xf71c('0x38')][_0xf71c('0x35')]({'where':{'type':_0x16a031,'resourceId':_0x53cdf0['id']}})[_0xf71c('0x22')](function(){return _0x20a7e8;});})[_0xf71c('0x22')](function(){_0x47b080[_0xf71c('0x2c')](0xcc)[_0xf71c('0x39')]();});}};}function handleEntityNotFound(_0xdcf687,_0xa7ef20){return function(_0x5268b4){if(!_0x5268b4){_0xdcf687[_0xf71c('0x2b')](0x194);}return _0x5268b4;};}function handleError(_0x2604ac,_0x261de7){_0x261de7=_0x261de7||0x1f4;return function(_0x5f1f8d){logger[_0xf71c('0x27')](_0x5f1f8d[_0xf71c('0x3a')]);if(_0x5f1f8d[_0xf71c('0x3b')]){delete _0x5f1f8d[_0xf71c('0x3b')];}_0x2604ac[_0xf71c('0x2c')](_0x261de7)[_0xf71c('0x3c')](_0x5f1f8d);};}exports['index']=function(_0x37ee21,_0x5ee090){var _0x27d3f0={'include':[{'model':db[_0xf71c('0x3d')],'as':_0xf71c('0x3e')}]},_0x5026f1={},_0x474338={'count':0x0,'rows':[]};var _0x1f2ac3=_['map'](db[_0xf71c('0x3f')]['rawAttributes'],function(_0x1a9793){return{'name':_0x1a9793[_0xf71c('0x40')],'type':_0x1a9793[_0xf71c('0x41')][_0xf71c('0x42')]};});_0x5026f1[_0xf71c('0x43')]=_[_0xf71c('0x44')](_0x1f2ac3,_0xf71c('0x3b'));_0x5026f1['query']=_[_0xf71c('0x45')](_0x37ee21[_0xf71c('0x46')]);_0x5026f1['filters']=_[_0xf71c('0x47')](_0x5026f1['model'],_0x5026f1[_0xf71c('0x46')]);_0x27d3f0[_0xf71c('0x48')]=_['intersection'](_0x5026f1['model'],qs[_0xf71c('0x49')](_0x37ee21['query']['fields']));_0x27d3f0[_0xf71c('0x48')]=_0x27d3f0[_0xf71c('0x48')]['length']?_0x27d3f0[_0xf71c('0x48')]:_0x5026f1[_0xf71c('0x43')];if(!_0x37ee21[_0xf71c('0x46')][_0xf71c('0x4a')](_0xf71c('0x4b'))){_0x27d3f0[_0xf71c('0x2f')]=qs['limit'](_0x37ee21[_0xf71c('0x46')]['limit']);_0x27d3f0['offset']=qs['offset'](_0x37ee21[_0xf71c('0x46')][_0xf71c('0x30')]);}_0x27d3f0['order']=qs[_0xf71c('0x4c')](_0x37ee21[_0xf71c('0x46')]['sort']);_0x27d3f0[_0xf71c('0x4d')]=qs[_0xf71c('0x4e')](_['pick'](_0x37ee21[_0xf71c('0x46')],_0x5026f1['filters']),_0x1f2ac3);if(_0x37ee21[_0xf71c('0x46')][_0xf71c('0x4f')]){_0x27d3f0[_0xf71c('0x4d')]=_[_0xf71c('0x50')](_0x27d3f0['where'],{'$or':_['map'](_0x1f2ac3,function(_0x2dba5c){if(_0x2dba5c['type']!==_0xf71c('0x51')){var _0x17e3ef={};_0x17e3ef[_0x2dba5c[_0xf71c('0x3b')]]={'$like':'%'+_0x37ee21[_0xf71c('0x46')][_0xf71c('0x4f')]+'%'};return _0x17e3ef;}})});}_0x27d3f0=_[_0xf71c('0x50')]({},_0x27d3f0,_0x37ee21[_0xf71c('0x52')]);var _0x1bb86a={'where':_0x27d3f0[_0xf71c('0x4d')]};return db[_0xf71c('0x3f')][_0xf71c('0x53')](_0x1bb86a)['then'](function(_0x361a47){_0x474338[_0xf71c('0x53')]=_0x361a47;if(_0x37ee21[_0xf71c('0x46')][_0xf71c('0x54')]){_0x27d3f0[_0xf71c('0x55')]=[{'all':!![]}];}return db[_0xf71c('0x3f')][_0xf71c('0x56')](_0x27d3f0);})['then'](function(_0x59d77e){_0x474338[_0xf71c('0x57')]=_0x59d77e;return _0x474338;})['then'](respondWithFilteredResult(_0x5ee090,_0x27d3f0))[_0xf71c('0x2a')](handleError(_0x5ee090,null));};exports['show']=function(_0xf2129b,_0x349690){var _0x5c1693={'raw':![],'where':{'id':_0xf2129b[_0xf71c('0x58')]['id']},'include':[{'model':db['Pause'],'as':_0xf71c('0x3e')}]},_0xf09f40={};_0xf09f40[_0xf71c('0x43')]=_['keys'](db['SmsAccount'][_0xf71c('0x59')]);_0xf09f40[_0xf71c('0x46')]=_['keys'](_0xf2129b[_0xf71c('0x46')]);_0xf09f40['filters']=_['intersection'](_0xf09f40[_0xf71c('0x43')],_0xf09f40[_0xf71c('0x46')]);_0x5c1693[_0xf71c('0x48')]=_['intersection'](_0xf09f40['model'],qs[_0xf71c('0x49')](_0xf2129b[_0xf71c('0x46')][_0xf71c('0x49')]));_0x5c1693[_0xf71c('0x48')]=_0x5c1693[_0xf71c('0x48')]['length']?_0x5c1693[_0xf71c('0x48')]:_0xf09f40[_0xf71c('0x43')];if(_0xf2129b[_0xf71c('0x46')][_0xf71c('0x54')]){_0x5c1693[_0xf71c('0x55')]=[{'all':!![]}];}_0x5c1693=_[_0xf71c('0x50')]({},_0x5c1693,_0xf2129b['options']);return db[_0xf71c('0x3f')]['find'](_0x5c1693)['then'](handleEntityNotFound(_0x349690,null))[_0xf71c('0x22')](respondWithResult(_0x349690,null))[_0xf71c('0x2a')](handleError(_0x349690,null));};exports[_0xf71c('0x5a')]=function(_0x1ffc0a,_0x188441){return db[_0xf71c('0x3f')][_0xf71c('0x5a')](_0x1ffc0a[_0xf71c('0x5b')],{})[_0xf71c('0x22')](function(_0x47c6d4){var _0x15e0d6=_0x1ffc0a[_0xf71c('0x5c')][_0xf71c('0x36')]({'plain':!![]});if(!_0x15e0d6)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x15e0d6['role']===_0xf71c('0x5c')){var _0x37c8c0=_0x47c6d4[_0xf71c('0x36')]({'plain':!![]});var _0x3c0753=_0xf71c('0x37');return db[_0xf71c('0x5d')][_0xf71c('0x5e')]({'where':{'name':_0x3c0753,'userProfileId':_0x15e0d6[_0xf71c('0x5f')]},'raw':!![]})[_0xf71c('0x22')](function(_0x47b5e8){if(_0x47b5e8&&_0x47b5e8[_0xf71c('0x60')]===0x0){return db[_0xf71c('0x38')][_0xf71c('0x5a')]({'name':_0x37c8c0[_0xf71c('0x3b')],'resourceId':_0x37c8c0['id'],'type':_0x47b5e8['name'],'sectionId':_0x47b5e8['id']},{})['then'](function(){return _0x47c6d4;});}else{return _0x47c6d4;}})[_0xf71c('0x2a')](function(_0x156e7c){logger[_0xf71c('0x27')](_0xf71c('0x61'),_0x156e7c);throw _0x156e7c;});}return _0x47c6d4;})[_0xf71c('0x22')](respondWithResult(_0x188441,0xc9))[_0xf71c('0x2a')](handleError(_0x188441,null));};exports[_0xf71c('0x62')]=function(_0x461980,_0x3d3d26){if(_0x461980[_0xf71c('0x5b')]['id']){delete _0x461980[_0xf71c('0x5b')]['id'];}return db[_0xf71c('0x3f')][_0xf71c('0x5e')]({'where':{'id':_0x461980[_0xf71c('0x58')]['id']},'include':[{'model':db[_0xf71c('0x3d')],'as':_0xf71c('0x3e')}]})[_0xf71c('0x22')](handleEntityNotFound(_0x3d3d26,null))[_0xf71c('0x22')](saveUpdates(_0x461980[_0xf71c('0x5b')],null))[_0xf71c('0x22')](respondWithResult(_0x3d3d26,null))['catch'](handleError(_0x3d3d26,null));};exports['destroy']=function(_0x2ba7de,_0x3581e4){return db['SmsAccount'][_0xf71c('0x5e')]({'where':{'id':_0x2ba7de[_0xf71c('0x58')]['id']}})['then'](handleEntityNotFound(_0x3581e4,null))['then'](removeEntity(_0x3581e4,null))[_0xf71c('0x2a')](handleError(_0x3581e4,null));};exports[_0xf71c('0x63')]=function(_0x2a8595,_0x50f999){return db[_0xf71c('0x3f')][_0xf71c('0x63')]()[_0xf71c('0x22')](respondWithResult(_0x50f999,null))[_0xf71c('0x2a')](handleError(_0x50f999,null));};exports[_0xf71c('0x64')]=function(_0x381bd0,_0x4652b2,_0x4d348c){if(_0x381bd0[_0xf71c('0x5b')]['id']){delete _0x381bd0[_0xf71c('0x5b')]['id'];}return db['SmsAccount'][_0xf71c('0x5e')]({'where':{'id':_0x381bd0[_0xf71c('0x58')]['id']}})[_0xf71c('0x22')](handleEntityNotFound(_0x4652b2,null))['then'](function(_0x595fa0){if(_0x595fa0){_0x381bd0['body'][_0xf71c('0x65')]=_0x595fa0['id'];return db[_0xf71c('0x66')][_0xf71c('0x5a')](_0x381bd0[_0xf71c('0x5b')]);}})[_0xf71c('0x22')](respondWithResult(_0x4652b2,null))[_0xf71c('0x2a')](handleError(_0x4652b2,null));};exports[_0xf71c('0x67')]=function(_0x2470d3,_0x4c6151,_0x2138e3){var _0x4fb709={'raw':![],'where':{}};var _0x5dd094={};var _0x5a7fae={'count':0x0,'rows':[]};return db[_0xf71c('0x3f')]['findOne']({'where':{'id':_0x2470d3[_0xf71c('0x58')]['id']}})[_0xf71c('0x22')](handleEntityNotFound(_0x4c6151,null))[_0xf71c('0x22')](function(_0x395267){if(_0x395267){_0x5dd094[_0xf71c('0x43')]=_[_0xf71c('0x45')](db[_0xf71c('0x66')][_0xf71c('0x59')]);_0x5dd094['query']=_[_0xf71c('0x45')](_0x2470d3[_0xf71c('0x46')]);_0x5dd094['filters']=_[_0xf71c('0x47')](_0x5dd094['model'],_0x5dd094[_0xf71c('0x46')]);_0x4fb709['attributes']=_[_0xf71c('0x47')](_0x5dd094[_0xf71c('0x43')],qs['fields'](_0x2470d3[_0xf71c('0x46')]['fields']));_0x4fb709[_0xf71c('0x48')]=_0x4fb709['attributes'][_0xf71c('0x68')]?_0x4fb709['attributes']:_0x5dd094[_0xf71c('0x43')];if(!_0x2470d3['query']['hasOwnProperty']('nolimit')){_0x4fb709['limit']=qs[_0xf71c('0x2f')](_0x2470d3[_0xf71c('0x46')][_0xf71c('0x2f')]);_0x4fb709[_0xf71c('0x30')]=qs['offset'](_0x2470d3[_0xf71c('0x46')][_0xf71c('0x30')]);}_0x4fb709['order']=qs['sort'](_0x2470d3['query'][_0xf71c('0x4c')]);_0x4fb709[_0xf71c('0x4d')]=qs[_0xf71c('0x4e')](_['pick'](_0x2470d3[_0xf71c('0x46')],_0x5dd094[_0xf71c('0x4e')]));_0x4fb709[_0xf71c('0x4d')][_0xf71c('0x65')]=_0x395267['id'];if(_0x2470d3[_0xf71c('0x46')][_0xf71c('0x4f')]){_0x4fb709[_0xf71c('0x4d')]=_[_0xf71c('0x50')](_0x4fb709[_0xf71c('0x4d')],{'$or':_[_0xf71c('0x44')](_0x4fb709['attributes'],function(_0x568e12){var _0x30e302={};_0x30e302[_0x568e12]={'$like':'%'+_0x2470d3[_0xf71c('0x46')][_0xf71c('0x4f')]+'%'};return _0x30e302;})});}_0x4fb709=_['merge']({},_0x4fb709,_0x2470d3[_0xf71c('0x52')]);return db[_0xf71c('0x66')][_0xf71c('0x53')]({'where':_0x4fb709[_0xf71c('0x4d')]})[_0xf71c('0x22')](function(_0x3c73e8){_0x5a7fae['count']=_0x3c73e8;if(_0x2470d3[_0xf71c('0x46')]['includeAll']){_0x4fb709[_0xf71c('0x55')]=[{'all':!![]}];}return db[_0xf71c('0x66')][_0xf71c('0x56')](_0x4fb709);})[_0xf71c('0x22')](function(_0x1fdcab){_0x5a7fae[_0xf71c('0x57')]=_0x1fdcab;return _0x5a7fae;});}})[_0xf71c('0x22')](respondWithFilteredResult(_0x4c6151,_0x4fb709))['catch'](handleError(_0x4c6151,null));};exports[_0xf71c('0x69')]=function(_0x1fb2b5,_0x5cb75c,_0x3106a3){return db[_0xf71c('0x3f')][_0xf71c('0x5e')]({'where':{'id':_0x1fb2b5[_0xf71c('0x58')]['id']}})['then'](handleEntityNotFound(_0x5cb75c,null))[_0xf71c('0x22')](function(_0x12b270){if(_0x12b270){return _0x12b270[_0xf71c('0x69')](_0x1fb2b5[_0xf71c('0x46')][_0xf71c('0x6a')]);}})['then'](respondWithStatusCode(_0x5cb75c,null))['catch'](handleError(_0x5cb75c,null));};exports[_0xf71c('0x6b')]=function(_0x480029,_0x32834c,_0x423676){if(_0x480029['body']['id']){delete _0x480029['body']['id'];}return db['SmsAccount'][_0xf71c('0x5e')]({'where':{'id':_0x480029[_0xf71c('0x58')]['id']}})['then'](handleEntityNotFound(_0x32834c,null))['then'](function(_0x158bb4){if(_0x158bb4){_0x480029[_0xf71c('0x5b')][_0xf71c('0x65')]=_0x158bb4['id'];return db['CannedAnswer']['create'](_0x480029[_0xf71c('0x5b')]);}})[_0xf71c('0x22')](respondWithResult(_0x32834c,null))[_0xf71c('0x2a')](handleError(_0x32834c,null));};exports['getAnswers']=function(_0x3365bc,_0x67bc09,_0x21846b){var _0x376c4b={'raw':![],'where':{}};var _0x37b416={};var _0x4276ee={'count':0x0,'rows':[]};return db['SmsAccount'][_0xf71c('0x6c')]({'where':{'id':_0x3365bc[_0xf71c('0x58')]['id']}})[_0xf71c('0x22')](handleEntityNotFound(_0x67bc09,null))[_0xf71c('0x22')](function(_0x1db478){if(_0x1db478){_0x37b416[_0xf71c('0x43')]=_[_0xf71c('0x45')](db['CannedAnswer']['rawAttributes']);_0x37b416['query']=_[_0xf71c('0x45')](_0x3365bc['query']);_0x37b416[_0xf71c('0x4e')]=_[_0xf71c('0x47')](_0x37b416['model'],_0x37b416[_0xf71c('0x46')]);_0x376c4b[_0xf71c('0x48')]=_[_0xf71c('0x47')](_0x37b416[_0xf71c('0x43')],qs[_0xf71c('0x49')](_0x3365bc[_0xf71c('0x46')][_0xf71c('0x49')]));_0x376c4b[_0xf71c('0x48')]=_0x376c4b[_0xf71c('0x48')][_0xf71c('0x68')]?_0x376c4b[_0xf71c('0x48')]:_0x37b416[_0xf71c('0x43')];if(!_0x3365bc['query'][_0xf71c('0x4a')]('nolimit')){_0x376c4b['limit']=qs[_0xf71c('0x2f')](_0x3365bc[_0xf71c('0x46')][_0xf71c('0x2f')]);_0x376c4b[_0xf71c('0x30')]=qs[_0xf71c('0x30')](_0x3365bc[_0xf71c('0x46')][_0xf71c('0x30')]);}_0x376c4b[_0xf71c('0x6d')]=qs[_0xf71c('0x4c')](_0x3365bc[_0xf71c('0x46')]['sort']);_0x376c4b[_0xf71c('0x4d')]=qs[_0xf71c('0x4e')](_[_0xf71c('0x6e')](_0x3365bc[_0xf71c('0x46')],_0x37b416['filters']));_0x376c4b[_0xf71c('0x4d')][_0xf71c('0x65')]=_0x1db478['id'];if(_0x3365bc[_0xf71c('0x46')]['filter']){_0x376c4b['where']=_[_0xf71c('0x50')](_0x376c4b['where'],{'$or':_['map'](_0x376c4b[_0xf71c('0x48')],function(_0x30c93b){var _0x1fc587={};_0x1fc587[_0x30c93b]={'$like':'%'+_0x3365bc[_0xf71c('0x46')][_0xf71c('0x4f')]+'%'};return _0x1fc587;})});}_0x376c4b=_[_0xf71c('0x50')]({},_0x376c4b,_0x3365bc[_0xf71c('0x52')]);return db[_0xf71c('0x6f')][_0xf71c('0x53')]({'where':_0x376c4b[_0xf71c('0x4d')]})[_0xf71c('0x22')](function(_0x360192){_0x4276ee[_0xf71c('0x53')]=_0x360192;if(_0x3365bc[_0xf71c('0x46')][_0xf71c('0x54')]){_0x376c4b[_0xf71c('0x55')]=[{'all':!![]}];}return db[_0xf71c('0x6f')][_0xf71c('0x56')](_0x376c4b);})[_0xf71c('0x22')](function(_0x23a6ea){_0x4276ee['rows']=_0x23a6ea;return _0x4276ee;});}})[_0xf71c('0x22')](respondWithFilteredResult(_0x67bc09,_0x376c4b))[_0xf71c('0x2a')](handleError(_0x67bc09,null));};exports['removeAnswers']=function(_0x5df3ec,_0x32f481,_0xaa7c9f){return db[_0xf71c('0x3f')][_0xf71c('0x5e')]({'where':{'id':_0x5df3ec['params']['id']}})[_0xf71c('0x22')](handleEntityNotFound(_0x32f481,null))[_0xf71c('0x22')](function(_0x40e21c){if(_0x40e21c){return _0x40e21c[_0xf71c('0x70')](_0x5df3ec[_0xf71c('0x46')][_0xf71c('0x6a')]);}})[_0xf71c('0x22')](respondWithStatusCode(_0x32f481,null))[_0xf71c('0x2a')](handleError(_0x32f481,null));};exports[_0xf71c('0x71')]=function(_0x1152e4,_0x3e8e60,_0x59d697){var _0x24b6f8={'channel':_0xf71c('0x72')};var _0x38eda1=[];var _0x3ddf85=[];var _0x2a730={};return db[_0xf71c('0x3f')][_0xf71c('0x5e')]({'where':{'id':_0x1152e4['params']['id']},'include':[{'model':db[_0xf71c('0x73')],'as':_0xf71c('0x74')}]})['then'](handleEntityNotFound(_0x3e8e60,null))[_0xf71c('0x22')](function(_0x1f790d){_0x24b6f8[_0xf71c('0x75')]=_0x1f790d;_0x24b6f8[_0xf71c('0x76')]=_[_0xf71c('0x77')](_0x1f790d['Applications'],[_0xf71c('0x78')],[_0xf71c('0x79')]);if(_0x24b6f8[_0xf71c('0x75')]['Applications']){delete _0x24b6f8[_0xf71c('0x75')][_0xf71c('0x74')];}return db[_0xf71c('0x7a')][_0xf71c('0x63')]();})['then'](function(_0x44d75e){if(!_0x44d75e){throw new db[(_0xf71c('0x7b'))][(_0xf71c('0x7c'))]('no\x20available\x20attributes');}_0x38eda1=_[_0xf71c('0x7d')](_[_0xf71c('0x45')](_0x44d75e),['createdAt',_0xf71c('0x7e')]);_0x3ddf85=_['difference'](_[_0xf71c('0x45')](_0x44d75e),[_0xf71c('0x7f'),_0xf71c('0x7e'),_0xf71c('0x80'),'ListId']);_0x24b6f8[_0xf71c('0x5b')]={'mapKey':_0xf71c('0x81')};if(_0x24b6f8[_0xf71c('0x75')]['type']){switch(_0x24b6f8[_0xf71c('0x75')][_0xf71c('0x41')]){case _0xf71c('0x82'):_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x83')]=_0x1152e4['body'][_0xf71c('0x84')];_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x5b')]=_0x1152e4[_0xf71c('0x5b')]['Body'];_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x85')]=_0x1152e4[_0xf71c('0x5b')][_0xf71c('0x86')];break;case _0xf71c('0x87'):_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x83')]='+'+_0x1152e4['body'][_0xf71c('0x88')];_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x5b')]=_0x1152e4[_0xf71c('0x5b')][_0xf71c('0x89')];break;case _0xf71c('0x8a'):_0x24b6f8[_0xf71c('0x5b')]['from']=_0x1152e4[_0xf71c('0x5b')]['from'];_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x5b')]=_0x1152e4[_0xf71c('0x5b')][_0xf71c('0x29')];break;case'clicksend':_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x83')]=_0x1152e4[_0xf71c('0x5b')]['from'];_0x24b6f8['body'][_0xf71c('0x5b')]=_0x1152e4['body'][_0xf71c('0x5b')];_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x85')]=_0x1152e4['body'][_0xf71c('0x8b')];break;case _0xf71c('0x8c'):_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x83')]=_0x1152e4['body'][_0xf71c('0x84')];_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x5b')]=_0x1152e4[_0xf71c('0x5b')][_0xf71c('0x8d')];_0x24b6f8[_0xf71c('0x5b')]['messageId']=_0x1152e4['body'][_0xf71c('0x8e')];break;case'infobip':_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x83')]='+'+_0x1152e4[_0xf71c('0x5b')][_0xf71c('0x8f')][0x0][_0xf71c('0x83')];_0x24b6f8['body'][_0xf71c('0x5b')]=_0x1152e4[_0xf71c('0x5b')][_0xf71c('0x8f')][0x0]['text'];_0x24b6f8['body'][_0xf71c('0x85')]=_0x1152e4[_0xf71c('0x5b')][_0xf71c('0x8f')][0x0][_0xf71c('0x85')];break;case _0xf71c('0x90'):_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x83')]=_0x1152e4[_0xf71c('0x5b')][_0xf71c('0x91')];_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x5b')]=_0x1152e4[_0xf71c('0x5b')][_0xf71c('0x92')][_0xf71c('0x29')];break;case _0xf71c('0x93'):_0x24b6f8['body'][_0xf71c('0x83')]=_0x1152e4[_0xf71c('0x5b')][_0xf71c('0x94')];_0x24b6f8[_0xf71c('0x5b')]['body']=_0x1152e4['body'][_0xf71c('0x89')];_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x85')]=_0x1152e4[_0xf71c('0x5b')]['replyMessageId'];break;case'bandwidth':_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x85')]=_0x1152e4[_0xf71c('0x5b')][0x0]['message']['id'];_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x83')]=_0x1152e4[_0xf71c('0x5b')][0x0][_0xf71c('0x29')][_0xf71c('0x83')];_0x24b6f8['body'][_0xf71c('0x5b')]=_0x1152e4[_0xf71c('0x5b')][0x0][_0xf71c('0x29')][_0xf71c('0x89')];break;case _0xf71c('0x95'):_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x83')]=_0x1152e4[_0xf71c('0x5b')][_0xf71c('0x83')];_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x5b')]=_0x1152e4['body'][_0xf71c('0x29')];_0x24b6f8[_0xf71c('0x5b')]['messageId']=_0x1152e4['body'][_0xf71c('0x96')];break;}}if(_0x24b6f8[_0xf71c('0x5b')]['id']){delete _0x24b6f8[_0xf71c('0x5b')]['id'];}if(_[_0xf71c('0x97')](_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x83')])){throw new db[(_0xf71c('0x7b'))][(_0xf71c('0x7c'))](_0xf71c('0x98'));}if(_[_0xf71c('0x97')](_0x24b6f8[_0xf71c('0x5b')]['body'])||_0x24b6f8[_0xf71c('0x5b')]['body']===''){throw new db[(_0xf71c('0x7b'))][(_0xf71c('0x7c'))](_0xf71c('0x99'));}if(_[_0xf71c('0x97')](_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x9a')])){throw new db[(_0xf71c('0x7b'))][(_0xf71c('0x7c'))](_0xf71c('0x9b')+_0x3ddf85);}if(!_[_0xf71c('0x9c')](_0x3ddf85,_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x9a')])){throw new db[(_0xf71c('0x7b'))]['ValidationError']('mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x3ddf85);}_0x2a730[_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x9a')]]=_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x83')];return db['CmList']['find']({'where':{'id':_0x24b6f8[_0xf71c('0x75')][_0xf71c('0x9d')]},'include':[{'model':db[_0xf71c('0x7a')],'as':_0xf71c('0x9e'),'where':_0x2a730,'limit':0x1,'order':[[_0xf71c('0x7e'),_0xf71c('0x9f')]]}]});})['then'](handleEntityNotFound(_0x3e8e60,null))[_0xf71c('0x22')](function(_0x96b0b2){if(_0x96b0b2){_0x24b6f8['list']=_[_0xf71c('0xa0')](_0x96b0b2,[_0xf71c('0x9e')]);if(_0x96b0b2['Contacts'][_0xf71c('0x68')]){return _0x96b0b2[_0xf71c('0x9e')][0x0];}var _0x1d68c0=_[_0xf71c('0x1b')](_0x24b6f8[_0xf71c('0x5b')],{'firstName':_0x24b6f8['body'][_0xf71c('0x83')],'ListId':_0x96b0b2['id']});_0x1d68c0[_0x24b6f8['body'][_0xf71c('0x9a')]]=_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x83')];return db['CmContact']['create'](_0x1d68c0,{'fields':_0x38eda1,'raw':!![]});}})[_0xf71c('0x22')](handleEntityNotFound(_0x3e8e60,null))[_0xf71c('0x22')](function(_0x30eb42){if(_0x30eb42){_0x24b6f8['contact']=_0x30eb42;var _0x5b0d4b={'ContactId':_0x30eb42['id'],'phone':_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x83')],'SmsAccountId':_0x1152e4[_0xf71c('0x58')]['id']};var _0x4d7c2b={'ContactId':_0x30eb42['id'],'SmsAccountId':_0x1152e4['params']['id'],'closed':![]};return db['SmsInteraction'][_0xf71c('0x5e')]({'where':_0x4d7c2b})['then'](function(_0x397bc6){if(_0x397bc6){return[_0x397bc6,![]];}return db[_0xf71c('0xa1')][_0xf71c('0x5a')](_0x5b0d4b)[_0xf71c('0x22')](function(_0x3ce0fe){return[_0x3ce0fe,!![]];});});}})['spread'](function(_0x2ab2ba,_0x5b83f8){_0x24b6f8[_0xf71c('0xa2')]=_0x2ab2ba['get']({'plain':!![]});_0x24b6f8[_0xf71c('0xa2')]['created']=![];if(_0x5b83f8){if(_0x24b6f8[_0xf71c('0x75')][_0xf71c('0xa3')]){_0x24b6f8['applications'][_0xf71c('0xa4')]({'id':0x0,'priority':_0x24b6f8['applications'][_0xf71c('0x68')]+0x1,'app':_0xf71c('0xa5'),'appdata':_0xf71c('0xa6'),'interval':_0xf71c('0xa7')});}_0x24b6f8['interaction'][_0xf71c('0xa8')]=!![];}return db['SmsMessage'][_0xf71c('0x5a')]({'phone':_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x83')],'messageId':_0x24b6f8[_0xf71c('0x5b')]['messageId'],'body':_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0x5b')],'SmsAccountId':_0x1152e4[_0xf71c('0x58')]['id'],'SmsInteractionId':_0x2ab2ba['id'],'direction':'in','ContactId':_0x2ab2ba[_0xf71c('0xa9')],'AttachmentId':_0x24b6f8[_0xf71c('0x5b')][_0xf71c('0xaa')]});})[_0xf71c('0x22')](function(_0x41e016){_0x24b6f8[_0xf71c('0x29')]=_0x41e016;if(_0x24b6f8[_0xf71c('0xa2')][_0xf71c('0xab')]){return db['User'][_0xf71c('0x5e')]({'attributes':['id',_0xf71c('0x3b')],'where':{'id':_0x24b6f8[_0xf71c('0xa2')][_0xf71c('0xab')]}})[_0xf71c('0x22')](function(_0x228817){if(_0x228817){_0x24b6f8[_0xf71c('0x76')][_0xf71c('0xac')]({'id':0x0,'priority':0x0,'app':_0xf71c('0xad'),'appdata':_0x228817['name']+','+(_0x24b6f8[_0xf71c('0x75')]['waitForTheAssignedAgent']||0xa),'interval':_0xf71c('0xa7')});}return respondWithRpcPromise(_0xf71c('0xae'),'startRouting',_0x24b6f8);});}return respondWithRpcPromise(_0xf71c('0xae'),_0xf71c('0xaf'),_0x24b6f8);})['then'](function(_0xf4caa4){if(_0xf4caa4){var _0x5c8064=0xc8;var _0x5c9322=_0xf71c('0xb0');logger[_0xf71c('0x23')](_0xf71c('0xb1'),_0x5c9322,_0x5c8064,'entity\x20found');logger[_0xf71c('0x26')](_0xf71c('0xb2'),_0x5c9322,_0x5c8064,_0xf71c('0xb3'),JSON[_0xf71c('0xb4')](_0xf4caa4));if(_0x24b6f8[_0xf71c('0x75')][_0xf71c('0x41')]===_0xf71c('0x82')){return _0x3e8e60['status'](_0x5c8064)[_0xf71c('0x31')](_0xf71c('0xb5'),_0xf71c('0xb6'))[_0xf71c('0x3c')](_0xf71c('0xb7'));}else{return _0x3e8e60['status'](_0x5c8064)[_0xf71c('0x2d')](_0xf4caa4);}}})['catch'](handleError(_0x3e8e60,null));};exports['addApplications']=function(_0x3c5b81,_0x3f2a37){var _0x99f370=_0x3c5b81[_0xf71c('0x58')]['id'];var _0x53252f=_0x3c5b81[_0xf71c('0x5b')];var _0x52a9cb=0xc8;var _0x5dc626=null;return db[_0xf71c('0xb8')][_0xf71c('0xb9')]({'isolationLevel':db[_0xf71c('0xb8')][_0xf71c('0xba')][_0xf71c('0xbb')]['READ_COMMITTED']},function(_0x466418){return db['SmsAccount']['findOne']({'where':{'id':_0x99f370},'transaction':_0x466418})[_0xf71c('0x22')](function(_0x387ae8){if(_0x387ae8){return db[_0xf71c('0x73')][_0xf71c('0x35')]({'where':{'SmsAccountId':_0x99f370},'transaction':_0x466418})[_0xf71c('0x22')](function(){var _0x1574cb=_['map'](_0x53252f,function(_0x488b08){_0x488b08[_0xf71c('0x65')]=_0x99f370;return _0x488b08;});return db[_0xf71c('0x73')]['bulkCreate'](_0x1574cb,{'transaction':_0x466418});});}else{_0x52a9cb=0x194;_0x5dc626=[];}});})['then'](function(){if(_0x52a9cb!==0x194){return db[_0xf71c('0x73')][_0xf71c('0xbc')]({'where':{'SmsAccountId':_0x99f370},'order':_0xf71c('0x78')})['then'](function(_0x3ca90b){_0x5dc626=_0x3ca90b;});}})[_0xf71c('0x2a')](function(_0x3cf8a9){_0x52a9cb=0x1f4;logger[_0xf71c('0x27')](_0x3cf8a9['stack']);if(_0x3cf8a9[_0xf71c('0x3b')]){delete _0x3cf8a9[_0xf71c('0x3b')];}_0x5dc626=_0x3cf8a9;})[_0xf71c('0xbd')](function(){if(_0x5dc626===null){_0x3f2a37[_0xf71c('0x2b')](_0x52a9cb);}else{if(_0x52a9cb===0x1f4){_0x3f2a37[_0xf71c('0x2c')](_0x52a9cb)[_0xf71c('0x3c')](_0x5dc626);}else{_0x3f2a37[_0xf71c('0x2c')](_0x52a9cb)[_0xf71c('0x2d')](_0x5dc626);}}});};exports[_0xf71c('0xbe')]=function(_0x1f2e30,_0x2fa292,_0x2bd97d){var _0x3c946e={};var _0x2e5213={};var _0x44860c;var _0x5c0370;return db[_0xf71c('0x3f')][_0xf71c('0x6c')]({'where':{'id':_0x1f2e30[_0xf71c('0x58')]['id']}})['then'](handleEntityNotFound(_0x2fa292,null))['then'](function(_0x1dbe1f){if(_0x1dbe1f){_0x44860c=_0x1dbe1f;_0x2e5213[_0xf71c('0x43')]=_['keys'](db[_0xf71c('0x73')][_0xf71c('0x59')]);_0x2e5213[_0xf71c('0x46')]=_['keys'](_0x1f2e30[_0xf71c('0x46')]);_0x2e5213[_0xf71c('0x4e')]=_[_0xf71c('0x47')](_0x2e5213[_0xf71c('0x43')],_0x2e5213['query']);_0x3c946e[_0xf71c('0x48')]=_[_0xf71c('0x47')](_0x2e5213['model'],qs['fields'](_0x1f2e30[_0xf71c('0x46')]['fields']));_0x3c946e['attributes']=_0x3c946e['attributes']['length']?_0x3c946e[_0xf71c('0x48')]:_0x2e5213[_0xf71c('0x43')];_0x3c946e[_0xf71c('0x6d')]=qs[_0xf71c('0x4c')](_0x1f2e30[_0xf71c('0x46')][_0xf71c('0x4c')]);_0x3c946e[_0xf71c('0x4d')]=qs[_0xf71c('0x4e')](_['pick'](_0x1f2e30[_0xf71c('0x46')],_0x2e5213[_0xf71c('0x4e')]));if(_0x1f2e30[_0xf71c('0x46')][_0xf71c('0x4f')]){_0x3c946e[_0xf71c('0x4d')]=_['merge'](_0x3c946e[_0xf71c('0x4d')],{'$or':_[_0xf71c('0x44')](_0x3c946e['attributes'],function(_0xbc84a9){var _0x3ca1e1={};_0x3ca1e1[_0xbc84a9]={'$like':'%'+_0x1f2e30['query'][_0xf71c('0x4f')]+'%'};return _0x3ca1e1;})});}_0x3c946e=_[_0xf71c('0x50')]({},_0x3c946e,_0x1f2e30['options']);return _0x44860c[_0xf71c('0xbe')](_0x3c946e);}})[_0xf71c('0x22')](function(_0x4e6b68){if(_0x4e6b68){_0x5c0370=_0x4e6b68[_0xf71c('0x68')];if(!_0x1f2e30[_0xf71c('0x46')][_0xf71c('0x4a')](_0xf71c('0x4b'))){_0x3c946e[_0xf71c('0x2f')]=qs[_0xf71c('0x2f')](_0x1f2e30[_0xf71c('0x46')][_0xf71c('0x2f')]);_0x3c946e[_0xf71c('0x30')]=qs[_0xf71c('0x30')](_0x1f2e30['query'][_0xf71c('0x30')]);}return _0x44860c[_0xf71c('0xbe')](_0x3c946e);}})[_0xf71c('0x22')](function(_0x4953d4){if(_0x4953d4){return _0x4953d4?{'count':_0x5c0370,'rows':_0x4953d4}:null;}})[_0xf71c('0x22')](respondWithResult(_0x2fa292,null))[_0xf71c('0x2a')](handleError(_0x2fa292,null));};exports['getInteractions']=function(_0x3497fd,_0x43846c,_0x58e5ef){var _0xde87b0={'raw':![],'where':{}};var _0x425f3a={};var _0x3b191e={'count':0x0,'rows':[]};return db[_0xf71c('0x3f')][_0xf71c('0x6c')]({'where':{'id':_0x3497fd[_0xf71c('0x58')]['id']}})['then'](handleEntityNotFound(_0x43846c,null))[_0xf71c('0x22')](function(_0x29ab6e){if(_0x29ab6e){_0x425f3a[_0xf71c('0x43')]=_[_0xf71c('0x45')](db[_0xf71c('0xa1')][_0xf71c('0x59')]);_0x425f3a['query']=_['keys'](_0x3497fd[_0xf71c('0x46')]);_0x425f3a['filters']=_[_0xf71c('0x47')](_0x425f3a[_0xf71c('0x43')],_0x425f3a[_0xf71c('0x46')]);_0xde87b0[_0xf71c('0x48')]=_['intersection'](_0x425f3a[_0xf71c('0x43')],qs[_0xf71c('0x49')](_0x3497fd[_0xf71c('0x46')][_0xf71c('0x49')]));_0xde87b0[_0xf71c('0x48')]=_0xde87b0[_0xf71c('0x48')][_0xf71c('0x68')]?_0xde87b0[_0xf71c('0x48')]:_0x425f3a[_0xf71c('0x43')];if(!_0x3497fd[_0xf71c('0x46')][_0xf71c('0x4a')](_0xf71c('0x4b'))){_0xde87b0[_0xf71c('0x2f')]=qs['limit'](_0x3497fd['query'][_0xf71c('0x2f')]);_0xde87b0['offset']=qs[_0xf71c('0x30')](_0x3497fd['query'][_0xf71c('0x30')]);}_0xde87b0['order']=qs[_0xf71c('0x4c')](_0x3497fd[_0xf71c('0x46')][_0xf71c('0x4c')]);_0xde87b0[_0xf71c('0x4d')]=qs[_0xf71c('0x4e')](_['pick'](_0x3497fd[_0xf71c('0x46')],_0x425f3a['filters']));_0xde87b0[_0xf71c('0x4d')][_0xf71c('0x65')]=_0x29ab6e['id'];if(_0x3497fd[_0xf71c('0x46')][_0xf71c('0x4f')]){_0xde87b0[_0xf71c('0x4d')]=_[_0xf71c('0x50')](_0xde87b0[_0xf71c('0x4d')],{'$or':_[_0xf71c('0x44')](_0xde87b0[_0xf71c('0x48')],function(_0x3b8a77){var _0x48df6a={};_0x48df6a[_0x3b8a77]={'$like':'%'+_0x3497fd['query']['filter']+'%'};return _0x48df6a;})});}_0xde87b0=_[_0xf71c('0x50')]({},_0xde87b0,_0x3497fd[_0xf71c('0x52')]);return db['SmsInteraction'][_0xf71c('0x53')]({'where':_0xde87b0[_0xf71c('0x4d')]})['then'](function(_0x5a274b){_0x3b191e['count']=_0x5a274b;if(_0x3497fd[_0xf71c('0x46')][_0xf71c('0x54')]){_0xde87b0[_0xf71c('0x55')]=[{'model':db[_0xf71c('0x7a')],'as':_0xf71c('0xbf'),'required':![]},{'model':db[_0xf71c('0xc0')],'as':_0xf71c('0xc1'),'attributes':['name',_0xf71c('0xc2'),_0xf71c('0xc3')],'required':![]},{'model':db[_0xf71c('0xc4')],'as':_0xf71c('0xc5'),'attributes':['id',_0xf71c('0x3b'),_0xf71c('0xc6')],'where':_0x3497fd[_0xf71c('0x46')][_0xf71c('0xc7')]?{'id':_0x3497fd[_0xf71c('0x46')]['tag']}:undefined,'required':_0x3497fd[_0xf71c('0x46')][_0xf71c('0xc7')]?!![]:![]}];}return db[_0xf71c('0xa1')][_0xf71c('0x56')](_0xde87b0);})[_0xf71c('0x22')](function(_0x546b61){_0x3b191e[_0xf71c('0x57')]=_0x546b61;return _0x3b191e;});}})['then'](respondWithFilteredResult(_0x43846c,_0xde87b0))[_0xf71c('0x2a')](handleError(_0x43846c,null));};exports['send']=function(_0x300160,_0x41f2e8,_0x3a6a44){var _0x5b7f24,_0x25cb6c,_0x2ecf03;if(_['isNil'](_0x300160[_0xf71c('0x5b')][_0xf71c('0x5b')])||_0x300160[_0xf71c('0x5b')][_0xf71c('0x5b')]===''){throw new db[(_0xf71c('0x7b'))][(_0xf71c('0x7c'))](_0xf71c('0x99'));}if(_[_0xf71c('0x97')](_0x300160[_0xf71c('0x5b')]['to'])){throw new db[(_0xf71c('0x7b'))][(_0xf71c('0x7c'))]('to\x20is\x20mandatory');}_0x5b7f24=_0x300160[_0xf71c('0x5b')]['to'];return db[_0xf71c('0x3f')][_0xf71c('0x5e')]({'where':{'id':_0x300160[_0xf71c('0x58')]['id']},'include':[{'model':db[_0xf71c('0xc8')],'as':'List','include':[{'model':db['CmContact'],'as':_0xf71c('0x9e'),'where':{'mobile':_0x5b7f24},'limit':0x1,'order':[['updatedAt',_0xf71c('0x9f')]]}]}]})['then'](handleEntityNotFound(_0x41f2e8,null))[_0xf71c('0x22')](function(_0x4f9514){if(_0x4f9514&&_0x4f9514[_0xf71c('0xc9')]){_0x25cb6c=_0x4f9514['get']({'plain':!![]});if(_0x25cb6c[_0xf71c('0xc9')]&&_0x25cb6c[_0xf71c('0xc9')][_0xf71c('0x9e')][_0xf71c('0x68')]){return _0x25cb6c[_0xf71c('0xc9')]['Contacts'][0x0];}return db['CmContact'][_0xf71c('0x5a')](_['defaults'](_0x300160[_0xf71c('0x5b')],{'firstName':_0x5b7f24,'mobile':_0x5b7f24,'phone':_0x5b7f24,'ListId':_0x25cb6c[_0xf71c('0x9d')]}));}})[_0xf71c('0x22')](handleEntityNotFound(_0x41f2e8,null))['then'](function(_0x22fc38){if(_0x22fc38){_0x2ecf03=_0x22fc38;return db[_0xf71c('0xa1')]['find']({'where':{'ContactId':_0x2ecf03['id'],'closed':![],'SmsAccountId':_0x25cb6c['id']}})[_0xf71c('0x22')](function(_0x1e86d6){if(_0x1e86d6){return[_0x1e86d6,![]];}return db[_0xf71c('0xa1')][_0xf71c('0x5a')]({'UserId':_0x300160['user']['id'],'ContactId':_0x2ecf03['id'],'SmsAccountId':_0x25cb6c['id'],'phone':_0x5b7f24,'firstMsgDirection':_0xf71c('0xca'),'Messages':[_[_0xf71c('0x50')](_0x300160[_0xf71c('0x5b')],{'read':![],'body':_0x300160['body'][_0xf71c('0x5b')],'phone':_0x5b7f24,'SmsAccountId':_0x25cb6c['id'],'UserId':_0x300160[_0xf71c('0x5c')]['id'],'ContactId':_0x2ecf03['id']})]},{'include':[{'model':db['SmsMessage'],'as':_0xf71c('0xcb')}]})[_0xf71c('0x22')](function(_0x302298){return[_0x302298,!![]];});});}})[_0xf71c('0xcc')](function(_0x47bcaa,_0x55c912){if(_0x55c912){return _0x47bcaa;}return db['SmsMessage'][_0xf71c('0x5a')](_['merge'](_0x300160[_0xf71c('0x5b')],{'read':![],'body':_0x300160['body']['body'],'phone':_0x5b7f24,'SmsAccountId':_0x25cb6c['id'],'SmsInteractionId':_0x47bcaa['id'],'UserId':_0x300160[_0xf71c('0x5c')]['id'],'ContactId':_0x2ecf03['id']}))[_0xf71c('0x22')](function(_0x315e26){_0x47bcaa[_0xf71c('0xcd')][_0xf71c('0xcb')]=[];_0x47bcaa[_0xf71c('0xcd')][_0xf71c('0xcb')][_0xf71c('0xa4')](_0x315e26[_0xf71c('0xcd')]);return _0x47bcaa;});})[_0xf71c('0x22')](respondWithResult(_0x41f2e8,null))[_0xf71c('0x2a')](handleError(_0x41f2e8,null));};exports[_0xf71c('0x2c')]=function(_0xd772f4,_0x4aa611,_0x105bb9){if(_0xd772f4[_0xf71c('0x5b')]['id']){delete _0xd772f4[_0xf71c('0x5b')]['id'];}var _0x164397,_0x2b1d3c,_0x2062a;return db['SmsAccount'][_0xf71c('0x5e')]({'where':{'id':_0xd772f4[_0xf71c('0x58')]['id']}})['then'](handleEntityNotFound(_0x4aa611,null))[_0xf71c('0x22')](function(_0x306346){if(_0x306346){_0x2062a=_0x306346;logger[_0xf71c('0x26')](_0xf71c('0xb1'),_0xf71c('0x2c'),_0xf71c('0xce'),_0x306346[_0xf71c('0x41')],util[_0xf71c('0xcf')](_0xd772f4[_0xf71c('0x5b')],{'showHidden':![],'depth':null}));switch(_0x306346[_0xf71c('0x41')]){case _0xf71c('0x87'):if(_0xd772f4['body']['skebby_dispatch_id']&&_0xd772f4[_0xf71c('0x5b')]['status']){_0x2b1d3c=_0xd772f4['body']['status'];return db['SmsMessage'][_0xf71c('0x5e')]({'where':{'messageId':_0xd772f4[_0xf71c('0x5b')][_0xf71c('0xd0')]}});}break;case _0xf71c('0x8a'):if(_0xd772f4[_0xf71c('0x5b')]['requestid']&&_0xd772f4[_0xf71c('0x5b')][_0xf71c('0xd1')]){_0x2b1d3c=_0xd772f4[_0xf71c('0x5b')][_0xf71c('0xd1')];return db[_0xf71c('0xd2')][_0xf71c('0x5e')]({'where':{'messageId':_0xd772f4['body'][_0xf71c('0xd3')]}});}break;case _0xf71c('0xd4'):if(_0xd772f4[_0xf71c('0x5b')]['message_id']&&_0xd772f4[_0xf71c('0x5b')]['status_code']){_0x2b1d3c=_0xd772f4['body'][_0xf71c('0xd5')];return db['SmsMessage'][_0xf71c('0x5e')]({'where':{'messageId':_0xd772f4[_0xf71c('0x5b')][_0xf71c('0x8b')]}});}break;case _0xf71c('0xd6'):var _0x207930=_0xd772f4[_0xf71c('0x5b')][_0xf71c('0x8f')][0x0];if(_0x207930[_0xf71c('0x85')]&&_0x207930[_0xf71c('0x2c')]['groupName']==='DELIVERED'){_0x2b1d3c=_0x207930[_0xf71c('0x2c')][_0xf71c('0xd7')];return db[_0xf71c('0xd2')][_0xf71c('0x5e')]({'where':{'messageId':_0x207930[_0xf71c('0x85')]}});}break;case _0xf71c('0x93'):if(_0xd772f4[_0xf71c('0x5b')][_0xf71c('0x85')]&&_0xd772f4[_0xf71c('0x5b')]['status']){_0x2b1d3c=_0xd772f4['body'][_0xf71c('0x2c')];return db['SmsMessage'][_0xf71c('0x5e')]({'where':{'messageId':_0xd772f4[_0xf71c('0x5b')][_0xf71c('0x85')]}});}break;case _0xf71c('0xd8'):if(_0xd772f4['body'][0x0][_0xf71c('0x29')]['id']){_0x2b1d3c=_0xd772f4[_0xf71c('0x5b')][0x0][_0xf71c('0x41')];return db['SmsMessage']['find']({'where':{'messageId':_0xd772f4[_0xf71c('0x5b')][0x0][_0xf71c('0x29')]['id']}});}break;case _0xf71c('0x95'):if(_0xd772f4[_0xf71c('0x5b')][_0xf71c('0xd9')]&&_0xd772f4[_0xf71c('0x5b')][_0xf71c('0x2c')]){_0x2b1d3c=_0xd772f4[_0xf71c('0x5b')][_0xf71c('0x2c')];return db['SmsMessage'][_0xf71c('0x5e')]({'where':{'messageId':_0xd772f4[_0xf71c('0x5b')]['redif']}});}break;}}})['then'](handleEntityNotFound(_0x4aa611,null))[_0xf71c('0x22')](function(_0x11eec2){if(_0x11eec2){_0x164397=_0x11eec2;switch(_0x2062a[_0xf71c('0x41')]){case _0xf71c('0x87'):if(_0x2b1d3c===_0xf71c('0xda')){return _0x164397[_0xf71c('0x62')]({'read':!![]});}else{logger[_0xf71c('0x27')](_0xf71c('0xb1'),_0xf71c('0x2c'),_0xf71c('0x87'),util['inspect'](_0xd772f4[_0xf71c('0x5b')],{'showHidden':![],'depth':null}));}break;case'connectel':if(_0x2b1d3c==='1'){return _0x164397[_0xf71c('0x62')]({'read':!![]});}else{logger['error'](_0xf71c('0xb1'),_0xf71c('0x2c'),'skebby',util[_0xf71c('0xcf')](_0xd772f4[_0xf71c('0x5b')],{'showHidden':![],'depth':null}));}break;case _0xf71c('0xd4'):if(!_0xd772f4[_0xf71c('0x5b')][_0xf71c('0xdb')]&&_0x2b1d3c>=0xc8&&_0x2b1d3c<=0xce){return _0x164397[_0xf71c('0x62')]({'read':!![]});}else{logger[_0xf71c('0x27')](_0xf71c('0xb1'),'status','clicksend',util[_0xf71c('0xcf')](_0xd772f4['body'],{'showHidden':![],'depth':null}));}break;case _0xf71c('0xd6'):var _0x4f03f9=_0xd772f4[_0xf71c('0x5b')][_0xf71c('0x8f')][0x0];if(_0x4f03f9[_0xf71c('0x2c')][_0xf71c('0xd7')]===_0xf71c('0xda')){return _0x164397[_0xf71c('0x62')]({'read':!![]});}else{logger['error'](_0xf71c('0xb1'),_0xf71c('0x2c'),_0xf71c('0xd6'),util[_0xf71c('0xcf')](_0xd772f4['body'],{'showHidden':![],'depth':null}));}break;case'clickatell':if(_0x2b1d3c=='DELIVERED_TO_GATEWAY'||_0x2b1d3c==_0xf71c('0xdc')){return _0x164397[_0xf71c('0x62')]({'read':!![]});}else{logger['error']('SmsAccount,\x20%s,\x20%s,\x20%s',_0xf71c('0x2c'),_0xf71c('0x93'),util[_0xf71c('0xcf')](_0xd772f4[_0xf71c('0x5b')],{'showHidden':![],'depth':null}));}break;case'bandwidth':if(_0x2b1d3c==_0xf71c('0xdd')){return _0x164397[_0xf71c('0x62')]({'read':!![]});}else{logger[_0xf71c('0x27')](_0xf71c('0xb1'),_0xf71c('0x2c'),'bandwidth',util['inspect'](_0xd772f4[_0xf71c('0x5b')],{'showHidden':![],'depth':null}));}break;case _0xf71c('0x95'):if(_0x2b1d3c===_0xf71c('0xde')||_0x2b1d3c===_0xf71c('0xdf')){return _0x164397[_0xf71c('0x62')]({'read':!![]});}else{logger['error'](_0xf71c('0xb1'),_0xf71c('0x2c'),_0xf71c('0x95'),util[_0xf71c('0xcf')](_0xd772f4[_0xf71c('0x5b')],{'showHidden':![],'depth':null}));}break;}return _0x164397;}})[_0xf71c('0x22')](respondWithResult(_0x4aa611,null))[_0xf71c('0x2a')](handleError(_0x4aa611,null));};exports['statusGet']=function(_0x5d911f,_0x163a1c,_0x4eb678){var _0x6b3006,_0x2094c4,_0x5a2205;return db['SmsAccount'][_0xf71c('0x5e')]({'where':{'id':_0x5d911f[_0xf71c('0x58')]['id']}})[_0xf71c('0x22')](handleEntityNotFound(_0x163a1c,null))['then'](function(_0x445d66){if(_0x445d66){_0x5a2205=_0x445d66;logger[_0xf71c('0x26')](_0xf71c('0xb1'),_0xf71c('0x2c'),_0xf71c('0xce'),_0x445d66[_0xf71c('0x41')],util[_0xf71c('0xcf')](_0x5d911f[_0xf71c('0x46')],{'showHidden':![],'depth':null}));switch(_0x445d66[_0xf71c('0x41')]){case _0xf71c('0x90'):if(_0x5d911f['query']['message-id']&&_0x5d911f['query'][_0xf71c('0xe0')]){_0x2094c4=_0x5d911f[_0xf71c('0x46')][_0xf71c('0xe0')];return db[_0xf71c('0xd2')][_0xf71c('0x5e')]({'where':{'messageId':_0x5d911f[_0xf71c('0x46')]['message-id']}});}break;}}})[_0xf71c('0x22')](handleEntityNotFound(_0x163a1c,null))[_0xf71c('0x22')](function(_0x6e1181){if(_0x6e1181){_0x6b3006=_0x6e1181;switch(_0x5a2205['type']){case _0xf71c('0x90'):if(_0x2094c4==='2'||_0x2094c4==='6'){return _0x6b3006['update']({'read':!![]});}else{logger[_0xf71c('0x27')](_0xf71c('0xb1'),'status','csc',util[_0xf71c('0xcf')](_0x5d911f[_0xf71c('0x46')],{'showHidden':![],'depth':null}));};break;}return _0x6b3006;}})[_0xf71c('0x22')](respondWithResult(_0x163a1c,null))[_0xf71c('0x2a')](handleError(_0x163a1c,null));};exports[_0xf71c('0xe1')]=function(_0x36fdbe,_0x8b39c1,_0x5c4784){return db[_0xf71c('0x3f')][_0xf71c('0x5e')]({'where':{'id':_0x36fdbe[_0xf71c('0x58')]['id']}})[_0xf71c('0x22')](handleEntityNotFound(_0x8b39c1,null))[_0xf71c('0x22')](function(_0x31679e){if(_0x31679e){return _0x31679e[_0xf71c('0xe1')](_0x36fdbe['body']['ids'],_[_0xf71c('0xa0')](_0x36fdbe[_0xf71c('0x5b')],[_0xf71c('0x6a'),'id'])||{})[_0xf71c('0xcc')](function(_0x117f3f){for(var _0x5893cf=0x0;_0x5893cf<_0x36fdbe[_0xf71c('0x5b')]['ids'][_0xf71c('0x68')];_0x5893cf+=0x1){socket[_0xf71c('0xe2')]('userSmsAccount:save',{'UserId':Number(_0x36fdbe[_0xf71c('0x5b')][_0xf71c('0x6a')][_0x5893cf]),'SmsAccountId':Number(_0x36fdbe['params']['id'])});}return _0x117f3f;});}})['then'](respondWithResult(_0x8b39c1,null))[_0xf71c('0x2a')](handleError(_0x8b39c1,null));};exports[_0xf71c('0xe3')]=function(_0x4bd5d1,_0x48d8ba,_0x46f4ff){return db[_0xf71c('0x3f')][_0xf71c('0x5e')]({'where':{'id':_0x4bd5d1['params']['id']}})['then'](handleEntityNotFound(_0x48d8ba,null))[_0xf71c('0x22')](function(_0x546c57){if(_0x546c57){return _0x546c57[_0xf71c('0xe3')](_0x4bd5d1['query'][_0xf71c('0x6a')])[_0xf71c('0x22')](function(){if(_['isArray'](_0x4bd5d1[_0xf71c('0x46')]['ids'])){for(var _0x2d6a55=0x0;_0x2d6a55<_0x4bd5d1[_0xf71c('0x46')][_0xf71c('0x6a')][_0xf71c('0x68')];_0x2d6a55+=0x1){socket['emit'](_0xf71c('0xe4'),{'UserId':Number(_0x4bd5d1['query'][_0xf71c('0x6a')][_0x2d6a55]),'SmsAccountId':Number(_0x4bd5d1[_0xf71c('0x58')]['id'])});}}else{socket[_0xf71c('0xe2')]('userSmsAccount:remove',{'UserId':Number(_0x4bd5d1[_0xf71c('0x46')][_0xf71c('0x6a')]),'SmsAccountId':Number(_0x4bd5d1['params']['id'])});}});}})[_0xf71c('0x22')](respondWithStatusCode(_0x48d8ba,null))[_0xf71c('0x2a')](handleError(_0x48d8ba,null));};exports['getAgents']=function(_0x40f37c,_0x1f1e66,_0x17cccc){var _0x4a998b={};var _0x27a6c1={};var _0x1957ef;var _0x55d273;return db[_0xf71c('0x3f')][_0xf71c('0x6c')]({'where':{'id':_0x40f37c[_0xf71c('0x58')]['id']}})[_0xf71c('0x22')](handleEntityNotFound(_0x1f1e66,null))['then'](function(_0x3bbb80){if(_0x3bbb80){_0x1957ef=_0x3bbb80;_0x27a6c1[_0xf71c('0x43')]=_['keys'](db['User']['rawAttributes']);_0x27a6c1[_0xf71c('0x46')]=_[_0xf71c('0x45')](_0x40f37c[_0xf71c('0x46')]);_0x27a6c1[_0xf71c('0x4e')]=_[_0xf71c('0x47')](_0x27a6c1[_0xf71c('0x43')],_0x27a6c1[_0xf71c('0x46')]);_0x4a998b[_0xf71c('0x48')]=_[_0xf71c('0x47')](_0x27a6c1[_0xf71c('0x43')],qs['fields'](_0x40f37c['query'][_0xf71c('0x49')]));_0x4a998b['attributes']=_0x4a998b[_0xf71c('0x48')][_0xf71c('0x68')]?_0x4a998b[_0xf71c('0x48')]:_0x27a6c1[_0xf71c('0x43')];_0x4a998b[_0xf71c('0x6d')]=qs['sort'](_0x40f37c[_0xf71c('0x46')][_0xf71c('0x4c')]);_0x4a998b[_0xf71c('0x4d')]=qs[_0xf71c('0x4e')](_[_0xf71c('0x6e')](_0x40f37c[_0xf71c('0x46')],_0x27a6c1['filters']));if(_0x40f37c[_0xf71c('0x46')][_0xf71c('0x4f')]){_0x4a998b[_0xf71c('0x4d')]=_[_0xf71c('0x50')](_0x4a998b[_0xf71c('0x4d')],{'$or':_[_0xf71c('0x44')](_0x4a998b[_0xf71c('0x48')],function(_0x7946e1){var _0x18c612={};_0x18c612[_0x7946e1]={'$like':'%'+_0x40f37c[_0xf71c('0x46')][_0xf71c('0x4f')]+'%'};return _0x18c612;})});}_0x4a998b=_[_0xf71c('0x50')]({},_0x4a998b,_0x40f37c[_0xf71c('0x52')]);return _0x1957ef[_0xf71c('0xe5')](_0x4a998b);}})[_0xf71c('0x22')](function(_0x20c2a9){if(_0x20c2a9){_0x55d273=_0x20c2a9['length'];if(!_0x40f37c[_0xf71c('0x46')]['hasOwnProperty'](_0xf71c('0x4b'))){_0x4a998b[_0xf71c('0x2f')]=qs[_0xf71c('0x2f')](_0x40f37c[_0xf71c('0x46')]['limit']);_0x4a998b[_0xf71c('0x30')]=qs[_0xf71c('0x30')](_0x40f37c[_0xf71c('0x46')][_0xf71c('0x30')]);}return _0x1957ef[_0xf71c('0xe5')](_0x4a998b);}})[_0xf71c('0x22')](function(_0x1dac6c){if(_0x1dac6c){return _0x1dac6c?{'count':_0x55d273,'rows':_0x1dac6c}:null;}})[_0xf71c('0x22')](respondWithResult(_0x1f1e66,null))[_0xf71c('0x2a')](handleError(_0x1f1e66,null));};