3328e3e6b2232737e64e42a6ea07021732d063a5
[motion2.git] / server / api / whatsappAccount / whatsappAccount.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 _0x2f8b=['offset','set','Content-Range','apply','reject','update','destroy','WhatsappAccounts','UserProfileResource','stack','name','send','index','Pause','map','WhatsappAccount','rawAttributes','fieldName','type','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','order','sort','where','pick','filter','merge','VIRTUAL','count','includeAll','include','findAll','show','params','mandatoryDispositionPause','options','find','catch','user','get','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','userProfileId','autoAssociation','create','body','describe','addDisposition','WhatsappAccountId','getDispositions','findOne','Disposition','nolimit','removeDispositions','ids','addAnswer','CannedAnswer','removeAnswers','addAgents','omit','spread','emit','removeAgents','userWhatsappAccount:remove','getAgents','User','addApplications','sequelize','Transaction','ISOLATION_LEVELS','WhatsappApplication','bulkCreate','findAndCountAll','finally','getApplications','notify','whatsapp','Applications','account','sinch','statuses','state','read','timestamp','message_id','applications','orderBy','priority','asc','CmContact','Sequelize','ValidationError','no\x20available\x20attributes','difference','createdAt','updatedAt','CompanyId','ListId','mobile','twilio','From','replace','messageId','MessageSid','notifications','from','isNil','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','includes','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','CmList','Contacts','list','WhatsappInteraction','interaction','created','autoclose','push','close','unmanaged','*,*,*,*','WhatsappMessage','UserId','unshift','agent','waitForTheAssignedAgent','queueId','WhatsappQueue','queue','waitForTheAssignedQueue','receiveMessage','entity\x20found','WhatsappAccount,\x20%s,\x20%s,\x20%s,\x20%s','Content-Type','text/xml','<?xml\x20version=\x221.0\x22\x20encoding=\x22UTF-8\x22?>\x0a<Response></Response>','to\x20is\x20mandatory','List','out','Messages','eml-format','rimraf','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','to-csv','ejs','fs-extra','squel','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','redis','defaults','localhost','socket.io-emitter','register','jayson/promise','http','then','WhatsappAccount,\x20%s,\x20%s','debug','WhatsappAccount,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code','message','info','result','sendStatus','status','end','json','undefined','limit'];(function(_0x38d4e5,_0x16acad){var _0x336665=function(_0x3be119){while(--_0x3be119){_0x38d4e5['push'](_0x38d4e5['shift']());}};_0x336665(++_0x16acad);}(_0x2f8b,0x96));var _0xb2f8=function(_0x99508b,_0x738feb){_0x99508b=_0x99508b-0x0;var _0x693e60=_0x2f8b[_0x99508b];return _0x693e60;};'use strict';var emlformat=require(_0xb2f8('0x0'));var rimraf=require(_0xb2f8('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0xb2f8('0x2'));var rp=require(_0xb2f8('0x3'));var moment=require(_0xb2f8('0x4'));var BPromise=require(_0xb2f8('0x5'));var Mustache=require(_0xb2f8('0x6'));var util=require(_0xb2f8('0x7'));var path=require(_0xb2f8('0x8'));var sox=require('sox');var csv=require(_0xb2f8('0x9'));var ejs=require(_0xb2f8('0xa'));var fs=require('fs');var fs_extra=require(_0xb2f8('0xb'));var _=require('lodash');var squel=require(_0xb2f8('0xc'));var crypto=require('crypto');var jsforce=require(_0xb2f8('0xd'));var deskjs=require(_0xb2f8('0xe'));var toCsv=require(_0xb2f8('0x9'));var querystring=require(_0xb2f8('0xf'));var Papa=require(_0xb2f8('0x10'));var Redis=require(_0xb2f8('0x11'));var authService=require(_0xb2f8('0x12'));var qs=require('../../components/parsers/qs');var as=require(_0xb2f8('0x13'));var hardwareService=require(_0xb2f8('0x14'));var logger=require(_0xb2f8('0x15'))(_0xb2f8('0x16'));var utils=require(_0xb2f8('0x17'));var config=require(_0xb2f8('0x18'));var licenseUtil=require(_0xb2f8('0x19'));var db=require(_0xb2f8('0x1a'))['db'];config[_0xb2f8('0x1b')]=_[_0xb2f8('0x1c')](config[_0xb2f8('0x1b')],{'host':_0xb2f8('0x1d'),'port':0x18eb});var socket=require(_0xb2f8('0x1e'))(new Redis(config['redis']));require('./whatsappAccount.socket')[_0xb2f8('0x1f')](socket);var jayson=require(_0xb2f8('0x20'));var client=jayson['client'][_0xb2f8('0x21')]({'port':0x232c});function respondWithRpcPromise(_0x6d73fd,_0x3c30a6,_0x2097e7,_0x57d3c8){return new BPromise(function(_0x4aabd4,_0x2a39a4){var _0x20ebf9=_0x57d3c8||client;return _0x20ebf9['request'](_0x6d73fd,_0x2097e7)[_0xb2f8('0x22')](function(_0x31e436){logger['info'](_0xb2f8('0x23'),_0x3c30a6,'request\x20sent');logger[_0xb2f8('0x24')](_0xb2f8('0x25'),_0x3c30a6,_0xb2f8('0x26'),JSON[_0xb2f8('0x27')](_0x31e436));if(_0x31e436[_0xb2f8('0x28')]){if(_0x31e436[_0xb2f8('0x28')][_0xb2f8('0x29')]===0x1f4){logger[_0xb2f8('0x28')](_0xb2f8('0x23'),_0x3c30a6,_0x31e436[_0xb2f8('0x28')]['message']);return _0x2a39a4(_0x31e436[_0xb2f8('0x28')]['message']);}logger[_0xb2f8('0x28')](_0xb2f8('0x23'),_0x3c30a6,_0x31e436[_0xb2f8('0x28')][_0xb2f8('0x2a')]);return _0x4aabd4(_0x31e436['error'][_0xb2f8('0x2a')]);}else{logger[_0xb2f8('0x2b')](_0xb2f8('0x23'),_0x3c30a6,'request\x20sent');_0x4aabd4(_0x31e436[_0xb2f8('0x2c')]['message']);}})['catch'](function(_0x184b24){logger[_0xb2f8('0x28')]('WhatsappAccount,\x20%s,\x20%s',_0x3c30a6,_0x184b24);_0x2a39a4(_0x184b24);});});}function respondWithStatusCode(_0x78fa37,_0x3643e4){_0x3643e4=_0x3643e4||0xcc;return function(_0x52e72b){if(_0x52e72b){return _0x78fa37[_0xb2f8('0x2d')](_0x3643e4);}return _0x78fa37[_0xb2f8('0x2e')](_0x3643e4)[_0xb2f8('0x2f')]();};}function respondWithResult(_0x55e4a3,_0x42355d){_0x42355d=_0x42355d||0xc8;return function(_0x3ca25b){if(_0x3ca25b){return _0x55e4a3[_0xb2f8('0x2e')](_0x42355d)[_0xb2f8('0x30')](_0x3ca25b);}};}function respondWithFilteredResult(_0x2ceb6e,_0x5de25e){return function(_0x2a2dcf){if(_0x2a2dcf){var _0x190115=typeof _0x5de25e['offset']===_0xb2f8('0x31')&&typeof _0x5de25e[_0xb2f8('0x32')]===_0xb2f8('0x31');var _0x40d9d0=_0x2a2dcf['count'];var _0x2695e7=_0x190115?0x0:_0x5de25e[_0xb2f8('0x33')];var _0x196a14=_0x190115?_0x2a2dcf['count']:_0x5de25e[_0xb2f8('0x33')]+_0x5de25e['limit'];var _0x19f582;if(_0x196a14>=_0x40d9d0){_0x196a14=_0x40d9d0;_0x19f582=0xc8;}else{_0x19f582=0xce;}_0x2ceb6e[_0xb2f8('0x2e')](_0x19f582);return _0x2ceb6e[_0xb2f8('0x34')](_0xb2f8('0x35'),_0x2695e7+'-'+_0x196a14+'/'+_0x40d9d0)[_0xb2f8('0x30')](_0x2a2dcf);}return null;};}function patchUpdates(_0x31f922){return function(_0x47720c){try{jsonpatch[_0xb2f8('0x36')](_0x47720c,_0x31f922,!![]);}catch(_0x38fb3a){return BPromise[_0xb2f8('0x37')](_0x38fb3a);}return _0x47720c['save']();};}function saveUpdates(_0xd3b017,_0x28f7ca){return function(_0x3d9df0){if(_0x3d9df0){return _0x3d9df0[_0xb2f8('0x38')](_0xd3b017)['then'](function(_0x17a62c){return _0x17a62c;});}return null;};}function removeEntity(_0xda8928,_0x3e05b1){return function(_0x20fbfe){if(_0x20fbfe){return _0x20fbfe[_0xb2f8('0x39')]()['then'](function(){var _0x3454ee=_0x20fbfe['get']({'plain':!![]});var _0x5d952f=_0xb2f8('0x3a');return db[_0xb2f8('0x3b')][_0xb2f8('0x39')]({'where':{'type':_0x5d952f,'resourceId':_0x3454ee['id']}})[_0xb2f8('0x22')](function(){return _0x20fbfe;});})[_0xb2f8('0x22')](function(){_0xda8928[_0xb2f8('0x2e')](0xcc)['end']();});}};}function handleEntityNotFound(_0x2d9a9a,_0x225d68){return function(_0x3f4295){if(!_0x3f4295){_0x2d9a9a['sendStatus'](0x194);}return _0x3f4295;};}function handleError(_0x4ca67a,_0x4d0b98){_0x4d0b98=_0x4d0b98||0x1f4;return function(_0x16f724){logger[_0xb2f8('0x28')](_0x16f724[_0xb2f8('0x3c')]);if(_0x16f724['name']){delete _0x16f724[_0xb2f8('0x3d')];}_0x4ca67a[_0xb2f8('0x2e')](_0x4d0b98)[_0xb2f8('0x3e')](_0x16f724);};}exports[_0xb2f8('0x3f')]=function(_0x340b3e,_0x11ad54){var _0x1e4428={'include':[{'model':db[_0xb2f8('0x40')],'as':'mandatoryDispositionPause'}]},_0x54ede4={},_0x144505={'count':0x0,'rows':[]};var _0x34cc83=_[_0xb2f8('0x41')](db[_0xb2f8('0x42')][_0xb2f8('0x43')],function(_0x7ac8e5){return{'name':_0x7ac8e5[_0xb2f8('0x44')],'type':_0x7ac8e5[_0xb2f8('0x45')][_0xb2f8('0x46')]};});_0x54ede4[_0xb2f8('0x47')]=_[_0xb2f8('0x41')](_0x34cc83,'name');_0x54ede4[_0xb2f8('0x48')]=_[_0xb2f8('0x49')](_0x340b3e[_0xb2f8('0x48')]);_0x54ede4[_0xb2f8('0x4a')]=_[_0xb2f8('0x4b')](_0x54ede4[_0xb2f8('0x47')],_0x54ede4['query']);_0x1e4428[_0xb2f8('0x4c')]=_[_0xb2f8('0x4b')](_0x54ede4['model'],qs[_0xb2f8('0x4d')](_0x340b3e[_0xb2f8('0x48')][_0xb2f8('0x4d')]));_0x1e4428['attributes']=_0x1e4428[_0xb2f8('0x4c')][_0xb2f8('0x4e')]?_0x1e4428['attributes']:_0x54ede4['model'];if(!_0x340b3e[_0xb2f8('0x48')][_0xb2f8('0x4f')]('nolimit')){_0x1e4428[_0xb2f8('0x32')]=qs['limit'](_0x340b3e[_0xb2f8('0x48')][_0xb2f8('0x32')]);_0x1e4428[_0xb2f8('0x33')]=qs[_0xb2f8('0x33')](_0x340b3e[_0xb2f8('0x48')]['offset']);}_0x1e4428[_0xb2f8('0x50')]=qs[_0xb2f8('0x51')](_0x340b3e['query'][_0xb2f8('0x51')]);_0x1e4428[_0xb2f8('0x52')]=qs[_0xb2f8('0x4a')](_[_0xb2f8('0x53')](_0x340b3e['query'],_0x54ede4[_0xb2f8('0x4a')]),_0x34cc83);if(_0x340b3e['query'][_0xb2f8('0x54')]){_0x1e4428['where']=_[_0xb2f8('0x55')](_0x1e4428[_0xb2f8('0x52')],{'$or':_[_0xb2f8('0x41')](_0x34cc83,function(_0x324216){if(_0x324216[_0xb2f8('0x45')]!==_0xb2f8('0x56')){var _0x5763f4={};_0x5763f4[_0x324216[_0xb2f8('0x3d')]]={'$like':'%'+_0x340b3e[_0xb2f8('0x48')][_0xb2f8('0x54')]+'%'};return _0x5763f4;}})});}_0x1e4428=_[_0xb2f8('0x55')]({},_0x1e4428,_0x340b3e['options']);var _0x1d42c1={'where':_0x1e4428['where']};return db[_0xb2f8('0x42')][_0xb2f8('0x57')](_0x1d42c1)['then'](function(_0x1155ff){_0x144505[_0xb2f8('0x57')]=_0x1155ff;if(_0x340b3e[_0xb2f8('0x48')][_0xb2f8('0x58')]){_0x1e4428[_0xb2f8('0x59')]=[{'all':!![]}];}return db['WhatsappAccount'][_0xb2f8('0x5a')](_0x1e4428);})[_0xb2f8('0x22')](function(_0x139354){_0x144505['rows']=_0x139354;return _0x144505;})[_0xb2f8('0x22')](respondWithFilteredResult(_0x11ad54,_0x1e4428))['catch'](handleError(_0x11ad54,null));};exports[_0xb2f8('0x5b')]=function(_0x28c367,_0x24b6ec){var _0x437790={'raw':![],'where':{'id':_0x28c367[_0xb2f8('0x5c')]['id']},'include':[{'model':db[_0xb2f8('0x40')],'as':_0xb2f8('0x5d')}]},_0x51e992={};_0x51e992[_0xb2f8('0x47')]=_['keys'](db['WhatsappAccount'][_0xb2f8('0x43')]);_0x51e992[_0xb2f8('0x48')]=_['keys'](_0x28c367[_0xb2f8('0x48')]);_0x51e992['filters']=_[_0xb2f8('0x4b')](_0x51e992[_0xb2f8('0x47')],_0x51e992[_0xb2f8('0x48')]);_0x437790[_0xb2f8('0x4c')]=_[_0xb2f8('0x4b')](_0x51e992[_0xb2f8('0x47')],qs['fields'](_0x28c367[_0xb2f8('0x48')][_0xb2f8('0x4d')]));_0x437790[_0xb2f8('0x4c')]=_0x437790[_0xb2f8('0x4c')]['length']?_0x437790[_0xb2f8('0x4c')]:_0x51e992['model'];if(_0x28c367[_0xb2f8('0x48')][_0xb2f8('0x58')]){_0x437790[_0xb2f8('0x59')]=[{'all':!![]}];}_0x437790=_[_0xb2f8('0x55')]({},_0x437790,_0x28c367[_0xb2f8('0x5e')]);return db[_0xb2f8('0x42')][_0xb2f8('0x5f')](_0x437790)[_0xb2f8('0x22')](handleEntityNotFound(_0x24b6ec,null))[_0xb2f8('0x22')](respondWithResult(_0x24b6ec,null))[_0xb2f8('0x60')](handleError(_0x24b6ec,null));};exports['create']=function(_0x5347a1,_0x2b453b){return db[_0xb2f8('0x42')]['create'](_0x5347a1['body'],{})['then'](function(_0x3ceba4){var _0x55d894=_0x5347a1[_0xb2f8('0x61')][_0xb2f8('0x62')]({'plain':!![]});if(!_0x55d894)throw new Error(_0xb2f8('0x63'));if(_0x55d894['role']===_0xb2f8('0x61')){var _0x4ec32a=_0x3ceba4['get']({'plain':!![]});var _0x594f02=_0xb2f8('0x3a');return db[_0xb2f8('0x64')][_0xb2f8('0x5f')]({'where':{'name':_0x594f02,'userProfileId':_0x55d894[_0xb2f8('0x65')]},'raw':!![]})[_0xb2f8('0x22')](function(_0x5af46e){if(_0x5af46e&&_0x5af46e[_0xb2f8('0x66')]===0x0){return db[_0xb2f8('0x3b')][_0xb2f8('0x67')]({'name':_0x4ec32a[_0xb2f8('0x3d')],'resourceId':_0x4ec32a['id'],'type':_0x5af46e['name'],'sectionId':_0x5af46e['id']},{})[_0xb2f8('0x22')](function(){return _0x3ceba4;});}else{return _0x3ceba4;}})['catch'](function(_0xed0baa){logger[_0xb2f8('0x28')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0xed0baa);throw _0xed0baa;});}return _0x3ceba4;})['then'](respondWithResult(_0x2b453b,0xc9))[_0xb2f8('0x60')](handleError(_0x2b453b,null));};exports[_0xb2f8('0x38')]=function(_0x24e90b,_0x3f250a){if(_0x24e90b[_0xb2f8('0x68')]['id']){delete _0x24e90b[_0xb2f8('0x68')]['id'];}return db[_0xb2f8('0x42')]['find']({'where':{'id':_0x24e90b['params']['id']},'include':[{'model':db[_0xb2f8('0x40')],'as':'mandatoryDispositionPause'}]})[_0xb2f8('0x22')](handleEntityNotFound(_0x3f250a,null))[_0xb2f8('0x22')](saveUpdates(_0x24e90b[_0xb2f8('0x68')],null))[_0xb2f8('0x22')](respondWithResult(_0x3f250a,null))[_0xb2f8('0x60')](handleError(_0x3f250a,null));};exports[_0xb2f8('0x39')]=function(_0x4ba64b,_0x501d7e){return db[_0xb2f8('0x42')][_0xb2f8('0x5f')]({'where':{'id':_0x4ba64b[_0xb2f8('0x5c')]['id']}})['then'](handleEntityNotFound(_0x501d7e,null))[_0xb2f8('0x22')](removeEntity(_0x501d7e,null))['catch'](handleError(_0x501d7e,null));};exports[_0xb2f8('0x69')]=function(_0x1173b7,_0xf12acc){return db[_0xb2f8('0x42')]['describe']()['then'](respondWithResult(_0xf12acc,null))[_0xb2f8('0x60')](handleError(_0xf12acc,null));};exports[_0xb2f8('0x6a')]=function(_0x4701a6,_0x143df7,_0xcf6c17){if(_0x4701a6[_0xb2f8('0x68')]['id']){delete _0x4701a6['body']['id'];}return db[_0xb2f8('0x42')]['find']({'where':{'id':_0x4701a6[_0xb2f8('0x5c')]['id']}})[_0xb2f8('0x22')](handleEntityNotFound(_0x143df7,null))[_0xb2f8('0x22')](function(_0x38e829){if(_0x38e829){_0x4701a6['body'][_0xb2f8('0x6b')]=_0x38e829['id'];return db['Disposition']['create'](_0x4701a6['body']);}})['then'](respondWithResult(_0x143df7,null))[_0xb2f8('0x60')](handleError(_0x143df7,null));};exports[_0xb2f8('0x6c')]=function(_0x3f77fb,_0x427cee,_0x12f1d6){var _0x53db48={'raw':![],'where':{}};var _0x28c6c2={};var _0x3016af={'count':0x0,'rows':[]};return db['WhatsappAccount'][_0xb2f8('0x6d')]({'where':{'id':_0x3f77fb[_0xb2f8('0x5c')]['id']}})[_0xb2f8('0x22')](handleEntityNotFound(_0x427cee,null))[_0xb2f8('0x22')](function(_0xd2f16c){if(_0xd2f16c){_0x28c6c2[_0xb2f8('0x47')]=_[_0xb2f8('0x49')](db[_0xb2f8('0x6e')][_0xb2f8('0x43')]);_0x28c6c2[_0xb2f8('0x48')]=_[_0xb2f8('0x49')](_0x3f77fb[_0xb2f8('0x48')]);_0x28c6c2[_0xb2f8('0x4a')]=_['intersection'](_0x28c6c2[_0xb2f8('0x47')],_0x28c6c2[_0xb2f8('0x48')]);_0x53db48[_0xb2f8('0x4c')]=_[_0xb2f8('0x4b')](_0x28c6c2[_0xb2f8('0x47')],qs[_0xb2f8('0x4d')](_0x3f77fb[_0xb2f8('0x48')]['fields']));_0x53db48[_0xb2f8('0x4c')]=_0x53db48[_0xb2f8('0x4c')][_0xb2f8('0x4e')]?_0x53db48['attributes']:_0x28c6c2[_0xb2f8('0x47')];if(!_0x3f77fb[_0xb2f8('0x48')]['hasOwnProperty'](_0xb2f8('0x6f'))){_0x53db48[_0xb2f8('0x32')]=qs[_0xb2f8('0x32')](_0x3f77fb[_0xb2f8('0x48')][_0xb2f8('0x32')]);_0x53db48[_0xb2f8('0x33')]=qs[_0xb2f8('0x33')](_0x3f77fb[_0xb2f8('0x48')][_0xb2f8('0x33')]);}_0x53db48[_0xb2f8('0x50')]=qs[_0xb2f8('0x51')](_0x3f77fb[_0xb2f8('0x48')]['sort']);_0x53db48[_0xb2f8('0x52')]=qs[_0xb2f8('0x4a')](_[_0xb2f8('0x53')](_0x3f77fb[_0xb2f8('0x48')],_0x28c6c2['filters']));_0x53db48['where'][_0xb2f8('0x6b')]=_0xd2f16c['id'];if(_0x3f77fb[_0xb2f8('0x48')]['filter']){_0x53db48[_0xb2f8('0x52')]=_[_0xb2f8('0x55')](_0x53db48['where'],{'$or':_[_0xb2f8('0x41')](_0x53db48[_0xb2f8('0x4c')],function(_0x31bed1){var _0x550feb={};_0x550feb[_0x31bed1]={'$like':'%'+_0x3f77fb[_0xb2f8('0x48')]['filter']+'%'};return _0x550feb;})});}_0x53db48=_[_0xb2f8('0x55')]({},_0x53db48,_0x3f77fb['options']);return db[_0xb2f8('0x6e')][_0xb2f8('0x57')]({'where':_0x53db48['where']})['then'](function(_0x30d76a){_0x3016af['count']=_0x30d76a;if(_0x3f77fb[_0xb2f8('0x48')][_0xb2f8('0x58')]){_0x53db48['include']=[{'all':!![]}];}return db['Disposition']['findAll'](_0x53db48);})[_0xb2f8('0x22')](function(_0x133cad){_0x3016af['rows']=_0x133cad;return _0x3016af;});}})['then'](respondWithFilteredResult(_0x427cee,_0x53db48))[_0xb2f8('0x60')](handleError(_0x427cee,null));};exports[_0xb2f8('0x70')]=function(_0x9d2772,_0x135b41,_0x1b76f1){return db['WhatsappAccount'][_0xb2f8('0x5f')]({'where':{'id':_0x9d2772[_0xb2f8('0x5c')]['id']}})[_0xb2f8('0x22')](handleEntityNotFound(_0x135b41,null))[_0xb2f8('0x22')](function(_0x5a6377){if(_0x5a6377){return _0x5a6377['removeDispositions'](_0x9d2772[_0xb2f8('0x48')][_0xb2f8('0x71')]);}})['then'](respondWithStatusCode(_0x135b41,null))[_0xb2f8('0x60')](handleError(_0x135b41,null));};exports[_0xb2f8('0x72')]=function(_0x52759c,_0x1afd7f,_0x6eaa14){if(_0x52759c[_0xb2f8('0x68')]['id']){delete _0x52759c[_0xb2f8('0x68')]['id'];}return db[_0xb2f8('0x42')][_0xb2f8('0x5f')]({'where':{'id':_0x52759c['params']['id']}})['then'](handleEntityNotFound(_0x1afd7f,null))[_0xb2f8('0x22')](function(_0x38405b){if(_0x38405b){_0x52759c[_0xb2f8('0x68')]['WhatsappAccountId']=_0x38405b['id'];return db[_0xb2f8('0x73')][_0xb2f8('0x67')](_0x52759c[_0xb2f8('0x68')]);}})[_0xb2f8('0x22')](respondWithResult(_0x1afd7f,null))[_0xb2f8('0x60')](handleError(_0x1afd7f,null));};exports['getAnswers']=function(_0x3388f0,_0x572d3d,_0x5c7415){var _0x1809e2={'raw':![],'where':{}};var _0x4e96c3={};var _0x11e155={'count':0x0,'rows':[]};return db['WhatsappAccount'][_0xb2f8('0x6d')]({'where':{'id':_0x3388f0[_0xb2f8('0x5c')]['id']}})[_0xb2f8('0x22')](handleEntityNotFound(_0x572d3d,null))[_0xb2f8('0x22')](function(_0x3f7f4b){if(_0x3f7f4b){_0x4e96c3[_0xb2f8('0x47')]=_[_0xb2f8('0x49')](db[_0xb2f8('0x73')][_0xb2f8('0x43')]);_0x4e96c3[_0xb2f8('0x48')]=_[_0xb2f8('0x49')](_0x3388f0[_0xb2f8('0x48')]);_0x4e96c3[_0xb2f8('0x4a')]=_[_0xb2f8('0x4b')](_0x4e96c3[_0xb2f8('0x47')],_0x4e96c3[_0xb2f8('0x48')]);_0x1809e2[_0xb2f8('0x4c')]=_[_0xb2f8('0x4b')](_0x4e96c3[_0xb2f8('0x47')],qs[_0xb2f8('0x4d')](_0x3388f0['query'][_0xb2f8('0x4d')]));_0x1809e2[_0xb2f8('0x4c')]=_0x1809e2[_0xb2f8('0x4c')][_0xb2f8('0x4e')]?_0x1809e2[_0xb2f8('0x4c')]:_0x4e96c3[_0xb2f8('0x47')];if(!_0x3388f0[_0xb2f8('0x48')][_0xb2f8('0x4f')]('nolimit')){_0x1809e2[_0xb2f8('0x32')]=qs[_0xb2f8('0x32')](_0x3388f0[_0xb2f8('0x48')][_0xb2f8('0x32')]);_0x1809e2[_0xb2f8('0x33')]=qs[_0xb2f8('0x33')](_0x3388f0[_0xb2f8('0x48')]['offset']);}_0x1809e2[_0xb2f8('0x50')]=qs[_0xb2f8('0x51')](_0x3388f0[_0xb2f8('0x48')]['sort']);_0x1809e2[_0xb2f8('0x52')]=qs['filters'](_[_0xb2f8('0x53')](_0x3388f0[_0xb2f8('0x48')],_0x4e96c3[_0xb2f8('0x4a')]));_0x1809e2[_0xb2f8('0x52')][_0xb2f8('0x6b')]=_0x3f7f4b['id'];if(_0x3388f0[_0xb2f8('0x48')][_0xb2f8('0x54')]){_0x1809e2[_0xb2f8('0x52')]=_[_0xb2f8('0x55')](_0x1809e2[_0xb2f8('0x52')],{'$or':_[_0xb2f8('0x41')](_0x1809e2[_0xb2f8('0x4c')],function(_0x3481c6){var _0x2a9456={};_0x2a9456[_0x3481c6]={'$like':'%'+_0x3388f0[_0xb2f8('0x48')][_0xb2f8('0x54')]+'%'};return _0x2a9456;})});}_0x1809e2=_['merge']({},_0x1809e2,_0x3388f0[_0xb2f8('0x5e')]);return db[_0xb2f8('0x73')][_0xb2f8('0x57')]({'where':_0x1809e2[_0xb2f8('0x52')]})[_0xb2f8('0x22')](function(_0x39f17a){_0x11e155[_0xb2f8('0x57')]=_0x39f17a;if(_0x3388f0[_0xb2f8('0x48')][_0xb2f8('0x58')]){_0x1809e2[_0xb2f8('0x59')]=[{'all':!![]}];}return db[_0xb2f8('0x73')][_0xb2f8('0x5a')](_0x1809e2);})[_0xb2f8('0x22')](function(_0x17333e){_0x11e155['rows']=_0x17333e;return _0x11e155;});}})['then'](respondWithFilteredResult(_0x572d3d,_0x1809e2))[_0xb2f8('0x60')](handleError(_0x572d3d,null));};exports[_0xb2f8('0x74')]=function(_0x211886,_0x302c4b,_0xc1ec9d){return db[_0xb2f8('0x42')]['find']({'where':{'id':_0x211886['params']['id']}})[_0xb2f8('0x22')](handleEntityNotFound(_0x302c4b,null))[_0xb2f8('0x22')](function(_0x4d3e7f){if(_0x4d3e7f){return _0x4d3e7f[_0xb2f8('0x74')](_0x211886['query'][_0xb2f8('0x71')]);}})[_0xb2f8('0x22')](respondWithStatusCode(_0x302c4b,null))[_0xb2f8('0x60')](handleError(_0x302c4b,null));};exports[_0xb2f8('0x75')]=function(_0x216b3f,_0x3d22dd,_0xe27a97){return db[_0xb2f8('0x42')][_0xb2f8('0x5f')]({'where':{'id':_0x216b3f[_0xb2f8('0x5c')]['id']}})[_0xb2f8('0x22')](handleEntityNotFound(_0x3d22dd,null))['then'](function(_0xfbae43){if(_0xfbae43){return _0xfbae43[_0xb2f8('0x75')](_0x216b3f[_0xb2f8('0x68')][_0xb2f8('0x71')],_[_0xb2f8('0x76')](_0x216b3f[_0xb2f8('0x68')],['ids','id'])||{})[_0xb2f8('0x77')](function(_0xf464c1){for(var _0x4aaee7=0x0;_0x4aaee7<_0x216b3f[_0xb2f8('0x68')][_0xb2f8('0x71')][_0xb2f8('0x4e')];_0x4aaee7+=0x1){socket[_0xb2f8('0x78')]('userWhatsappAccount:save',{'UserId':Number(_0x216b3f[_0xb2f8('0x68')][_0xb2f8('0x71')][_0x4aaee7]),'WhatsappAccountId':Number(_0x216b3f[_0xb2f8('0x5c')]['id'])});}return _0xf464c1;});}})[_0xb2f8('0x22')](respondWithResult(_0x3d22dd,null))[_0xb2f8('0x60')](handleError(_0x3d22dd,null));};exports[_0xb2f8('0x79')]=function(_0x2a5af8,_0x4c03dc,_0x2a4ca2){return db[_0xb2f8('0x42')]['find']({'where':{'id':_0x2a5af8[_0xb2f8('0x5c')]['id']}})[_0xb2f8('0x22')](handleEntityNotFound(_0x4c03dc,null))[_0xb2f8('0x22')](function(_0x585e7f){if(_0x585e7f){return _0x585e7f[_0xb2f8('0x79')](_0x2a5af8['query'][_0xb2f8('0x71')])[_0xb2f8('0x22')](function(){if(_['isArray'](_0x2a5af8[_0xb2f8('0x48')][_0xb2f8('0x71')])){for(var _0x5bb3d9=0x0;_0x5bb3d9<_0x2a5af8[_0xb2f8('0x48')]['ids'][_0xb2f8('0x4e')];_0x5bb3d9+=0x1){socket[_0xb2f8('0x78')](_0xb2f8('0x7a'),{'UserId':Number(_0x2a5af8[_0xb2f8('0x48')][_0xb2f8('0x71')][_0x5bb3d9]),'WhatsappAccountId':Number(_0x2a5af8[_0xb2f8('0x5c')]['id'])});}}else{socket[_0xb2f8('0x78')](_0xb2f8('0x7a'),{'UserId':Number(_0x2a5af8[_0xb2f8('0x48')][_0xb2f8('0x71')]),'WhatsappAccountId':Number(_0x2a5af8[_0xb2f8('0x5c')]['id'])});}});}})[_0xb2f8('0x22')](respondWithStatusCode(_0x4c03dc,null))[_0xb2f8('0x60')](handleError(_0x4c03dc,null));};exports[_0xb2f8('0x7b')]=function(_0x40a220,_0x1484e1,_0x3e753a){var _0x5b9775={};var _0x3b42ac={};var _0x1ad553;var _0x3f48ef;return db[_0xb2f8('0x42')]['findOne']({'where':{'id':_0x40a220[_0xb2f8('0x5c')]['id']}})[_0xb2f8('0x22')](handleEntityNotFound(_0x1484e1,null))[_0xb2f8('0x22')](function(_0x12ae08){if(_0x12ae08){_0x1ad553=_0x12ae08;_0x3b42ac[_0xb2f8('0x47')]=_[_0xb2f8('0x49')](db[_0xb2f8('0x7c')][_0xb2f8('0x43')]);_0x3b42ac[_0xb2f8('0x48')]=_[_0xb2f8('0x49')](_0x40a220[_0xb2f8('0x48')]);_0x3b42ac[_0xb2f8('0x4a')]=_['intersection'](_0x3b42ac[_0xb2f8('0x47')],_0x3b42ac[_0xb2f8('0x48')]);_0x5b9775[_0xb2f8('0x4c')]=_['intersection'](_0x3b42ac[_0xb2f8('0x47')],qs['fields'](_0x40a220[_0xb2f8('0x48')][_0xb2f8('0x4d')]));_0x5b9775[_0xb2f8('0x4c')]=_0x5b9775['attributes'][_0xb2f8('0x4e')]?_0x5b9775[_0xb2f8('0x4c')]:_0x3b42ac[_0xb2f8('0x47')];_0x5b9775[_0xb2f8('0x50')]=qs[_0xb2f8('0x51')](_0x40a220['query'][_0xb2f8('0x51')]);_0x5b9775[_0xb2f8('0x52')]=qs['filters'](_[_0xb2f8('0x53')](_0x40a220['query'],_0x3b42ac['filters']));if(_0x40a220[_0xb2f8('0x48')][_0xb2f8('0x54')]){_0x5b9775[_0xb2f8('0x52')]=_[_0xb2f8('0x55')](_0x5b9775['where'],{'$or':_[_0xb2f8('0x41')](_0x5b9775[_0xb2f8('0x4c')],function(_0x4ddf87){var _0x2449a6={};_0x2449a6[_0x4ddf87]={'$like':'%'+_0x40a220[_0xb2f8('0x48')][_0xb2f8('0x54')]+'%'};return _0x2449a6;})});}_0x5b9775=_[_0xb2f8('0x55')]({},_0x5b9775,_0x40a220['options']);return _0x1ad553[_0xb2f8('0x7b')](_0x5b9775);}})[_0xb2f8('0x22')](function(_0x1827cc){if(_0x1827cc){_0x3f48ef=_0x1827cc[_0xb2f8('0x4e')];if(!_0x40a220[_0xb2f8('0x48')][_0xb2f8('0x4f')]('nolimit')){_0x5b9775[_0xb2f8('0x32')]=qs[_0xb2f8('0x32')](_0x40a220[_0xb2f8('0x48')]['limit']);_0x5b9775[_0xb2f8('0x33')]=qs[_0xb2f8('0x33')](_0x40a220[_0xb2f8('0x48')][_0xb2f8('0x33')]);}return _0x1ad553[_0xb2f8('0x7b')](_0x5b9775);}})[_0xb2f8('0x22')](function(_0x345769){if(_0x345769){return _0x345769?{'count':_0x3f48ef,'rows':_0x345769}:null;}})[_0xb2f8('0x22')](respondWithResult(_0x1484e1,null))['catch'](handleError(_0x1484e1,null));};exports[_0xb2f8('0x7d')]=function(_0x1adee6,_0x503c87){var _0x423a34=_0x1adee6[_0xb2f8('0x5c')]['id'];var _0x525e4b=_0x1adee6['body'];var _0x3da60c=0xc8;var _0x4f0a52=null;return db[_0xb2f8('0x7e')]['transaction']({'isolationLevel':db[_0xb2f8('0x7e')][_0xb2f8('0x7f')][_0xb2f8('0x80')]['READ_COMMITTED']},function(_0x3f0485){return db['WhatsappAccount'][_0xb2f8('0x6d')]({'where':{'id':_0x423a34},'transaction':_0x3f0485})[_0xb2f8('0x22')](function(_0x6e063e){if(_0x6e063e){return db[_0xb2f8('0x81')][_0xb2f8('0x39')]({'where':{'WhatsappAccountId':_0x423a34},'transaction':_0x3f0485})['then'](function(){var _0x3ed67d=_[_0xb2f8('0x41')](_0x525e4b,function(_0x2103c1){_0x2103c1['WhatsappAccountId']=_0x423a34;return _0x2103c1;});return db['WhatsappApplication'][_0xb2f8('0x82')](_0x3ed67d,{'transaction':_0x3f0485});});}else{_0x3da60c=0x194;_0x4f0a52=[];}});})[_0xb2f8('0x22')](function(){if(_0x3da60c!==0x194){return db[_0xb2f8('0x81')][_0xb2f8('0x83')]({'where':{'WhatsappAccountId':_0x423a34},'order':'priority'})[_0xb2f8('0x22')](function(_0x289882){_0x4f0a52=_0x289882;});}})[_0xb2f8('0x60')](function(_0x56f275){_0x3da60c=0x1f4;logger['error'](_0x56f275[_0xb2f8('0x3c')]);if(_0x56f275['name']){delete _0x56f275[_0xb2f8('0x3d')];}_0x4f0a52=_0x56f275;})[_0xb2f8('0x84')](function(){if(_0x4f0a52===null){_0x503c87[_0xb2f8('0x2d')](_0x3da60c);}else{if(_0x3da60c===0x1f4){_0x503c87[_0xb2f8('0x2e')](_0x3da60c)[_0xb2f8('0x3e')](_0x4f0a52);}else{_0x503c87[_0xb2f8('0x2e')](_0x3da60c)['json'](_0x4f0a52);}}});};exports[_0xb2f8('0x85')]=function(_0x10aab6,_0x50aab6,_0x563319){var _0xa1c492={};var _0xe6404c={};var _0x5a8921;var _0x3e1faf;return db[_0xb2f8('0x42')]['findOne']({'where':{'id':_0x10aab6['params']['id']}})[_0xb2f8('0x22')](handleEntityNotFound(_0x50aab6,null))[_0xb2f8('0x22')](function(_0x12a01b){if(_0x12a01b){_0x5a8921=_0x12a01b;_0xe6404c[_0xb2f8('0x47')]=_[_0xb2f8('0x49')](db[_0xb2f8('0x81')][_0xb2f8('0x43')]);_0xe6404c['query']=_['keys'](_0x10aab6[_0xb2f8('0x48')]);_0xe6404c[_0xb2f8('0x4a')]=_[_0xb2f8('0x4b')](_0xe6404c[_0xb2f8('0x47')],_0xe6404c[_0xb2f8('0x48')]);_0xa1c492[_0xb2f8('0x4c')]=_[_0xb2f8('0x4b')](_0xe6404c[_0xb2f8('0x47')],qs[_0xb2f8('0x4d')](_0x10aab6[_0xb2f8('0x48')]['fields']));_0xa1c492[_0xb2f8('0x4c')]=_0xa1c492[_0xb2f8('0x4c')]['length']?_0xa1c492[_0xb2f8('0x4c')]:_0xe6404c['model'];_0xa1c492['order']=qs['sort'](_0x10aab6[_0xb2f8('0x48')][_0xb2f8('0x51')]);_0xa1c492[_0xb2f8('0x52')]=qs[_0xb2f8('0x4a')](_[_0xb2f8('0x53')](_0x10aab6['query'],_0xe6404c[_0xb2f8('0x4a')]));if(_0x10aab6['query'][_0xb2f8('0x54')]){_0xa1c492[_0xb2f8('0x52')]=_[_0xb2f8('0x55')](_0xa1c492[_0xb2f8('0x52')],{'$or':_[_0xb2f8('0x41')](_0xa1c492[_0xb2f8('0x4c')],function(_0x5f4a6a){var _0x5b0038={};_0x5b0038[_0x5f4a6a]={'$like':'%'+_0x10aab6[_0xb2f8('0x48')][_0xb2f8('0x54')]+'%'};return _0x5b0038;})});}_0xa1c492=_[_0xb2f8('0x55')]({},_0xa1c492,_0x10aab6[_0xb2f8('0x5e')]);return _0x5a8921[_0xb2f8('0x85')](_0xa1c492);}})[_0xb2f8('0x22')](function(_0x518902){if(_0x518902){_0x3e1faf=_0x518902[_0xb2f8('0x4e')];if(!_0x10aab6[_0xb2f8('0x48')][_0xb2f8('0x4f')](_0xb2f8('0x6f'))){_0xa1c492['limit']=qs['limit'](_0x10aab6[_0xb2f8('0x48')][_0xb2f8('0x32')]);_0xa1c492['offset']=qs[_0xb2f8('0x33')](_0x10aab6[_0xb2f8('0x48')]['offset']);}return _0x5a8921['getApplications'](_0xa1c492);}})[_0xb2f8('0x22')](function(_0x33697f){if(_0x33697f){return _0x33697f?{'count':_0x3e1faf,'rows':_0x33697f}:null;}})[_0xb2f8('0x22')](respondWithResult(_0x50aab6,null))[_0xb2f8('0x60')](handleError(_0x50aab6,null));};exports[_0xb2f8('0x86')]=function(_0x48cd26,_0x2595eb,_0x464649){var _0x453c07={'channel':_0xb2f8('0x87')};var _0x10d574=[];var _0x3d6fa5=[];var _0x4a2a6e={};return db['WhatsappAccount'][_0xb2f8('0x5f')]({'where':{'id':_0x48cd26[_0xb2f8('0x5c')]['id']},'include':[{'model':db[_0xb2f8('0x81')],'as':_0xb2f8('0x88')}]})[_0xb2f8('0x22')](handleEntityNotFound(_0x2595eb,null))[_0xb2f8('0x22')](function(_0x5d6c41){_0x453c07[_0xb2f8('0x89')]=_0x5d6c41;if(_0x453c07[_0xb2f8('0x89')]&&_0x453c07['account'][_0xb2f8('0x45')]===_0xb2f8('0x8a')&&_0x48cd26[_0xb2f8('0x68')][_0xb2f8('0x8b')]){if(_0x48cd26['body']['statuses'][_0xb2f8('0x4e')]===0x0)return;if(_0x48cd26[_0xb2f8('0x68')]['statuses'][0x0][_0xb2f8('0x8c')]!==_0xb2f8('0x8d'))return;return db['WhatsappMessage'][_0xb2f8('0x38')]({'read':!![],'readAt':_0x48cd26[_0xb2f8('0x68')]['statuses'][0x0][_0xb2f8('0x8e')]},{'where':{'messageId':_0x48cd26[_0xb2f8('0x68')][_0xb2f8('0x8b')][0x0][_0xb2f8('0x8f')]},'individualHooks':!![]})[_0xb2f8('0x22')](function(_0x2f5e66){return _0x2595eb[_0xb2f8('0x2e')](0xc8)['json'](_0x2f5e66);});}_0x453c07[_0xb2f8('0x90')]=_[_0xb2f8('0x91')](_0x5d6c41[_0xb2f8('0x88')],[_0xb2f8('0x92')],[_0xb2f8('0x93')]);if(_0x453c07['account'][_0xb2f8('0x88')]){delete _0x453c07[_0xb2f8('0x89')][_0xb2f8('0x88')];}return db[_0xb2f8('0x94')][_0xb2f8('0x69')]()[_0xb2f8('0x22')](function(_0x365cd6){if(!_0x365cd6){throw new db[(_0xb2f8('0x95'))][(_0xb2f8('0x96'))](_0xb2f8('0x97'));}_0x10d574=_[_0xb2f8('0x98')](_['keys'](_0x365cd6),[_0xb2f8('0x99'),_0xb2f8('0x9a')]);_0x3d6fa5=_[_0xb2f8('0x98')](_['keys'](_0x365cd6),[_0xb2f8('0x99'),_0xb2f8('0x9a'),_0xb2f8('0x9b'),_0xb2f8('0x9c')]);_0x453c07[_0xb2f8('0x68')]={'mapKey':_0xb2f8('0x9d')};if(_0x453c07['account']&&_0x453c07['account'][_0xb2f8('0x45')]===_0xb2f8('0x9e')){_0x453c07[_0xb2f8('0x68')]['from']=_0x48cd26[_0xb2f8('0x68')][_0xb2f8('0x9f')][_0xb2f8('0xa0')]('whatsapp:','');_0x453c07[_0xb2f8('0x68')][_0xb2f8('0x68')]=_0x48cd26[_0xb2f8('0x68')]['Body'];_0x453c07[_0xb2f8('0x68')][_0xb2f8('0xa1')]=_0x48cd26[_0xb2f8('0x68')][_0xb2f8('0xa2')];}if(_0x453c07[_0xb2f8('0x89')]&&_0x453c07['account'][_0xb2f8('0x45')]==='sinch'){_0x453c07[_0xb2f8('0x68')]['from']=_0x48cd26[_0xb2f8('0x68')][_0xb2f8('0xa3')][0x0][_0xb2f8('0xa4')];_0x453c07['body'][_0xb2f8('0x68')]=_0x48cd26[_0xb2f8('0x68')]['notifications'][0x0][_0xb2f8('0x2a')][_0xb2f8('0x68')];_0x453c07['body']['messageId']=_0x48cd26[_0xb2f8('0x68')]['notifications'][0x0][_0xb2f8('0x8f')];}if(_0x453c07['body']['id']){delete _0x453c07['body']['id'];}if(_[_0xb2f8('0xa5')](_0x453c07[_0xb2f8('0x68')][_0xb2f8('0xa4')])){throw new db[(_0xb2f8('0x95'))][(_0xb2f8('0x96'))](_0xb2f8('0xa6'));}if(_['isNil'](_0x453c07[_0xb2f8('0x68')][_0xb2f8('0x68')])||_0x453c07[_0xb2f8('0x68')][_0xb2f8('0x68')]===''){throw new db[(_0xb2f8('0x95'))]['ValidationError'](_0xb2f8('0xa7'));}if(_['isNil'](_0x453c07['body'][_0xb2f8('0xa8')])){throw new db[(_0xb2f8('0x95'))][(_0xb2f8('0x96'))]('mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x3d6fa5);}if(!_[_0xb2f8('0xa9')](_0x3d6fa5,_0x453c07[_0xb2f8('0x68')][_0xb2f8('0xa8')])){throw new db['Sequelize'][(_0xb2f8('0x96'))](_0xb2f8('0xaa')+_0x3d6fa5);}_0x4a2a6e[_0x453c07[_0xb2f8('0x68')]['mapKey']]=_0x453c07['body'][_0xb2f8('0xa4')];return db[_0xb2f8('0xab')][_0xb2f8('0x5f')]({'where':{'id':_0x453c07[_0xb2f8('0x89')][_0xb2f8('0x9c')]},'include':[{'model':db[_0xb2f8('0x94')],'as':_0xb2f8('0xac'),'where':_0x4a2a6e,'limit':0x1,'order':[[_0xb2f8('0x9a'),'DESC']]}]});})[_0xb2f8('0x22')](handleEntityNotFound(_0x2595eb,null))['then'](function(_0x3f730a){if(_0x3f730a){_0x453c07[_0xb2f8('0xad')]=_['omit'](_0x3f730a,[_0xb2f8('0xac')]);if(_0x3f730a[_0xb2f8('0xac')][_0xb2f8('0x4e')]){return _0x3f730a['Contacts'][0x0];}var _0x475f6e=_[_0xb2f8('0x1c')](_0x453c07[_0xb2f8('0x68')],{'firstName':_0x453c07['body']['from'],'ListId':_0x3f730a['id']});_0x475f6e[_0x453c07[_0xb2f8('0x68')][_0xb2f8('0xa8')]]=_0x453c07[_0xb2f8('0x68')][_0xb2f8('0xa4')];return db['CmContact']['create'](_0x475f6e,{'fields':_0x10d574,'raw':!![]});}})['then'](handleEntityNotFound(_0x2595eb,null))[_0xb2f8('0x22')](function(_0x381ef8){if(_0x381ef8){_0x453c07['contact']=_0x381ef8;var _0x12f498={'ContactId':_0x381ef8['id'],'phone':_0x453c07[_0xb2f8('0x68')]['from'],'WhatsappAccountId':_0x48cd26['params']['id']};var _0x4297ac={'ContactId':_0x381ef8['id'],'WhatsappAccountId':_0x48cd26[_0xb2f8('0x5c')]['id'],'closed':![]};return db[_0xb2f8('0xae')]['find']({'where':_0x4297ac})['then'](function(_0x28c8a9){if(_0x28c8a9){return[_0x28c8a9,![]];}return db[_0xb2f8('0xae')][_0xb2f8('0x67')](_0x12f498)['then'](function(_0xa46ced){return[_0xa46ced,!![]];});});}})[_0xb2f8('0x77')](function(_0xb3ead3,_0x895bed){_0x453c07['interaction']=_0xb3ead3['get']({'plain':!![]});_0x453c07[_0xb2f8('0xaf')][_0xb2f8('0xb0')]=![];if(_0x895bed){if(_0x453c07[_0xb2f8('0x89')][_0xb2f8('0xb1')]){_0x453c07[_0xb2f8('0x90')][_0xb2f8('0xb2')]({'id':0x0,'priority':_0x453c07['applications']['length']+0x1,'app':_0xb2f8('0xb3'),'appdata':_0xb2f8('0xb4'),'interval':_0xb2f8('0xb5')});}_0x453c07[_0xb2f8('0xaf')][_0xb2f8('0xb0')]=!![];}return db[_0xb2f8('0xb6')][_0xb2f8('0x67')]({'phone':_0x453c07[_0xb2f8('0x68')][_0xb2f8('0xa4')],'messageId':_0x453c07[_0xb2f8('0x68')][_0xb2f8('0xa1')],'body':_0x453c07[_0xb2f8('0x68')][_0xb2f8('0x68')],'WhatsappAccountId':_0x48cd26['params']['id'],'WhatsappInteractionId':_0xb3ead3['id'],'direction':'in','ContactId':_0xb3ead3['ContactId'],'AttachmentId':_0x453c07[_0xb2f8('0x68')]['AttachmentId']});})[_0xb2f8('0x22')](function(_0x30ac46){_0x453c07[_0xb2f8('0x2a')]=_0x30ac46;if(!_0x453c07[_0xb2f8('0xaf')][_0xb2f8('0xb7')])return;return db[_0xb2f8('0x7c')][_0xb2f8('0x5f')]({'attributes':['id',_0xb2f8('0x3d')],'where':{'id':_0x453c07[_0xb2f8('0xaf')][_0xb2f8('0xb7')]}});})[_0xb2f8('0x22')](function(_0x534bfb){if(_0x534bfb){_0x453c07[_0xb2f8('0x90')][_0xb2f8('0xb8')]({'id':0x0,'priority':0x0,'app':_0xb2f8('0xb9'),'appdata':_0x534bfb[_0xb2f8('0x3d')]+','+(_0x453c07[_0xb2f8('0x89')][_0xb2f8('0xba')]||0xa),'interval':_0xb2f8('0xb5')});}if(!_0x453c07[_0xb2f8('0xaf')][_0xb2f8('0xbb')])return;return db[_0xb2f8('0xbc')]['find']({'attributes':['id',_0xb2f8('0x3d')],'where':{'id':_0x453c07[_0xb2f8('0xaf')][_0xb2f8('0xbb')]}});})[_0xb2f8('0x22')](function(_0x52db86){if(_0x52db86){_0x453c07[_0xb2f8('0x90')][_0xb2f8('0xb8')]({'id':0x0,'priority':0x1,'app':_0xb2f8('0xbd'),'appdata':_0x52db86[_0xb2f8('0x3d')]+','+(_0x453c07[_0xb2f8('0x89')][_0xb2f8('0xbe')]||0x12c),'interval':_0xb2f8('0xb5')});}return respondWithRpcPromise('Start','startRouting',_0x453c07);})[_0xb2f8('0x22')](function(_0x7d1d1){if(_0x7d1d1){var _0x3c6b20=0xc8;var _0x11576d=_0xb2f8('0xbf');logger['info'](_0xb2f8('0x25'),_0x11576d,_0x3c6b20,_0xb2f8('0xc0'));logger[_0xb2f8('0x24')](_0xb2f8('0xc1'),_0x11576d,_0x3c6b20,_0xb2f8('0xc0'),JSON['stringify'](_0x7d1d1));if(_0x453c07[_0xb2f8('0x89')]['type']===_0xb2f8('0x9e')){return _0x2595eb[_0xb2f8('0x2e')](_0x3c6b20)[_0xb2f8('0x34')](_0xb2f8('0xc2'),_0xb2f8('0xc3'))[_0xb2f8('0x3e')](_0xb2f8('0xc4'));}else{return _0x2595eb[_0xb2f8('0x2e')](_0x3c6b20)[_0xb2f8('0x30')](_0x7d1d1);}}});})[_0xb2f8('0x60')](handleError(_0x2595eb,null));};exports['send']=function(_0x3979f8,_0x266197,_0x11d68c){var _0x538c38,_0x1930fb,_0x14691c;if(_['isNil'](_0x3979f8[_0xb2f8('0x68')][_0xb2f8('0x68')])||_0x3979f8[_0xb2f8('0x68')][_0xb2f8('0x68')]===''){throw new db[(_0xb2f8('0x95'))]['ValidationError']('body\x20is\x20mandatory\x20and\x20not\x20empty');}if(_['isNil'](_0x3979f8['body']['to'])){throw new db[(_0xb2f8('0x95'))][(_0xb2f8('0x96'))](_0xb2f8('0xc5'));}_0x538c38=_0x3979f8[_0xb2f8('0x68')]['to'];return db[_0xb2f8('0x42')]['find']({'where':{'id':_0x3979f8['params']['id']},'include':[{'model':db['CmList'],'as':_0xb2f8('0xc6'),'include':[{'model':db[_0xb2f8('0x94')],'as':_0xb2f8('0xac'),'where':{'mobile':_0x538c38},'limit':0x1,'order':[[_0xb2f8('0x9a'),'DESC']]}]}]})['then'](handleEntityNotFound(_0x266197,null))['then'](function(_0x17f37e){if(_0x17f37e&&_0x17f37e[_0xb2f8('0xc6')]){_0x1930fb=_0x17f37e['get']({'plain':!![]});if(_0x1930fb[_0xb2f8('0xc6')]&&_0x1930fb[_0xb2f8('0xc6')][_0xb2f8('0xac')][_0xb2f8('0x4e')]){return _0x1930fb[_0xb2f8('0xc6')][_0xb2f8('0xac')][0x0];}return db[_0xb2f8('0x94')][_0xb2f8('0x67')](_[_0xb2f8('0x1c')](_0x3979f8[_0xb2f8('0x68')],{'firstName':_0x538c38,'mobile':_0x538c38,'phone':_0x538c38,'ListId':_0x1930fb[_0xb2f8('0x9c')]}));}})[_0xb2f8('0x22')](handleEntityNotFound(_0x266197,null))[_0xb2f8('0x22')](function(_0x49a645){if(_0x49a645){_0x14691c=_0x49a645;return db['WhatsappInteraction'][_0xb2f8('0x5f')]({'where':{'ContactId':_0x14691c['id'],'closed':![],'WhatsappAccountId':_0x1930fb['id']}})[_0xb2f8('0x22')](function(_0x27c601){if(_0x27c601){return[_0x27c601,![]];}return db[_0xb2f8('0xae')][_0xb2f8('0x67')]({'UserId':_0x3979f8[_0xb2f8('0x61')]['id'],'ContactId':_0x14691c['id'],'WhatsappAccountId':_0x1930fb['id'],'phone':_0x538c38,'firstMsgDirection':_0xb2f8('0xc7'),'Messages':[_['merge'](_0x3979f8[_0xb2f8('0x68')],{'read':![],'body':_0x3979f8['body'][_0xb2f8('0x68')],'phone':_0x538c38,'WhatsappAccountId':_0x1930fb['id'],'UserId':_0x3979f8[_0xb2f8('0x61')]['id'],'ContactId':_0x14691c['id']})]},{'include':[{'model':db[_0xb2f8('0xb6')],'as':_0xb2f8('0xc8')}]})[_0xb2f8('0x22')](function(_0x5555ee){return[_0x5555ee,!![]];});});}})[_0xb2f8('0x77')](function(_0x3404c6,_0x5d36b1){if(_0x5d36b1){return _0x3404c6;}return db[_0xb2f8('0xb6')][_0xb2f8('0x67')](_['merge'](_0x3979f8['body'],{'read':![],'body':_0x3979f8[_0xb2f8('0x68')][_0xb2f8('0x68')],'phone':_0x538c38,'WhatsappAccountId':_0x1930fb['id'],'WhatsappInteractionId':_0x3404c6['id'],'UserId':_0x3979f8[_0xb2f8('0x61')]['id'],'ContactId':_0x14691c['id']}))[_0xb2f8('0x22')](function(_0xcfa8ec){_0x3404c6['dataValues'][_0xb2f8('0xc8')]=[];_0x3404c6['dataValues'][_0xb2f8('0xc8')][_0xb2f8('0xb2')](_0xcfa8ec['dataValues']);return _0x3404c6;});})[_0xb2f8('0x22')](respondWithResult(_0x266197,null))['catch'](handleError(_0x266197,null));};