Built motion from commit (unavailable).|2.5.18
[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 _0x2f88=['json','count','offset','limit','set','Content-Range','apply','reject','save','update','destroy','get','WhatsappAccounts','UserProfileResource','sendStatus','stack','name','send','index','Pause','map','rawAttributes','fieldName','type','model','query','keys','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','filters','filter','merge','options','includeAll','include','WhatsappAccount','findAll','rows','catch','show','mandatoryDispositionPause','find','create','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','autoAssociation','body','params','describe','addDisposition','WhatsappAccountId','getDispositions','findOne','Disposition','pick','ids','addAnswer','omit','spread','userWhatsappAccount:save','removeAgents','isArray','emit','userWhatsappAccount:remove','User','getAgents','addApplications','transaction','WhatsappApplication','bulkCreate','findAndCountAll','priority','getApplications','notify','whatsapp','Applications','account','applications','orderBy','asc','CmContact','Sequelize','ValidationError','no\x20available\x20attributes','difference','createdAt','updatedAt','CompanyId','ListId','mobile','twilio','From','replace','whatsapp:','Body','messageId','isNil','from','from\x20is\x20mandatory','mapKey','mapKey\x20is\x20mandatory,\x20please\x20use\x20one\x20of\x20these:\x20','includes','mapKey\x20invalid,\x20please\x20use\x20one\x20of\x20these:\x20','CmList','Contacts','DESC','contact','WhatsappInteraction','interaction','created','push','unmanaged','*,*,*,*','ContactId','UserId','unshift','agent','waitForTheAssignedAgent','startRouting','Start','receiveMessage','WhatsappAccount,\x20%s,\x20%s,\x20%s','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>','body\x20is\x20mandatory\x20and\x20not\x20empty','List','out','WhatsappMessage','Messages','dataValues','html-pdf','eml-format','zip-dir','fast-json-patch','request-promise','bluebird','mustache','util','path','sox','ejs','lodash','squel','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','redis','defaults','localhost','socket.io-emitter','register','jayson/promise','client','http','request','then','info','WhatsappAccount,\x20%s,\x20%s','request\x20sent','debug','stringify','error','code','message','result','end','status'];(function(_0x46378c,_0x14ad76){var _0x1b39de=function(_0x394673){while(--_0x394673){_0x46378c['push'](_0x46378c['shift']());}};_0x1b39de(++_0x14ad76);}(_0x2f88,0x14b));var _0x82f8=function(_0x549d1b,_0x4c0c38){_0x549d1b=_0x549d1b-0x0;var _0x13c79f=_0x2f88[_0x549d1b];return _0x13c79f;};'use strict';var pdf=require(_0x82f8('0x0'));var emlformat=require(_0x82f8('0x1'));var rimraf=require('rimraf');var zipdir=require(_0x82f8('0x2'));var jsonpatch=require(_0x82f8('0x3'));var rp=require(_0x82f8('0x4'));var moment=require('moment');var BPromise=require(_0x82f8('0x5'));var Mustache=require(_0x82f8('0x6'));var util=require(_0x82f8('0x7'));var path=require(_0x82f8('0x8'));var sox=require(_0x82f8('0x9'));var csv=require('to-csv');var ejs=require(_0x82f8('0xa'));var fs=require('fs');var _=require(_0x82f8('0xb'));var squel=require(_0x82f8('0xc'));var crypto=require('crypto');var jsforce=require('jsforce');var deskjs=require(_0x82f8('0xd'));var toCsv=require('to-csv');var querystring=require(_0x82f8('0xe'));var Papa=require(_0x82f8('0xf'));var Redis=require(_0x82f8('0x10'));var authService=require(_0x82f8('0x11'));var qs=require(_0x82f8('0x12'));var as=require(_0x82f8('0x13'));var hardwareService=require(_0x82f8('0x14'));var logger=require(_0x82f8('0x15'))(_0x82f8('0x16'));var utils=require(_0x82f8('0x17'));var config=require(_0x82f8('0x18'));var licenseUtil=require('../../config/license/util');var db=require(_0x82f8('0x19'))['db'];config[_0x82f8('0x1a')]=_[_0x82f8('0x1b')](config[_0x82f8('0x1a')],{'host':_0x82f8('0x1c'),'port':0x18eb});var socket=require(_0x82f8('0x1d'))(new Redis(config['redis']));require('./whatsappAccount.socket')[_0x82f8('0x1e')](socket);var jayson=require(_0x82f8('0x1f'));var client=jayson[_0x82f8('0x20')][_0x82f8('0x21')]({'port':0x232c});function respondWithRpcPromise(_0x26d3ad,_0x3c89ac,_0x5483be,_0x1e49c2){return new BPromise(function(_0x294e5e,_0xcd12df){var _0x48dfeb=_0x1e49c2||client;return _0x48dfeb[_0x82f8('0x22')](_0x26d3ad,_0x5483be)[_0x82f8('0x23')](function(_0x40b526){logger[_0x82f8('0x24')](_0x82f8('0x25'),_0x3c89ac,_0x82f8('0x26'));logger[_0x82f8('0x27')]('WhatsappAccount,\x20%s,\x20%s,\x20%s',_0x3c89ac,_0x82f8('0x26'),JSON[_0x82f8('0x28')](_0x40b526));if(_0x40b526[_0x82f8('0x29')]){if(_0x40b526[_0x82f8('0x29')][_0x82f8('0x2a')]===0x1f4){logger[_0x82f8('0x29')](_0x82f8('0x25'),_0x3c89ac,_0x40b526['error'][_0x82f8('0x2b')]);return _0xcd12df(_0x40b526['error'][_0x82f8('0x2b')]);}logger[_0x82f8('0x29')](_0x82f8('0x25'),_0x3c89ac,_0x40b526['error'][_0x82f8('0x2b')]);return _0x294e5e(_0x40b526[_0x82f8('0x29')][_0x82f8('0x2b')]);}else{logger['info'](_0x82f8('0x25'),_0x3c89ac,_0x82f8('0x26'));_0x294e5e(_0x40b526[_0x82f8('0x2c')]['message']);}})['catch'](function(_0x3375bf){logger[_0x82f8('0x29')](_0x82f8('0x25'),_0x3c89ac,_0x3375bf);_0xcd12df(_0x3375bf);});});}function respondWithStatusCode(_0x32a237,_0x3c5427){_0x3c5427=_0x3c5427||0xcc;return function(_0x32c831){if(_0x32c831){return _0x32a237['sendStatus'](_0x3c5427);}return _0x32a237['status'](_0x3c5427)[_0x82f8('0x2d')]();};}function respondWithResult(_0x4764ce,_0x40f2a7){_0x40f2a7=_0x40f2a7||0xc8;return function(_0x25f4b7){if(_0x25f4b7){return _0x4764ce[_0x82f8('0x2e')](_0x40f2a7)[_0x82f8('0x2f')](_0x25f4b7);}};}function respondWithFilteredResult(_0x205929,_0x449fb5){return function(_0x318d48){if(_0x318d48){var _0xe1c1a9=_0x318d48[_0x82f8('0x30')],_0x1ed11b=_0x449fb5['offset'],_0x38e6e0=_0x449fb5[_0x82f8('0x31')]+_0x449fb5[_0x82f8('0x32')],_0x11e6fa;if(_0x38e6e0>=_0xe1c1a9){_0x38e6e0=_0xe1c1a9;_0x11e6fa=0xc8;}else{_0x11e6fa=0xce;}_0x205929['status'](_0x11e6fa);return _0x205929[_0x82f8('0x33')](_0x82f8('0x34'),_0x1ed11b+'-'+_0x38e6e0+'/'+_0xe1c1a9)[_0x82f8('0x2f')](_0x318d48);}return null;};}function patchUpdates(_0x9ba9a4){return function(_0x6e2d67){try{jsonpatch[_0x82f8('0x35')](_0x6e2d67,_0x9ba9a4,!![]);}catch(_0x435ed4){return BPromise[_0x82f8('0x36')](_0x435ed4);}return _0x6e2d67[_0x82f8('0x37')]();};}function saveUpdates(_0x31753a,_0x1277db){return function(_0x35b889){if(_0x35b889){return _0x35b889[_0x82f8('0x38')](_0x31753a)[_0x82f8('0x23')](function(_0x1a8fe6){return _0x1a8fe6;});}return null;};}function removeEntity(_0x3030e1,_0x5ba904){return function(_0x2e9333){if(_0x2e9333){return _0x2e9333[_0x82f8('0x39')]()['then'](function(){var _0x3d8da4=_0x2e9333[_0x82f8('0x3a')]({'plain':!![]});var _0x520932=_0x82f8('0x3b');return db[_0x82f8('0x3c')][_0x82f8('0x39')]({'where':{'type':_0x520932,'resourceId':_0x3d8da4['id']}})[_0x82f8('0x23')](function(){return _0x2e9333;});})['then'](function(){_0x3030e1[_0x82f8('0x2e')](0xcc)[_0x82f8('0x2d')]();});}};}function handleEntityNotFound(_0x2d1bee,_0x5d228c){return function(_0x24bd02){if(!_0x24bd02){_0x2d1bee[_0x82f8('0x3d')](0x194);}return _0x24bd02;};}function handleError(_0x333867,_0x4fd1d5){_0x4fd1d5=_0x4fd1d5||0x1f4;return function(_0x3ddefc){logger[_0x82f8('0x29')](_0x3ddefc[_0x82f8('0x3e')]);if(_0x3ddefc[_0x82f8('0x3f')]){delete _0x3ddefc['name'];}_0x333867['status'](_0x4fd1d5)[_0x82f8('0x40')](_0x3ddefc);};}exports[_0x82f8('0x41')]=function(_0x11e62e,_0x37306a){var _0x598159={'include':[{'model':db[_0x82f8('0x42')],'as':'mandatoryDispositionPause'}]},_0x5f1b3f={},_0x505676={'count':0x0,'rows':[]};var _0x458b2a=_[_0x82f8('0x43')](db['WhatsappAccount'][_0x82f8('0x44')],function(_0x4aa496){return{'name':_0x4aa496[_0x82f8('0x45')],'type':_0x4aa496[_0x82f8('0x46')]['key']};});_0x5f1b3f[_0x82f8('0x47')]=_[_0x82f8('0x43')](_0x458b2a,_0x82f8('0x3f'));_0x5f1b3f[_0x82f8('0x48')]=_[_0x82f8('0x49')](_0x11e62e[_0x82f8('0x48')]);_0x5f1b3f['filters']=_[_0x82f8('0x4a')](_0x5f1b3f['model'],_0x5f1b3f['query']);_0x598159[_0x82f8('0x4b')]=_[_0x82f8('0x4a')](_0x5f1b3f[_0x82f8('0x47')],qs[_0x82f8('0x4c')](_0x11e62e[_0x82f8('0x48')][_0x82f8('0x4c')]));_0x598159[_0x82f8('0x4b')]=_0x598159[_0x82f8('0x4b')][_0x82f8('0x4d')]?_0x598159[_0x82f8('0x4b')]:_0x5f1b3f[_0x82f8('0x47')];if(!_0x11e62e[_0x82f8('0x48')][_0x82f8('0x4e')](_0x82f8('0x4f'))){_0x598159[_0x82f8('0x32')]=qs['limit'](_0x11e62e['query'][_0x82f8('0x32')]);_0x598159[_0x82f8('0x31')]=qs[_0x82f8('0x31')](_0x11e62e['query'][_0x82f8('0x31')]);}_0x598159[_0x82f8('0x50')]=qs[_0x82f8('0x51')](_0x11e62e[_0x82f8('0x48')][_0x82f8('0x51')]);_0x598159[_0x82f8('0x52')]=qs[_0x82f8('0x53')](_['pick'](_0x11e62e['query'],_0x5f1b3f[_0x82f8('0x53')]),_0x458b2a);if(_0x11e62e[_0x82f8('0x48')][_0x82f8('0x54')]){_0x598159['where']=_[_0x82f8('0x55')](_0x598159[_0x82f8('0x52')],{'$or':_[_0x82f8('0x43')](_0x458b2a,function(_0x31b283){if(_0x31b283[_0x82f8('0x46')]!=='VIRTUAL'){var _0x5d890={};_0x5d890[_0x31b283[_0x82f8('0x3f')]]={'$like':'%'+_0x11e62e[_0x82f8('0x48')][_0x82f8('0x54')]+'%'};return _0x5d890;}})});}_0x598159=_[_0x82f8('0x55')]({},_0x598159,_0x11e62e[_0x82f8('0x56')]);var _0x24bb3d={'where':_0x598159[_0x82f8('0x52')]};return db['WhatsappAccount'][_0x82f8('0x30')](_0x24bb3d)[_0x82f8('0x23')](function(_0x3941f4){_0x505676[_0x82f8('0x30')]=_0x3941f4;if(_0x11e62e[_0x82f8('0x48')][_0x82f8('0x57')]){_0x598159[_0x82f8('0x58')]=[{'all':!![]}];}return db[_0x82f8('0x59')][_0x82f8('0x5a')](_0x598159);})[_0x82f8('0x23')](function(_0xa53e7d){_0x505676[_0x82f8('0x5b')]=_0xa53e7d;return _0x505676;})[_0x82f8('0x23')](respondWithFilteredResult(_0x37306a,_0x598159))[_0x82f8('0x5c')](handleError(_0x37306a,null));};exports[_0x82f8('0x5d')]=function(_0x300602,_0x341411){var _0x33c4d1={'raw':![],'where':{'id':_0x300602['params']['id']},'include':[{'model':db['Pause'],'as':_0x82f8('0x5e')}]},_0x1e1732={};_0x1e1732[_0x82f8('0x47')]=_['keys'](db[_0x82f8('0x59')][_0x82f8('0x44')]);_0x1e1732['query']=_[_0x82f8('0x49')](_0x300602[_0x82f8('0x48')]);_0x1e1732[_0x82f8('0x53')]=_[_0x82f8('0x4a')](_0x1e1732[_0x82f8('0x47')],_0x1e1732[_0x82f8('0x48')]);_0x33c4d1[_0x82f8('0x4b')]=_[_0x82f8('0x4a')](_0x1e1732['model'],qs[_0x82f8('0x4c')](_0x300602[_0x82f8('0x48')][_0x82f8('0x4c')]));_0x33c4d1['attributes']=_0x33c4d1[_0x82f8('0x4b')]['length']?_0x33c4d1['attributes']:_0x1e1732[_0x82f8('0x47')];if(_0x300602[_0x82f8('0x48')][_0x82f8('0x57')]){_0x33c4d1[_0x82f8('0x58')]=[{'all':!![]}];}_0x33c4d1=_[_0x82f8('0x55')]({},_0x33c4d1,_0x300602[_0x82f8('0x56')]);return db[_0x82f8('0x59')][_0x82f8('0x5f')](_0x33c4d1)[_0x82f8('0x23')](handleEntityNotFound(_0x341411,null))[_0x82f8('0x23')](respondWithResult(_0x341411,null))[_0x82f8('0x5c')](handleError(_0x341411,null));};exports[_0x82f8('0x60')]=function(_0x11c21f,_0x4b487c){return db[_0x82f8('0x59')]['create'](_0x11c21f['body'],{})[_0x82f8('0x23')](function(_0xdd0123){var _0x3742dd=_0x11c21f[_0x82f8('0x61')][_0x82f8('0x3a')]({'plain':!![]});if(!_0x3742dd)throw new Error(_0x82f8('0x62'));if(_0x3742dd[_0x82f8('0x63')]===_0x82f8('0x61')){var _0x319f2b=_0xdd0123['get']({'plain':!![]});var _0x46566d=_0x82f8('0x3b');return db[_0x82f8('0x64')][_0x82f8('0x5f')]({'where':{'name':_0x46566d,'userProfileId':_0x3742dd['userProfileId']},'raw':!![]})[_0x82f8('0x23')](function(_0x355c5e){if(_0x355c5e&&_0x355c5e[_0x82f8('0x65')]===0x0){return db[_0x82f8('0x3c')][_0x82f8('0x60')]({'name':_0x319f2b[_0x82f8('0x3f')],'resourceId':_0x319f2b['id'],'type':_0x355c5e[_0x82f8('0x3f')],'sectionId':_0x355c5e['id']},{})[_0x82f8('0x23')](function(){return _0xdd0123;});}else{return _0xdd0123;}})[_0x82f8('0x5c')](function(_0x42e02d){logger[_0x82f8('0x29')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x42e02d);throw _0x42e02d;});}return _0xdd0123;})[_0x82f8('0x23')](respondWithResult(_0x4b487c,0xc9))['catch'](handleError(_0x4b487c,null));};exports['update']=function(_0x4266c6,_0x45a210){if(_0x4266c6[_0x82f8('0x66')]['id']){delete _0x4266c6[_0x82f8('0x66')]['id'];}return db['WhatsappAccount'][_0x82f8('0x5f')]({'where':{'id':_0x4266c6[_0x82f8('0x67')]['id']},'include':[{'model':db['Pause'],'as':'mandatoryDispositionPause'}]})[_0x82f8('0x23')](handleEntityNotFound(_0x45a210,null))[_0x82f8('0x23')](saveUpdates(_0x4266c6[_0x82f8('0x66')],null))['then'](respondWithResult(_0x45a210,null))['catch'](handleError(_0x45a210,null));};exports['destroy']=function(_0x32801e,_0x43372f){return db[_0x82f8('0x59')][_0x82f8('0x5f')]({'where':{'id':_0x32801e[_0x82f8('0x67')]['id']}})[_0x82f8('0x23')](handleEntityNotFound(_0x43372f,null))[_0x82f8('0x23')](removeEntity(_0x43372f,null))['catch'](handleError(_0x43372f,null));};exports[_0x82f8('0x68')]=function(_0x45245a,_0x5f4aa7){return db['WhatsappAccount'][_0x82f8('0x68')]()[_0x82f8('0x23')](respondWithResult(_0x5f4aa7,null))[_0x82f8('0x5c')](handleError(_0x5f4aa7,null));};exports[_0x82f8('0x69')]=function(_0x5d7dbc,_0x42c4ac,_0x300166){if(_0x5d7dbc[_0x82f8('0x66')]['id']){delete _0x5d7dbc['body']['id'];}return db[_0x82f8('0x59')]['find']({'where':{'id':_0x5d7dbc['params']['id']}})[_0x82f8('0x23')](handleEntityNotFound(_0x42c4ac,null))['then'](function(_0x58ceca){if(_0x58ceca){_0x5d7dbc['body'][_0x82f8('0x6a')]=_0x58ceca['id'];return db['Disposition']['create'](_0x5d7dbc[_0x82f8('0x66')]);}})['then'](respondWithResult(_0x42c4ac,null))[_0x82f8('0x5c')](handleError(_0x42c4ac,null));};exports[_0x82f8('0x6b')]=function(_0xff42b9,_0x22e54a,_0x349810){var _0x4d16b2={'raw':![],'where':{}};var _0x2954d5={};var _0x13a3b9={'count':0x0,'rows':[]};return db[_0x82f8('0x59')][_0x82f8('0x6c')]({'where':{'id':_0xff42b9['params']['id']}})[_0x82f8('0x23')](handleEntityNotFound(_0x22e54a,null))['then'](function(_0x9df6cc){if(_0x9df6cc){_0x2954d5[_0x82f8('0x47')]=_[_0x82f8('0x49')](db[_0x82f8('0x6d')][_0x82f8('0x44')]);_0x2954d5[_0x82f8('0x48')]=_[_0x82f8('0x49')](_0xff42b9[_0x82f8('0x48')]);_0x2954d5[_0x82f8('0x53')]=_[_0x82f8('0x4a')](_0x2954d5[_0x82f8('0x47')],_0x2954d5[_0x82f8('0x48')]);_0x4d16b2['attributes']=_[_0x82f8('0x4a')](_0x2954d5['model'],qs[_0x82f8('0x4c')](_0xff42b9[_0x82f8('0x48')][_0x82f8('0x4c')]));_0x4d16b2[_0x82f8('0x4b')]=_0x4d16b2[_0x82f8('0x4b')][_0x82f8('0x4d')]?_0x4d16b2[_0x82f8('0x4b')]:_0x2954d5[_0x82f8('0x47')];if(!_0xff42b9[_0x82f8('0x48')][_0x82f8('0x4e')](_0x82f8('0x4f'))){_0x4d16b2[_0x82f8('0x32')]=qs[_0x82f8('0x32')](_0xff42b9[_0x82f8('0x48')][_0x82f8('0x32')]);_0x4d16b2['offset']=qs[_0x82f8('0x31')](_0xff42b9[_0x82f8('0x48')][_0x82f8('0x31')]);}_0x4d16b2[_0x82f8('0x50')]=qs[_0x82f8('0x51')](_0xff42b9[_0x82f8('0x48')][_0x82f8('0x51')]);_0x4d16b2[_0x82f8('0x52')]=qs[_0x82f8('0x53')](_[_0x82f8('0x6e')](_0xff42b9[_0x82f8('0x48')],_0x2954d5[_0x82f8('0x53')]));_0x4d16b2['where'][_0x82f8('0x6a')]=_0x9df6cc['id'];if(_0xff42b9['query'][_0x82f8('0x54')]){_0x4d16b2[_0x82f8('0x52')]=_[_0x82f8('0x55')](_0x4d16b2[_0x82f8('0x52')],{'$or':_['map'](_0x4d16b2[_0x82f8('0x4b')],function(_0xddff19){var _0x1d43d3={};_0x1d43d3[_0xddff19]={'$like':'%'+_0xff42b9[_0x82f8('0x48')][_0x82f8('0x54')]+'%'};return _0x1d43d3;})});}_0x4d16b2=_['merge']({},_0x4d16b2,_0xff42b9[_0x82f8('0x56')]);return db[_0x82f8('0x6d')][_0x82f8('0x30')]({'where':_0x4d16b2[_0x82f8('0x52')]})['then'](function(_0xe7105f){_0x13a3b9['count']=_0xe7105f;if(_0xff42b9[_0x82f8('0x48')]['includeAll']){_0x4d16b2['include']=[{'all':!![]}];}return db['Disposition'][_0x82f8('0x5a')](_0x4d16b2);})['then'](function(_0x590e72){_0x13a3b9[_0x82f8('0x5b')]=_0x590e72;return _0x13a3b9;});}})['then'](respondWithFilteredResult(_0x22e54a,_0x4d16b2))[_0x82f8('0x5c')](handleError(_0x22e54a,null));};exports['removeDispositions']=function(_0x187c33,_0x547e88,_0x130cc7){return db['WhatsappAccount'][_0x82f8('0x5f')]({'where':{'id':_0x187c33['params']['id']}})['then'](handleEntityNotFound(_0x547e88,null))[_0x82f8('0x23')](function(_0x4cf4f4){if(_0x4cf4f4){return _0x4cf4f4['removeDispositions'](_0x187c33[_0x82f8('0x48')][_0x82f8('0x6f')]);}})['then'](respondWithStatusCode(_0x547e88,null))['catch'](handleError(_0x547e88,null));};exports[_0x82f8('0x70')]=function(_0x30345a,_0x4f6cab,_0x19465e){if(_0x30345a[_0x82f8('0x66')]['id']){delete _0x30345a[_0x82f8('0x66')]['id'];}return db[_0x82f8('0x59')][_0x82f8('0x5f')]({'where':{'id':_0x30345a[_0x82f8('0x67')]['id']}})[_0x82f8('0x23')](handleEntityNotFound(_0x4f6cab,null))['then'](function(_0x4c1e6a){if(_0x4c1e6a){_0x30345a[_0x82f8('0x66')]['WhatsappAccountId']=_0x4c1e6a['id'];return db['CannedAnswer'][_0x82f8('0x60')](_0x30345a[_0x82f8('0x66')]);}})['then'](respondWithResult(_0x4f6cab,null))[_0x82f8('0x5c')](handleError(_0x4f6cab,null));};exports['getAnswers']=function(_0x322fe9,_0x359a1e,_0x249b77){var _0x554bf2={'raw':![],'where':{}};var _0x3f3c00={};var _0x3ab622={'count':0x0,'rows':[]};return db['WhatsappAccount']['findOne']({'where':{'id':_0x322fe9[_0x82f8('0x67')]['id']}})['then'](handleEntityNotFound(_0x359a1e,null))[_0x82f8('0x23')](function(_0x1e64cf){if(_0x1e64cf){_0x3f3c00[_0x82f8('0x47')]=_[_0x82f8('0x49')](db['CannedAnswer']['rawAttributes']);_0x3f3c00['query']=_['keys'](_0x322fe9['query']);_0x3f3c00[_0x82f8('0x53')]=_[_0x82f8('0x4a')](_0x3f3c00[_0x82f8('0x47')],_0x3f3c00[_0x82f8('0x48')]);_0x554bf2[_0x82f8('0x4b')]=_['intersection'](_0x3f3c00[_0x82f8('0x47')],qs[_0x82f8('0x4c')](_0x322fe9[_0x82f8('0x48')][_0x82f8('0x4c')]));_0x554bf2[_0x82f8('0x4b')]=_0x554bf2[_0x82f8('0x4b')][_0x82f8('0x4d')]?_0x554bf2[_0x82f8('0x4b')]:_0x3f3c00['model'];if(!_0x322fe9['query']['hasOwnProperty'](_0x82f8('0x4f'))){_0x554bf2[_0x82f8('0x32')]=qs[_0x82f8('0x32')](_0x322fe9['query'][_0x82f8('0x32')]);_0x554bf2[_0x82f8('0x31')]=qs[_0x82f8('0x31')](_0x322fe9['query'][_0x82f8('0x31')]);}_0x554bf2['order']=qs[_0x82f8('0x51')](_0x322fe9[_0x82f8('0x48')][_0x82f8('0x51')]);_0x554bf2[_0x82f8('0x52')]=qs[_0x82f8('0x53')](_['pick'](_0x322fe9[_0x82f8('0x48')],_0x3f3c00['filters']));_0x554bf2[_0x82f8('0x52')][_0x82f8('0x6a')]=_0x1e64cf['id'];if(_0x322fe9[_0x82f8('0x48')][_0x82f8('0x54')]){_0x554bf2['where']=_[_0x82f8('0x55')](_0x554bf2[_0x82f8('0x52')],{'$or':_['map'](_0x554bf2[_0x82f8('0x4b')],function(_0x291bad){var _0x4cd72a={};_0x4cd72a[_0x291bad]={'$like':'%'+_0x322fe9[_0x82f8('0x48')][_0x82f8('0x54')]+'%'};return _0x4cd72a;})});}_0x554bf2=_[_0x82f8('0x55')]({},_0x554bf2,_0x322fe9['options']);return db['CannedAnswer'][_0x82f8('0x30')]({'where':_0x554bf2['where']})[_0x82f8('0x23')](function(_0x4de2d2){_0x3ab622['count']=_0x4de2d2;if(_0x322fe9[_0x82f8('0x48')][_0x82f8('0x57')]){_0x554bf2[_0x82f8('0x58')]=[{'all':!![]}];}return db['CannedAnswer'][_0x82f8('0x5a')](_0x554bf2);})['then'](function(_0x42bce0){_0x3ab622[_0x82f8('0x5b')]=_0x42bce0;return _0x3ab622;});}})[_0x82f8('0x23')](respondWithFilteredResult(_0x359a1e,_0x554bf2))['catch'](handleError(_0x359a1e,null));};exports['removeAnswers']=function(_0x2fe671,_0xd35e86,_0x13b222){return db[_0x82f8('0x59')][_0x82f8('0x5f')]({'where':{'id':_0x2fe671[_0x82f8('0x67')]['id']}})[_0x82f8('0x23')](handleEntityNotFound(_0xd35e86,null))[_0x82f8('0x23')](function(_0x168ac6){if(_0x168ac6){return _0x168ac6['removeAnswers'](_0x2fe671[_0x82f8('0x48')][_0x82f8('0x6f')]);}})[_0x82f8('0x23')](respondWithStatusCode(_0xd35e86,null))['catch'](handleError(_0xd35e86,null));};exports['addAgents']=function(_0x1f92b1,_0x3cf550,_0x47d047){return db[_0x82f8('0x59')][_0x82f8('0x5f')]({'where':{'id':_0x1f92b1[_0x82f8('0x67')]['id']}})[_0x82f8('0x23')](handleEntityNotFound(_0x3cf550,null))['then'](function(_0x22efaf){if(_0x22efaf){return _0x22efaf['addAgents'](_0x1f92b1['body'][_0x82f8('0x6f')],_[_0x82f8('0x71')](_0x1f92b1[_0x82f8('0x66')],[_0x82f8('0x6f'),'id'])||{})[_0x82f8('0x72')](function(_0xf77a4){for(var _0x36d500=0x0;_0x36d500<_0x1f92b1[_0x82f8('0x66')][_0x82f8('0x6f')][_0x82f8('0x4d')];_0x36d500+=0x1){socket['emit'](_0x82f8('0x73'),{'UserId':_0x1f92b1[_0x82f8('0x66')]['ids'][_0x36d500],'WhatsappAccountId':_0x1f92b1[_0x82f8('0x67')]['id']});}return _0xf77a4;});}})[_0x82f8('0x23')](respondWithResult(_0x3cf550,null))[_0x82f8('0x5c')](handleError(_0x3cf550,null));};exports['removeAgents']=function(_0x296622,_0x355726,_0x82707){return db[_0x82f8('0x59')][_0x82f8('0x5f')]({'where':{'id':_0x296622[_0x82f8('0x67')]['id']}})['then'](handleEntityNotFound(_0x355726,null))[_0x82f8('0x23')](function(_0x4aee9b){if(_0x4aee9b){return _0x4aee9b[_0x82f8('0x74')](_0x296622['query'][_0x82f8('0x6f')])['then'](function(){if(_[_0x82f8('0x75')](_0x296622[_0x82f8('0x48')][_0x82f8('0x6f')])){for(var _0x5752aa=0x0;_0x5752aa<_0x296622[_0x82f8('0x48')][_0x82f8('0x6f')][_0x82f8('0x4d')];_0x5752aa+=0x1){socket[_0x82f8('0x76')](_0x82f8('0x77'),{'UserId':Number(_0x296622[_0x82f8('0x48')][_0x82f8('0x6f')][_0x5752aa]),'WhatsappAccountId':Number(_0x296622['params']['id'])});}}else{socket[_0x82f8('0x76')](_0x82f8('0x77'),{'UserId':Number(_0x296622['query'][_0x82f8('0x6f')]),'WhatsappAccountId':Number(_0x296622[_0x82f8('0x67')]['id'])});}});}})[_0x82f8('0x23')](respondWithStatusCode(_0x355726,null))[_0x82f8('0x5c')](handleError(_0x355726,null));};exports['getAgents']=function(_0x2549b3,_0x5085cf,_0x389aca){var _0x43cef0={};var _0x516e66={};var _0x8f208c;var _0x293399;return db[_0x82f8('0x59')][_0x82f8('0x6c')]({'where':{'id':_0x2549b3[_0x82f8('0x67')]['id']}})[_0x82f8('0x23')](handleEntityNotFound(_0x5085cf,null))[_0x82f8('0x23')](function(_0x7dcabb){if(_0x7dcabb){_0x8f208c=_0x7dcabb;_0x516e66[_0x82f8('0x47')]=_[_0x82f8('0x49')](db[_0x82f8('0x78')][_0x82f8('0x44')]);_0x516e66['query']=_[_0x82f8('0x49')](_0x2549b3[_0x82f8('0x48')]);_0x516e66['filters']=_['intersection'](_0x516e66[_0x82f8('0x47')],_0x516e66[_0x82f8('0x48')]);_0x43cef0[_0x82f8('0x4b')]=_['intersection'](_0x516e66['model'],qs[_0x82f8('0x4c')](_0x2549b3['query']['fields']));_0x43cef0[_0x82f8('0x4b')]=_0x43cef0[_0x82f8('0x4b')]['length']?_0x43cef0[_0x82f8('0x4b')]:_0x516e66[_0x82f8('0x47')];_0x43cef0[_0x82f8('0x50')]=qs[_0x82f8('0x51')](_0x2549b3['query'][_0x82f8('0x51')]);_0x43cef0[_0x82f8('0x52')]=qs[_0x82f8('0x53')](_['pick'](_0x2549b3[_0x82f8('0x48')],_0x516e66['filters']));if(_0x2549b3[_0x82f8('0x48')][_0x82f8('0x54')]){_0x43cef0[_0x82f8('0x52')]=_[_0x82f8('0x55')](_0x43cef0[_0x82f8('0x52')],{'$or':_['map'](_0x43cef0[_0x82f8('0x4b')],function(_0x3f2bc3){var _0x5f13ad={};_0x5f13ad[_0x3f2bc3]={'$like':'%'+_0x2549b3[_0x82f8('0x48')]['filter']+'%'};return _0x5f13ad;})});}_0x43cef0=_[_0x82f8('0x55')]({},_0x43cef0,_0x2549b3[_0x82f8('0x56')]);return _0x8f208c[_0x82f8('0x79')](_0x43cef0);}})[_0x82f8('0x23')](function(_0x29e477){if(_0x29e477){_0x293399=_0x29e477[_0x82f8('0x4d')];if(!_0x2549b3[_0x82f8('0x48')][_0x82f8('0x4e')]('nolimit')){_0x43cef0['limit']=qs['limit'](_0x2549b3[_0x82f8('0x48')][_0x82f8('0x32')]);_0x43cef0['offset']=qs['offset'](_0x2549b3[_0x82f8('0x48')][_0x82f8('0x31')]);}return _0x8f208c['getAgents'](_0x43cef0);}})[_0x82f8('0x23')](function(_0x2afb28){if(_0x2afb28){return _0x2afb28?{'count':_0x293399,'rows':_0x2afb28}:null;}})[_0x82f8('0x23')](respondWithResult(_0x5085cf,null))[_0x82f8('0x5c')](handleError(_0x5085cf,null));};exports[_0x82f8('0x7a')]=function(_0x3e43bf,_0x308252,_0x134b68){return db['WhatsappAccount'][_0x82f8('0x6c')]({'where':{'id':_0x3e43bf[_0x82f8('0x67')]['id']}})[_0x82f8('0x23')](handleEntityNotFound(_0x308252,null))[_0x82f8('0x23')](function(_0x299c93){if(_0x299c93){return db['sequelize'][_0x82f8('0x7b')](function(_0x2e6520){return db[_0x82f8('0x7c')][_0x82f8('0x39')]({'where':{'WhatsappAccountId':_0x3e43bf[_0x82f8('0x67')]['id']},'transaction':_0x2e6520})[_0x82f8('0x23')](function(_0x2ed019){var _0x128353=_[_0x82f8('0x43')](_0x3e43bf[_0x82f8('0x66')],function(_0x5db8b2){_0x5db8b2[_0x82f8('0x6a')]=_0x3e43bf[_0x82f8('0x67')]['id'];return _0x5db8b2;});return db[_0x82f8('0x7c')][_0x82f8('0x7d')](_0x128353,{'transaction':_0x2e6520});});})[_0x82f8('0x23')](function(){return db[_0x82f8('0x7c')][_0x82f8('0x7e')]({'where':{'WhatsappAccountId':_0x3e43bf[_0x82f8('0x67')]['id']},'order':_0x82f8('0x7f')});});}})[_0x82f8('0x23')](respondWithResult(_0x308252,null))['catch'](handleError(_0x308252,null));};exports[_0x82f8('0x80')]=function(_0x3e874d,_0x4ed666,_0xf2e5fa){var _0x10f49a={};var _0xd0b352={};var _0x33ae1c;var _0x3af940;return db['WhatsappAccount']['findOne']({'where':{'id':_0x3e874d[_0x82f8('0x67')]['id']}})[_0x82f8('0x23')](handleEntityNotFound(_0x4ed666,null))[_0x82f8('0x23')](function(_0x3dee34){if(_0x3dee34){_0x33ae1c=_0x3dee34;_0xd0b352[_0x82f8('0x47')]=_[_0x82f8('0x49')](db[_0x82f8('0x7c')]['rawAttributes']);_0xd0b352[_0x82f8('0x48')]=_[_0x82f8('0x49')](_0x3e874d[_0x82f8('0x48')]);_0xd0b352[_0x82f8('0x53')]=_[_0x82f8('0x4a')](_0xd0b352['model'],_0xd0b352[_0x82f8('0x48')]);_0x10f49a['attributes']=_['intersection'](_0xd0b352[_0x82f8('0x47')],qs[_0x82f8('0x4c')](_0x3e874d['query'][_0x82f8('0x4c')]));_0x10f49a[_0x82f8('0x4b')]=_0x10f49a[_0x82f8('0x4b')][_0x82f8('0x4d')]?_0x10f49a[_0x82f8('0x4b')]:_0xd0b352[_0x82f8('0x47')];_0x10f49a['order']=qs['sort'](_0x3e874d[_0x82f8('0x48')][_0x82f8('0x51')]);_0x10f49a[_0x82f8('0x52')]=qs[_0x82f8('0x53')](_[_0x82f8('0x6e')](_0x3e874d[_0x82f8('0x48')],_0xd0b352[_0x82f8('0x53')]));if(_0x3e874d['query'][_0x82f8('0x54')]){_0x10f49a[_0x82f8('0x52')]=_[_0x82f8('0x55')](_0x10f49a[_0x82f8('0x52')],{'$or':_[_0x82f8('0x43')](_0x10f49a[_0x82f8('0x4b')],function(_0x3bbf08){var _0x2fd4da={};_0x2fd4da[_0x3bbf08]={'$like':'%'+_0x3e874d[_0x82f8('0x48')]['filter']+'%'};return _0x2fd4da;})});}_0x10f49a=_[_0x82f8('0x55')]({},_0x10f49a,_0x3e874d[_0x82f8('0x56')]);return _0x33ae1c['getApplications'](_0x10f49a);}})['then'](function(_0x4a4dc0){if(_0x4a4dc0){_0x3af940=_0x4a4dc0[_0x82f8('0x4d')];if(!_0x3e874d['query']['hasOwnProperty']('nolimit')){_0x10f49a['limit']=qs['limit'](_0x3e874d[_0x82f8('0x48')][_0x82f8('0x32')]);_0x10f49a[_0x82f8('0x31')]=qs[_0x82f8('0x31')](_0x3e874d[_0x82f8('0x48')][_0x82f8('0x31')]);}return _0x33ae1c[_0x82f8('0x80')](_0x10f49a);}})[_0x82f8('0x23')](function(_0x1d982a){if(_0x1d982a){return _0x1d982a?{'count':_0x3af940,'rows':_0x1d982a}:null;}})[_0x82f8('0x23')](respondWithResult(_0x4ed666,null))['catch'](handleError(_0x4ed666,null));};exports[_0x82f8('0x81')]=function(_0x41f9a4,_0x2d57df,_0x51e36a){var _0x2b8739={'channel':_0x82f8('0x82')};var _0x38c571=[];var _0x2c85e6=[];var _0x24d384={};return db[_0x82f8('0x59')][_0x82f8('0x5f')]({'where':{'id':_0x41f9a4['params']['id']},'include':[{'model':db['WhatsappApplication'],'as':_0x82f8('0x83')}]})[_0x82f8('0x23')](handleEntityNotFound(_0x2d57df,null))[_0x82f8('0x23')](function(_0x10269f){_0x2b8739[_0x82f8('0x84')]=_0x10269f;_0x2b8739[_0x82f8('0x85')]=_[_0x82f8('0x86')](_0x10269f[_0x82f8('0x83')],['priority'],[_0x82f8('0x87')]);if(_0x2b8739['account'][_0x82f8('0x83')]){delete _0x2b8739[_0x82f8('0x84')][_0x82f8('0x83')];}return db[_0x82f8('0x88')][_0x82f8('0x68')]();})[_0x82f8('0x23')](function(_0x525e83){if(!_0x525e83){throw new db[(_0x82f8('0x89'))][(_0x82f8('0x8a'))](_0x82f8('0x8b'));}_0x38c571=_[_0x82f8('0x8c')](_[_0x82f8('0x49')](_0x525e83),[_0x82f8('0x8d'),_0x82f8('0x8e')]);_0x2c85e6=_[_0x82f8('0x8c')](_['keys'](_0x525e83),[_0x82f8('0x8d'),_0x82f8('0x8e'),_0x82f8('0x8f'),_0x82f8('0x90')]);_0x2b8739[_0x82f8('0x66')]={'mapKey':_0x82f8('0x91')};if(_0x2b8739[_0x82f8('0x84')]['type']&&_0x2b8739['account'][_0x82f8('0x46')]===_0x82f8('0x92')){_0x2b8739[_0x82f8('0x66')]['from']=_0x41f9a4[_0x82f8('0x66')][_0x82f8('0x93')][_0x82f8('0x94')](_0x82f8('0x95'),'');_0x2b8739[_0x82f8('0x66')][_0x82f8('0x66')]=_0x41f9a4['body'][_0x82f8('0x96')];_0x2b8739['body'][_0x82f8('0x97')]=_0x41f9a4[_0x82f8('0x66')]['MessageSid'];}if(_0x2b8739[_0x82f8('0x66')]['id']){delete _0x2b8739['body']['id'];}if(_[_0x82f8('0x98')](_0x2b8739[_0x82f8('0x66')][_0x82f8('0x99')])){throw new db['Sequelize'][(_0x82f8('0x8a'))](_0x82f8('0x9a'));}if(_[_0x82f8('0x98')](_0x2b8739[_0x82f8('0x66')][_0x82f8('0x66')])||_0x2b8739[_0x82f8('0x66')][_0x82f8('0x66')]===''){throw new db[(_0x82f8('0x89'))][(_0x82f8('0x8a'))]('body\x20is\x20mandatory\x20and\x20not\x20empty');}if(_[_0x82f8('0x98')](_0x2b8739[_0x82f8('0x66')][_0x82f8('0x9b')])){throw new db[(_0x82f8('0x89'))][(_0x82f8('0x8a'))](_0x82f8('0x9c')+_0x2c85e6);}if(!_[_0x82f8('0x9d')](_0x2c85e6,_0x2b8739['body'][_0x82f8('0x9b')])){throw new db[(_0x82f8('0x89'))][(_0x82f8('0x8a'))](_0x82f8('0x9e')+_0x2c85e6);}_0x24d384[_0x2b8739[_0x82f8('0x66')][_0x82f8('0x9b')]]=_0x2b8739[_0x82f8('0x66')][_0x82f8('0x99')];return db[_0x82f8('0x9f')][_0x82f8('0x5f')]({'where':{'id':_0x2b8739[_0x82f8('0x84')][_0x82f8('0x90')]},'include':[{'model':db[_0x82f8('0x88')],'as':_0x82f8('0xa0'),'where':_0x24d384,'limit':0x1,'order':[[_0x82f8('0x8e'),_0x82f8('0xa1')]]}]});})[_0x82f8('0x23')](handleEntityNotFound(_0x2d57df,null))[_0x82f8('0x23')](function(_0x2ef681){if(_0x2ef681){_0x2b8739['list']=_['omit'](_0x2ef681,[_0x82f8('0xa0')]);if(_0x2ef681[_0x82f8('0xa0')][_0x82f8('0x4d')]){return _0x2ef681[_0x82f8('0xa0')][0x0];}var _0x4216bd=_[_0x82f8('0x1b')](_0x2b8739[_0x82f8('0x66')],{'firstName':_0x2b8739[_0x82f8('0x66')][_0x82f8('0x99')],'ListId':_0x2ef681['id']});_0x4216bd[_0x2b8739[_0x82f8('0x66')][_0x82f8('0x9b')]]=_0x2b8739[_0x82f8('0x66')]['from'];return db[_0x82f8('0x88')][_0x82f8('0x60')](_0x4216bd,{'fields':_0x38c571,'raw':!![]});}})[_0x82f8('0x23')](handleEntityNotFound(_0x2d57df,null))['then'](function(_0x2c0462){if(_0x2c0462){_0x2b8739[_0x82f8('0xa2')]=_0x2c0462;var _0x518092={'ContactId':_0x2c0462['id'],'phone':_0x2b8739[_0x82f8('0x66')]['from'],'WhatsappAccountId':_0x41f9a4[_0x82f8('0x67')]['id']};var _0x5eeeec={'ContactId':_0x2c0462['id'],'WhatsappAccountId':_0x41f9a4[_0x82f8('0x67')]['id'],'closed':![]};return db[_0x82f8('0xa3')][_0x82f8('0x5f')]({'where':_0x5eeeec})[_0x82f8('0x23')](function(_0x4941ad){if(_0x4941ad){return[_0x4941ad,![]];}return db[_0x82f8('0xa3')][_0x82f8('0x60')](_0x518092)[_0x82f8('0x23')](function(_0x1a117b){return[_0x1a117b,!![]];});});}})[_0x82f8('0x72')](function(_0x3f8a20,_0x2570ae){_0x2b8739[_0x82f8('0xa4')]=_0x3f8a20['get']({'plain':!![]});_0x2b8739[_0x82f8('0xa4')][_0x82f8('0xa5')]=![];if(_0x2570ae){if(_0x2b8739[_0x82f8('0x84')]['autoclose']){_0x2b8739[_0x82f8('0x85')][_0x82f8('0xa6')]({'id':0x0,'priority':_0x2b8739[_0x82f8('0x85')][_0x82f8('0x4d')]+0x1,'app':'close','appdata':_0x82f8('0xa7'),'interval':_0x82f8('0xa8')});}_0x2b8739[_0x82f8('0xa4')][_0x82f8('0xa5')]=!![];}return db['WhatsappMessage'][_0x82f8('0x60')]({'phone':_0x2b8739['body'][_0x82f8('0x99')],'messageId':_0x2b8739[_0x82f8('0x66')][_0x82f8('0x97')],'body':_0x2b8739[_0x82f8('0x66')][_0x82f8('0x66')],'WhatsappAccountId':_0x41f9a4['params']['id'],'WhatsappInteractionId':_0x3f8a20['id'],'direction':'in','ContactId':_0x3f8a20[_0x82f8('0xa9')],'AttachmentId':_0x2b8739[_0x82f8('0x66')]['AttachmentId']});})[_0x82f8('0x23')](function(_0x347d2a){_0x2b8739[_0x82f8('0x2b')]=_0x347d2a;if(_0x2b8739['interaction'][_0x82f8('0xaa')]){return db['User'][_0x82f8('0x5f')]({'attributes':['id','name'],'where':{'id':_0x2b8739[_0x82f8('0xa4')][_0x82f8('0xaa')]}})[_0x82f8('0x23')](function(_0x193287){if(_0x193287){_0x2b8739[_0x82f8('0x85')][_0x82f8('0xab')]({'id':0x0,'priority':0x0,'app':_0x82f8('0xac'),'appdata':_0x193287[_0x82f8('0x3f')]+','+(_0x2b8739['account'][_0x82f8('0xad')]||0xa),'interval':'*,*,*,*'});}return respondWithRpcPromise('Start',_0x82f8('0xae'),_0x2b8739);});}return respondWithRpcPromise(_0x82f8('0xaf'),'startRouting',_0x2b8739);})['then'](function(_0x22a771){if(_0x22a771){var _0x2f5c24=0xc8;var _0x19c7ef=_0x82f8('0xb0');logger[_0x82f8('0x24')](_0x82f8('0xb1'),_0x19c7ef,_0x2f5c24,_0x82f8('0xb2'));logger[_0x82f8('0x27')](_0x82f8('0xb3'),_0x19c7ef,_0x2f5c24,'entity\x20found',JSON['stringify'](_0x22a771));if(_0x2b8739[_0x82f8('0x84')][_0x82f8('0x46')]===_0x82f8('0x92')){return _0x2d57df[_0x82f8('0x2e')](_0x2f5c24)[_0x82f8('0x33')](_0x82f8('0xb4'),_0x82f8('0xb5'))[_0x82f8('0x40')](_0x82f8('0xb6'));}else{return _0x2d57df[_0x82f8('0x2e')](_0x2f5c24)[_0x82f8('0x2f')](_0x22a771);}}})[_0x82f8('0x5c')](handleError(_0x2d57df,null));};exports[_0x82f8('0x40')]=function(_0x42a762,_0x1a5f62,_0x168ad4){var _0x4e66a3,_0xaf9813,_0x41cf40;if(_[_0x82f8('0x98')](_0x42a762[_0x82f8('0x66')][_0x82f8('0x66')])||_0x42a762[_0x82f8('0x66')][_0x82f8('0x66')]===''){throw new db[(_0x82f8('0x89'))][(_0x82f8('0x8a'))](_0x82f8('0xb7'));}if(_['isNil'](_0x42a762[_0x82f8('0x66')]['to'])){throw new db[(_0x82f8('0x89'))][(_0x82f8('0x8a'))]('to\x20is\x20mandatory');}_0x4e66a3=_0x42a762[_0x82f8('0x66')]['to'];return db[_0x82f8('0x59')][_0x82f8('0x5f')]({'where':{'id':_0x42a762[_0x82f8('0x67')]['id']},'include':[{'model':db['CmList'],'as':_0x82f8('0xb8'),'include':[{'model':db['CmContact'],'as':_0x82f8('0xa0'),'where':{'mobile':_0x4e66a3},'limit':0x1,'order':[[_0x82f8('0x8e'),'DESC']]}]}]})[_0x82f8('0x23')](handleEntityNotFound(_0x1a5f62,null))['then'](function(_0x3c3e24){if(_0x3c3e24&&_0x3c3e24[_0x82f8('0xb8')]){_0xaf9813=_0x3c3e24[_0x82f8('0x3a')]({'plain':!![]});if(_0xaf9813[_0x82f8('0xb8')]&&_0xaf9813[_0x82f8('0xb8')][_0x82f8('0xa0')][_0x82f8('0x4d')]){return _0xaf9813[_0x82f8('0xb8')]['Contacts'][0x0];}return db[_0x82f8('0x88')][_0x82f8('0x60')](_[_0x82f8('0x1b')](_0x42a762['body'],{'firstName':_0x4e66a3,'mobile':_0x4e66a3,'phone':_0x4e66a3,'ListId':_0xaf9813[_0x82f8('0x90')]}));}})[_0x82f8('0x23')](handleEntityNotFound(_0x1a5f62,null))[_0x82f8('0x23')](function(_0x143e28){if(_0x143e28){_0x41cf40=_0x143e28;return db[_0x82f8('0xa3')]['find']({'where':{'ContactId':_0x41cf40['id'],'closed':![],'WhatsappAccountId':_0xaf9813['id']}})[_0x82f8('0x23')](function(_0x143304){if(_0x143304){return[_0x143304,![]];}return db[_0x82f8('0xa3')][_0x82f8('0x60')]({'UserId':_0x42a762[_0x82f8('0x61')]['id'],'ContactId':_0x41cf40['id'],'WhatsappAccountId':_0xaf9813['id'],'phone':_0x4e66a3,'firstMsgDirection':_0x82f8('0xb9'),'Messages':[_[_0x82f8('0x55')](_0x42a762['body'],{'read':![],'body':_0x42a762['body'][_0x82f8('0x66')],'phone':_0x4e66a3,'WhatsappAccountId':_0xaf9813['id'],'UserId':_0x42a762['user']['id'],'ContactId':_0x41cf40['id']})]},{'include':[{'model':db[_0x82f8('0xba')],'as':_0x82f8('0xbb')}]})['then'](function(_0x5c9b03){return[_0x5c9b03,!![]];});});}})['spread'](function(_0x1c2b4e,_0x658009){if(_0x658009){return _0x1c2b4e;}return db[_0x82f8('0xba')]['create'](_[_0x82f8('0x55')](_0x42a762['body'],{'read':![],'body':_0x42a762[_0x82f8('0x66')][_0x82f8('0x66')],'phone':_0x4e66a3,'WhatsappAccountId':_0xaf9813['id'],'WhatsappInteractionId':_0x1c2b4e['id'],'UserId':_0x42a762['user']['id'],'ContactId':_0x41cf40['id']}))[_0x82f8('0x23')](function(_0x3164a3){_0x1c2b4e[_0x82f8('0xbc')]['Messages']=[];_0x1c2b4e[_0x82f8('0xbc')][_0x82f8('0xbb')][_0x82f8('0xa6')](_0x3164a3['dataValues']);return _0x1c2b4e;});})[_0x82f8('0x23')](respondWithResult(_0x1a5f62,null))[_0x82f8('0x5c')](handleError(_0x1a5f62,null));};