300895ae25f57443e8977957b85228d5df8d9278
[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 _0xb4af=['userWhatsappAccount:remove','getAgents','sequelize','Transaction','ISOLATION_LEVELS','READ_COMMITTED','WhatsappApplication','bulkCreate','priority','getApplications','notify','whatsapp','Applications','account','sinch','statuses','state','read','WhatsappMessage','timestamp','message_id','applications','orderBy','asc','ValidationError','no\x20available\x20attributes','difference','createdAt','updatedAt','ListId','mobile','twilio','from','From','replace','whatsapp:','Body','messageId','notifications','isNil','Sequelize','from\x20is\x20mandatory','body\x20is\x20mandatory\x20and\x20not\x20empty','mapKey','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','CmList','Contacts','DESC','list','CmContact','contact','WhatsappInteraction','interaction','created','push','unmanaged','ContactId','AttachmentId','UserId','User','unshift','agent','waitForTheAssignedAgent','*,*,*,*','queueId','waitForTheAssignedQueue','Start','startRouting','receiveMessage','entity\x20found','Content-Type','text/xml','<?xml\x20version=\x221.0\x22\x20encoding=\x22UTF-8\x22?>\x0a<Response></Response>','to\x20is\x20mandatory','List','out','dataValues','eml-format','rimraf','zip-dir','fast-json-patch','bluebird','util','path','sox','to-csv','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','redis','defaults','localhost','socket.io-emitter','./whatsappAccount.socket','register','request','WhatsappAccount,\x20%s,\x20%s','request\x20sent','debug','WhatsappAccount,\x20%s,\x20%s,\x20%s','stringify','error','message','info','result','catch','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','reject','save','update','then','destroy','get','WhatsappAccounts','UserProfileResource','stack','name','send','index','Pause','map','WhatsappAccount','fieldName','key','model','query','keys','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','filters','pick','filter','where','type','VIRTUAL','merge','includeAll','include','rows','show','params','rawAttributes','find','create','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','body','mandatoryDispositionPause','describe','Disposition','getDispositions','WhatsappAccountId','findAll','removeDispositions','ids','addAnswer','CannedAnswer','findOne','options','removeAnswers','addAgents','omit','spread','emit','userWhatsappAccount:save','removeAgents'];(function(_0x6cb701,_0x6e56db){var _0x28259e=function(_0x556354){while(--_0x556354){_0x6cb701['push'](_0x6cb701['shift']());}};_0x28259e(++_0x6e56db);}(_0xb4af,0x114));var _0xfb4a=function(_0x40220a,_0x22feb1){_0x40220a=_0x40220a-0x0;var _0x3903cc=_0xb4af[_0x40220a];return _0x3903cc;};'use strict';var emlformat=require(_0xfb4a('0x0'));var rimraf=require(_0xfb4a('0x1'));var zipdir=require(_0xfb4a('0x2'));var jsonpatch=require(_0xfb4a('0x3'));var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0xfb4a('0x4'));var Mustache=require('mustache');var util=require(_0xfb4a('0x5'));var path=require(_0xfb4a('0x6'));var sox=require(_0xfb4a('0x7'));var csv=require(_0xfb4a('0x8'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0xfb4a('0x9'));var _=require(_0xfb4a('0xa'));var squel=require(_0xfb4a('0xb'));var crypto=require(_0xfb4a('0xc'));var jsforce=require(_0xfb4a('0xd'));var deskjs=require(_0xfb4a('0xe'));var toCsv=require('to-csv');var querystring=require(_0xfb4a('0xf'));var Papa=require(_0xfb4a('0x10'));var Redis=require('ioredis');var authService=require(_0xfb4a('0x11'));var qs=require(_0xfb4a('0x12'));var as=require(_0xfb4a('0x13'));var hardwareService=require(_0xfb4a('0x14'));var logger=require(_0xfb4a('0x15'))(_0xfb4a('0x16'));var utils=require('../../config/utils');var config=require(_0xfb4a('0x17'));var licenseUtil=require(_0xfb4a('0x18'));var db=require(_0xfb4a('0x19'))['db'];config[_0xfb4a('0x1a')]=_[_0xfb4a('0x1b')](config[_0xfb4a('0x1a')],{'host':_0xfb4a('0x1c'),'port':0x18eb});var socket=require(_0xfb4a('0x1d'))(new Redis(config[_0xfb4a('0x1a')]));require(_0xfb4a('0x1e'))[_0xfb4a('0x1f')](socket);var jayson=require('jayson/promise');var client=jayson['client']['http']({'port':0x232c});function respondWithRpcPromise(_0x58993c,_0x541f6b,_0x396712,_0x325dbf){return new BPromise(function(_0x5aa526,_0x14f583){var _0x422988=_0x325dbf||client;return _0x422988[_0xfb4a('0x20')](_0x58993c,_0x396712)['then'](function(_0x1105cd){logger['info'](_0xfb4a('0x21'),_0x541f6b,_0xfb4a('0x22'));logger[_0xfb4a('0x23')](_0xfb4a('0x24'),_0x541f6b,_0xfb4a('0x22'),JSON[_0xfb4a('0x25')](_0x1105cd));if(_0x1105cd[_0xfb4a('0x26')]){if(_0x1105cd['error']['code']===0x1f4){logger[_0xfb4a('0x26')](_0xfb4a('0x21'),_0x541f6b,_0x1105cd[_0xfb4a('0x26')]['message']);return _0x14f583(_0x1105cd[_0xfb4a('0x26')][_0xfb4a('0x27')]);}logger['error'](_0xfb4a('0x21'),_0x541f6b,_0x1105cd[_0xfb4a('0x26')]['message']);return _0x5aa526(_0x1105cd['error']['message']);}else{logger[_0xfb4a('0x28')]('WhatsappAccount,\x20%s,\x20%s',_0x541f6b,_0xfb4a('0x22'));_0x5aa526(_0x1105cd[_0xfb4a('0x29')][_0xfb4a('0x27')]);}})[_0xfb4a('0x2a')](function(_0x5999ee){logger[_0xfb4a('0x26')]('WhatsappAccount,\x20%s,\x20%s',_0x541f6b,_0x5999ee);_0x14f583(_0x5999ee);});});}function respondWithStatusCode(_0x426790,_0x21cd01){_0x21cd01=_0x21cd01||0xcc;return function(_0x1bc326){if(_0x1bc326){return _0x426790[_0xfb4a('0x2b')](_0x21cd01);}return _0x426790[_0xfb4a('0x2c')](_0x21cd01)[_0xfb4a('0x2d')]();};}function respondWithResult(_0x3e9639,_0x2791f8){_0x2791f8=_0x2791f8||0xc8;return function(_0x3dbe06){if(_0x3dbe06){return _0x3e9639['status'](_0x2791f8)[_0xfb4a('0x2e')](_0x3dbe06);}};}function respondWithFilteredResult(_0x338b78,_0x1927d1){return function(_0x99fb58){if(_0x99fb58){var _0xe01003=typeof _0x1927d1[_0xfb4a('0x2f')]===_0xfb4a('0x30')&&typeof _0x1927d1[_0xfb4a('0x31')]===_0xfb4a('0x30');var _0x4d8ec6=_0x99fb58['count'];var _0x47e926=_0xe01003?0x0:_0x1927d1['offset'];var _0x33409d=_0xe01003?_0x99fb58[_0xfb4a('0x32')]:_0x1927d1['offset']+_0x1927d1['limit'];var _0x210dde;if(_0x33409d>=_0x4d8ec6){_0x33409d=_0x4d8ec6;_0x210dde=0xc8;}else{_0x210dde=0xce;}_0x338b78['status'](_0x210dde);return _0x338b78[_0xfb4a('0x33')](_0xfb4a('0x34'),_0x47e926+'-'+_0x33409d+'/'+_0x4d8ec6)['json'](_0x99fb58);}return null;};}function patchUpdates(_0x4e25c7){return function(_0xd26ee2){try{jsonpatch['apply'](_0xd26ee2,_0x4e25c7,!![]);}catch(_0x59e465){return BPromise[_0xfb4a('0x35')](_0x59e465);}return _0xd26ee2[_0xfb4a('0x36')]();};}function saveUpdates(_0x56c206,_0x4fe8d6){return function(_0x1b7c46){if(_0x1b7c46){return _0x1b7c46[_0xfb4a('0x37')](_0x56c206)[_0xfb4a('0x38')](function(_0x24bebf){return _0x24bebf;});}return null;};}function removeEntity(_0x4f215a,_0x252602){return function(_0x2f96c0){if(_0x2f96c0){return _0x2f96c0[_0xfb4a('0x39')]()[_0xfb4a('0x38')](function(){var _0x4d8077=_0x2f96c0[_0xfb4a('0x3a')]({'plain':!![]});var _0x2d1286=_0xfb4a('0x3b');return db[_0xfb4a('0x3c')]['destroy']({'where':{'type':_0x2d1286,'resourceId':_0x4d8077['id']}})[_0xfb4a('0x38')](function(){return _0x2f96c0;});})[_0xfb4a('0x38')](function(){_0x4f215a[_0xfb4a('0x2c')](0xcc)['end']();});}};}function handleEntityNotFound(_0x8f4253,_0x505ef8){return function(_0x5bcb44){if(!_0x5bcb44){_0x8f4253[_0xfb4a('0x2b')](0x194);}return _0x5bcb44;};}function handleError(_0x34af19,_0x4c606d){_0x4c606d=_0x4c606d||0x1f4;return function(_0xe3b68a){logger[_0xfb4a('0x26')](_0xe3b68a[_0xfb4a('0x3d')]);if(_0xe3b68a[_0xfb4a('0x3e')]){delete _0xe3b68a[_0xfb4a('0x3e')];}_0x34af19[_0xfb4a('0x2c')](_0x4c606d)[_0xfb4a('0x3f')](_0xe3b68a);};}exports[_0xfb4a('0x40')]=function(_0x1f1cec,_0x3d6164){var _0x354a32={'include':[{'model':db[_0xfb4a('0x41')],'as':'mandatoryDispositionPause'}]},_0x4d3491={},_0x2cc511={'count':0x0,'rows':[]};var _0x454ef9=_[_0xfb4a('0x42')](db[_0xfb4a('0x43')]['rawAttributes'],function(_0x185f09){return{'name':_0x185f09[_0xfb4a('0x44')],'type':_0x185f09['type'][_0xfb4a('0x45')]};});_0x4d3491[_0xfb4a('0x46')]=_[_0xfb4a('0x42')](_0x454ef9,_0xfb4a('0x3e'));_0x4d3491[_0xfb4a('0x47')]=_[_0xfb4a('0x48')](_0x1f1cec[_0xfb4a('0x47')]);_0x4d3491['filters']=_[_0xfb4a('0x49')](_0x4d3491[_0xfb4a('0x46')],_0x4d3491['query']);_0x354a32[_0xfb4a('0x4a')]=_[_0xfb4a('0x49')](_0x4d3491[_0xfb4a('0x46')],qs[_0xfb4a('0x4b')](_0x1f1cec[_0xfb4a('0x47')][_0xfb4a('0x4b')]));_0x354a32[_0xfb4a('0x4a')]=_0x354a32[_0xfb4a('0x4a')][_0xfb4a('0x4c')]?_0x354a32[_0xfb4a('0x4a')]:_0x4d3491['model'];if(!_0x1f1cec[_0xfb4a('0x47')][_0xfb4a('0x4d')](_0xfb4a('0x4e'))){_0x354a32[_0xfb4a('0x31')]=qs[_0xfb4a('0x31')](_0x1f1cec[_0xfb4a('0x47')][_0xfb4a('0x31')]);_0x354a32[_0xfb4a('0x2f')]=qs[_0xfb4a('0x2f')](_0x1f1cec[_0xfb4a('0x47')][_0xfb4a('0x2f')]);}_0x354a32[_0xfb4a('0x4f')]=qs[_0xfb4a('0x50')](_0x1f1cec[_0xfb4a('0x47')][_0xfb4a('0x50')]);_0x354a32['where']=qs[_0xfb4a('0x51')](_[_0xfb4a('0x52')](_0x1f1cec[_0xfb4a('0x47')],_0x4d3491['filters']),_0x454ef9);if(_0x1f1cec['query'][_0xfb4a('0x53')]){_0x354a32['where']=_['merge'](_0x354a32[_0xfb4a('0x54')],{'$or':_[_0xfb4a('0x42')](_0x454ef9,function(_0x398674){if(_0x398674[_0xfb4a('0x55')]!==_0xfb4a('0x56')){var _0xdd1b70={};_0xdd1b70[_0x398674[_0xfb4a('0x3e')]]={'$like':'%'+_0x1f1cec[_0xfb4a('0x47')][_0xfb4a('0x53')]+'%'};return _0xdd1b70;}})});}_0x354a32=_[_0xfb4a('0x57')]({},_0x354a32,_0x1f1cec['options']);var _0x48934d={'where':_0x354a32[_0xfb4a('0x54')]};return db[_0xfb4a('0x43')][_0xfb4a('0x32')](_0x48934d)['then'](function(_0x523600){_0x2cc511[_0xfb4a('0x32')]=_0x523600;if(_0x1f1cec['query'][_0xfb4a('0x58')]){_0x354a32[_0xfb4a('0x59')]=[{'all':!![]}];}return db[_0xfb4a('0x43')]['findAll'](_0x354a32);})[_0xfb4a('0x38')](function(_0x1dcce2){_0x2cc511[_0xfb4a('0x5a')]=_0x1dcce2;return _0x2cc511;})[_0xfb4a('0x38')](respondWithFilteredResult(_0x3d6164,_0x354a32))['catch'](handleError(_0x3d6164,null));};exports[_0xfb4a('0x5b')]=function(_0x493c29,_0x49b1e8){var _0x4d254a={'raw':![],'where':{'id':_0x493c29[_0xfb4a('0x5c')]['id']},'include':[{'model':db[_0xfb4a('0x41')],'as':'mandatoryDispositionPause'}]},_0x515647={};_0x515647['model']=_[_0xfb4a('0x48')](db['WhatsappAccount'][_0xfb4a('0x5d')]);_0x515647[_0xfb4a('0x47')]=_[_0xfb4a('0x48')](_0x493c29[_0xfb4a('0x47')]);_0x515647[_0xfb4a('0x51')]=_['intersection'](_0x515647[_0xfb4a('0x46')],_0x515647[_0xfb4a('0x47')]);_0x4d254a[_0xfb4a('0x4a')]=_[_0xfb4a('0x49')](_0x515647[_0xfb4a('0x46')],qs[_0xfb4a('0x4b')](_0x493c29['query'][_0xfb4a('0x4b')]));_0x4d254a[_0xfb4a('0x4a')]=_0x4d254a[_0xfb4a('0x4a')][_0xfb4a('0x4c')]?_0x4d254a[_0xfb4a('0x4a')]:_0x515647[_0xfb4a('0x46')];if(_0x493c29[_0xfb4a('0x47')][_0xfb4a('0x58')]){_0x4d254a[_0xfb4a('0x59')]=[{'all':!![]}];}_0x4d254a=_[_0xfb4a('0x57')]({},_0x4d254a,_0x493c29['options']);return db[_0xfb4a('0x43')][_0xfb4a('0x5e')](_0x4d254a)['then'](handleEntityNotFound(_0x49b1e8,null))[_0xfb4a('0x38')](respondWithResult(_0x49b1e8,null))[_0xfb4a('0x2a')](handleError(_0x49b1e8,null));};exports[_0xfb4a('0x5f')]=function(_0x2dce55,_0x256797){return db[_0xfb4a('0x43')][_0xfb4a('0x5f')](_0x2dce55['body'],{})[_0xfb4a('0x38')](function(_0x303fe7){var _0x151abc=_0x2dce55[_0xfb4a('0x60')][_0xfb4a('0x3a')]({'plain':!![]});if(!_0x151abc)throw new Error(_0xfb4a('0x61'));if(_0x151abc[_0xfb4a('0x62')]==='user'){var _0x3726a9=_0x303fe7[_0xfb4a('0x3a')]({'plain':!![]});var _0x266055=_0xfb4a('0x3b');return db['UserProfileSection'][_0xfb4a('0x5e')]({'where':{'name':_0x266055,'userProfileId':_0x151abc[_0xfb4a('0x63')]},'raw':!![]})[_0xfb4a('0x38')](function(_0xa79539){if(_0xa79539&&_0xa79539[_0xfb4a('0x64')]===0x0){return db[_0xfb4a('0x3c')][_0xfb4a('0x5f')]({'name':_0x3726a9[_0xfb4a('0x3e')],'resourceId':_0x3726a9['id'],'type':_0xa79539['name'],'sectionId':_0xa79539['id']},{})[_0xfb4a('0x38')](function(){return _0x303fe7;});}else{return _0x303fe7;}})[_0xfb4a('0x2a')](function(_0x58f45b){logger[_0xfb4a('0x26')](_0xfb4a('0x65'),_0x58f45b);throw _0x58f45b;});}return _0x303fe7;})['then'](respondWithResult(_0x256797,0xc9))[_0xfb4a('0x2a')](handleError(_0x256797,null));};exports['update']=function(_0x52d745,_0x1d721d){if(_0x52d745['body']['id']){delete _0x52d745[_0xfb4a('0x66')]['id'];}return db['WhatsappAccount'][_0xfb4a('0x5e')]({'where':{'id':_0x52d745['params']['id']},'include':[{'model':db[_0xfb4a('0x41')],'as':_0xfb4a('0x67')}]})['then'](handleEntityNotFound(_0x1d721d,null))[_0xfb4a('0x38')](saveUpdates(_0x52d745[_0xfb4a('0x66')],null))[_0xfb4a('0x38')](respondWithResult(_0x1d721d,null))[_0xfb4a('0x2a')](handleError(_0x1d721d,null));};exports[_0xfb4a('0x39')]=function(_0x45e6bb,_0x5c9f8d){return db[_0xfb4a('0x43')][_0xfb4a('0x5e')]({'where':{'id':_0x45e6bb[_0xfb4a('0x5c')]['id']}})[_0xfb4a('0x38')](handleEntityNotFound(_0x5c9f8d,null))[_0xfb4a('0x38')](removeEntity(_0x5c9f8d,null))[_0xfb4a('0x2a')](handleError(_0x5c9f8d,null));};exports['describe']=function(_0x2e5cbc,_0x4a1316){return db['WhatsappAccount'][_0xfb4a('0x68')]()[_0xfb4a('0x38')](respondWithResult(_0x4a1316,null))[_0xfb4a('0x2a')](handleError(_0x4a1316,null));};exports['addDisposition']=function(_0x4a566c,_0x382cad,_0x13bff6){if(_0x4a566c[_0xfb4a('0x66')]['id']){delete _0x4a566c['body']['id'];}return db[_0xfb4a('0x43')][_0xfb4a('0x5e')]({'where':{'id':_0x4a566c[_0xfb4a('0x5c')]['id']}})['then'](handleEntityNotFound(_0x382cad,null))[_0xfb4a('0x38')](function(_0x3df0ef){if(_0x3df0ef){_0x4a566c[_0xfb4a('0x66')]['WhatsappAccountId']=_0x3df0ef['id'];return db[_0xfb4a('0x69')][_0xfb4a('0x5f')](_0x4a566c['body']);}})[_0xfb4a('0x38')](respondWithResult(_0x382cad,null))[_0xfb4a('0x2a')](handleError(_0x382cad,null));};exports[_0xfb4a('0x6a')]=function(_0x21d4c5,_0x23ed18,_0x3747d2){var _0x50095b={'raw':![],'where':{}};var _0x84509d={};var _0x4fd831={'count':0x0,'rows':[]};return db[_0xfb4a('0x43')]['findOne']({'where':{'id':_0x21d4c5[_0xfb4a('0x5c')]['id']}})['then'](handleEntityNotFound(_0x23ed18,null))[_0xfb4a('0x38')](function(_0x2bd2f3){if(_0x2bd2f3){_0x84509d[_0xfb4a('0x46')]=_[_0xfb4a('0x48')](db['Disposition']['rawAttributes']);_0x84509d['query']=_[_0xfb4a('0x48')](_0x21d4c5['query']);_0x84509d[_0xfb4a('0x51')]=_[_0xfb4a('0x49')](_0x84509d[_0xfb4a('0x46')],_0x84509d[_0xfb4a('0x47')]);_0x50095b[_0xfb4a('0x4a')]=_['intersection'](_0x84509d[_0xfb4a('0x46')],qs[_0xfb4a('0x4b')](_0x21d4c5[_0xfb4a('0x47')][_0xfb4a('0x4b')]));_0x50095b[_0xfb4a('0x4a')]=_0x50095b[_0xfb4a('0x4a')][_0xfb4a('0x4c')]?_0x50095b[_0xfb4a('0x4a')]:_0x84509d[_0xfb4a('0x46')];if(!_0x21d4c5[_0xfb4a('0x47')]['hasOwnProperty'](_0xfb4a('0x4e'))){_0x50095b[_0xfb4a('0x31')]=qs['limit'](_0x21d4c5[_0xfb4a('0x47')][_0xfb4a('0x31')]);_0x50095b[_0xfb4a('0x2f')]=qs[_0xfb4a('0x2f')](_0x21d4c5[_0xfb4a('0x47')][_0xfb4a('0x2f')]);}_0x50095b[_0xfb4a('0x4f')]=qs[_0xfb4a('0x50')](_0x21d4c5[_0xfb4a('0x47')][_0xfb4a('0x50')]);_0x50095b[_0xfb4a('0x54')]=qs['filters'](_[_0xfb4a('0x52')](_0x21d4c5['query'],_0x84509d[_0xfb4a('0x51')]));_0x50095b[_0xfb4a('0x54')][_0xfb4a('0x6b')]=_0x2bd2f3['id'];if(_0x21d4c5[_0xfb4a('0x47')][_0xfb4a('0x53')]){_0x50095b[_0xfb4a('0x54')]=_[_0xfb4a('0x57')](_0x50095b[_0xfb4a('0x54')],{'$or':_[_0xfb4a('0x42')](_0x50095b[_0xfb4a('0x4a')],function(_0xba5bdc){var _0xe84c1c={};_0xe84c1c[_0xba5bdc]={'$like':'%'+_0x21d4c5[_0xfb4a('0x47')][_0xfb4a('0x53')]+'%'};return _0xe84c1c;})});}_0x50095b=_['merge']({},_0x50095b,_0x21d4c5['options']);return db[_0xfb4a('0x69')]['count']({'where':_0x50095b[_0xfb4a('0x54')]})[_0xfb4a('0x38')](function(_0x10bd97){_0x4fd831[_0xfb4a('0x32')]=_0x10bd97;if(_0x21d4c5[_0xfb4a('0x47')][_0xfb4a('0x58')]){_0x50095b[_0xfb4a('0x59')]=[{'all':!![]}];}return db[_0xfb4a('0x69')][_0xfb4a('0x6c')](_0x50095b);})['then'](function(_0x260355){_0x4fd831[_0xfb4a('0x5a')]=_0x260355;return _0x4fd831;});}})[_0xfb4a('0x38')](respondWithFilteredResult(_0x23ed18,_0x50095b))[_0xfb4a('0x2a')](handleError(_0x23ed18,null));};exports[_0xfb4a('0x6d')]=function(_0x57b8b9,_0xa2bbe8,_0x3ff209){return db[_0xfb4a('0x43')][_0xfb4a('0x5e')]({'where':{'id':_0x57b8b9[_0xfb4a('0x5c')]['id']}})[_0xfb4a('0x38')](handleEntityNotFound(_0xa2bbe8,null))[_0xfb4a('0x38')](function(_0x239dfe){if(_0x239dfe){return _0x239dfe['removeDispositions'](_0x57b8b9[_0xfb4a('0x47')][_0xfb4a('0x6e')]);}})[_0xfb4a('0x38')](respondWithStatusCode(_0xa2bbe8,null))[_0xfb4a('0x2a')](handleError(_0xa2bbe8,null));};exports[_0xfb4a('0x6f')]=function(_0x1fc51a,_0x518ca0,_0x1d9360){if(_0x1fc51a[_0xfb4a('0x66')]['id']){delete _0x1fc51a[_0xfb4a('0x66')]['id'];}return db['WhatsappAccount']['find']({'where':{'id':_0x1fc51a[_0xfb4a('0x5c')]['id']}})[_0xfb4a('0x38')](handleEntityNotFound(_0x518ca0,null))[_0xfb4a('0x38')](function(_0x13736f){if(_0x13736f){_0x1fc51a[_0xfb4a('0x66')][_0xfb4a('0x6b')]=_0x13736f['id'];return db[_0xfb4a('0x70')][_0xfb4a('0x5f')](_0x1fc51a[_0xfb4a('0x66')]);}})[_0xfb4a('0x38')](respondWithResult(_0x518ca0,null))[_0xfb4a('0x2a')](handleError(_0x518ca0,null));};exports['getAnswers']=function(_0x1a1850,_0xc4c437,_0x163ab5){var _0x388ab1={'raw':![],'where':{}};var _0x41a077={};var _0x4ae562={'count':0x0,'rows':[]};return db[_0xfb4a('0x43')][_0xfb4a('0x71')]({'where':{'id':_0x1a1850[_0xfb4a('0x5c')]['id']}})[_0xfb4a('0x38')](handleEntityNotFound(_0xc4c437,null))[_0xfb4a('0x38')](function(_0x28ea30){if(_0x28ea30){_0x41a077['model']=_[_0xfb4a('0x48')](db[_0xfb4a('0x70')][_0xfb4a('0x5d')]);_0x41a077[_0xfb4a('0x47')]=_[_0xfb4a('0x48')](_0x1a1850['query']);_0x41a077['filters']=_[_0xfb4a('0x49')](_0x41a077[_0xfb4a('0x46')],_0x41a077[_0xfb4a('0x47')]);_0x388ab1[_0xfb4a('0x4a')]=_['intersection'](_0x41a077[_0xfb4a('0x46')],qs['fields'](_0x1a1850[_0xfb4a('0x47')][_0xfb4a('0x4b')]));_0x388ab1[_0xfb4a('0x4a')]=_0x388ab1[_0xfb4a('0x4a')][_0xfb4a('0x4c')]?_0x388ab1[_0xfb4a('0x4a')]:_0x41a077[_0xfb4a('0x46')];if(!_0x1a1850[_0xfb4a('0x47')][_0xfb4a('0x4d')](_0xfb4a('0x4e'))){_0x388ab1[_0xfb4a('0x31')]=qs['limit'](_0x1a1850[_0xfb4a('0x47')][_0xfb4a('0x31')]);_0x388ab1['offset']=qs[_0xfb4a('0x2f')](_0x1a1850['query']['offset']);}_0x388ab1[_0xfb4a('0x4f')]=qs[_0xfb4a('0x50')](_0x1a1850[_0xfb4a('0x47')]['sort']);_0x388ab1[_0xfb4a('0x54')]=qs['filters'](_['pick'](_0x1a1850['query'],_0x41a077['filters']));_0x388ab1[_0xfb4a('0x54')][_0xfb4a('0x6b')]=_0x28ea30['id'];if(_0x1a1850[_0xfb4a('0x47')][_0xfb4a('0x53')]){_0x388ab1[_0xfb4a('0x54')]=_[_0xfb4a('0x57')](_0x388ab1[_0xfb4a('0x54')],{'$or':_[_0xfb4a('0x42')](_0x388ab1['attributes'],function(_0xf6ee92){var _0x33fc68={};_0x33fc68[_0xf6ee92]={'$like':'%'+_0x1a1850[_0xfb4a('0x47')][_0xfb4a('0x53')]+'%'};return _0x33fc68;})});}_0x388ab1=_[_0xfb4a('0x57')]({},_0x388ab1,_0x1a1850[_0xfb4a('0x72')]);return db[_0xfb4a('0x70')]['count']({'where':_0x388ab1['where']})['then'](function(_0xfe8b61){_0x4ae562[_0xfb4a('0x32')]=_0xfe8b61;if(_0x1a1850[_0xfb4a('0x47')][_0xfb4a('0x58')]){_0x388ab1['include']=[{'all':!![]}];}return db['CannedAnswer'][_0xfb4a('0x6c')](_0x388ab1);})[_0xfb4a('0x38')](function(_0x322f2d){_0x4ae562['rows']=_0x322f2d;return _0x4ae562;});}})[_0xfb4a('0x38')](respondWithFilteredResult(_0xc4c437,_0x388ab1))['catch'](handleError(_0xc4c437,null));};exports[_0xfb4a('0x73')]=function(_0xb744c5,_0x3b7da7,_0x4fe1e7){return db[_0xfb4a('0x43')]['find']({'where':{'id':_0xb744c5[_0xfb4a('0x5c')]['id']}})[_0xfb4a('0x38')](handleEntityNotFound(_0x3b7da7,null))[_0xfb4a('0x38')](function(_0x125652){if(_0x125652){return _0x125652[_0xfb4a('0x73')](_0xb744c5[_0xfb4a('0x47')][_0xfb4a('0x6e')]);}})['then'](respondWithStatusCode(_0x3b7da7,null))[_0xfb4a('0x2a')](handleError(_0x3b7da7,null));};exports[_0xfb4a('0x74')]=function(_0xacc416,_0x22b523,_0x51d14b){return db[_0xfb4a('0x43')]['find']({'where':{'id':_0xacc416[_0xfb4a('0x5c')]['id']}})['then'](handleEntityNotFound(_0x22b523,null))['then'](function(_0x2b6158){if(_0x2b6158){return _0x2b6158[_0xfb4a('0x74')](_0xacc416['body'][_0xfb4a('0x6e')],_[_0xfb4a('0x75')](_0xacc416[_0xfb4a('0x66')],[_0xfb4a('0x6e'),'id'])||{})[_0xfb4a('0x76')](function(_0x1a4637){for(var _0x4e7b4c=0x0;_0x4e7b4c<_0xacc416[_0xfb4a('0x66')]['ids'][_0xfb4a('0x4c')];_0x4e7b4c+=0x1){socket[_0xfb4a('0x77')](_0xfb4a('0x78'),{'UserId':Number(_0xacc416['body'][_0xfb4a('0x6e')][_0x4e7b4c]),'WhatsappAccountId':Number(_0xacc416['params']['id'])});}return _0x1a4637;});}})[_0xfb4a('0x38')](respondWithResult(_0x22b523,null))[_0xfb4a('0x2a')](handleError(_0x22b523,null));};exports['removeAgents']=function(_0x2a1d16,_0x9fed80,_0x5ae091){return db[_0xfb4a('0x43')][_0xfb4a('0x5e')]({'where':{'id':_0x2a1d16['params']['id']}})[_0xfb4a('0x38')](handleEntityNotFound(_0x9fed80,null))[_0xfb4a('0x38')](function(_0x3ccc78){if(_0x3ccc78){return _0x3ccc78[_0xfb4a('0x79')](_0x2a1d16[_0xfb4a('0x47')][_0xfb4a('0x6e')])[_0xfb4a('0x38')](function(){if(_['isArray'](_0x2a1d16['query'][_0xfb4a('0x6e')])){for(var _0x5deb13=0x0;_0x5deb13<_0x2a1d16[_0xfb4a('0x47')][_0xfb4a('0x6e')][_0xfb4a('0x4c')];_0x5deb13+=0x1){socket[_0xfb4a('0x77')](_0xfb4a('0x7a'),{'UserId':Number(_0x2a1d16[_0xfb4a('0x47')][_0xfb4a('0x6e')][_0x5deb13]),'WhatsappAccountId':Number(_0x2a1d16['params']['id'])});}}else{socket[_0xfb4a('0x77')](_0xfb4a('0x7a'),{'UserId':Number(_0x2a1d16[_0xfb4a('0x47')][_0xfb4a('0x6e')]),'WhatsappAccountId':Number(_0x2a1d16['params']['id'])});}});}})[_0xfb4a('0x38')](respondWithStatusCode(_0x9fed80,null))[_0xfb4a('0x2a')](handleError(_0x9fed80,null));};exports['getAgents']=function(_0x21d666,_0x483151,_0x262211){var _0x4769c1={};var _0x447b90={};var _0x161f24;var _0x160578;return db[_0xfb4a('0x43')]['findOne']({'where':{'id':_0x21d666[_0xfb4a('0x5c')]['id']}})[_0xfb4a('0x38')](handleEntityNotFound(_0x483151,null))[_0xfb4a('0x38')](function(_0x349c00){if(_0x349c00){_0x161f24=_0x349c00;_0x447b90['model']=_[_0xfb4a('0x48')](db['User']['rawAttributes']);_0x447b90[_0xfb4a('0x47')]=_['keys'](_0x21d666[_0xfb4a('0x47')]);_0x447b90[_0xfb4a('0x51')]=_[_0xfb4a('0x49')](_0x447b90[_0xfb4a('0x46')],_0x447b90[_0xfb4a('0x47')]);_0x4769c1['attributes']=_[_0xfb4a('0x49')](_0x447b90[_0xfb4a('0x46')],qs[_0xfb4a('0x4b')](_0x21d666[_0xfb4a('0x47')]['fields']));_0x4769c1[_0xfb4a('0x4a')]=_0x4769c1[_0xfb4a('0x4a')][_0xfb4a('0x4c')]?_0x4769c1[_0xfb4a('0x4a')]:_0x447b90['model'];_0x4769c1[_0xfb4a('0x4f')]=qs['sort'](_0x21d666['query'][_0xfb4a('0x50')]);_0x4769c1[_0xfb4a('0x54')]=qs[_0xfb4a('0x51')](_[_0xfb4a('0x52')](_0x21d666['query'],_0x447b90['filters']));if(_0x21d666[_0xfb4a('0x47')][_0xfb4a('0x53')]){_0x4769c1[_0xfb4a('0x54')]=_[_0xfb4a('0x57')](_0x4769c1['where'],{'$or':_[_0xfb4a('0x42')](_0x4769c1[_0xfb4a('0x4a')],function(_0x348e9f){var _0x80ec67={};_0x80ec67[_0x348e9f]={'$like':'%'+_0x21d666[_0xfb4a('0x47')]['filter']+'%'};return _0x80ec67;})});}_0x4769c1=_[_0xfb4a('0x57')]({},_0x4769c1,_0x21d666[_0xfb4a('0x72')]);return _0x161f24[_0xfb4a('0x7b')](_0x4769c1);}})[_0xfb4a('0x38')](function(_0x3b0e0b){if(_0x3b0e0b){_0x160578=_0x3b0e0b['length'];if(!_0x21d666[_0xfb4a('0x47')][_0xfb4a('0x4d')]('nolimit')){_0x4769c1[_0xfb4a('0x31')]=qs[_0xfb4a('0x31')](_0x21d666[_0xfb4a('0x47')][_0xfb4a('0x31')]);_0x4769c1['offset']=qs['offset'](_0x21d666['query'][_0xfb4a('0x2f')]);}return _0x161f24[_0xfb4a('0x7b')](_0x4769c1);}})['then'](function(_0x48cbaa){if(_0x48cbaa){return _0x48cbaa?{'count':_0x160578,'rows':_0x48cbaa}:null;}})[_0xfb4a('0x38')](respondWithResult(_0x483151,null))[_0xfb4a('0x2a')](handleError(_0x483151,null));};exports['addApplications']=function(_0xeeed41,_0x10ccde){var _0x1f87e1=_0xeeed41[_0xfb4a('0x5c')]['id'];var _0x3085db=_0xeeed41[_0xfb4a('0x66')];var _0x1c86a3=0xc8;var _0xd36e4b=null;return db['sequelize']['transaction']({'isolationLevel':db[_0xfb4a('0x7c')][_0xfb4a('0x7d')][_0xfb4a('0x7e')][_0xfb4a('0x7f')]},function(_0x4b2609){return db[_0xfb4a('0x43')][_0xfb4a('0x71')]({'where':{'id':_0x1f87e1},'transaction':_0x4b2609})['then'](function(_0x262a20){if(_0x262a20){return db[_0xfb4a('0x80')][_0xfb4a('0x39')]({'where':{'WhatsappAccountId':_0x1f87e1},'transaction':_0x4b2609})[_0xfb4a('0x38')](function(){var _0x51f888=_[_0xfb4a('0x42')](_0x3085db,function(_0x310e90){_0x310e90[_0xfb4a('0x6b')]=_0x1f87e1;return _0x310e90;});return db[_0xfb4a('0x80')][_0xfb4a('0x81')](_0x51f888,{'transaction':_0x4b2609});});}else{_0x1c86a3=0x194;_0xd36e4b=[];}});})[_0xfb4a('0x38')](function(){if(_0x1c86a3!==0x194){return db[_0xfb4a('0x80')]['findAndCountAll']({'where':{'WhatsappAccountId':_0x1f87e1},'order':_0xfb4a('0x82')})[_0xfb4a('0x38')](function(_0x57894b){_0xd36e4b=_0x57894b;});}})[_0xfb4a('0x2a')](function(_0x584ae6){_0x1c86a3=0x1f4;logger[_0xfb4a('0x26')](_0x584ae6[_0xfb4a('0x3d')]);if(_0x584ae6[_0xfb4a('0x3e')]){delete _0x584ae6[_0xfb4a('0x3e')];}_0xd36e4b=_0x584ae6;})['finally'](function(){if(_0xd36e4b===null){_0x10ccde[_0xfb4a('0x2b')](_0x1c86a3);}else{if(_0x1c86a3===0x1f4){_0x10ccde[_0xfb4a('0x2c')](_0x1c86a3)[_0xfb4a('0x3f')](_0xd36e4b);}else{_0x10ccde['status'](_0x1c86a3)['json'](_0xd36e4b);}}});};exports['getApplications']=function(_0x4d438c,_0x479f00,_0x5196d5){var _0x4121e3={};var _0x1db36c={};var _0x5acd4d;var _0x38fa30;return db[_0xfb4a('0x43')][_0xfb4a('0x71')]({'where':{'id':_0x4d438c[_0xfb4a('0x5c')]['id']}})[_0xfb4a('0x38')](handleEntityNotFound(_0x479f00,null))[_0xfb4a('0x38')](function(_0x4f4730){if(_0x4f4730){_0x5acd4d=_0x4f4730;_0x1db36c[_0xfb4a('0x46')]=_['keys'](db['WhatsappApplication'][_0xfb4a('0x5d')]);_0x1db36c[_0xfb4a('0x47')]=_[_0xfb4a('0x48')](_0x4d438c[_0xfb4a('0x47')]);_0x1db36c[_0xfb4a('0x51')]=_['intersection'](_0x1db36c[_0xfb4a('0x46')],_0x1db36c[_0xfb4a('0x47')]);_0x4121e3['attributes']=_['intersection'](_0x1db36c['model'],qs['fields'](_0x4d438c['query'][_0xfb4a('0x4b')]));_0x4121e3[_0xfb4a('0x4a')]=_0x4121e3[_0xfb4a('0x4a')][_0xfb4a('0x4c')]?_0x4121e3[_0xfb4a('0x4a')]:_0x1db36c['model'];_0x4121e3[_0xfb4a('0x4f')]=qs['sort'](_0x4d438c[_0xfb4a('0x47')][_0xfb4a('0x50')]);_0x4121e3[_0xfb4a('0x54')]=qs[_0xfb4a('0x51')](_[_0xfb4a('0x52')](_0x4d438c[_0xfb4a('0x47')],_0x1db36c['filters']));if(_0x4d438c['query']['filter']){_0x4121e3[_0xfb4a('0x54')]=_[_0xfb4a('0x57')](_0x4121e3[_0xfb4a('0x54')],{'$or':_['map'](_0x4121e3[_0xfb4a('0x4a')],function(_0x17598a){var _0x59d43={};_0x59d43[_0x17598a]={'$like':'%'+_0x4d438c[_0xfb4a('0x47')]['filter']+'%'};return _0x59d43;})});}_0x4121e3=_[_0xfb4a('0x57')]({},_0x4121e3,_0x4d438c[_0xfb4a('0x72')]);return _0x5acd4d[_0xfb4a('0x83')](_0x4121e3);}})[_0xfb4a('0x38')](function(_0x29c642){if(_0x29c642){_0x38fa30=_0x29c642[_0xfb4a('0x4c')];if(!_0x4d438c[_0xfb4a('0x47')][_0xfb4a('0x4d')](_0xfb4a('0x4e'))){_0x4121e3[_0xfb4a('0x31')]=qs[_0xfb4a('0x31')](_0x4d438c[_0xfb4a('0x47')]['limit']);_0x4121e3[_0xfb4a('0x2f')]=qs[_0xfb4a('0x2f')](_0x4d438c[_0xfb4a('0x47')]['offset']);}return _0x5acd4d['getApplications'](_0x4121e3);}})['then'](function(_0x171b6e){if(_0x171b6e){return _0x171b6e?{'count':_0x38fa30,'rows':_0x171b6e}:null;}})[_0xfb4a('0x38')](respondWithResult(_0x479f00,null))[_0xfb4a('0x2a')](handleError(_0x479f00,null));};exports[_0xfb4a('0x84')]=function(_0x21306a,_0x18fcb1,_0x5a67d3){var _0x26cc26={'channel':_0xfb4a('0x85')};var _0x18d1a0=[];var _0x50774c=[];var _0x22db29={};return db[_0xfb4a('0x43')][_0xfb4a('0x5e')]({'where':{'id':_0x21306a[_0xfb4a('0x5c')]['id']},'include':[{'model':db[_0xfb4a('0x80')],'as':_0xfb4a('0x86')}]})[_0xfb4a('0x38')](handleEntityNotFound(_0x18fcb1,null))[_0xfb4a('0x38')](function(_0xa94035){_0x26cc26[_0xfb4a('0x87')]=_0xa94035;if(_0x26cc26[_0xfb4a('0x87')]&&_0x26cc26[_0xfb4a('0x87')][_0xfb4a('0x55')]===_0xfb4a('0x88')&&_0x21306a['body']['statuses']){if(_0x21306a[_0xfb4a('0x66')][_0xfb4a('0x89')][_0xfb4a('0x4c')]===0x0)return;if(_0x21306a[_0xfb4a('0x66')][_0xfb4a('0x89')][0x0][_0xfb4a('0x8a')]!==_0xfb4a('0x8b'))return;return db[_0xfb4a('0x8c')][_0xfb4a('0x37')]({'read':!![],'readAt':_0x21306a[_0xfb4a('0x66')][_0xfb4a('0x89')][0x0][_0xfb4a('0x8d')]},{'where':{'messageId':_0x21306a[_0xfb4a('0x66')][_0xfb4a('0x89')][0x0][_0xfb4a('0x8e')]},'individualHooks':!![]})[_0xfb4a('0x38')](function(_0x9214c){return _0x18fcb1[_0xfb4a('0x2c')](0xc8)['json'](_0x9214c);});}_0x26cc26[_0xfb4a('0x8f')]=_[_0xfb4a('0x90')](_0xa94035['Applications'],['priority'],[_0xfb4a('0x91')]);if(_0x26cc26['account'][_0xfb4a('0x86')]){delete _0x26cc26[_0xfb4a('0x87')]['Applications'];}return db['CmContact'][_0xfb4a('0x68')]()['then'](function(_0x5cf74f){if(!_0x5cf74f){throw new db['Sequelize'][(_0xfb4a('0x92'))](_0xfb4a('0x93'));}_0x18d1a0=_[_0xfb4a('0x94')](_[_0xfb4a('0x48')](_0x5cf74f),[_0xfb4a('0x95'),_0xfb4a('0x96')]);_0x50774c=_[_0xfb4a('0x94')](_[_0xfb4a('0x48')](_0x5cf74f),[_0xfb4a('0x95'),'updatedAt','CompanyId',_0xfb4a('0x97')]);_0x26cc26[_0xfb4a('0x66')]={'mapKey':_0xfb4a('0x98')};if(_0x26cc26['account']&&_0x26cc26['account'][_0xfb4a('0x55')]===_0xfb4a('0x99')){_0x26cc26['body'][_0xfb4a('0x9a')]=_0x21306a[_0xfb4a('0x66')][_0xfb4a('0x9b')][_0xfb4a('0x9c')](_0xfb4a('0x9d'),'');_0x26cc26['body'][_0xfb4a('0x66')]=_0x21306a[_0xfb4a('0x66')][_0xfb4a('0x9e')];_0x26cc26['body'][_0xfb4a('0x9f')]=_0x21306a['body']['MessageSid'];}if(_0x26cc26[_0xfb4a('0x87')]&&_0x26cc26[_0xfb4a('0x87')]['type']===_0xfb4a('0x88')){_0x26cc26[_0xfb4a('0x66')][_0xfb4a('0x9a')]=_0x21306a[_0xfb4a('0x66')][_0xfb4a('0xa0')][0x0][_0xfb4a('0x9a')];_0x26cc26[_0xfb4a('0x66')][_0xfb4a('0x66')]=_0x21306a[_0xfb4a('0x66')][_0xfb4a('0xa0')][0x0][_0xfb4a('0x27')]['body'];_0x26cc26[_0xfb4a('0x66')][_0xfb4a('0x9f')]=_0x21306a[_0xfb4a('0x66')][_0xfb4a('0xa0')][0x0][_0xfb4a('0x8e')];}if(_0x26cc26[_0xfb4a('0x66')]['id']){delete _0x26cc26[_0xfb4a('0x66')]['id'];}if(_[_0xfb4a('0xa1')](_0x26cc26['body']['from'])){throw new db[(_0xfb4a('0xa2'))][(_0xfb4a('0x92'))](_0xfb4a('0xa3'));}if(_[_0xfb4a('0xa1')](_0x26cc26[_0xfb4a('0x66')][_0xfb4a('0x66')])||_0x26cc26[_0xfb4a('0x66')]['body']===''){throw new db[(_0xfb4a('0xa2'))]['ValidationError'](_0xfb4a('0xa4'));}if(_['isNil'](_0x26cc26[_0xfb4a('0x66')][_0xfb4a('0xa5')])){throw new db['Sequelize'][(_0xfb4a('0x92'))]('mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20'+_0x50774c);}if(!_['includes'](_0x50774c,_0x26cc26['body'][_0xfb4a('0xa5')])){throw new db[(_0xfb4a('0xa2'))][(_0xfb4a('0x92'))](_0xfb4a('0xa6')+_0x50774c);}_0x22db29[_0x26cc26[_0xfb4a('0x66')][_0xfb4a('0xa5')]]=_0x26cc26['body'][_0xfb4a('0x9a')];return db[_0xfb4a('0xa7')][_0xfb4a('0x5e')]({'where':{'id':_0x26cc26[_0xfb4a('0x87')][_0xfb4a('0x97')]},'include':[{'model':db['CmContact'],'as':_0xfb4a('0xa8'),'where':_0x22db29,'limit':0x1,'order':[['updatedAt',_0xfb4a('0xa9')]]}]});})[_0xfb4a('0x38')](handleEntityNotFound(_0x18fcb1,null))[_0xfb4a('0x38')](function(_0x150d5f){if(_0x150d5f){_0x26cc26[_0xfb4a('0xaa')]=_[_0xfb4a('0x75')](_0x150d5f,[_0xfb4a('0xa8')]);if(_0x150d5f[_0xfb4a('0xa8')]['length']){return _0x150d5f['Contacts'][0x0];}var _0x36eb28=_['defaults'](_0x26cc26[_0xfb4a('0x66')],{'firstName':_0x26cc26[_0xfb4a('0x66')][_0xfb4a('0x9a')],'ListId':_0x150d5f['id']});_0x36eb28[_0x26cc26[_0xfb4a('0x66')][_0xfb4a('0xa5')]]=_0x26cc26['body'][_0xfb4a('0x9a')];return db[_0xfb4a('0xab')][_0xfb4a('0x5f')](_0x36eb28,{'fields':_0x18d1a0,'raw':!![]});}})[_0xfb4a('0x38')](handleEntityNotFound(_0x18fcb1,null))[_0xfb4a('0x38')](function(_0x1e1451){if(_0x1e1451){_0x26cc26[_0xfb4a('0xac')]=_0x1e1451;var _0x2c37c8={'ContactId':_0x1e1451['id'],'phone':_0x26cc26['body'][_0xfb4a('0x9a')],'WhatsappAccountId':_0x21306a['params']['id']};var _0x3d7150={'ContactId':_0x1e1451['id'],'WhatsappAccountId':_0x21306a[_0xfb4a('0x5c')]['id'],'closed':![]};return db[_0xfb4a('0xad')][_0xfb4a('0x5e')]({'where':_0x3d7150})[_0xfb4a('0x38')](function(_0x2060a9){if(_0x2060a9){return[_0x2060a9,![]];}return db['WhatsappInteraction'][_0xfb4a('0x5f')](_0x2c37c8)[_0xfb4a('0x38')](function(_0x4103ed){return[_0x4103ed,!![]];});});}})[_0xfb4a('0x76')](function(_0x51e26e,_0x3d0ac2){_0x26cc26[_0xfb4a('0xae')]=_0x51e26e[_0xfb4a('0x3a')]({'plain':!![]});_0x26cc26[_0xfb4a('0xae')][_0xfb4a('0xaf')]=![];if(_0x3d0ac2){if(_0x26cc26[_0xfb4a('0x87')]['autoclose']){_0x26cc26[_0xfb4a('0x8f')][_0xfb4a('0xb0')]({'id':0x0,'priority':_0x26cc26[_0xfb4a('0x8f')]['length']+0x1,'app':'close','appdata':_0xfb4a('0xb1'),'interval':'*,*,*,*'});}_0x26cc26['interaction'][_0xfb4a('0xaf')]=!![];}return db[_0xfb4a('0x8c')][_0xfb4a('0x5f')]({'phone':_0x26cc26[_0xfb4a('0x66')][_0xfb4a('0x9a')],'messageId':_0x26cc26[_0xfb4a('0x66')][_0xfb4a('0x9f')],'body':_0x26cc26[_0xfb4a('0x66')][_0xfb4a('0x66')],'WhatsappAccountId':_0x21306a['params']['id'],'WhatsappInteractionId':_0x51e26e['id'],'direction':'in','ContactId':_0x51e26e[_0xfb4a('0xb2')],'AttachmentId':_0x26cc26[_0xfb4a('0x66')][_0xfb4a('0xb3')]});})[_0xfb4a('0x38')](function(_0x30de88){_0x26cc26[_0xfb4a('0x27')]=_0x30de88;if(!_0x26cc26[_0xfb4a('0xae')][_0xfb4a('0xb4')])return;return db[_0xfb4a('0xb5')][_0xfb4a('0x5e')]({'attributes':['id',_0xfb4a('0x3e')],'where':{'id':_0x26cc26['interaction'][_0xfb4a('0xb4')]}});})[_0xfb4a('0x38')](function(_0x478e96){if(_0x478e96){_0x26cc26[_0xfb4a('0x8f')][_0xfb4a('0xb6')]({'id':0x0,'priority':0x0,'app':_0xfb4a('0xb7'),'appdata':_0x478e96[_0xfb4a('0x3e')]+','+(_0x26cc26[_0xfb4a('0x87')][_0xfb4a('0xb8')]||0xa),'interval':_0xfb4a('0xb9')});}if(!_0x26cc26['interaction'][_0xfb4a('0xba')])return;return db['WhatsappQueue']['find']({'attributes':['id',_0xfb4a('0x3e')],'where':{'id':_0x26cc26[_0xfb4a('0xae')][_0xfb4a('0xba')]}});})[_0xfb4a('0x38')](function(_0x43dbfa){if(_0x43dbfa){_0x26cc26[_0xfb4a('0x8f')][_0xfb4a('0xb6')]({'id':0x0,'priority':0x1,'app':'queue','appdata':_0x43dbfa[_0xfb4a('0x3e')]+','+(_0x26cc26['account'][_0xfb4a('0xbb')]||0x12c),'interval':_0xfb4a('0xb9')});}return respondWithRpcPromise(_0xfb4a('0xbc'),_0xfb4a('0xbd'),_0x26cc26);})['then'](function(_0x2531df){if(_0x2531df){var _0x21d0c1=0xc8;var _0x1a8373=_0xfb4a('0xbe');logger['info'](_0xfb4a('0x24'),_0x1a8373,_0x21d0c1,_0xfb4a('0xbf'));logger[_0xfb4a('0x23')]('WhatsappAccount,\x20%s,\x20%s,\x20%s,\x20%s',_0x1a8373,_0x21d0c1,'entity\x20found',JSON['stringify'](_0x2531df));if(_0x26cc26['account']['type']===_0xfb4a('0x99')){return _0x18fcb1['status'](_0x21d0c1)[_0xfb4a('0x33')](_0xfb4a('0xc0'),_0xfb4a('0xc1'))[_0xfb4a('0x3f')](_0xfb4a('0xc2'));}else{return _0x18fcb1[_0xfb4a('0x2c')](_0x21d0c1)[_0xfb4a('0x2e')](_0x2531df);}}});})['catch'](handleError(_0x18fcb1,null));};exports[_0xfb4a('0x3f')]=function(_0x355c57,_0x4328aa,_0x345990){var _0x54383f,_0x2988a5,_0x219f73;if(_[_0xfb4a('0xa1')](_0x355c57[_0xfb4a('0x66')][_0xfb4a('0x66')])||_0x355c57[_0xfb4a('0x66')][_0xfb4a('0x66')]===''){throw new db[(_0xfb4a('0xa2'))][(_0xfb4a('0x92'))](_0xfb4a('0xa4'));}if(_[_0xfb4a('0xa1')](_0x355c57[_0xfb4a('0x66')]['to'])){throw new db[(_0xfb4a('0xa2'))][(_0xfb4a('0x92'))](_0xfb4a('0xc3'));}_0x54383f=_0x355c57[_0xfb4a('0x66')]['to'];return db['WhatsappAccount']['find']({'where':{'id':_0x355c57['params']['id']},'include':[{'model':db[_0xfb4a('0xa7')],'as':_0xfb4a('0xc4'),'include':[{'model':db[_0xfb4a('0xab')],'as':_0xfb4a('0xa8'),'where':{'mobile':_0x54383f},'limit':0x1,'order':[['updatedAt',_0xfb4a('0xa9')]]}]}]})['then'](handleEntityNotFound(_0x4328aa,null))[_0xfb4a('0x38')](function(_0x2463eb){if(_0x2463eb&&_0x2463eb[_0xfb4a('0xc4')]){_0x2988a5=_0x2463eb[_0xfb4a('0x3a')]({'plain':!![]});if(_0x2988a5['List']&&_0x2988a5[_0xfb4a('0xc4')][_0xfb4a('0xa8')][_0xfb4a('0x4c')]){return _0x2988a5[_0xfb4a('0xc4')][_0xfb4a('0xa8')][0x0];}return db[_0xfb4a('0xab')][_0xfb4a('0x5f')](_[_0xfb4a('0x1b')](_0x355c57['body'],{'firstName':_0x54383f,'mobile':_0x54383f,'phone':_0x54383f,'ListId':_0x2988a5[_0xfb4a('0x97')]}));}})['then'](handleEntityNotFound(_0x4328aa,null))[_0xfb4a('0x38')](function(_0x3190e8){if(_0x3190e8){_0x219f73=_0x3190e8;return db[_0xfb4a('0xad')][_0xfb4a('0x5e')]({'where':{'ContactId':_0x219f73['id'],'closed':![],'WhatsappAccountId':_0x2988a5['id']}})['then'](function(_0x4aacac){if(_0x4aacac){return[_0x4aacac,![]];}return db[_0xfb4a('0xad')]['create']({'UserId':_0x355c57[_0xfb4a('0x60')]['id'],'ContactId':_0x219f73['id'],'WhatsappAccountId':_0x2988a5['id'],'phone':_0x54383f,'firstMsgDirection':_0xfb4a('0xc5'),'Messages':[_[_0xfb4a('0x57')](_0x355c57[_0xfb4a('0x66')],{'read':![],'body':_0x355c57[_0xfb4a('0x66')]['body'],'phone':_0x54383f,'WhatsappAccountId':_0x2988a5['id'],'UserId':_0x355c57[_0xfb4a('0x60')]['id'],'ContactId':_0x219f73['id']})]},{'include':[{'model':db['WhatsappMessage'],'as':'Messages'}]})[_0xfb4a('0x38')](function(_0x197c8b){return[_0x197c8b,!![]];});});}})[_0xfb4a('0x76')](function(_0x42504c,_0x51a5d1){if(_0x51a5d1){return _0x42504c;}return db['WhatsappMessage']['create'](_[_0xfb4a('0x57')](_0x355c57[_0xfb4a('0x66')],{'read':![],'body':_0x355c57[_0xfb4a('0x66')][_0xfb4a('0x66')],'phone':_0x54383f,'WhatsappAccountId':_0x2988a5['id'],'WhatsappInteractionId':_0x42504c['id'],'UserId':_0x355c57['user']['id'],'ContactId':_0x219f73['id']}))[_0xfb4a('0x38')](function(_0x2678c3){_0x42504c[_0xfb4a('0xc6')]['Messages']=[];_0x42504c['dataValues']['Messages'][_0xfb4a('0xb0')](_0x2678c3[_0xfb4a('0xc6')]);return _0x42504c;});})[_0xfb4a('0x38')](respondWithResult(_0x4328aa,null))[_0xfb4a('0x2a')](handleError(_0x4328aa,null));};