Built motion from commit d5e4af8c.|2.6.23
[motion2.git] / server / api / whatsappInteraction / whatsappInteraction.controller.js
index 889f181..c36579b 100644 (file)
@@ -15,4 +15,4 @@
 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                        *
 // *                                                                       *
 // *************************************************************************
-var _0x934b=['set','Content-Range','destroy','then','error','stack','name','send','index','type','key','getOptions','findAll','query','user','catch','show','model','WhatsappInteraction','rawAttributes','keys','filters','attributes','intersection','fields','length','include','merge','options','find','create','body','update','params','describe','addMessage','WhatsappMessage','omit','ids','getMessages','findOne','sort','where','pick','$gte','format','YYYY-MM-DD\x20HH:mm:ss','includeAll','rows','addTags','setTags','spread','Tag','color','emit','whatsappInteractionTags:save','removeTags','download','unix','toString','join','root','server','files','tmp','attachments','transcript-%d-%s.zip','push','User','Contact','firstName','lastName','Attachment','Messages','createdAt','direction','read','WhatsappAccount','Account','get','mkdirSync','Owner','fullname','closed','closedAt','basename','existsSync','createReadStream','pipe','createWriteStream','out','System','secret','value','renderFile','views','downloadInteraction.ejs','Whatsapp','createPdfFromHTML','log','err','rimraf','zip-dir','moment','bluebird','path','ejs','lodash','ioredis','../../components/interaction/service','../../components/parsers/qs','../../components/export/pdf','../../config/logger','../../config/environment','redis','localhost','socket.io-emitter','./whatsappInteraction.socket','register','sendStatus','status','end','json','offset','undefined','limit','count'];(function(_0x2becba,_0x4ea656){var _0x90b5dd=function(_0x26ae2b){while(--_0x26ae2b){_0x2becba['push'](_0x2becba['shift']());}};_0x90b5dd(++_0x4ea656);}(_0x934b,0xe4));var _0xb934=function(_0x36f5cb,_0x2a720f){_0x36f5cb=_0x36f5cb-0x0;var _0x58d6ce=_0x934b[_0x36f5cb];return _0x58d6ce;};'use strict';var rimraf=require(_0xb934('0x0'));var zipdir=require(_0xb934('0x1'));var moment=require(_0xb934('0x2'));var BPromise=require(_0xb934('0x3'));var util=require('util');var path=require(_0xb934('0x4'));var ejs=require(_0xb934('0x5'));var fs=require('fs');var _=require(_0xb934('0x6'));var Redis=require(_0xb934('0x7'));var interaction=require(_0xb934('0x8'));var qs=require(_0xb934('0x9'));var pdf=require(_0xb934('0xa'));var logger=require(_0xb934('0xb'))('api');var config=require(_0xb934('0xc'));var db=require('../../mysqldb')['db'];config[_0xb934('0xd')]=_['defaults'](config[_0xb934('0xd')],{'host':_0xb934('0xe'),'port':0x18eb});var socket=require(_0xb934('0xf'))(new Redis(config[_0xb934('0xd')]));require(_0xb934('0x10'))[_0xb934('0x11')](socket);function respondWithStatusCode(_0x275c3f,_0x552155){_0x552155=_0x552155||0xcc;return function(_0x4ab59d){if(_0x4ab59d){return _0x275c3f[_0xb934('0x12')](_0x552155);}return _0x275c3f[_0xb934('0x13')](_0x552155)[_0xb934('0x14')]();};}function respondWithResult(_0x1c7234,_0x4b1bab){_0x4b1bab=_0x4b1bab||0xc8;return function(_0x431934){if(_0x431934){return _0x1c7234[_0xb934('0x13')](_0x4b1bab)[_0xb934('0x15')](_0x431934);}};}function respondWithFilteredResult(_0xd5fab4,_0x516d6a){return function(_0x3590d9){if(_0x3590d9){var _0x24ce2f=typeof _0x516d6a[_0xb934('0x16')]===_0xb934('0x17')&&typeof _0x516d6a[_0xb934('0x18')]===_0xb934('0x17');var _0x512642=_0x3590d9[_0xb934('0x19')];var _0x1ea202=_0x24ce2f?0x0:_0x516d6a[_0xb934('0x16')];var _0x15f749=_0x24ce2f?_0x3590d9[_0xb934('0x19')]:_0x516d6a[_0xb934('0x16')]+_0x516d6a[_0xb934('0x18')];var _0x122b4a;if(_0x15f749>=_0x512642){_0x15f749=_0x512642;_0x122b4a=0xc8;}else{_0x122b4a=0xce;}_0xd5fab4['status'](_0x122b4a);return _0xd5fab4[_0xb934('0x1a')](_0xb934('0x1b'),_0x1ea202+'-'+_0x15f749+'/'+_0x512642)[_0xb934('0x15')](_0x3590d9);}return null;};}function saveUpdates(_0x4d2de5){return function(_0x474d89){if(_0x474d89){return _0x474d89['update'](_0x4d2de5)['then'](function(_0xde2c30){return _0xde2c30;});}return null;};}function removeEntity(_0xf0c0d){return function(_0x2cfdc4){if(_0x2cfdc4){return _0x2cfdc4[_0xb934('0x1c')]()[_0xb934('0x1d')](function(){_0xf0c0d[_0xb934('0x13')](0xcc)[_0xb934('0x14')]();});}};}function handleEntityNotFound(_0x2db191){return function(_0x46ff39){if(!_0x46ff39){_0x2db191[_0xb934('0x12')](0x194);}return _0x46ff39;};}function handleError(_0x8b21f6,_0x269644){_0x269644=_0x269644||0x1f4;return function(_0x36416c){logger[_0xb934('0x1e')](_0x36416c[_0xb934('0x1f')]);if(_0x36416c[_0xb934('0x20')]){delete _0x36416c['name'];}_0x8b21f6[_0xb934('0x13')](_0x269644)[_0xb934('0x21')](_0x36416c);};}exports[_0xb934('0x22')]=function(_0x3a8ed3,_0x103329){var _0x1c8078={};var _0x4517e0=_['map'](db['WhatsappInteraction']['rawAttributes'],function(_0x214c23){return{'name':_0x214c23['fieldName'],'type':_0x214c23[_0xb934('0x23')][_0xb934('0x24')]};});_0x1c8078=qs[_0xb934('0x25')](_0x4517e0,_0x3a8ed3);return interaction[_0xb934('0x26')]('whatsapp',_0x3a8ed3[_0xb934('0x27')],_0x3a8ed3[_0xb934('0x28')],_0x1c8078,_0x4517e0)[_0xb934('0x1d')](respondWithFilteredResult(_0x103329,_0x1c8078))[_0xb934('0x29')](handleError(_0x103329,null));};exports[_0xb934('0x2a')]=function(_0xf30b02,_0x402e3f){var _0x50e0f5={'raw':![],'where':{'id':_0xf30b02['params']['id']}},_0x26c32b={};_0x26c32b[_0xb934('0x2b')]=_['keys'](db[_0xb934('0x2c')][_0xb934('0x2d')]);_0x26c32b['query']=_[_0xb934('0x2e')](_0xf30b02[_0xb934('0x27')]);_0x26c32b[_0xb934('0x2f')]=_['intersection'](_0x26c32b[_0xb934('0x2b')],_0x26c32b[_0xb934('0x27')]);_0x50e0f5[_0xb934('0x30')]=_[_0xb934('0x31')](_0x26c32b[_0xb934('0x2b')],qs[_0xb934('0x32')](_0xf30b02[_0xb934('0x27')][_0xb934('0x32')]));_0x50e0f5[_0xb934('0x30')]=_0x50e0f5[_0xb934('0x30')][_0xb934('0x33')]?_0x50e0f5[_0xb934('0x30')]:_0x26c32b[_0xb934('0x2b')];if(_0xf30b02[_0xb934('0x27')]['includeAll']){_0x50e0f5[_0xb934('0x34')]=[{'all':!![]}];}_0x50e0f5=_[_0xb934('0x35')]({},_0x50e0f5,_0xf30b02[_0xb934('0x36')]);return db['WhatsappInteraction'][_0xb934('0x37')](_0x50e0f5)[_0xb934('0x1d')](handleEntityNotFound(_0x402e3f,null))[_0xb934('0x1d')](respondWithResult(_0x402e3f,null))[_0xb934('0x29')](handleError(_0x402e3f,null));};exports[_0xb934('0x38')]=function(_0x26a08e,_0x43f6a5){return db[_0xb934('0x2c')]['create'](_0x26a08e[_0xb934('0x39')],{})[_0xb934('0x1d')](respondWithResult(_0x43f6a5,0xc9))[_0xb934('0x29')](handleError(_0x43f6a5,null));};exports[_0xb934('0x3a')]=function(_0x76fbf5,_0x1db6d2){if(_0x76fbf5[_0xb934('0x39')]['id']){delete _0x76fbf5['body']['id'];}return db[_0xb934('0x2c')]['find']({'where':{'id':_0x76fbf5[_0xb934('0x3b')]['id']}})[_0xb934('0x1d')](handleEntityNotFound(_0x1db6d2,null))[_0xb934('0x1d')](saveUpdates(_0x76fbf5[_0xb934('0x39')],null))[_0xb934('0x1d')](respondWithResult(_0x1db6d2,null))[_0xb934('0x29')](handleError(_0x1db6d2,null));};exports[_0xb934('0x1c')]=function(_0x41fb3e,_0x3203aa){return db[_0xb934('0x2c')]['find']({'where':{'id':_0x41fb3e[_0xb934('0x3b')]['id']}})[_0xb934('0x1d')](handleEntityNotFound(_0x3203aa,null))[_0xb934('0x1d')](removeEntity(_0x3203aa,null))[_0xb934('0x29')](handleError(_0x3203aa,null));};exports[_0xb934('0x3c')]=function(_0xcffc90,_0x2d3265){return db['WhatsappInteraction']['describe']()[_0xb934('0x1d')](respondWithResult(_0x2d3265,null))[_0xb934('0x29')](handleError(_0x2d3265,null));};exports[_0xb934('0x3d')]=function(_0x28b3af,_0x4f9054){return db[_0xb934('0x3e')]['find']({'where':{'id':_0x28b3af[_0xb934('0x3b')]['id']}})[_0xb934('0x1d')](handleEntityNotFound(_0x4f9054,null))[_0xb934('0x1d')](function(_0x2cb12d){if(_0x2cb12d){return _0x2cb12d['addMessage'](_0x28b3af[_0xb934('0x39')]['ids'],_[_0xb934('0x3f')](_0x28b3af[_0xb934('0x39')],[_0xb934('0x40'),'id'])||{});}})[_0xb934('0x1d')](respondWithResult(_0x4f9054,null))[_0xb934('0x29')](handleError(_0x4f9054,null));};exports[_0xb934('0x41')]=function(_0x567ed8,_0x4119a6){var _0xb65684={'raw':![],'where':{}};var _0x2d8070={};var _0x5f41ee={'count':0x0,'rows':[]};return db[_0xb934('0x2c')][_0xb934('0x42')]({'where':{'id':_0x567ed8[_0xb934('0x3b')]['id']}})[_0xb934('0x1d')](handleEntityNotFound(_0x4119a6,null))[_0xb934('0x1d')](function(_0x470a2e){if(_0x470a2e){_0x2d8070[_0xb934('0x2b')]=_[_0xb934('0x2e')](db[_0xb934('0x3e')]['rawAttributes']);_0x2d8070[_0xb934('0x27')]=_[_0xb934('0x2e')](_0x567ed8[_0xb934('0x27')]);_0x2d8070[_0xb934('0x2f')]=_[_0xb934('0x31')](_0x2d8070['model'],_0x2d8070[_0xb934('0x27')]);_0xb65684['attributes']=_[_0xb934('0x31')](_0x2d8070['model'],qs[_0xb934('0x32')](_0x567ed8['query']['fields']));_0xb65684[_0xb934('0x30')]=_0xb65684[_0xb934('0x30')][_0xb934('0x33')]?_0xb65684[_0xb934('0x30')]:_0x2d8070[_0xb934('0x2b')];if(!_0x567ed8[_0xb934('0x27')]['hasOwnProperty']('nolimit')){_0xb65684[_0xb934('0x18')]=qs[_0xb934('0x18')](_0x567ed8['query'][_0xb934('0x18')]);_0xb65684['offset']=qs[_0xb934('0x16')](_0x567ed8[_0xb934('0x27')]['offset']);}_0xb65684['order']=qs[_0xb934('0x43')](_0x567ed8[_0xb934('0x27')][_0xb934('0x43')]);_0xb65684[_0xb934('0x44')]=qs[_0xb934('0x2f')](_[_0xb934('0x45')](_0x567ed8[_0xb934('0x27')],_0x2d8070[_0xb934('0x2f')]));_0xb65684[_0xb934('0x44')]['WhatsappInteractionId']=_0x470a2e['id'];if(_0x567ed8[_0xb934('0x27')]['filter']){_0xb65684[_0xb934('0x44')]=_[_0xb934('0x35')](_0xb65684[_0xb934('0x44')],{'$or':_['map'](_0xb65684[_0xb934('0x30')],function(_0x48ae08){var _0xcfef17={};_0xcfef17[_0x48ae08]={'$like':'%'+_0x567ed8[_0xb934('0x27')]['filter']+'%'};return _0xcfef17;})});}if(_0x567ed8['query'][_0xb934('0x46')]){var _0x509247=_0x567ed8[_0xb934('0x27')][_0xb934('0x46')]['split'](',');var _0x5ae47c={};_0x5ae47c[_0x509247[0x0]]={'$gte':moment(_0x509247[0x1])[_0xb934('0x47')](_0xb934('0x48'))};_0xb65684[_0xb934('0x44')]=_[_0xb934('0x35')](_0xb65684[_0xb934('0x44')],_0x5ae47c);}_0xb65684=_[_0xb934('0x35')]({},_0xb65684,_0x567ed8['options']);return db['WhatsappMessage']['count']({'where':_0xb65684[_0xb934('0x44')]})[_0xb934('0x1d')](function(_0x18c4d0){_0x5f41ee['count']=_0x18c4d0;if(_0x567ed8[_0xb934('0x27')][_0xb934('0x49')]){_0xb65684[_0xb934('0x34')]=[{'all':!![]}];}return db[_0xb934('0x3e')][_0xb934('0x26')](_0xb65684);})[_0xb934('0x1d')](function(_0x2ccd57){_0x5f41ee[_0xb934('0x4a')]=_0x2ccd57;return _0x5f41ee;});}})['then'](respondWithFilteredResult(_0x4119a6,_0xb65684))['catch'](handleError(_0x4119a6,null));};exports[_0xb934('0x4b')]=function(_0x571297,_0x24f179){return db[_0xb934('0x2c')]['find']({'where':{'id':_0x571297[_0xb934('0x3b')]['id']}})[_0xb934('0x1d')](handleEntityNotFound(_0x24f179,null))[_0xb934('0x1d')](function(_0x55a508){if(_0x55a508){return _0x55a508[_0xb934('0x4c')](_0x571297['body'][_0xb934('0x40')],_[_0xb934('0x3f')](_0x571297[_0xb934('0x39')],[_0xb934('0x40'),'id'])||{})[_0xb934('0x4d')](function(){return db[_0xb934('0x4e')][_0xb934('0x26')]({'attributes':['id',_0xb934('0x20'),_0xb934('0x4f')],'where':{'id':_0x571297[_0xb934('0x39')][_0xb934('0x40')]}});})[_0xb934('0x1d')](function(_0x4976c2){socket[_0xb934('0x50')](_0xb934('0x51'),{'id':Number(_0x571297['params']['id']),'tags':_0x4976c2||[]});return{'id':Number(_0x571297[_0xb934('0x3b')]['id']),'tags':_0x4976c2||[]};});}})[_0xb934('0x1d')](respondWithResult(_0x24f179,null))[_0xb934('0x29')](handleError(_0x24f179,null));};exports['removeTags']=function(_0x9cee34,_0x2584fd){return db['WhatsappInteraction'][_0xb934('0x37')]({'where':{'id':_0x9cee34['params']['id']}})[_0xb934('0x1d')](handleEntityNotFound(_0x2584fd,null))[_0xb934('0x1d')](function(_0x170f87){if(_0x170f87){return _0x170f87[_0xb934('0x52')](_0x9cee34['query'][_0xb934('0x40')]);}})['then'](respondWithStatusCode(_0x2584fd,null))['catch'](handleError(_0x2584fd,null));};exports[_0xb934('0x53')]=function(_0x25064e,_0x1feef5){var _0x3ed5e2=moment()[_0xb934('0x54')]()[_0xb934('0x55')]();var _0x17a0ad=path[_0xb934('0x56')](config[_0xb934('0x57')],_0xb934('0x58'),_0xb934('0x59'),_0xb934('0x5a'));var _0x281a14=path['join'](config[_0xb934('0x57')],_0xb934('0x58'),_0xb934('0x59'),_0xb934('0x5b'));var _0xb0eb06=path[_0xb934('0x56')](_0x17a0ad,_0x3ed5e2);var _0x154c39=util[_0xb934('0x47')](_0xb934('0x5c'),_0x25064e[_0xb934('0x3b')]['id'],_0x3ed5e2);var _0x3c11fd=path['join'](_0x17a0ad,_0x154c39);var _0x2467bc=[];_0x2467bc[_0xb934('0x5d')]({'model':db[_0xb934('0x5e')],'as':'User','attributes':['id','fullname'],'raw':!![]});_0x2467bc[_0xb934('0x5d')]({'model':db['CmContact'],'as':_0xb934('0x5f'),'attributes':['id',_0xb934('0x60'),_0xb934('0x61')],'raw':!![]});if(_0x25064e[_0xb934('0x27')][_0xb934('0x5b')]){_0x2467bc[_0xb934('0x5d')]({'model':db[_0xb934('0x62')],'as':_0xb934('0x62'),'raw':!![]});}var _0x3efa21=[{'model':db['WhatsappMessage'],'as':_0xb934('0x63'),'attributes':['id',_0xb934('0x39'),_0xb934('0x64'),_0xb934('0x65'),'secret',_0xb934('0x66')],'include':_0x2467bc}];_0x3efa21[_0xb934('0x5d')]({'model':db[_0xb934('0x67')],'as':_0xb934('0x68'),'attributes':['id',_0xb934('0x20')]});_0x3efa21[_0xb934('0x5d')]({'model':db[_0xb934('0x5e')],'as':'Owner','attributes':['id','fullname']});return db[_0xb934('0x2c')][_0xb934('0x37')]({'where':{'id':_0x25064e[_0xb934('0x3b')]['id']},'include':_0x3efa21})['then'](handleEntityNotFound(_0x1feef5,null))[_0xb934('0x1d')](function(_0x346aeb){if(_0x346aeb){var _0x3c7eba=_0x346aeb[_0xb934('0x69')]({'plain':!![]});fs[_0xb934('0x6a')](_0xb0eb06);var _0x18ad00={'channel':'Whatsapp','account':_0x3c7eba[_0xb934('0x68')]['name'],'agent':_0x3c7eba[_0xb934('0x6b')]?_0x3c7eba['Owner'][_0xb934('0x6c')]:'N.A.','createdAt':moment(_0x3c7eba['createdAt'])[_0xb934('0x47')](_0xb934('0x48'))[_0xb934('0x55')](),'closedAt':_0x3c7eba[_0xb934('0x6d')]?moment(_0x3c7eba[_0xb934('0x6e')])[_0xb934('0x47')](_0xb934('0x48'))[_0xb934('0x55')]():'','disposition':_0x3c7eba['disposition'],'messages':_(_0x3c7eba[_0xb934('0x63')])['filter']({'secret':![]})['map'](function(_0x682c88){if(_0x682c88[_0xb934('0x62')]){var _0x2f99ce=path[_0xb934('0x56')](_0x281a14,_0x682c88[_0xb934('0x62')][_0xb934('0x6f')]);if(fs[_0xb934('0x70')](_0x2f99ce)){fs[_0xb934('0x71')](_0x2f99ce)[_0xb934('0x72')](fs[_0xb934('0x73')](path[_0xb934('0x56')](_0xb0eb06,_0x682c88[_0xb934('0x62')][_0xb934('0x6f')])));}}return{'date':moment(_0x682c88[_0xb934('0x64')])[_0xb934('0x47')](_0xb934('0x48'))['toString'](),'sender':_0x682c88[_0xb934('0x65')]===_0xb934('0x74')?_0x682c88['User']?_0x682c88[_0xb934('0x5e')][_0xb934('0x6c')]:_0xb934('0x75'):_0x682c88['Contact'][_0xb934('0x60')]+(_0x682c88['Contact']['lastName']?'\x20'+_0x682c88[_0xb934('0x5f')]['lastName']:''),'body':_0x682c88[_0xb934('0x62')]?_0x682c88[_0xb934('0x62')][_0xb934('0x6f')]:_0x682c88[_0xb934('0x39')],'direction':_0x682c88[_0xb934('0x65')]===_0xb934('0x74')?'A':'C','secret':_0x682c88[_0xb934('0x76')],'read':_0x682c88[_0xb934('0x66')],'attachment':_0x682c88[_0xb934('0x62')]};})[_0xb934('0x77')]()};return ejs[_0xb934('0x78')](path[_0xb934('0x56')](config[_0xb934('0x57')],_0xb934('0x58'),_0xb934('0x79'),_0xb934('0x7a')),{'interaction':_0x18ad00})[_0xb934('0x1d')](function(_0x251d4e){var _0x355610=path[_0xb934('0x56')](_0xb0eb06,'transcript-'+_0x3c7eba['id']+'-'+_0x3ed5e2+'.pdf');var _0x195fa5={'path':_0x355610,'channel':_0xb934('0x7b'),'interactionId':_0x3c7eba['id']};return pdf[_0xb934('0x7c')](_0x251d4e,_0x195fa5);})[_0xb934('0x1d')](function(){return new BPromise(function(_0x2a622d,_0x519545){zipdir(_0xb0eb06,{'saveTo':_0x3c11fd},function(_0x40cede,_0x622182){if(_0x40cede)return _0x519545(_0x40cede);return _0x2a622d(_0x622182);});})[_0xb934('0x1d')](function(){return new BPromise(function(_0x382072,_0x36b155){rimraf(_0xb0eb06,function(_0x50bb81){if(_0x50bb81)_0x36b155(_0x50bb81);return _0x382072();});});})[_0xb934('0x1d')](function(){return _0x1feef5[_0xb934('0x53')](_0x3c11fd,_0x154c39,function(_0x337fe4){if(_0x337fe4){console[_0xb934('0x7d')](_0xb934('0x7e'),_0x337fe4);}else{fs['unlinkSync'](_0x3c11fd);}});});});}else{return _0x1feef5[_0xb934('0x12')](0xc8);}})[_0xb934('0x29')](handleError(_0x1feef5,null));};
\ No newline at end of file
+var _0xef75=['catch','describe','addMessage','WhatsappMessage','ids','findOne','length','hasOwnProperty','nolimit','order','sort','where','pick','WhatsappInteractionId','merge','map','$gte','split','format','findAll','rows','spread','Tag','whatsappInteractionTags:save','removeTags','download','toString','root','server','files','tmp','join','push','User','fullname','CmContact','Contact','firstName','lastName','createdAt','direction','secret','read','WhatsappAccount','Account','Owner','mkdirSync','Whatsapp','N.A.','YYYY-MM-DD\x20HH:mm:ss','closed','disposition','Messages','filter','Attachment','basename','existsSync','pipe','createWriteStream','out','System','value','renderFile','views','downloadInteraction.ejs','.pdf','createPdfFromHTML','log','err','unlinkSync','rimraf','zip-dir','bluebird','ejs','lodash','ioredis','../../components/interaction/service','../../components/parsers/qs','../../components/export/pdf','../../config/logger','api','redis','defaults','localhost','socket.io-emitter','./whatsappInteraction.socket','register','status','end','json','offset','undefined','limit','count','Content-Range','update','then','destroy','sendStatus','stack','name','send','index','type','key','getOptions','whatsapp','query','user','show','params','model','keys','WhatsappInteraction','rawAttributes','filters','attributes','intersection','fields','includeAll','options','find','create','body'];(function(_0x18235d,_0x378e2e){var _0xa3ea08=function(_0x29170c){while(--_0x29170c){_0x18235d['push'](_0x18235d['shift']());}};_0xa3ea08(++_0x378e2e);}(_0xef75,0x13e));var _0x5ef7=function(_0x5d6772,_0x42674a){_0x5d6772=_0x5d6772-0x0;var _0x495b87=_0xef75[_0x5d6772];return _0x495b87;};'use strict';var rimraf=require(_0x5ef7('0x0'));var zipdir=require(_0x5ef7('0x1'));var moment=require('moment');var BPromise=require(_0x5ef7('0x2'));var util=require('util');var path=require('path');var ejs=require(_0x5ef7('0x3'));var fs=require('fs');var _=require(_0x5ef7('0x4'));var Redis=require(_0x5ef7('0x5'));var interaction=require(_0x5ef7('0x6'));var qs=require(_0x5ef7('0x7'));var pdf=require(_0x5ef7('0x8'));var logger=require(_0x5ef7('0x9'))(_0x5ef7('0xa'));var config=require('../../config/environment');var db=require('../../mysqldb')['db'];config[_0x5ef7('0xb')]=_[_0x5ef7('0xc')](config[_0x5ef7('0xb')],{'host':_0x5ef7('0xd'),'port':0x18eb});var socket=require(_0x5ef7('0xe'))(new Redis(config[_0x5ef7('0xb')]));require(_0x5ef7('0xf'))[_0x5ef7('0x10')](socket);function respondWithStatusCode(_0x3d5b2b,_0x4fae86){_0x4fae86=_0x4fae86||0xcc;return function(_0x4ff186){if(_0x4ff186){return _0x3d5b2b['sendStatus'](_0x4fae86);}return _0x3d5b2b[_0x5ef7('0x11')](_0x4fae86)[_0x5ef7('0x12')]();};}function respondWithResult(_0x21ed14,_0x33f100){_0x33f100=_0x33f100||0xc8;return function(_0x3e583a){if(_0x3e583a){return _0x21ed14[_0x5ef7('0x11')](_0x33f100)[_0x5ef7('0x13')](_0x3e583a);}};}function respondWithFilteredResult(_0x12e3cb,_0x3ac47f){return function(_0x254c45){if(_0x254c45){var _0x4ad4e2=typeof _0x3ac47f[_0x5ef7('0x14')]===_0x5ef7('0x15')&&typeof _0x3ac47f[_0x5ef7('0x16')]===_0x5ef7('0x15');var _0x377e1b=_0x254c45[_0x5ef7('0x17')];var _0x591bbf=_0x4ad4e2?0x0:_0x3ac47f[_0x5ef7('0x14')];var _0x21ee80=_0x4ad4e2?_0x254c45[_0x5ef7('0x17')]:_0x3ac47f['offset']+_0x3ac47f['limit'];var _0xf87929;if(_0x21ee80>=_0x377e1b){_0x21ee80=_0x377e1b;_0xf87929=0xc8;}else{_0xf87929=0xce;}_0x12e3cb[_0x5ef7('0x11')](_0xf87929);return _0x12e3cb['set'](_0x5ef7('0x18'),_0x591bbf+'-'+_0x21ee80+'/'+_0x377e1b)[_0x5ef7('0x13')](_0x254c45);}return null;};}function saveUpdates(_0x841e3a){return function(_0x2e2bdc){if(_0x2e2bdc){return _0x2e2bdc[_0x5ef7('0x19')](_0x841e3a)[_0x5ef7('0x1a')](function(_0x475771){return _0x475771;});}return null;};}function removeEntity(_0x2eeb82){return function(_0x2fad17){if(_0x2fad17){return _0x2fad17[_0x5ef7('0x1b')]()['then'](function(){_0x2eeb82[_0x5ef7('0x11')](0xcc)['end']();});}};}function handleEntityNotFound(_0x4354e8){return function(_0xdc57e3){if(!_0xdc57e3){_0x4354e8[_0x5ef7('0x1c')](0x194);}return _0xdc57e3;};}function handleError(_0x2fa958,_0x34873f){_0x34873f=_0x34873f||0x1f4;return function(_0x1678c1){logger['error'](_0x1678c1[_0x5ef7('0x1d')]);if(_0x1678c1[_0x5ef7('0x1e')]){delete _0x1678c1[_0x5ef7('0x1e')];}_0x2fa958[_0x5ef7('0x11')](_0x34873f)[_0x5ef7('0x1f')](_0x1678c1);};}exports[_0x5ef7('0x20')]=function(_0x547023,_0x272b2c){var _0x5c7539={};var _0x308692=_['map'](db['WhatsappInteraction']['rawAttributes'],function(_0x186e0a){return{'name':_0x186e0a['fieldName'],'type':_0x186e0a[_0x5ef7('0x21')][_0x5ef7('0x22')]};});_0x5c7539=qs[_0x5ef7('0x23')](_0x308692,_0x547023);return interaction['findAll'](_0x5ef7('0x24'),_0x547023[_0x5ef7('0x25')],_0x547023[_0x5ef7('0x26')],_0x5c7539,_0x308692)['then'](respondWithFilteredResult(_0x272b2c,_0x5c7539))['catch'](handleError(_0x272b2c,null));};exports[_0x5ef7('0x27')]=function(_0x50cec8,_0xd90890){var _0x3482bf={'raw':![],'where':{'id':_0x50cec8[_0x5ef7('0x28')]['id']}},_0x1900a0={};_0x1900a0[_0x5ef7('0x29')]=_[_0x5ef7('0x2a')](db[_0x5ef7('0x2b')][_0x5ef7('0x2c')]);_0x1900a0[_0x5ef7('0x25')]=_[_0x5ef7('0x2a')](_0x50cec8[_0x5ef7('0x25')]);_0x1900a0[_0x5ef7('0x2d')]=_['intersection'](_0x1900a0[_0x5ef7('0x29')],_0x1900a0[_0x5ef7('0x25')]);_0x3482bf[_0x5ef7('0x2e')]=_[_0x5ef7('0x2f')](_0x1900a0[_0x5ef7('0x29')],qs[_0x5ef7('0x30')](_0x50cec8['query'][_0x5ef7('0x30')]));_0x3482bf['attributes']=_0x3482bf[_0x5ef7('0x2e')]['length']?_0x3482bf[_0x5ef7('0x2e')]:_0x1900a0[_0x5ef7('0x29')];if(_0x50cec8['query'][_0x5ef7('0x31')]){_0x3482bf['include']=[{'all':!![]}];}_0x3482bf=_['merge']({},_0x3482bf,_0x50cec8[_0x5ef7('0x32')]);return db[_0x5ef7('0x2b')][_0x5ef7('0x33')](_0x3482bf)[_0x5ef7('0x1a')](handleEntityNotFound(_0xd90890,null))[_0x5ef7('0x1a')](respondWithResult(_0xd90890,null))['catch'](handleError(_0xd90890,null));};exports['create']=function(_0x5edb10,_0x22da0d){return db['WhatsappInteraction'][_0x5ef7('0x34')](_0x5edb10[_0x5ef7('0x35')],{})[_0x5ef7('0x1a')](respondWithResult(_0x22da0d,0xc9))[_0x5ef7('0x36')](handleError(_0x22da0d,null));};exports['update']=function(_0x3b8aea,_0x1e0c54){if(_0x3b8aea[_0x5ef7('0x35')]['id']){delete _0x3b8aea['body']['id'];}return db[_0x5ef7('0x2b')]['find']({'where':{'id':_0x3b8aea[_0x5ef7('0x28')]['id']}})['then'](handleEntityNotFound(_0x1e0c54,null))[_0x5ef7('0x1a')](saveUpdates(_0x3b8aea['body'],null))[_0x5ef7('0x1a')](respondWithResult(_0x1e0c54,null))[_0x5ef7('0x36')](handleError(_0x1e0c54,null));};exports['destroy']=function(_0x248358,_0x543b85){return db[_0x5ef7('0x2b')]['find']({'where':{'id':_0x248358[_0x5ef7('0x28')]['id']}})[_0x5ef7('0x1a')](handleEntityNotFound(_0x543b85,null))[_0x5ef7('0x1a')](removeEntity(_0x543b85,null))[_0x5ef7('0x36')](handleError(_0x543b85,null));};exports['describe']=function(_0xd15fa9,_0x2e5660){return db[_0x5ef7('0x2b')][_0x5ef7('0x37')]()['then'](respondWithResult(_0x2e5660,null))[_0x5ef7('0x36')](handleError(_0x2e5660,null));};exports[_0x5ef7('0x38')]=function(_0x1237c4,_0x1d69f6){return db[_0x5ef7('0x39')][_0x5ef7('0x33')]({'where':{'id':_0x1237c4[_0x5ef7('0x28')]['id']}})['then'](handleEntityNotFound(_0x1d69f6,null))['then'](function(_0x307cd7){if(_0x307cd7){return _0x307cd7[_0x5ef7('0x38')](_0x1237c4['body'][_0x5ef7('0x3a')],_['omit'](_0x1237c4[_0x5ef7('0x35')],[_0x5ef7('0x3a'),'id'])||{});}})['then'](respondWithResult(_0x1d69f6,null))[_0x5ef7('0x36')](handleError(_0x1d69f6,null));};exports['getMessages']=function(_0x558496,_0x5c84ad){var _0x1cc88b={'raw':![],'where':{}};var _0xa1f331={};var _0x3e2347={'count':0x0,'rows':[]};return db[_0x5ef7('0x2b')][_0x5ef7('0x3b')]({'where':{'id':_0x558496[_0x5ef7('0x28')]['id']}})[_0x5ef7('0x1a')](handleEntityNotFound(_0x5c84ad,null))[_0x5ef7('0x1a')](function(_0x4d7f4d){if(_0x4d7f4d){_0xa1f331[_0x5ef7('0x29')]=_['keys'](db['WhatsappMessage']['rawAttributes']);_0xa1f331[_0x5ef7('0x25')]=_[_0x5ef7('0x2a')](_0x558496['query']);_0xa1f331[_0x5ef7('0x2d')]=_[_0x5ef7('0x2f')](_0xa1f331[_0x5ef7('0x29')],_0xa1f331[_0x5ef7('0x25')]);_0x1cc88b[_0x5ef7('0x2e')]=_[_0x5ef7('0x2f')](_0xa1f331[_0x5ef7('0x29')],qs[_0x5ef7('0x30')](_0x558496[_0x5ef7('0x25')][_0x5ef7('0x30')]));_0x1cc88b[_0x5ef7('0x2e')]=_0x1cc88b[_0x5ef7('0x2e')][_0x5ef7('0x3c')]?_0x1cc88b[_0x5ef7('0x2e')]:_0xa1f331[_0x5ef7('0x29')];if(!_0x558496[_0x5ef7('0x25')][_0x5ef7('0x3d')](_0x5ef7('0x3e'))){_0x1cc88b['limit']=qs[_0x5ef7('0x16')](_0x558496['query'][_0x5ef7('0x16')]);_0x1cc88b[_0x5ef7('0x14')]=qs[_0x5ef7('0x14')](_0x558496[_0x5ef7('0x25')][_0x5ef7('0x14')]);}_0x1cc88b[_0x5ef7('0x3f')]=qs['sort'](_0x558496['query'][_0x5ef7('0x40')]);_0x1cc88b[_0x5ef7('0x41')]=qs[_0x5ef7('0x2d')](_[_0x5ef7('0x42')](_0x558496['query'],_0xa1f331['filters']));_0x1cc88b['where'][_0x5ef7('0x43')]=_0x4d7f4d['id'];if(_0x558496[_0x5ef7('0x25')]['filter']){_0x1cc88b[_0x5ef7('0x41')]=_[_0x5ef7('0x44')](_0x1cc88b['where'],{'$or':_[_0x5ef7('0x45')](_0x1cc88b[_0x5ef7('0x2e')],function(_0x5655a4){var _0x47a50f={};_0x47a50f[_0x5655a4]={'$like':'%'+_0x558496[_0x5ef7('0x25')]['filter']+'%'};return _0x47a50f;})});}if(_0x558496[_0x5ef7('0x25')][_0x5ef7('0x46')]){var _0x6e7ee2=_0x558496[_0x5ef7('0x25')][_0x5ef7('0x46')][_0x5ef7('0x47')](',');var _0x36d56a={};_0x36d56a[_0x6e7ee2[0x0]]={'$gte':moment(_0x6e7ee2[0x1])[_0x5ef7('0x48')]('YYYY-MM-DD\x20HH:mm:ss')};_0x1cc88b[_0x5ef7('0x41')]=_[_0x5ef7('0x44')](_0x1cc88b['where'],_0x36d56a);}_0x1cc88b=_[_0x5ef7('0x44')]({},_0x1cc88b,_0x558496[_0x5ef7('0x32')]);return db[_0x5ef7('0x39')]['count']({'where':_0x1cc88b[_0x5ef7('0x41')]})[_0x5ef7('0x1a')](function(_0x1c0a7e){_0x3e2347[_0x5ef7('0x17')]=_0x1c0a7e;if(_0x558496[_0x5ef7('0x25')][_0x5ef7('0x31')]){_0x1cc88b['include']=[{'all':!![]}];}return db[_0x5ef7('0x39')][_0x5ef7('0x49')](_0x1cc88b);})[_0x5ef7('0x1a')](function(_0x16b89d){_0x3e2347[_0x5ef7('0x4a')]=_0x16b89d;return _0x3e2347;});}})['then'](respondWithFilteredResult(_0x5c84ad,_0x1cc88b))['catch'](handleError(_0x5c84ad,null));};exports['addTags']=function(_0x105f49,_0x3ac1d2){return db['WhatsappInteraction'][_0x5ef7('0x33')]({'where':{'id':_0x105f49[_0x5ef7('0x28')]['id']}})[_0x5ef7('0x1a')](handleEntityNotFound(_0x3ac1d2,null))[_0x5ef7('0x1a')](function(_0x14167a){if(_0x14167a){return _0x14167a['setTags'](_0x105f49['body']['ids'],_['omit'](_0x105f49[_0x5ef7('0x35')],[_0x5ef7('0x3a'),'id'])||{})[_0x5ef7('0x4b')](function(){return db[_0x5ef7('0x4c')][_0x5ef7('0x49')]({'attributes':['id','name','color'],'where':{'id':_0x105f49['body'][_0x5ef7('0x3a')]}});})['then'](function(_0x1a646e){socket['emit'](_0x5ef7('0x4d'),{'id':Number(_0x105f49[_0x5ef7('0x28')]['id']),'tags':_0x1a646e||[]});return{'id':Number(_0x105f49[_0x5ef7('0x28')]['id']),'tags':_0x1a646e||[]};});}})[_0x5ef7('0x1a')](respondWithResult(_0x3ac1d2,null))[_0x5ef7('0x36')](handleError(_0x3ac1d2,null));};exports[_0x5ef7('0x4e')]=function(_0x395893,_0x15dae4){return db[_0x5ef7('0x2b')][_0x5ef7('0x33')]({'where':{'id':_0x395893[_0x5ef7('0x28')]['id']}})[_0x5ef7('0x1a')](handleEntityNotFound(_0x15dae4,null))['then'](function(_0x447747){if(_0x447747){return _0x447747[_0x5ef7('0x4e')](_0x395893[_0x5ef7('0x25')][_0x5ef7('0x3a')]);}})[_0x5ef7('0x1a')](respondWithStatusCode(_0x15dae4,null))['catch'](handleError(_0x15dae4,null));};exports[_0x5ef7('0x4f')]=function(_0x1d7301,_0x515048){var _0x45b8d3=moment()['unix']()[_0x5ef7('0x50')]();var _0x2df73c=path['join'](config[_0x5ef7('0x51')],_0x5ef7('0x52'),_0x5ef7('0x53'),_0x5ef7('0x54'));var _0x112689=path[_0x5ef7('0x55')](config['root'],_0x5ef7('0x52'),_0x5ef7('0x53'),'attachments');var _0x57ccba=path[_0x5ef7('0x55')](_0x2df73c,_0x45b8d3);var _0x30cf1e=util[_0x5ef7('0x48')]('transcript-%d-%s.zip',_0x1d7301[_0x5ef7('0x28')]['id'],_0x45b8d3);var _0x2381ef=path['join'](_0x2df73c,_0x30cf1e);var _0x1a9fca=[];_0x1a9fca[_0x5ef7('0x56')]({'model':db[_0x5ef7('0x57')],'as':_0x5ef7('0x57'),'attributes':['id',_0x5ef7('0x58')],'raw':!![]});_0x1a9fca[_0x5ef7('0x56')]({'model':db[_0x5ef7('0x59')],'as':_0x5ef7('0x5a'),'attributes':['id',_0x5ef7('0x5b'),_0x5ef7('0x5c')],'raw':!![]});if(_0x1d7301[_0x5ef7('0x25')]['attachments']){_0x1a9fca[_0x5ef7('0x56')]({'model':db['Attachment'],'as':'Attachment','raw':!![]});}var _0xe0eb88=[{'model':db[_0x5ef7('0x39')],'as':'Messages','attributes':['id',_0x5ef7('0x35'),_0x5ef7('0x5d'),_0x5ef7('0x5e'),_0x5ef7('0x5f'),_0x5ef7('0x60')],'include':_0x1a9fca}];_0xe0eb88[_0x5ef7('0x56')]({'model':db[_0x5ef7('0x61')],'as':_0x5ef7('0x62'),'attributes':['id','name']});_0xe0eb88['push']({'model':db['User'],'as':_0x5ef7('0x63'),'attributes':['id',_0x5ef7('0x58')]});return db[_0x5ef7('0x2b')]['find']({'where':{'id':_0x1d7301['params']['id']},'include':_0xe0eb88})['then'](handleEntityNotFound(_0x515048,null))[_0x5ef7('0x1a')](function(_0x41fe9c){if(_0x41fe9c){var _0x4e3a19=_0x41fe9c['get']({'plain':!![]});fs[_0x5ef7('0x64')](_0x57ccba);var _0x3b169e={'channel':_0x5ef7('0x65'),'account':_0x4e3a19[_0x5ef7('0x62')][_0x5ef7('0x1e')],'agent':_0x4e3a19['Owner']?_0x4e3a19[_0x5ef7('0x63')][_0x5ef7('0x58')]:_0x5ef7('0x66'),'createdAt':moment(_0x4e3a19[_0x5ef7('0x5d')])[_0x5ef7('0x48')](_0x5ef7('0x67'))[_0x5ef7('0x50')](),'closedAt':_0x4e3a19[_0x5ef7('0x68')]?moment(_0x4e3a19['closedAt'])[_0x5ef7('0x48')](_0x5ef7('0x67'))[_0x5ef7('0x50')]():'','disposition':_0x4e3a19[_0x5ef7('0x69')],'messages':_(_0x4e3a19[_0x5ef7('0x6a')])[_0x5ef7('0x6b')]({'secret':![]})[_0x5ef7('0x45')](function(_0x69d307){if(_0x69d307[_0x5ef7('0x6c')]){var _0x3ac1ec=path[_0x5ef7('0x55')](_0x112689,_0x69d307[_0x5ef7('0x6c')][_0x5ef7('0x6d')]);if(fs[_0x5ef7('0x6e')](_0x3ac1ec)){fs['createReadStream'](_0x3ac1ec)[_0x5ef7('0x6f')](fs[_0x5ef7('0x70')](path[_0x5ef7('0x55')](_0x57ccba,_0x69d307[_0x5ef7('0x6c')]['basename'])));}}return{'date':moment(_0x69d307[_0x5ef7('0x5d')])[_0x5ef7('0x48')](_0x5ef7('0x67'))[_0x5ef7('0x50')](),'sender':_0x69d307[_0x5ef7('0x5e')]===_0x5ef7('0x71')?_0x69d307[_0x5ef7('0x57')]?_0x69d307[_0x5ef7('0x57')][_0x5ef7('0x58')]:_0x5ef7('0x72'):_0x69d307['Contact']['firstName']+(_0x69d307[_0x5ef7('0x5a')][_0x5ef7('0x5c')]?'\x20'+_0x69d307[_0x5ef7('0x5a')][_0x5ef7('0x5c')]:''),'body':_0x69d307[_0x5ef7('0x6c')]?_0x69d307[_0x5ef7('0x6c')][_0x5ef7('0x6d')]:_0x69d307['body'],'direction':_0x69d307[_0x5ef7('0x5e')]===_0x5ef7('0x71')?'A':'C','secret':_0x69d307[_0x5ef7('0x5f')],'read':_0x69d307[_0x5ef7('0x60')],'attachment':_0x69d307[_0x5ef7('0x6c')]};})[_0x5ef7('0x73')]()};return ejs[_0x5ef7('0x74')](path[_0x5ef7('0x55')](config[_0x5ef7('0x51')],_0x5ef7('0x52'),_0x5ef7('0x75'),_0x5ef7('0x76')),{'interaction':_0x3b169e})['then'](function(_0x1a979d){var _0x56ff86=path['join'](_0x57ccba,'transcript-'+_0x4e3a19['id']+'-'+_0x45b8d3+_0x5ef7('0x77'));var _0x40d7b7={'path':_0x56ff86,'channel':'Whatsapp','interactionId':_0x4e3a19['id']};return pdf[_0x5ef7('0x78')](_0x1a979d,_0x40d7b7);})[_0x5ef7('0x1a')](function(){return new BPromise(function(_0x26e063,_0x4a06e4){zipdir(_0x57ccba,{'saveTo':_0x2381ef},function(_0x99d2ed,_0xa042b6){if(_0x99d2ed)return _0x4a06e4(_0x99d2ed);return _0x26e063(_0xa042b6);});})[_0x5ef7('0x1a')](function(){return new BPromise(function(_0xba5f65,_0x2bfb3d){rimraf(_0x57ccba,function(_0x41d301){if(_0x41d301)_0x2bfb3d(_0x41d301);return _0xba5f65();});});})[_0x5ef7('0x1a')](function(){return _0x515048['download'](_0x2381ef,_0x30cf1e,function(_0x2b2f72){if(_0x2b2f72){console[_0x5ef7('0x79')](_0x5ef7('0x7a'),_0x2b2f72);}else{fs[_0x5ef7('0x7b')](_0x2381ef);}});});});}else{return _0x515048['sendStatus'](0xc8);}})[_0x5ef7('0x36')](handleError(_0x515048,null));};
\ No newline at end of file