b2520ec6996b06d4d43c68367bbed7335be8c561
[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 _0x43f7=['../../config/environment','../../config/license/util','redis','defaults','localhost','socket.io-emitter','register','jayson/promise','client','then','info','SmsAccount,\x20%s,\x20%s','debug','SmsAccount,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code','message','result','catch','sendStatus','status','end','json','offset','limit','count','set','Content-Range','apply','reject','save','update','SmsAccounts','UserProfileResource','destroy','name','send','Pause','mandatoryDispositionPause','SmsAccount','rawAttributes','fieldName','type','model','filters','intersection','query','attributes','fields','hasOwnProperty','nolimit','order','sort','where','filter','merge','map','options','includeAll','include','findAll','show','params','keys','find','create','body','user','UserProfileSection','userProfileId','autoAssociation','describe','SmsAccountId','getDispositions','findOne','Disposition','length','pick','rows','removeDispositions','ids','addAnswer','getAnswers','CannedAnswer','notify','SmsApplication','Applications','orderBy','priority','account','CmContact','Sequelize','ValidationError','difference','createdAt','updatedAt','mobile','twilio','From','Body','messageId','MessageSid','skebby','from','sender','text','connectel','clicksend','message_id','plivo','Text','MessageUUID','infobip','results','csc','originatingAddress','segmentedMessage','clickatell','fromNumber','intelepeer','refid','isNil','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey','CmList','Contacts','DESC','list','omit','contact','SmsInteraction','interaction','get','created','applications','close','*,*,*,*','SmsMessage','ContactId','User','UserId','agent','startRouting','Start','receiveMessage','SmsAccount,\x20%s,\x20%s,\x20%s,\x20%s','entity\x20found','text/xml','<?xml\x20version=\x221.0\x22\x20encoding=\x22UTF-8\x22?>\x0a<Response></Response>','addApplications','sequelize','bulkCreate','findAndCountAll','getApplications','getInteractions','Contact','Owner','internal','Tag','Tags','color','tag','to\x20is\x20mandatory','List','ListId','out','Messages','spread','dataValues','push','received\x20delivery\x20report\x20from','statusInt','requestid','DELIVERED','groupName','bandwidth','redif','inspect','error_code','DELIVERED_TO_GATEWAY','RECEIVED_BY_RECIPIENT','message-delivered','DELIVRD','ACCEPTD','statusGet','message-id','state','addAgents','emit','userSmsAccount:save','removeAgents','isArray','userSmsAccount:remove','getAgents','eml-format','rimraf','zip-dir','request-promise','moment','util','path','sox','to-csv','ejs','fs-extra','lodash','crypto','jsforce','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils'];(function(_0xecbf91,_0x2d6777){var _0x470f1c=function(_0x264885){while(--_0x264885){_0xecbf91['push'](_0xecbf91['shift']());}};_0x470f1c(++_0x2d6777);}(_0x43f7,0xc6));var _0x743f=function(_0x103e1f,_0x4f7384){_0x103e1f=_0x103e1f-0x0;var _0x394f36=_0x43f7[_0x103e1f];return _0x394f36;};'use strict';var emlformat=require(_0x743f('0x0'));var rimraf=require(_0x743f('0x1'));var zipdir=require(_0x743f('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0x743f('0x3'));var moment=require(_0x743f('0x4'));var BPromise=require('bluebird');var Mustache=require('mustache');var util=require(_0x743f('0x5'));var path=require(_0x743f('0x6'));var sox=require(_0x743f('0x7'));var csv=require(_0x743f('0x8'));var ejs=require(_0x743f('0x9'));var fs=require('fs');var fs_extra=require(_0x743f('0xa'));var _=require(_0x743f('0xb'));var squel=require('squel');var crypto=require(_0x743f('0xc'));var jsforce=require(_0x743f('0xd'));var deskjs=require('desk.js');var toCsv=require(_0x743f('0x8'));var querystring=require(_0x743f('0xe'));var Papa=require(_0x743f('0xf'));var Redis=require(_0x743f('0x10'));var authService=require(_0x743f('0x11'));var qs=require(_0x743f('0x12'));var as=require(_0x743f('0x13'));var hardwareService=require(_0x743f('0x14'));var logger=require(_0x743f('0x15'))(_0x743f('0x16'));var utils=require(_0x743f('0x17'));var config=require(_0x743f('0x18'));var licenseUtil=require(_0x743f('0x19'));var db=require('../../mysqldb')['db'];config[_0x743f('0x1a')]=_[_0x743f('0x1b')](config['redis'],{'host':_0x743f('0x1c'),'port':0x18eb});var socket=require(_0x743f('0x1d'))(new Redis(config['redis']));require('./smsAccount.socket')[_0x743f('0x1e')](socket);var jayson=require(_0x743f('0x1f'));var client=jayson[_0x743f('0x20')]['http']({'port':0x232c});function respondWithRpcPromise(_0x24eeef,_0x260588,_0x49b772,_0x4c2fc0){return new BPromise(function(_0x59035e,_0x1e051c){var _0x449887=_0x4c2fc0||client;return _0x449887['request'](_0x24eeef,_0x49b772)[_0x743f('0x21')](function(_0x1bc3fc){logger[_0x743f('0x22')](_0x743f('0x23'),_0x260588,'request\x20sent');logger[_0x743f('0x24')](_0x743f('0x25'),_0x260588,_0x743f('0x26'),JSON[_0x743f('0x27')](_0x1bc3fc));if(_0x1bc3fc[_0x743f('0x28')]){if(_0x1bc3fc[_0x743f('0x28')][_0x743f('0x29')]===0x1f4){logger['error']('SmsAccount,\x20%s,\x20%s',_0x260588,_0x1bc3fc[_0x743f('0x28')]['message']);return _0x1e051c(_0x1bc3fc[_0x743f('0x28')][_0x743f('0x2a')]);}logger['error'](_0x743f('0x23'),_0x260588,_0x1bc3fc[_0x743f('0x28')][_0x743f('0x2a')]);return _0x59035e(_0x1bc3fc['error']['message']);}else{logger[_0x743f('0x22')](_0x743f('0x23'),_0x260588,_0x743f('0x26'));_0x59035e(_0x1bc3fc[_0x743f('0x2b')][_0x743f('0x2a')]);}})[_0x743f('0x2c')](function(_0x48b3d4){logger[_0x743f('0x28')]('SmsAccount,\x20%s,\x20%s',_0x260588,_0x48b3d4);_0x1e051c(_0x48b3d4);});});}function respondWithStatusCode(_0xf6826c,_0x20c4a2){_0x20c4a2=_0x20c4a2||0xcc;return function(_0xcdfb9a){if(_0xcdfb9a){return _0xf6826c[_0x743f('0x2d')](_0x20c4a2);}return _0xf6826c[_0x743f('0x2e')](_0x20c4a2)[_0x743f('0x2f')]();};}function respondWithResult(_0x57982a,_0x4e9a51){_0x4e9a51=_0x4e9a51||0xc8;return function(_0x51ac95){if(_0x51ac95){return _0x57982a[_0x743f('0x2e')](_0x4e9a51)[_0x743f('0x30')](_0x51ac95);}};}function respondWithFilteredResult(_0x3d3c5c,_0x3bcc5b){return function(_0x2f6b88){if(_0x2f6b88){var _0x3448f0=typeof _0x3bcc5b[_0x743f('0x31')]==='undefined'&&typeof _0x3bcc5b[_0x743f('0x32')]==='undefined';var _0x292855=_0x2f6b88['count'];var _0x219c85=_0x3448f0?0x0:_0x3bcc5b[_0x743f('0x31')];var _0x57c006=_0x3448f0?_0x2f6b88[_0x743f('0x33')]:_0x3bcc5b[_0x743f('0x31')]+_0x3bcc5b['limit'];var _0x316cc1;if(_0x57c006>=_0x292855){_0x57c006=_0x292855;_0x316cc1=0xc8;}else{_0x316cc1=0xce;}_0x3d3c5c['status'](_0x316cc1);return _0x3d3c5c[_0x743f('0x34')](_0x743f('0x35'),_0x219c85+'-'+_0x57c006+'/'+_0x292855)[_0x743f('0x30')](_0x2f6b88);}return null;};}function patchUpdates(_0x4b79c1){return function(_0xbdae72){try{jsonpatch[_0x743f('0x36')](_0xbdae72,_0x4b79c1,!![]);}catch(_0x410ad5){return BPromise[_0x743f('0x37')](_0x410ad5);}return _0xbdae72[_0x743f('0x38')]();};}function saveUpdates(_0x6acea9,_0x398b4c){return function(_0x76c6af){if(_0x76c6af){return _0x76c6af[_0x743f('0x39')](_0x6acea9)[_0x743f('0x21')](function(_0xe9badb){return _0xe9badb;});}return null;};}function removeEntity(_0x4030f9,_0x7a0d72){return function(_0x403825){if(_0x403825){return _0x403825['destroy']()[_0x743f('0x21')](function(){var _0x4660ac=_0x403825['get']({'plain':!![]});var _0x1d84dd=_0x743f('0x3a');return db[_0x743f('0x3b')][_0x743f('0x3c')]({'where':{'type':_0x1d84dd,'resourceId':_0x4660ac['id']}})['then'](function(){return _0x403825;});})[_0x743f('0x21')](function(){_0x4030f9['status'](0xcc)[_0x743f('0x2f')]();});}};}function handleEntityNotFound(_0x12e295,_0x46a3ee){return function(_0x50b242){if(!_0x50b242){_0x12e295[_0x743f('0x2d')](0x194);}return _0x50b242;};}function handleError(_0x5e2a4d,_0xa21b69){_0xa21b69=_0xa21b69||0x1f4;return function(_0x3708b9){logger[_0x743f('0x28')](_0x3708b9['stack']);if(_0x3708b9[_0x743f('0x3d')]){delete _0x3708b9[_0x743f('0x3d')];}_0x5e2a4d[_0x743f('0x2e')](_0xa21b69)[_0x743f('0x3e')](_0x3708b9);};}exports['index']=function(_0x249542,_0x298893){var _0x4518c4={'include':[{'model':db[_0x743f('0x3f')],'as':_0x743f('0x40')}]},_0x4f087e={},_0x33cf98={'count':0x0,'rows':[]};var _0x4fa6c4=_['map'](db[_0x743f('0x41')][_0x743f('0x42')],function(_0x574e1c){return{'name':_0x574e1c[_0x743f('0x43')],'type':_0x574e1c[_0x743f('0x44')]['key']};});_0x4f087e[_0x743f('0x45')]=_['map'](_0x4fa6c4,_0x743f('0x3d'));_0x4f087e['query']=_['keys'](_0x249542['query']);_0x4f087e[_0x743f('0x46')]=_[_0x743f('0x47')](_0x4f087e[_0x743f('0x45')],_0x4f087e[_0x743f('0x48')]);_0x4518c4[_0x743f('0x49')]=_[_0x743f('0x47')](_0x4f087e[_0x743f('0x45')],qs[_0x743f('0x4a')](_0x249542['query'][_0x743f('0x4a')]));_0x4518c4[_0x743f('0x49')]=_0x4518c4[_0x743f('0x49')]['length']?_0x4518c4[_0x743f('0x49')]:_0x4f087e[_0x743f('0x45')];if(!_0x249542[_0x743f('0x48')][_0x743f('0x4b')](_0x743f('0x4c'))){_0x4518c4[_0x743f('0x32')]=qs['limit'](_0x249542[_0x743f('0x48')][_0x743f('0x32')]);_0x4518c4[_0x743f('0x31')]=qs['offset'](_0x249542[_0x743f('0x48')]['offset']);}_0x4518c4[_0x743f('0x4d')]=qs[_0x743f('0x4e')](_0x249542[_0x743f('0x48')]['sort']);_0x4518c4[_0x743f('0x4f')]=qs[_0x743f('0x46')](_['pick'](_0x249542[_0x743f('0x48')],_0x4f087e[_0x743f('0x46')]),_0x4fa6c4);if(_0x249542[_0x743f('0x48')][_0x743f('0x50')]){_0x4518c4['where']=_[_0x743f('0x51')](_0x4518c4[_0x743f('0x4f')],{'$or':_[_0x743f('0x52')](_0x4fa6c4,function(_0x1b1c66){if(_0x1b1c66[_0x743f('0x44')]!=='VIRTUAL'){var _0x3ac7e7={};_0x3ac7e7[_0x1b1c66[_0x743f('0x3d')]]={'$like':'%'+_0x249542[_0x743f('0x48')][_0x743f('0x50')]+'%'};return _0x3ac7e7;}})});}_0x4518c4=_[_0x743f('0x51')]({},_0x4518c4,_0x249542[_0x743f('0x53')]);var _0x1cae20={'where':_0x4518c4[_0x743f('0x4f')]};return db[_0x743f('0x41')][_0x743f('0x33')](_0x1cae20)[_0x743f('0x21')](function(_0x26b299){_0x33cf98[_0x743f('0x33')]=_0x26b299;if(_0x249542['query'][_0x743f('0x54')]){_0x4518c4[_0x743f('0x55')]=[{'all':!![]}];}return db[_0x743f('0x41')][_0x743f('0x56')](_0x4518c4);})[_0x743f('0x21')](function(_0x371d14){_0x33cf98['rows']=_0x371d14;return _0x33cf98;})[_0x743f('0x21')](respondWithFilteredResult(_0x298893,_0x4518c4))['catch'](handleError(_0x298893,null));};exports[_0x743f('0x57')]=function(_0xea3401,_0x4d988f){var _0x225741={'raw':![],'where':{'id':_0xea3401[_0x743f('0x58')]['id']},'include':[{'model':db[_0x743f('0x3f')],'as':'mandatoryDispositionPause'}]},_0x4a6118={};_0x4a6118[_0x743f('0x45')]=_[_0x743f('0x59')](db[_0x743f('0x41')][_0x743f('0x42')]);_0x4a6118['query']=_['keys'](_0xea3401[_0x743f('0x48')]);_0x4a6118['filters']=_[_0x743f('0x47')](_0x4a6118[_0x743f('0x45')],_0x4a6118[_0x743f('0x48')]);_0x225741[_0x743f('0x49')]=_[_0x743f('0x47')](_0x4a6118['model'],qs[_0x743f('0x4a')](_0xea3401['query']['fields']));_0x225741[_0x743f('0x49')]=_0x225741[_0x743f('0x49')]['length']?_0x225741['attributes']:_0x4a6118[_0x743f('0x45')];if(_0xea3401['query'][_0x743f('0x54')]){_0x225741['include']=[{'all':!![]}];}_0x225741=_[_0x743f('0x51')]({},_0x225741,_0xea3401['options']);return db['SmsAccount'][_0x743f('0x5a')](_0x225741)[_0x743f('0x21')](handleEntityNotFound(_0x4d988f,null))[_0x743f('0x21')](respondWithResult(_0x4d988f,null))[_0x743f('0x2c')](handleError(_0x4d988f,null));};exports[_0x743f('0x5b')]=function(_0x20e3e1,_0x1f4de9){return db[_0x743f('0x41')][_0x743f('0x5b')](_0x20e3e1[_0x743f('0x5c')],{})[_0x743f('0x21')](function(_0x377299){var _0x23b7a0=_0x20e3e1['user']['get']({'plain':!![]});if(!_0x23b7a0)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x23b7a0['role']===_0x743f('0x5d')){var _0x510982=_0x377299['get']({'plain':!![]});var _0x54a3a2=_0x743f('0x3a');return db[_0x743f('0x5e')][_0x743f('0x5a')]({'where':{'name':_0x54a3a2,'userProfileId':_0x23b7a0[_0x743f('0x5f')]},'raw':!![]})['then'](function(_0x46975e){if(_0x46975e&&_0x46975e[_0x743f('0x60')]===0x0){return db['UserProfileResource']['create']({'name':_0x510982[_0x743f('0x3d')],'resourceId':_0x510982['id'],'type':_0x46975e[_0x743f('0x3d')],'sectionId':_0x46975e['id']},{})['then'](function(){return _0x377299;});}else{return _0x377299;}})[_0x743f('0x2c')](function(_0x27fe76){logger[_0x743f('0x28')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x27fe76);throw _0x27fe76;});}return _0x377299;})[_0x743f('0x21')](respondWithResult(_0x1f4de9,0xc9))[_0x743f('0x2c')](handleError(_0x1f4de9,null));};exports[_0x743f('0x39')]=function(_0x294420,_0x591526){if(_0x294420[_0x743f('0x5c')]['id']){delete _0x294420['body']['id'];}return db[_0x743f('0x41')][_0x743f('0x5a')]({'where':{'id':_0x294420[_0x743f('0x58')]['id']},'include':[{'model':db[_0x743f('0x3f')],'as':_0x743f('0x40')}]})[_0x743f('0x21')](handleEntityNotFound(_0x591526,null))['then'](saveUpdates(_0x294420[_0x743f('0x5c')],null))[_0x743f('0x21')](respondWithResult(_0x591526,null))['catch'](handleError(_0x591526,null));};exports[_0x743f('0x3c')]=function(_0x3a7e2e,_0x304391){return db['SmsAccount'][_0x743f('0x5a')]({'where':{'id':_0x3a7e2e[_0x743f('0x58')]['id']}})[_0x743f('0x21')](handleEntityNotFound(_0x304391,null))[_0x743f('0x21')](removeEntity(_0x304391,null))[_0x743f('0x2c')](handleError(_0x304391,null));};exports[_0x743f('0x61')]=function(_0x617f66,_0x1f98b0){return db['SmsAccount'][_0x743f('0x61')]()[_0x743f('0x21')](respondWithResult(_0x1f98b0,null))[_0x743f('0x2c')](handleError(_0x1f98b0,null));};exports['addDisposition']=function(_0xe0f30f,_0x55a9ad,_0x38e3e0){if(_0xe0f30f[_0x743f('0x5c')]['id']){delete _0xe0f30f[_0x743f('0x5c')]['id'];}return db[_0x743f('0x41')]['find']({'where':{'id':_0xe0f30f[_0x743f('0x58')]['id']}})['then'](handleEntityNotFound(_0x55a9ad,null))[_0x743f('0x21')](function(_0x35123a){if(_0x35123a){_0xe0f30f[_0x743f('0x5c')][_0x743f('0x62')]=_0x35123a['id'];return db['Disposition']['create'](_0xe0f30f[_0x743f('0x5c')]);}})[_0x743f('0x21')](respondWithResult(_0x55a9ad,null))['catch'](handleError(_0x55a9ad,null));};exports[_0x743f('0x63')]=function(_0x4bd6a6,_0x44b9d0,_0x477fdc){var _0xc4a865={'raw':![],'where':{}};var _0x27e11d={};var _0x5e5e73={'count':0x0,'rows':[]};return db['SmsAccount'][_0x743f('0x64')]({'where':{'id':_0x4bd6a6[_0x743f('0x58')]['id']}})[_0x743f('0x21')](handleEntityNotFound(_0x44b9d0,null))[_0x743f('0x21')](function(_0x4f5d97){if(_0x4f5d97){_0x27e11d[_0x743f('0x45')]=_[_0x743f('0x59')](db[_0x743f('0x65')][_0x743f('0x42')]);_0x27e11d[_0x743f('0x48')]=_[_0x743f('0x59')](_0x4bd6a6[_0x743f('0x48')]);_0x27e11d[_0x743f('0x46')]=_[_0x743f('0x47')](_0x27e11d[_0x743f('0x45')],_0x27e11d[_0x743f('0x48')]);_0xc4a865[_0x743f('0x49')]=_[_0x743f('0x47')](_0x27e11d[_0x743f('0x45')],qs[_0x743f('0x4a')](_0x4bd6a6[_0x743f('0x48')][_0x743f('0x4a')]));_0xc4a865[_0x743f('0x49')]=_0xc4a865[_0x743f('0x49')][_0x743f('0x66')]?_0xc4a865['attributes']:_0x27e11d[_0x743f('0x45')];if(!_0x4bd6a6[_0x743f('0x48')][_0x743f('0x4b')](_0x743f('0x4c'))){_0xc4a865[_0x743f('0x32')]=qs[_0x743f('0x32')](_0x4bd6a6[_0x743f('0x48')][_0x743f('0x32')]);_0xc4a865[_0x743f('0x31')]=qs[_0x743f('0x31')](_0x4bd6a6['query'][_0x743f('0x31')]);}_0xc4a865[_0x743f('0x4d')]=qs[_0x743f('0x4e')](_0x4bd6a6[_0x743f('0x48')][_0x743f('0x4e')]);_0xc4a865[_0x743f('0x4f')]=qs[_0x743f('0x46')](_[_0x743f('0x67')](_0x4bd6a6[_0x743f('0x48')],_0x27e11d[_0x743f('0x46')]));_0xc4a865[_0x743f('0x4f')]['SmsAccountId']=_0x4f5d97['id'];if(_0x4bd6a6[_0x743f('0x48')][_0x743f('0x50')]){_0xc4a865[_0x743f('0x4f')]=_[_0x743f('0x51')](_0xc4a865[_0x743f('0x4f')],{'$or':_[_0x743f('0x52')](_0xc4a865[_0x743f('0x49')],function(_0x2f5670){var _0x1d55d1={};_0x1d55d1[_0x2f5670]={'$like':'%'+_0x4bd6a6[_0x743f('0x48')][_0x743f('0x50')]+'%'};return _0x1d55d1;})});}_0xc4a865=_[_0x743f('0x51')]({},_0xc4a865,_0x4bd6a6['options']);return db[_0x743f('0x65')]['count']({'where':_0xc4a865[_0x743f('0x4f')]})['then'](function(_0x4afe29){_0x5e5e73['count']=_0x4afe29;if(_0x4bd6a6[_0x743f('0x48')]['includeAll']){_0xc4a865[_0x743f('0x55')]=[{'all':!![]}];}return db['Disposition']['findAll'](_0xc4a865);})[_0x743f('0x21')](function(_0x3c3524){_0x5e5e73[_0x743f('0x68')]=_0x3c3524;return _0x5e5e73;});}})[_0x743f('0x21')](respondWithFilteredResult(_0x44b9d0,_0xc4a865))[_0x743f('0x2c')](handleError(_0x44b9d0,null));};exports[_0x743f('0x69')]=function(_0x5f07e1,_0x51ea0a,_0x21330e){return db[_0x743f('0x41')][_0x743f('0x5a')]({'where':{'id':_0x5f07e1[_0x743f('0x58')]['id']}})['then'](handleEntityNotFound(_0x51ea0a,null))[_0x743f('0x21')](function(_0x52ff34){if(_0x52ff34){return _0x52ff34[_0x743f('0x69')](_0x5f07e1[_0x743f('0x48')][_0x743f('0x6a')]);}})[_0x743f('0x21')](respondWithStatusCode(_0x51ea0a,null))[_0x743f('0x2c')](handleError(_0x51ea0a,null));};exports[_0x743f('0x6b')]=function(_0x3f0308,_0x46d680,_0x56358c){if(_0x3f0308[_0x743f('0x5c')]['id']){delete _0x3f0308['body']['id'];}return db[_0x743f('0x41')][_0x743f('0x5a')]({'where':{'id':_0x3f0308[_0x743f('0x58')]['id']}})[_0x743f('0x21')](handleEntityNotFound(_0x46d680,null))[_0x743f('0x21')](function(_0x3e0385){if(_0x3e0385){_0x3f0308['body'][_0x743f('0x62')]=_0x3e0385['id'];return db['CannedAnswer']['create'](_0x3f0308[_0x743f('0x5c')]);}})['then'](respondWithResult(_0x46d680,null))[_0x743f('0x2c')](handleError(_0x46d680,null));};exports[_0x743f('0x6c')]=function(_0x3457ad,_0x4212a9,_0x21c5ca){var _0x571f4a={'raw':![],'where':{}};var _0x2c1c6f={};var _0x5dab6e={'count':0x0,'rows':[]};return db[_0x743f('0x41')][_0x743f('0x64')]({'where':{'id':_0x3457ad[_0x743f('0x58')]['id']}})[_0x743f('0x21')](handleEntityNotFound(_0x4212a9,null))['then'](function(_0x32ee18){if(_0x32ee18){_0x2c1c6f[_0x743f('0x45')]=_[_0x743f('0x59')](db[_0x743f('0x6d')][_0x743f('0x42')]);_0x2c1c6f[_0x743f('0x48')]=_[_0x743f('0x59')](_0x3457ad['query']);_0x2c1c6f[_0x743f('0x46')]=_[_0x743f('0x47')](_0x2c1c6f[_0x743f('0x45')],_0x2c1c6f[_0x743f('0x48')]);_0x571f4a[_0x743f('0x49')]=_[_0x743f('0x47')](_0x2c1c6f[_0x743f('0x45')],qs[_0x743f('0x4a')](_0x3457ad[_0x743f('0x48')]['fields']));_0x571f4a[_0x743f('0x49')]=_0x571f4a[_0x743f('0x49')]['length']?_0x571f4a[_0x743f('0x49')]:_0x2c1c6f[_0x743f('0x45')];if(!_0x3457ad[_0x743f('0x48')][_0x743f('0x4b')](_0x743f('0x4c'))){_0x571f4a['limit']=qs[_0x743f('0x32')](_0x3457ad[_0x743f('0x48')][_0x743f('0x32')]);_0x571f4a[_0x743f('0x31')]=qs[_0x743f('0x31')](_0x3457ad[_0x743f('0x48')][_0x743f('0x31')]);}_0x571f4a['order']=qs[_0x743f('0x4e')](_0x3457ad[_0x743f('0x48')][_0x743f('0x4e')]);_0x571f4a['where']=qs[_0x743f('0x46')](_[_0x743f('0x67')](_0x3457ad[_0x743f('0x48')],_0x2c1c6f[_0x743f('0x46')]));_0x571f4a['where'][_0x743f('0x62')]=_0x32ee18['id'];if(_0x3457ad[_0x743f('0x48')][_0x743f('0x50')]){_0x571f4a['where']=_[_0x743f('0x51')](_0x571f4a['where'],{'$or':_[_0x743f('0x52')](_0x571f4a[_0x743f('0x49')],function(_0xa7c256){var _0x569783={};_0x569783[_0xa7c256]={'$like':'%'+_0x3457ad['query'][_0x743f('0x50')]+'%'};return _0x569783;})});}_0x571f4a=_[_0x743f('0x51')]({},_0x571f4a,_0x3457ad[_0x743f('0x53')]);return db[_0x743f('0x6d')][_0x743f('0x33')]({'where':_0x571f4a['where']})[_0x743f('0x21')](function(_0xdc82ec){_0x5dab6e['count']=_0xdc82ec;if(_0x3457ad[_0x743f('0x48')][_0x743f('0x54')]){_0x571f4a[_0x743f('0x55')]=[{'all':!![]}];}return db['CannedAnswer'][_0x743f('0x56')](_0x571f4a);})[_0x743f('0x21')](function(_0x1e5425){_0x5dab6e[_0x743f('0x68')]=_0x1e5425;return _0x5dab6e;});}})[_0x743f('0x21')](respondWithFilteredResult(_0x4212a9,_0x571f4a))[_0x743f('0x2c')](handleError(_0x4212a9,null));};exports['removeAnswers']=function(_0x4c34a9,_0x24b9b1,_0x10049c){return db['SmsAccount']['find']({'where':{'id':_0x4c34a9[_0x743f('0x58')]['id']}})[_0x743f('0x21')](handleEntityNotFound(_0x24b9b1,null))[_0x743f('0x21')](function(_0x5449b5){if(_0x5449b5){return _0x5449b5['removeAnswers'](_0x4c34a9[_0x743f('0x48')][_0x743f('0x6a')]);}})['then'](respondWithStatusCode(_0x24b9b1,null))[_0x743f('0x2c')](handleError(_0x24b9b1,null));};exports[_0x743f('0x6e')]=function(_0x5b1d7f,_0x45a14f,_0x36a443){var _0x375734={'channel':'sms'};var _0x4149db=[];var _0xbf5380=[];var _0x36cd99={};return db[_0x743f('0x41')][_0x743f('0x5a')]({'where':{'id':_0x5b1d7f[_0x743f('0x58')]['id']},'include':[{'model':db[_0x743f('0x6f')],'as':_0x743f('0x70')}]})['then'](handleEntityNotFound(_0x45a14f,null))[_0x743f('0x21')](function(_0x5ee4c8){_0x375734['account']=_0x5ee4c8;_0x375734['applications']=_[_0x743f('0x71')](_0x5ee4c8[_0x743f('0x70')],[_0x743f('0x72')],['asc']);if(_0x375734[_0x743f('0x73')][_0x743f('0x70')]){delete _0x375734[_0x743f('0x73')][_0x743f('0x70')];}return db[_0x743f('0x74')][_0x743f('0x61')]();})[_0x743f('0x21')](function(_0x14c10b){if(!_0x14c10b){throw new db[(_0x743f('0x75'))][(_0x743f('0x76'))]('no\x20available\x20attributes');}_0x4149db=_[_0x743f('0x77')](_[_0x743f('0x59')](_0x14c10b),[_0x743f('0x78'),_0x743f('0x79')]);_0xbf5380=_[_0x743f('0x77')](_[_0x743f('0x59')](_0x14c10b),[_0x743f('0x78'),_0x743f('0x79'),'CompanyId','ListId']);_0x375734[_0x743f('0x5c')]={'mapKey':_0x743f('0x7a')};if(_0x375734[_0x743f('0x73')]['type']){switch(_0x375734[_0x743f('0x73')]['type']){case _0x743f('0x7b'):_0x375734[_0x743f('0x5c')]['from']=_0x5b1d7f['body'][_0x743f('0x7c')];_0x375734[_0x743f('0x5c')][_0x743f('0x5c')]=_0x5b1d7f[_0x743f('0x5c')][_0x743f('0x7d')];_0x375734['body'][_0x743f('0x7e')]=_0x5b1d7f[_0x743f('0x5c')][_0x743f('0x7f')];break;case _0x743f('0x80'):_0x375734['body'][_0x743f('0x81')]='+'+_0x5b1d7f[_0x743f('0x5c')][_0x743f('0x82')];_0x375734[_0x743f('0x5c')][_0x743f('0x5c')]=_0x5b1d7f[_0x743f('0x5c')][_0x743f('0x83')];break;case _0x743f('0x84'):_0x375734[_0x743f('0x5c')]['from']=_0x5b1d7f['body'][_0x743f('0x81')];_0x375734[_0x743f('0x5c')][_0x743f('0x5c')]=_0x5b1d7f[_0x743f('0x5c')]['message'];break;case _0x743f('0x85'):_0x375734[_0x743f('0x5c')][_0x743f('0x81')]=_0x5b1d7f[_0x743f('0x5c')][_0x743f('0x81')];_0x375734[_0x743f('0x5c')]['body']=_0x5b1d7f[_0x743f('0x5c')][_0x743f('0x5c')];_0x375734[_0x743f('0x5c')]['messageId']=_0x5b1d7f[_0x743f('0x5c')][_0x743f('0x86')];break;case _0x743f('0x87'):_0x375734[_0x743f('0x5c')][_0x743f('0x81')]=_0x5b1d7f[_0x743f('0x5c')]['From'];_0x375734[_0x743f('0x5c')][_0x743f('0x5c')]=_0x5b1d7f[_0x743f('0x5c')][_0x743f('0x88')];_0x375734[_0x743f('0x5c')][_0x743f('0x7e')]=_0x5b1d7f[_0x743f('0x5c')][_0x743f('0x89')];break;case _0x743f('0x8a'):_0x375734['body'][_0x743f('0x81')]='+'+_0x5b1d7f[_0x743f('0x5c')][_0x743f('0x8b')][0x0]['from'];_0x375734['body'][_0x743f('0x5c')]=_0x5b1d7f['body'][_0x743f('0x8b')][0x0]['text'];_0x375734[_0x743f('0x5c')][_0x743f('0x7e')]=_0x5b1d7f[_0x743f('0x5c')][_0x743f('0x8b')][0x0][_0x743f('0x7e')];break;case _0x743f('0x8c'):_0x375734['body'][_0x743f('0x81')]=_0x5b1d7f[_0x743f('0x5c')][_0x743f('0x8d')];_0x375734['body'][_0x743f('0x5c')]=_0x5b1d7f[_0x743f('0x5c')][_0x743f('0x8e')]['message'];break;case _0x743f('0x8f'):_0x375734['body'][_0x743f('0x81')]=_0x5b1d7f[_0x743f('0x5c')][_0x743f('0x90')];_0x375734['body']['body']=_0x5b1d7f['body'][_0x743f('0x83')];_0x375734[_0x743f('0x5c')][_0x743f('0x7e')]=_0x5b1d7f[_0x743f('0x5c')]['replyMessageId'];break;case'bandwidth':_0x375734[_0x743f('0x5c')][_0x743f('0x7e')]=_0x5b1d7f[_0x743f('0x5c')][0x0][_0x743f('0x2a')]['id'];_0x375734[_0x743f('0x5c')][_0x743f('0x81')]=_0x5b1d7f[_0x743f('0x5c')][0x0][_0x743f('0x2a')][_0x743f('0x81')];_0x375734[_0x743f('0x5c')]['body']=_0x5b1d7f[_0x743f('0x5c')][0x0][_0x743f('0x2a')][_0x743f('0x83')];break;case _0x743f('0x91'):_0x375734[_0x743f('0x5c')][_0x743f('0x81')]=_0x5b1d7f[_0x743f('0x5c')][_0x743f('0x81')];_0x375734['body'][_0x743f('0x5c')]=_0x5b1d7f[_0x743f('0x5c')]['message'];_0x375734[_0x743f('0x5c')][_0x743f('0x7e')]=_0x5b1d7f[_0x743f('0x5c')][_0x743f('0x92')];break;}}if(_0x375734['body']['id']){delete _0x375734['body']['id'];}if(_[_0x743f('0x93')](_0x375734[_0x743f('0x5c')][_0x743f('0x81')])){throw new db[(_0x743f('0x75'))]['ValidationError']('from\x20is\x20mandatory');}if(_[_0x743f('0x93')](_0x375734['body'][_0x743f('0x5c')])||_0x375734[_0x743f('0x5c')][_0x743f('0x5c')]===''){throw new db['Sequelize'][(_0x743f('0x76'))](_0x743f('0x94'));}if(_['isNil'](_0x375734[_0x743f('0x5c')]['mapKey'])){throw new db['Sequelize'][(_0x743f('0x76'))](_0x743f('0x95')+_0xbf5380);}if(!_[_0x743f('0x96')](_0xbf5380,_0x375734[_0x743f('0x5c')][_0x743f('0x97')])){throw new db[(_0x743f('0x75'))]['ValidationError']('mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20'+_0xbf5380);}_0x36cd99[_0x375734['body'][_0x743f('0x97')]]=_0x375734[_0x743f('0x5c')][_0x743f('0x81')];return db[_0x743f('0x98')][_0x743f('0x5a')]({'where':{'id':_0x375734['account']['ListId']},'include':[{'model':db[_0x743f('0x74')],'as':_0x743f('0x99'),'where':_0x36cd99,'limit':0x1,'order':[[_0x743f('0x79'),_0x743f('0x9a')]]}]});})[_0x743f('0x21')](handleEntityNotFound(_0x45a14f,null))['then'](function(_0x344d14){if(_0x344d14){_0x375734[_0x743f('0x9b')]=_[_0x743f('0x9c')](_0x344d14,[_0x743f('0x99')]);if(_0x344d14[_0x743f('0x99')][_0x743f('0x66')]){return _0x344d14[_0x743f('0x99')][0x0];}var _0x1598cf=_[_0x743f('0x1b')](_0x375734[_0x743f('0x5c')],{'firstName':_0x375734[_0x743f('0x5c')]['from'],'ListId':_0x344d14['id']});_0x1598cf[_0x375734[_0x743f('0x5c')]['mapKey']]=_0x375734[_0x743f('0x5c')]['from'];return db['CmContact'][_0x743f('0x5b')](_0x1598cf,{'fields':_0x4149db,'raw':!![]});}})[_0x743f('0x21')](handleEntityNotFound(_0x45a14f,null))[_0x743f('0x21')](function(_0x24e06f){if(_0x24e06f){_0x375734[_0x743f('0x9d')]=_0x24e06f;var _0x48c63a={'ContactId':_0x24e06f['id'],'phone':_0x375734[_0x743f('0x5c')][_0x743f('0x81')],'SmsAccountId':_0x5b1d7f[_0x743f('0x58')]['id']};var _0x33bb62={'ContactId':_0x24e06f['id'],'SmsAccountId':_0x5b1d7f['params']['id'],'closed':![]};return db[_0x743f('0x9e')][_0x743f('0x5a')]({'where':_0x33bb62})['then'](function(_0x10e05f){if(_0x10e05f){return[_0x10e05f,![]];}return db[_0x743f('0x9e')][_0x743f('0x5b')](_0x48c63a)[_0x743f('0x21')](function(_0x454013){return[_0x454013,!![]];});});}})['spread'](function(_0x594d05,_0x548b3c){_0x375734[_0x743f('0x9f')]=_0x594d05[_0x743f('0xa0')]({'plain':!![]});_0x375734[_0x743f('0x9f')][_0x743f('0xa1')]=![];if(_0x548b3c){if(_0x375734[_0x743f('0x73')]['autoclose']){_0x375734['applications']['push']({'id':0x0,'priority':_0x375734[_0x743f('0xa2')]['length']+0x1,'app':_0x743f('0xa3'),'appdata':'unmanaged','interval':_0x743f('0xa4')});}_0x375734['interaction'][_0x743f('0xa1')]=!![];}return db[_0x743f('0xa5')][_0x743f('0x5b')]({'phone':_0x375734[_0x743f('0x5c')][_0x743f('0x81')],'messageId':_0x375734[_0x743f('0x5c')][_0x743f('0x7e')],'body':_0x375734[_0x743f('0x5c')][_0x743f('0x5c')],'SmsAccountId':_0x5b1d7f[_0x743f('0x58')]['id'],'SmsInteractionId':_0x594d05['id'],'direction':'in','ContactId':_0x594d05[_0x743f('0xa6')],'AttachmentId':_0x375734[_0x743f('0x5c')]['AttachmentId']});})[_0x743f('0x21')](function(_0x2fd8cf){_0x375734[_0x743f('0x2a')]=_0x2fd8cf;if(_0x375734['interaction']['UserId']){return db[_0x743f('0xa7')][_0x743f('0x5a')]({'attributes':['id','name'],'where':{'id':_0x375734[_0x743f('0x9f')][_0x743f('0xa8')]}})[_0x743f('0x21')](function(_0x4acf8f){if(_0x4acf8f){_0x375734['applications']['unshift']({'id':0x0,'priority':0x0,'app':_0x743f('0xa9'),'appdata':_0x4acf8f[_0x743f('0x3d')]+','+(_0x375734[_0x743f('0x73')]['waitForTheAssignedAgent']||0xa),'interval':_0x743f('0xa4')});}return respondWithRpcPromise('Start',_0x743f('0xaa'),_0x375734);});}return respondWithRpcPromise(_0x743f('0xab'),_0x743f('0xaa'),_0x375734);})['then'](function(_0x416e10){if(_0x416e10){var _0x1d4831=0xc8;var _0x97b168=_0x743f('0xac');logger[_0x743f('0x22')]('SmsAccount,\x20%s,\x20%s,\x20%s',_0x97b168,_0x1d4831,'entity\x20found');logger[_0x743f('0x24')](_0x743f('0xad'),_0x97b168,_0x1d4831,_0x743f('0xae'),JSON[_0x743f('0x27')](_0x416e10));if(_0x375734['account'][_0x743f('0x44')]===_0x743f('0x7b')){return _0x45a14f['status'](_0x1d4831)['set']('Content-Type',_0x743f('0xaf'))[_0x743f('0x3e')](_0x743f('0xb0'));}else{return _0x45a14f[_0x743f('0x2e')](_0x1d4831)[_0x743f('0x30')](_0x416e10);}}})[_0x743f('0x2c')](handleError(_0x45a14f,null));};exports[_0x743f('0xb1')]=function(_0x3a12a4,_0x56ee96,_0x5b4d92){return db[_0x743f('0x41')][_0x743f('0x64')]({'where':{'id':_0x3a12a4[_0x743f('0x58')]['id']}})['then'](handleEntityNotFound(_0x56ee96,null))[_0x743f('0x21')](function(_0x172472){if(_0x172472){return db[_0x743f('0xb2')]['transaction'](function(_0x53f1be){return db[_0x743f('0x6f')]['destroy']({'where':{'SmsAccountId':_0x3a12a4[_0x743f('0x58')]['id']},'transaction':_0x53f1be})[_0x743f('0x21')](function(_0xace6d5){var _0x1f0bc3=_[_0x743f('0x52')](_0x3a12a4['body'],function(_0x377c1e){_0x377c1e[_0x743f('0x62')]=_0x3a12a4[_0x743f('0x58')]['id'];return _0x377c1e;});return db[_0x743f('0x6f')][_0x743f('0xb3')](_0x1f0bc3,{'transaction':_0x53f1be});});})[_0x743f('0x21')](function(){return db[_0x743f('0x6f')][_0x743f('0xb4')]({'where':{'SmsAccountId':_0x3a12a4['params']['id']},'order':_0x743f('0x72')});});}})[_0x743f('0x21')](respondWithResult(_0x56ee96,null))[_0x743f('0x2c')](handleError(_0x56ee96,null));};exports[_0x743f('0xb5')]=function(_0x2d4bf5,_0x580dcd,_0x12e4e5){var _0x5c1306={};var _0x14e115={};var _0x41efe;var _0x588a46;return db[_0x743f('0x41')]['findOne']({'where':{'id':_0x2d4bf5[_0x743f('0x58')]['id']}})[_0x743f('0x21')](handleEntityNotFound(_0x580dcd,null))[_0x743f('0x21')](function(_0x2277f2){if(_0x2277f2){_0x41efe=_0x2277f2;_0x14e115[_0x743f('0x45')]=_[_0x743f('0x59')](db[_0x743f('0x6f')][_0x743f('0x42')]);_0x14e115['query']=_[_0x743f('0x59')](_0x2d4bf5[_0x743f('0x48')]);_0x14e115['filters']=_[_0x743f('0x47')](_0x14e115[_0x743f('0x45')],_0x14e115[_0x743f('0x48')]);_0x5c1306[_0x743f('0x49')]=_[_0x743f('0x47')](_0x14e115['model'],qs[_0x743f('0x4a')](_0x2d4bf5[_0x743f('0x48')][_0x743f('0x4a')]));_0x5c1306['attributes']=_0x5c1306[_0x743f('0x49')][_0x743f('0x66')]?_0x5c1306[_0x743f('0x49')]:_0x14e115['model'];_0x5c1306[_0x743f('0x4d')]=qs[_0x743f('0x4e')](_0x2d4bf5[_0x743f('0x48')][_0x743f('0x4e')]);_0x5c1306[_0x743f('0x4f')]=qs['filters'](_['pick'](_0x2d4bf5[_0x743f('0x48')],_0x14e115['filters']));if(_0x2d4bf5[_0x743f('0x48')][_0x743f('0x50')]){_0x5c1306[_0x743f('0x4f')]=_[_0x743f('0x51')](_0x5c1306['where'],{'$or':_[_0x743f('0x52')](_0x5c1306['attributes'],function(_0xa53f03){var _0x2f93ac={};_0x2f93ac[_0xa53f03]={'$like':'%'+_0x2d4bf5[_0x743f('0x48')]['filter']+'%'};return _0x2f93ac;})});}_0x5c1306=_['merge']({},_0x5c1306,_0x2d4bf5[_0x743f('0x53')]);return _0x41efe['getApplications'](_0x5c1306);}})[_0x743f('0x21')](function(_0x1387a4){if(_0x1387a4){_0x588a46=_0x1387a4[_0x743f('0x66')];if(!_0x2d4bf5[_0x743f('0x48')][_0x743f('0x4b')]('nolimit')){_0x5c1306['limit']=qs['limit'](_0x2d4bf5['query'][_0x743f('0x32')]);_0x5c1306[_0x743f('0x31')]=qs[_0x743f('0x31')](_0x2d4bf5['query'][_0x743f('0x31')]);}return _0x41efe['getApplications'](_0x5c1306);}})['then'](function(_0x3b8fe0){if(_0x3b8fe0){return _0x3b8fe0?{'count':_0x588a46,'rows':_0x3b8fe0}:null;}})[_0x743f('0x21')](respondWithResult(_0x580dcd,null))['catch'](handleError(_0x580dcd,null));};exports[_0x743f('0xb6')]=function(_0x3011ed,_0x51b69c,_0x4e4cd3){var _0x5e12c2={'raw':![],'where':{}};var _0x1ae126={};var _0xef2b6b={'count':0x0,'rows':[]};return db[_0x743f('0x41')][_0x743f('0x64')]({'where':{'id':_0x3011ed['params']['id']}})[_0x743f('0x21')](handleEntityNotFound(_0x51b69c,null))[_0x743f('0x21')](function(_0x3d690c){if(_0x3d690c){_0x1ae126[_0x743f('0x45')]=_['keys'](db[_0x743f('0x9e')]['rawAttributes']);_0x1ae126[_0x743f('0x48')]=_[_0x743f('0x59')](_0x3011ed[_0x743f('0x48')]);_0x1ae126[_0x743f('0x46')]=_[_0x743f('0x47')](_0x1ae126[_0x743f('0x45')],_0x1ae126[_0x743f('0x48')]);_0x5e12c2[_0x743f('0x49')]=_[_0x743f('0x47')](_0x1ae126[_0x743f('0x45')],qs[_0x743f('0x4a')](_0x3011ed[_0x743f('0x48')][_0x743f('0x4a')]));_0x5e12c2[_0x743f('0x49')]=_0x5e12c2[_0x743f('0x49')][_0x743f('0x66')]?_0x5e12c2['attributes']:_0x1ae126[_0x743f('0x45')];if(!_0x3011ed[_0x743f('0x48')][_0x743f('0x4b')](_0x743f('0x4c'))){_0x5e12c2[_0x743f('0x32')]=qs[_0x743f('0x32')](_0x3011ed[_0x743f('0x48')][_0x743f('0x32')]);_0x5e12c2[_0x743f('0x31')]=qs[_0x743f('0x31')](_0x3011ed['query']['offset']);}_0x5e12c2[_0x743f('0x4d')]=qs[_0x743f('0x4e')](_0x3011ed[_0x743f('0x48')]['sort']);_0x5e12c2[_0x743f('0x4f')]=qs['filters'](_[_0x743f('0x67')](_0x3011ed[_0x743f('0x48')],_0x1ae126[_0x743f('0x46')]));_0x5e12c2[_0x743f('0x4f')][_0x743f('0x62')]=_0x3d690c['id'];if(_0x3011ed[_0x743f('0x48')][_0x743f('0x50')]){_0x5e12c2[_0x743f('0x4f')]=_[_0x743f('0x51')](_0x5e12c2['where'],{'$or':_[_0x743f('0x52')](_0x5e12c2[_0x743f('0x49')],function(_0x174ce6){var _0x5a59d6={};_0x5a59d6[_0x174ce6]={'$like':'%'+_0x3011ed['query'][_0x743f('0x50')]+'%'};return _0x5a59d6;})});}_0x5e12c2=_[_0x743f('0x51')]({},_0x5e12c2,_0x3011ed[_0x743f('0x53')]);return db[_0x743f('0x9e')][_0x743f('0x33')]({'where':_0x5e12c2['where']})[_0x743f('0x21')](function(_0xa6001b){_0xef2b6b[_0x743f('0x33')]=_0xa6001b;if(_0x3011ed[_0x743f('0x48')][_0x743f('0x54')]){_0x5e12c2[_0x743f('0x55')]=[{'model':db['CmContact'],'as':_0x743f('0xb7'),'required':![]},{'model':db[_0x743f('0xa7')],'as':_0x743f('0xb8'),'attributes':['name','fullname',_0x743f('0xb9')],'required':![]},{'model':db[_0x743f('0xba')],'as':_0x743f('0xbb'),'attributes':['id',_0x743f('0x3d'),_0x743f('0xbc')],'where':_0x3011ed[_0x743f('0x48')][_0x743f('0xbd')]?{'id':_0x3011ed[_0x743f('0x48')]['tag']}:undefined,'required':_0x3011ed[_0x743f('0x48')][_0x743f('0xbd')]?!![]:![]}];}return db[_0x743f('0x9e')][_0x743f('0x56')](_0x5e12c2);})[_0x743f('0x21')](function(_0x185df2){_0xef2b6b[_0x743f('0x68')]=_0x185df2;return _0xef2b6b;});}})[_0x743f('0x21')](respondWithFilteredResult(_0x51b69c,_0x5e12c2))['catch'](handleError(_0x51b69c,null));};exports[_0x743f('0x3e')]=function(_0x1afe9f,_0x5abc6f,_0x36a7e6){var _0x20a57e,_0x45d88e,_0x2fde5e;if(_['isNil'](_0x1afe9f['body'][_0x743f('0x5c')])||_0x1afe9f[_0x743f('0x5c')]['body']===''){throw new db[(_0x743f('0x75'))]['ValidationError']('body\x20is\x20mandatory\x20and\x20not\x20empty');}if(_[_0x743f('0x93')](_0x1afe9f[_0x743f('0x5c')]['to'])){throw new db[(_0x743f('0x75'))][(_0x743f('0x76'))](_0x743f('0xbe'));}_0x20a57e=_0x1afe9f[_0x743f('0x5c')]['to'];return db['SmsAccount']['find']({'where':{'id':_0x1afe9f['params']['id']},'include':[{'model':db[_0x743f('0x98')],'as':_0x743f('0xbf'),'include':[{'model':db[_0x743f('0x74')],'as':_0x743f('0x99'),'where':{'mobile':_0x20a57e},'limit':0x1,'order':[[_0x743f('0x79'),_0x743f('0x9a')]]}]}]})[_0x743f('0x21')](handleEntityNotFound(_0x5abc6f,null))[_0x743f('0x21')](function(_0x46fb07){if(_0x46fb07&&_0x46fb07[_0x743f('0xbf')]){_0x45d88e=_0x46fb07['get']({'plain':!![]});if(_0x45d88e[_0x743f('0xbf')]&&_0x45d88e[_0x743f('0xbf')][_0x743f('0x99')][_0x743f('0x66')]){return _0x45d88e[_0x743f('0xbf')][_0x743f('0x99')][0x0];}return db[_0x743f('0x74')][_0x743f('0x5b')](_[_0x743f('0x1b')](_0x1afe9f[_0x743f('0x5c')],{'firstName':_0x20a57e,'mobile':_0x20a57e,'phone':_0x20a57e,'ListId':_0x45d88e[_0x743f('0xc0')]}));}})[_0x743f('0x21')](handleEntityNotFound(_0x5abc6f,null))[_0x743f('0x21')](function(_0x128c1c){if(_0x128c1c){_0x2fde5e=_0x128c1c;return db[_0x743f('0x9e')][_0x743f('0x5a')]({'where':{'ContactId':_0x2fde5e['id'],'closed':![],'SmsAccountId':_0x45d88e['id']}})[_0x743f('0x21')](function(_0x50decf){if(_0x50decf){return[_0x50decf,![]];}return db[_0x743f('0x9e')][_0x743f('0x5b')]({'UserId':_0x1afe9f[_0x743f('0x5d')]['id'],'ContactId':_0x2fde5e['id'],'SmsAccountId':_0x45d88e['id'],'phone':_0x20a57e,'firstMsgDirection':_0x743f('0xc1'),'Messages':[_[_0x743f('0x51')](_0x1afe9f[_0x743f('0x5c')],{'read':![],'body':_0x1afe9f[_0x743f('0x5c')]['body'],'phone':_0x20a57e,'SmsAccountId':_0x45d88e['id'],'UserId':_0x1afe9f[_0x743f('0x5d')]['id'],'ContactId':_0x2fde5e['id']})]},{'include':[{'model':db['SmsMessage'],'as':_0x743f('0xc2')}]})[_0x743f('0x21')](function(_0x291d9f){return[_0x291d9f,!![]];});});}})[_0x743f('0xc3')](function(_0x43efad,_0x2c04d1){if(_0x2c04d1){return _0x43efad;}return db['SmsMessage'][_0x743f('0x5b')](_['merge'](_0x1afe9f[_0x743f('0x5c')],{'read':![],'body':_0x1afe9f['body'][_0x743f('0x5c')],'phone':_0x20a57e,'SmsAccountId':_0x45d88e['id'],'SmsInteractionId':_0x43efad['id'],'UserId':_0x1afe9f[_0x743f('0x5d')]['id'],'ContactId':_0x2fde5e['id']}))[_0x743f('0x21')](function(_0xc61c49){_0x43efad[_0x743f('0xc4')][_0x743f('0xc2')]=[];_0x43efad['dataValues'][_0x743f('0xc2')][_0x743f('0xc5')](_0xc61c49[_0x743f('0xc4')]);return _0x43efad;});})[_0x743f('0x21')](respondWithResult(_0x5abc6f,null))['catch'](handleError(_0x5abc6f,null));};exports[_0x743f('0x2e')]=function(_0x180202,_0x55e7b2,_0x2cd93f){if(_0x180202['body']['id']){delete _0x180202[_0x743f('0x5c')]['id'];}var _0x25bd42,_0x2d1e28,_0x4be4a6;return db[_0x743f('0x41')]['find']({'where':{'id':_0x180202['params']['id']}})['then'](handleEntityNotFound(_0x55e7b2,null))[_0x743f('0x21')](function(_0x3d675b){if(_0x3d675b){_0x4be4a6=_0x3d675b;logger[_0x743f('0x24')]('SmsAccount,\x20%s,\x20%s,\x20%s',_0x743f('0x2e'),_0x743f('0xc6'),_0x3d675b[_0x743f('0x44')],util['inspect'](_0x180202[_0x743f('0x5c')],{'showHidden':![],'depth':null}));switch(_0x3d675b[_0x743f('0x44')]){case _0x743f('0x80'):if(_0x180202['body']['skebby_dispatch_id']&&_0x180202[_0x743f('0x5c')][_0x743f('0x2e')]){_0x2d1e28=_0x180202['body'][_0x743f('0x2e')];return db[_0x743f('0xa5')][_0x743f('0x5a')]({'where':{'messageId':_0x180202[_0x743f('0x5c')]['skebby_dispatch_id']}});}break;case _0x743f('0x84'):if(_0x180202['body']['requestid']&&_0x180202[_0x743f('0x5c')][_0x743f('0xc7')]){_0x2d1e28=_0x180202[_0x743f('0x5c')][_0x743f('0xc7')];return db['SmsMessage']['find']({'where':{'messageId':_0x180202[_0x743f('0x5c')][_0x743f('0xc8')]}});}break;case'clicksend':if(_0x180202[_0x743f('0x5c')][_0x743f('0x86')]&&_0x180202[_0x743f('0x5c')]['status_code']){_0x2d1e28=_0x180202['body']['status_code'];return db[_0x743f('0xa5')][_0x743f('0x5a')]({'where':{'messageId':_0x180202[_0x743f('0x5c')][_0x743f('0x86')]}});}break;case'infobip':var _0x53f766=_0x180202[_0x743f('0x5c')][_0x743f('0x8b')][0x0];if(_0x53f766['messageId']&&_0x53f766[_0x743f('0x2e')]['groupName']===_0x743f('0xc9')){_0x2d1e28=_0x53f766[_0x743f('0x2e')][_0x743f('0xca')];return db[_0x743f('0xa5')][_0x743f('0x5a')]({'where':{'messageId':_0x53f766[_0x743f('0x7e')]}});}break;case'clickatell':if(_0x180202['body'][_0x743f('0x7e')]&&_0x180202['body'][_0x743f('0x2e')]){_0x2d1e28=_0x180202['body']['status'];return db['SmsMessage'][_0x743f('0x5a')]({'where':{'messageId':_0x180202[_0x743f('0x5c')][_0x743f('0x7e')]}});}break;case _0x743f('0xcb'):if(_0x180202['body'][0x0]['message']['id']){_0x2d1e28=_0x180202[_0x743f('0x5c')][0x0][_0x743f('0x44')];return db[_0x743f('0xa5')][_0x743f('0x5a')]({'where':{'messageId':_0x180202[_0x743f('0x5c')][0x0][_0x743f('0x2a')]['id']}});}break;case _0x743f('0x91'):if(_0x180202[_0x743f('0x5c')][_0x743f('0xcc')]&&_0x180202[_0x743f('0x5c')][_0x743f('0x2e')]){_0x2d1e28=_0x180202['body'][_0x743f('0x2e')];return db[_0x743f('0xa5')]['find']({'where':{'messageId':_0x180202[_0x743f('0x5c')]['redif']}});}break;}}})[_0x743f('0x21')](handleEntityNotFound(_0x55e7b2,null))['then'](function(_0x9267aa){if(_0x9267aa){_0x25bd42=_0x9267aa;switch(_0x4be4a6[_0x743f('0x44')]){case _0x743f('0x80'):if(_0x2d1e28===_0x743f('0xc9')){return _0x25bd42[_0x743f('0x39')]({'read':!![]});}else{logger[_0x743f('0x28')](_0x743f('0x25'),_0x743f('0x2e'),'skebby',util['inspect'](_0x180202['body'],{'showHidden':![],'depth':null}));}break;case'connectel':if(_0x2d1e28==='1'){return _0x25bd42['update']({'read':!![]});}else{logger['error']('SmsAccount,\x20%s,\x20%s,\x20%s','status',_0x743f('0x80'),util[_0x743f('0xcd')](_0x180202[_0x743f('0x5c')],{'showHidden':![],'depth':null}));}break;case _0x743f('0x85'):if(!_0x180202[_0x743f('0x5c')][_0x743f('0xce')]&&_0x2d1e28>=0xc8&&_0x2d1e28<=0xce){return _0x25bd42[_0x743f('0x39')]({'read':!![]});}else{logger[_0x743f('0x28')](_0x743f('0x25'),_0x743f('0x2e'),_0x743f('0x85'),util[_0x743f('0xcd')](_0x180202[_0x743f('0x5c')],{'showHidden':![],'depth':null}));}break;case _0x743f('0x8a'):var _0x3f9121=_0x180202[_0x743f('0x5c')]['results'][0x0];if(_0x3f9121['status']['groupName']===_0x743f('0xc9')){return _0x25bd42[_0x743f('0x39')]({'read':!![]});}else{logger[_0x743f('0x28')](_0x743f('0x25'),_0x743f('0x2e'),_0x743f('0x8a'),util[_0x743f('0xcd')](_0x180202['body'],{'showHidden':![],'depth':null}));}break;case'clickatell':if(_0x2d1e28==_0x743f('0xcf')||_0x2d1e28==_0x743f('0xd0')){return _0x25bd42[_0x743f('0x39')]({'read':!![]});}else{logger[_0x743f('0x28')](_0x743f('0x25'),_0x743f('0x2e'),_0x743f('0x8f'),util[_0x743f('0xcd')](_0x180202[_0x743f('0x5c')],{'showHidden':![],'depth':null}));}break;case _0x743f('0xcb'):if(_0x2d1e28==_0x743f('0xd1')){return _0x25bd42[_0x743f('0x39')]({'read':!![]});}else{logger[_0x743f('0x28')](_0x743f('0x25'),'status',_0x743f('0xcb'),util['inspect'](_0x180202[_0x743f('0x5c')],{'showHidden':![],'depth':null}));}break;case _0x743f('0x91'):if(_0x2d1e28===_0x743f('0xd2')||_0x2d1e28===_0x743f('0xd3')){return _0x25bd42[_0x743f('0x39')]({'read':!![]});}else{logger['error'](_0x743f('0x25'),_0x743f('0x2e'),_0x743f('0x91'),util[_0x743f('0xcd')](_0x180202[_0x743f('0x5c')],{'showHidden':![],'depth':null}));}break;}return _0x25bd42;}})['then'](respondWithResult(_0x55e7b2,null))['catch'](handleError(_0x55e7b2,null));};exports[_0x743f('0xd4')]=function(_0x3157ae,_0x522791,_0xee0b11){var _0x177cd4,_0x8c1bef,_0x4e1e70;return db[_0x743f('0x41')][_0x743f('0x5a')]({'where':{'id':_0x3157ae[_0x743f('0x58')]['id']}})['then'](handleEntityNotFound(_0x522791,null))['then'](function(_0x212ac7){if(_0x212ac7){_0x4e1e70=_0x212ac7;logger[_0x743f('0x24')](_0x743f('0x25'),_0x743f('0x2e'),_0x743f('0xc6'),_0x212ac7[_0x743f('0x44')],util['inspect'](_0x3157ae[_0x743f('0x48')],{'showHidden':![],'depth':null}));switch(_0x212ac7[_0x743f('0x44')]){case _0x743f('0x8c'):if(_0x3157ae[_0x743f('0x48')][_0x743f('0xd5')]&&_0x3157ae[_0x743f('0x48')]['state']){_0x8c1bef=_0x3157ae['query'][_0x743f('0xd6')];return db[_0x743f('0xa5')][_0x743f('0x5a')]({'where':{'messageId':_0x3157ae[_0x743f('0x48')][_0x743f('0xd5')]}});}break;}}})[_0x743f('0x21')](handleEntityNotFound(_0x522791,null))['then'](function(_0x166ef0){if(_0x166ef0){_0x177cd4=_0x166ef0;switch(_0x4e1e70['type']){case _0x743f('0x8c'):if(_0x8c1bef==='2'||_0x8c1bef==='6'){return _0x177cd4[_0x743f('0x39')]({'read':!![]});}else{logger[_0x743f('0x28')]('SmsAccount,\x20%s,\x20%s,\x20%s',_0x743f('0x2e'),'csc',util[_0x743f('0xcd')](_0x3157ae[_0x743f('0x48')],{'showHidden':![],'depth':null}));};break;}return _0x177cd4;}})[_0x743f('0x21')](respondWithResult(_0x522791,null))['catch'](handleError(_0x522791,null));};exports['addAgents']=function(_0x1de4ce,_0x4dfa5b,_0x4b973e){return db['SmsAccount'][_0x743f('0x5a')]({'where':{'id':_0x1de4ce[_0x743f('0x58')]['id']}})[_0x743f('0x21')](handleEntityNotFound(_0x4dfa5b,null))[_0x743f('0x21')](function(_0x1ef36e){if(_0x1ef36e){return _0x1ef36e[_0x743f('0xd7')](_0x1de4ce[_0x743f('0x5c')][_0x743f('0x6a')],_[_0x743f('0x9c')](_0x1de4ce[_0x743f('0x5c')],[_0x743f('0x6a'),'id'])||{})['spread'](function(_0x5f0d30){for(var _0x2da846=0x0;_0x2da846<_0x1de4ce[_0x743f('0x5c')][_0x743f('0x6a')]['length'];_0x2da846+=0x1){socket[_0x743f('0xd8')](_0x743f('0xd9'),{'UserId':Number(_0x1de4ce[_0x743f('0x5c')][_0x743f('0x6a')][_0x2da846]),'SmsAccountId':Number(_0x1de4ce['params']['id'])});}return _0x5f0d30;});}})['then'](respondWithResult(_0x4dfa5b,null))['catch'](handleError(_0x4dfa5b,null));};exports['removeAgents']=function(_0xd93e95,_0x56dab2,_0x3ef19b){return db[_0x743f('0x41')]['find']({'where':{'id':_0xd93e95[_0x743f('0x58')]['id']}})[_0x743f('0x21')](handleEntityNotFound(_0x56dab2,null))['then'](function(_0xa38042){if(_0xa38042){return _0xa38042[_0x743f('0xda')](_0xd93e95[_0x743f('0x48')][_0x743f('0x6a')])[_0x743f('0x21')](function(){if(_[_0x743f('0xdb')](_0xd93e95['query'][_0x743f('0x6a')])){for(var _0x3c918e=0x0;_0x3c918e<_0xd93e95[_0x743f('0x48')]['ids'][_0x743f('0x66')];_0x3c918e+=0x1){socket['emit'](_0x743f('0xdc'),{'UserId':Number(_0xd93e95['query']['ids'][_0x3c918e]),'SmsAccountId':Number(_0xd93e95[_0x743f('0x58')]['id'])});}}else{socket[_0x743f('0xd8')](_0x743f('0xdc'),{'UserId':Number(_0xd93e95['query'][_0x743f('0x6a')]),'SmsAccountId':Number(_0xd93e95[_0x743f('0x58')]['id'])});}});}})[_0x743f('0x21')](respondWithStatusCode(_0x56dab2,null))[_0x743f('0x2c')](handleError(_0x56dab2,null));};exports['getAgents']=function(_0x2deec0,_0x58f3a0,_0x12b1d1){var _0x217f10={};var _0x4a871b={};var _0x7ce21b;var _0x125e6f;return db[_0x743f('0x41')]['findOne']({'where':{'id':_0x2deec0['params']['id']}})[_0x743f('0x21')](handleEntityNotFound(_0x58f3a0,null))[_0x743f('0x21')](function(_0x407c0f){if(_0x407c0f){_0x7ce21b=_0x407c0f;_0x4a871b[_0x743f('0x45')]=_['keys'](db[_0x743f('0xa7')][_0x743f('0x42')]);_0x4a871b[_0x743f('0x48')]=_[_0x743f('0x59')](_0x2deec0[_0x743f('0x48')]);_0x4a871b[_0x743f('0x46')]=_[_0x743f('0x47')](_0x4a871b['model'],_0x4a871b[_0x743f('0x48')]);_0x217f10[_0x743f('0x49')]=_['intersection'](_0x4a871b[_0x743f('0x45')],qs[_0x743f('0x4a')](_0x2deec0[_0x743f('0x48')][_0x743f('0x4a')]));_0x217f10['attributes']=_0x217f10[_0x743f('0x49')][_0x743f('0x66')]?_0x217f10[_0x743f('0x49')]:_0x4a871b[_0x743f('0x45')];_0x217f10[_0x743f('0x4d')]=qs['sort'](_0x2deec0['query'][_0x743f('0x4e')]);_0x217f10[_0x743f('0x4f')]=qs[_0x743f('0x46')](_[_0x743f('0x67')](_0x2deec0['query'],_0x4a871b[_0x743f('0x46')]));if(_0x2deec0[_0x743f('0x48')][_0x743f('0x50')]){_0x217f10[_0x743f('0x4f')]=_['merge'](_0x217f10[_0x743f('0x4f')],{'$or':_[_0x743f('0x52')](_0x217f10['attributes'],function(_0x4f91b0){var _0x2fc076={};_0x2fc076[_0x4f91b0]={'$like':'%'+_0x2deec0[_0x743f('0x48')][_0x743f('0x50')]+'%'};return _0x2fc076;})});}_0x217f10=_['merge']({},_0x217f10,_0x2deec0[_0x743f('0x53')]);return _0x7ce21b[_0x743f('0xdd')](_0x217f10);}})[_0x743f('0x21')](function(_0xed55cd){if(_0xed55cd){_0x125e6f=_0xed55cd[_0x743f('0x66')];if(!_0x2deec0[_0x743f('0x48')][_0x743f('0x4b')]('nolimit')){_0x217f10[_0x743f('0x32')]=qs[_0x743f('0x32')](_0x2deec0[_0x743f('0x48')]['limit']);_0x217f10['offset']=qs['offset'](_0x2deec0[_0x743f('0x48')][_0x743f('0x31')]);}return _0x7ce21b['getAgents'](_0x217f10);}})[_0x743f('0x21')](function(_0x33f847){if(_0x33f847){return _0x33f847?{'count':_0x125e6f,'rows':_0x33f847}:null;}})['then'](respondWithResult(_0x58f3a0,null))['catch'](handleError(_0x58f3a0,null));};