Built motion from commit 10af8726.|2.6.34
[motion2.git] / server / api / whatsappInteraction / whatsappInteraction.controller.js
index 0aca277..36629f4 100644 (file)
@@ -15,4 +15,4 @@
 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                        *
 // *                                                                       *
 // *************************************************************************
-var _0xfe70=['closedAt','disposition','createWriteStream','out','System','basename','value','renderFile','views','downloadInteraction.ejs','.pdf','log','rimraf','zip-dir','bluebird','util','path','ejs','../../components/interaction/service','../../components/parsers/qs','../../components/export/pdf','../../config/logger','api','../../config/environment','../../mysqldb','redis','defaults','localhost','./whatsappInteraction.socket','register','sendStatus','status','end','json','offset','undefined','count','limit','set','Content-Range','update','then','destroy','error','stack','name','send','index','map','rawAttributes','fieldName','key','findAll','query','user','params','WhatsappInteraction','keys','filters','model','attributes','intersection','fields','length','includeAll','include','options','find','catch','create','body','describe','ids','getMessages','findOne','hasOwnProperty','order','sort','where','pick','filter','$gte','format','YYYY-MM-DD\x20HH:mm:ss','merge','WhatsappMessage','addTags','omit','spread','Tag','color','emit','whatsappInteractionTags:save','removeTags','download','unix','join','root','server','files','tmp','attachments','transcript-%d-%s.zip','push','User','CmContact','Contact','firstName','lastName','Attachment','Messages','direction','secret','read','WhatsappAccount','Account','Owner','fullname','mkdirSync','N.A.','createdAt','toString','closed'];(function(_0x169790,_0x48aa33){var _0x4d0f36=function(_0x2a848d){while(--_0x2a848d){_0x169790['push'](_0x169790['shift']());}};_0x4d0f36(++_0x48aa33);}(_0xfe70,0x102));var _0x0fe7=function(_0x393500,_0x1037af){_0x393500=_0x393500-0x0;var _0x120d3b=_0xfe70[_0x393500];return _0x120d3b;};'use strict';var rimraf=require(_0x0fe7('0x0'));var zipdir=require(_0x0fe7('0x1'));var moment=require('moment');var BPromise=require(_0x0fe7('0x2'));var util=require(_0x0fe7('0x3'));var path=require(_0x0fe7('0x4'));var ejs=require(_0x0fe7('0x5'));var fs=require('fs');var _=require('lodash');var Redis=require('ioredis');var interaction=require(_0x0fe7('0x6'));var qs=require(_0x0fe7('0x7'));var pdf=require(_0x0fe7('0x8'));var logger=require(_0x0fe7('0x9'))(_0x0fe7('0xa'));var config=require(_0x0fe7('0xb'));var db=require(_0x0fe7('0xc'))['db'];config[_0x0fe7('0xd')]=_[_0x0fe7('0xe')](config[_0x0fe7('0xd')],{'host':_0x0fe7('0xf'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x0fe7('0xd')]));require(_0x0fe7('0x10'))[_0x0fe7('0x11')](socket);function respondWithStatusCode(_0x3f9c23,_0x6fb19c){_0x6fb19c=_0x6fb19c||0xcc;return function(_0x5db0fc){if(_0x5db0fc){return _0x3f9c23[_0x0fe7('0x12')](_0x6fb19c);}return _0x3f9c23[_0x0fe7('0x13')](_0x6fb19c)[_0x0fe7('0x14')]();};}function respondWithResult(_0x35ddd2,_0xa99399){_0xa99399=_0xa99399||0xc8;return function(_0xe83fd9){if(_0xe83fd9){return _0x35ddd2[_0x0fe7('0x13')](_0xa99399)[_0x0fe7('0x15')](_0xe83fd9);}};}function respondWithFilteredResult(_0x190323,_0x37f371){return function(_0x3fee6d){if(_0x3fee6d){var _0x47c520=typeof _0x37f371[_0x0fe7('0x16')]===_0x0fe7('0x17')&&typeof _0x37f371['limit']===_0x0fe7('0x17');var _0x31abcb=_0x3fee6d[_0x0fe7('0x18')];var _0x48100f=_0x47c520?0x0:_0x37f371[_0x0fe7('0x16')];var _0x4a27ed=_0x47c520?_0x3fee6d[_0x0fe7('0x18')]:_0x37f371[_0x0fe7('0x16')]+_0x37f371[_0x0fe7('0x19')];var _0xf37f37;if(_0x4a27ed>=_0x31abcb){_0x4a27ed=_0x31abcb;_0xf37f37=0xc8;}else{_0xf37f37=0xce;}_0x190323[_0x0fe7('0x13')](_0xf37f37);return _0x190323[_0x0fe7('0x1a')](_0x0fe7('0x1b'),_0x48100f+'-'+_0x4a27ed+'/'+_0x31abcb)[_0x0fe7('0x15')](_0x3fee6d);}return null;};}function saveUpdates(_0x1cf43f){return function(_0x4cb390){if(_0x4cb390){return _0x4cb390[_0x0fe7('0x1c')](_0x1cf43f)[_0x0fe7('0x1d')](function(_0x123875){return _0x123875;});}return null;};}function removeEntity(_0x325777){return function(_0x2efef8){if(_0x2efef8){return _0x2efef8[_0x0fe7('0x1e')]()[_0x0fe7('0x1d')](function(){_0x325777['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x32a479){return function(_0xf60d31){if(!_0xf60d31){_0x32a479[_0x0fe7('0x12')](0x194);}return _0xf60d31;};}function handleError(_0x7cd368,_0x534dd9){_0x534dd9=_0x534dd9||0x1f4;return function(_0x1ff208){logger[_0x0fe7('0x1f')](_0x1ff208[_0x0fe7('0x20')]);if(_0x1ff208['name']){delete _0x1ff208[_0x0fe7('0x21')];}_0x7cd368['status'](_0x534dd9)[_0x0fe7('0x22')](_0x1ff208);};}exports[_0x0fe7('0x23')]=function(_0x51c69b,_0x36f5a6){var _0x39a35b={};var _0x262146=_[_0x0fe7('0x24')](db['WhatsappInteraction'][_0x0fe7('0x25')],function(_0x34a3df){return{'name':_0x34a3df[_0x0fe7('0x26')],'type':_0x34a3df['type'][_0x0fe7('0x27')]};});_0x39a35b=qs['getOptions'](_0x262146,_0x51c69b);return interaction[_0x0fe7('0x28')]('whatsapp',_0x51c69b[_0x0fe7('0x29')],_0x51c69b[_0x0fe7('0x2a')],_0x39a35b,_0x262146)[_0x0fe7('0x1d')](respondWithFilteredResult(_0x36f5a6,_0x39a35b))['catch'](handleError(_0x36f5a6,null));};exports['show']=function(_0x1c25ae,_0x12b527){var _0x3e19ec={'raw':![],'where':{'id':_0x1c25ae[_0x0fe7('0x2b')]['id']}},_0x3388c9={};_0x3388c9['model']=_['keys'](db[_0x0fe7('0x2c')][_0x0fe7('0x25')]);_0x3388c9['query']=_[_0x0fe7('0x2d')](_0x1c25ae['query']);_0x3388c9[_0x0fe7('0x2e')]=_['intersection'](_0x3388c9[_0x0fe7('0x2f')],_0x3388c9['query']);_0x3e19ec[_0x0fe7('0x30')]=_[_0x0fe7('0x31')](_0x3388c9['model'],qs[_0x0fe7('0x32')](_0x1c25ae[_0x0fe7('0x29')][_0x0fe7('0x32')]));_0x3e19ec[_0x0fe7('0x30')]=_0x3e19ec[_0x0fe7('0x30')][_0x0fe7('0x33')]?_0x3e19ec[_0x0fe7('0x30')]:_0x3388c9[_0x0fe7('0x2f')];if(_0x1c25ae['query'][_0x0fe7('0x34')]){_0x3e19ec[_0x0fe7('0x35')]=[{'all':!![]}];}_0x3e19ec=_['merge']({},_0x3e19ec,_0x1c25ae[_0x0fe7('0x36')]);return db[_0x0fe7('0x2c')][_0x0fe7('0x37')](_0x3e19ec)['then'](handleEntityNotFound(_0x12b527,null))[_0x0fe7('0x1d')](respondWithResult(_0x12b527,null))[_0x0fe7('0x38')](handleError(_0x12b527,null));};exports[_0x0fe7('0x39')]=function(_0x1a14f4,_0xbbe2d){return db[_0x0fe7('0x2c')][_0x0fe7('0x39')](_0x1a14f4[_0x0fe7('0x3a')],{})[_0x0fe7('0x1d')](respondWithResult(_0xbbe2d,0xc9))[_0x0fe7('0x38')](handleError(_0xbbe2d,null));};exports[_0x0fe7('0x1c')]=function(_0x2994c2,_0x4be3ab){if(_0x2994c2['body']['id']){delete _0x2994c2[_0x0fe7('0x3a')]['id'];}return db[_0x0fe7('0x2c')][_0x0fe7('0x37')]({'where':{'id':_0x2994c2[_0x0fe7('0x2b')]['id']}})[_0x0fe7('0x1d')](handleEntityNotFound(_0x4be3ab,null))[_0x0fe7('0x1d')](saveUpdates(_0x2994c2['body'],null))['then'](respondWithResult(_0x4be3ab,null))['catch'](handleError(_0x4be3ab,null));};exports['destroy']=function(_0x56bedb,_0x2ab407){return db[_0x0fe7('0x2c')][_0x0fe7('0x37')]({'where':{'id':_0x56bedb['params']['id']}})['then'](handleEntityNotFound(_0x2ab407,null))[_0x0fe7('0x1d')](removeEntity(_0x2ab407,null))['catch'](handleError(_0x2ab407,null));};exports[_0x0fe7('0x3b')]=function(_0x4d733d,_0x427da7){return db[_0x0fe7('0x2c')][_0x0fe7('0x3b')]()[_0x0fe7('0x1d')](respondWithResult(_0x427da7,null))['catch'](handleError(_0x427da7,null));};exports['addMessage']=function(_0x40bd15,_0xd46358){return db['WhatsappMessage'][_0x0fe7('0x37')]({'where':{'id':_0x40bd15[_0x0fe7('0x2b')]['id']}})[_0x0fe7('0x1d')](handleEntityNotFound(_0xd46358,null))[_0x0fe7('0x1d')](function(_0x33efcf){if(_0x33efcf){return _0x33efcf['addMessage'](_0x40bd15[_0x0fe7('0x3a')]['ids'],_['omit'](_0x40bd15[_0x0fe7('0x3a')],[_0x0fe7('0x3c'),'id'])||{});}})[_0x0fe7('0x1d')](respondWithResult(_0xd46358,null))[_0x0fe7('0x38')](handleError(_0xd46358,null));};exports[_0x0fe7('0x3d')]=function(_0x45f206,_0x532298){var _0x8623b2={'raw':![],'where':{}};var _0x446380={};var _0x4ec809={'count':0x0,'rows':[]};return db[_0x0fe7('0x2c')][_0x0fe7('0x3e')]({'where':{'id':_0x45f206[_0x0fe7('0x2b')]['id']}})['then'](handleEntityNotFound(_0x532298,null))[_0x0fe7('0x1d')](function(_0x14dbd6){if(_0x14dbd6){_0x446380['model']=_[_0x0fe7('0x2d')](db['WhatsappMessage'][_0x0fe7('0x25')]);_0x446380[_0x0fe7('0x29')]=_['keys'](_0x45f206[_0x0fe7('0x29')]);_0x446380[_0x0fe7('0x2e')]=_[_0x0fe7('0x31')](_0x446380['model'],_0x446380[_0x0fe7('0x29')]);_0x8623b2[_0x0fe7('0x30')]=_['intersection'](_0x446380[_0x0fe7('0x2f')],qs[_0x0fe7('0x32')](_0x45f206[_0x0fe7('0x29')][_0x0fe7('0x32')]));_0x8623b2[_0x0fe7('0x30')]=_0x8623b2['attributes']['length']?_0x8623b2[_0x0fe7('0x30')]:_0x446380[_0x0fe7('0x2f')];if(!_0x45f206[_0x0fe7('0x29')][_0x0fe7('0x3f')]('nolimit')){_0x8623b2[_0x0fe7('0x19')]=qs['limit'](_0x45f206[_0x0fe7('0x29')][_0x0fe7('0x19')]);_0x8623b2[_0x0fe7('0x16')]=qs['offset'](_0x45f206[_0x0fe7('0x29')][_0x0fe7('0x16')]);}_0x8623b2[_0x0fe7('0x40')]=qs[_0x0fe7('0x41')](_0x45f206[_0x0fe7('0x29')][_0x0fe7('0x41')]);_0x8623b2[_0x0fe7('0x42')]=qs[_0x0fe7('0x2e')](_[_0x0fe7('0x43')](_0x45f206[_0x0fe7('0x29')],_0x446380[_0x0fe7('0x2e')]));_0x8623b2[_0x0fe7('0x42')]['WhatsappInteractionId']=_0x14dbd6['id'];if(_0x45f206[_0x0fe7('0x29')][_0x0fe7('0x44')]){_0x8623b2[_0x0fe7('0x42')]=_['merge'](_0x8623b2['where'],{'$or':_[_0x0fe7('0x24')](_0x8623b2[_0x0fe7('0x30')],function(_0xc59220){var _0x35559d={};_0x35559d[_0xc59220]={'$like':'%'+_0x45f206[_0x0fe7('0x29')]['filter']+'%'};return _0x35559d;})});}if(_0x45f206[_0x0fe7('0x29')][_0x0fe7('0x45')]){var _0xdfd569=_0x45f206[_0x0fe7('0x29')][_0x0fe7('0x45')]['split'](',');var _0x1efa90={};_0x1efa90[_0xdfd569[0x0]]={'$gte':moment(_0xdfd569[0x1])[_0x0fe7('0x46')](_0x0fe7('0x47'))};_0x8623b2['where']=_[_0x0fe7('0x48')](_0x8623b2['where'],_0x1efa90);}_0x8623b2=_[_0x0fe7('0x48')]({},_0x8623b2,_0x45f206[_0x0fe7('0x36')]);return db[_0x0fe7('0x49')]['count']({'where':_0x8623b2[_0x0fe7('0x42')]})[_0x0fe7('0x1d')](function(_0xd981cc){_0x4ec809[_0x0fe7('0x18')]=_0xd981cc;if(_0x45f206[_0x0fe7('0x29')]['includeAll']){_0x8623b2[_0x0fe7('0x35')]=[{'all':!![]}];}return db[_0x0fe7('0x49')]['findAll'](_0x8623b2);})[_0x0fe7('0x1d')](function(_0x2ddc91){_0x4ec809['rows']=_0x2ddc91;return _0x4ec809;});}})[_0x0fe7('0x1d')](respondWithFilteredResult(_0x532298,_0x8623b2))[_0x0fe7('0x38')](handleError(_0x532298,null));};exports[_0x0fe7('0x4a')]=function(_0x14aac3,_0x55cd66){return db[_0x0fe7('0x2c')][_0x0fe7('0x37')]({'where':{'id':_0x14aac3['params']['id']}})[_0x0fe7('0x1d')](handleEntityNotFound(_0x55cd66,null))[_0x0fe7('0x1d')](function(_0x112b01){if(_0x112b01){return _0x112b01['setTags'](_0x14aac3[_0x0fe7('0x3a')][_0x0fe7('0x3c')],_[_0x0fe7('0x4b')](_0x14aac3['body'],[_0x0fe7('0x3c'),'id'])||{})[_0x0fe7('0x4c')](function(){return db[_0x0fe7('0x4d')][_0x0fe7('0x28')]({'attributes':['id',_0x0fe7('0x21'),_0x0fe7('0x4e')],'where':{'id':_0x14aac3[_0x0fe7('0x3a')][_0x0fe7('0x3c')]}});})['then'](function(_0x12c9a0){socket[_0x0fe7('0x4f')](_0x0fe7('0x50'),{'id':Number(_0x14aac3[_0x0fe7('0x2b')]['id']),'tags':_0x12c9a0||[]});return{'id':Number(_0x14aac3[_0x0fe7('0x2b')]['id']),'tags':_0x12c9a0||[]};});}})[_0x0fe7('0x1d')](respondWithResult(_0x55cd66,null))[_0x0fe7('0x38')](handleError(_0x55cd66,null));};exports[_0x0fe7('0x51')]=function(_0x584d16,_0xe35056){return db[_0x0fe7('0x2c')][_0x0fe7('0x37')]({'where':{'id':_0x584d16['params']['id']}})[_0x0fe7('0x1d')](handleEntityNotFound(_0xe35056,null))['then'](function(_0x421c92){if(_0x421c92){return _0x421c92['removeTags'](_0x584d16['query']['ids']);}})[_0x0fe7('0x1d')](respondWithStatusCode(_0xe35056,null))[_0x0fe7('0x38')](handleError(_0xe35056,null));};exports[_0x0fe7('0x52')]=function(_0x56f61c,_0x5d4d02){var _0x3a9808=moment()[_0x0fe7('0x53')]()['toString']();var _0x1f17e5=path[_0x0fe7('0x54')](config[_0x0fe7('0x55')],_0x0fe7('0x56'),_0x0fe7('0x57'),_0x0fe7('0x58'));var _0x697102=path[_0x0fe7('0x54')](config[_0x0fe7('0x55')],_0x0fe7('0x56'),_0x0fe7('0x57'),_0x0fe7('0x59'));var _0x27b7c2=path[_0x0fe7('0x54')](_0x1f17e5,_0x3a9808);var _0x279a38=util['format'](_0x0fe7('0x5a'),_0x56f61c[_0x0fe7('0x2b')]['id'],_0x3a9808);var _0x583f48=path[_0x0fe7('0x54')](_0x1f17e5,_0x279a38);var _0x4d0355=[];_0x4d0355[_0x0fe7('0x5b')]({'model':db[_0x0fe7('0x5c')],'as':_0x0fe7('0x5c'),'attributes':['id','fullname'],'raw':!![]});_0x4d0355[_0x0fe7('0x5b')]({'model':db[_0x0fe7('0x5d')],'as':_0x0fe7('0x5e'),'attributes':['id',_0x0fe7('0x5f'),_0x0fe7('0x60')],'raw':!![]});if(_0x56f61c[_0x0fe7('0x29')]['attachments']){_0x4d0355[_0x0fe7('0x5b')]({'model':db[_0x0fe7('0x61')],'as':_0x0fe7('0x61'),'raw':!![]});}var _0x11e00b=[{'model':db[_0x0fe7('0x49')],'as':_0x0fe7('0x62'),'attributes':['id',_0x0fe7('0x3a'),'createdAt',_0x0fe7('0x63'),_0x0fe7('0x64'),_0x0fe7('0x65')],'include':_0x4d0355}];_0x11e00b['push']({'model':db[_0x0fe7('0x66')],'as':_0x0fe7('0x67'),'attributes':['id',_0x0fe7('0x21')]});_0x11e00b[_0x0fe7('0x5b')]({'model':db['User'],'as':_0x0fe7('0x68'),'attributes':['id',_0x0fe7('0x69')]});return db['WhatsappInteraction'][_0x0fe7('0x37')]({'where':{'id':_0x56f61c[_0x0fe7('0x2b')]['id']},'include':_0x11e00b})['then'](handleEntityNotFound(_0x5d4d02,null))[_0x0fe7('0x1d')](function(_0x50b705){if(_0x50b705){var _0xe06ba2=_0x50b705['get']({'plain':!![]});fs[_0x0fe7('0x6a')](_0x27b7c2);var _0x245a16={'channel':'Whatsapp','account':_0xe06ba2[_0x0fe7('0x67')][_0x0fe7('0x21')],'agent':_0xe06ba2['Owner']?_0xe06ba2[_0x0fe7('0x68')][_0x0fe7('0x69')]:_0x0fe7('0x6b'),'createdAt':moment(_0xe06ba2[_0x0fe7('0x6c')])[_0x0fe7('0x46')](_0x0fe7('0x47'))[_0x0fe7('0x6d')](),'closedAt':_0xe06ba2[_0x0fe7('0x6e')]?moment(_0xe06ba2[_0x0fe7('0x6f')])[_0x0fe7('0x46')](_0x0fe7('0x47'))[_0x0fe7('0x6d')]():'','disposition':_0xe06ba2[_0x0fe7('0x70')],'messages':_(_0xe06ba2[_0x0fe7('0x62')])[_0x0fe7('0x44')]({'secret':![]})[_0x0fe7('0x24')](function(_0xbc0500){if(_0xbc0500[_0x0fe7('0x61')]){var _0x490982=path[_0x0fe7('0x54')](_0x697102,_0xbc0500[_0x0fe7('0x61')]['basename']);if(fs['existsSync'](_0x490982)){fs['createReadStream'](_0x490982)['pipe'](fs[_0x0fe7('0x71')](path[_0x0fe7('0x54')](_0x27b7c2,_0xbc0500[_0x0fe7('0x61')]['basename'])));}}return{'date':moment(_0xbc0500[_0x0fe7('0x6c')])[_0x0fe7('0x46')](_0x0fe7('0x47'))[_0x0fe7('0x6d')](),'sender':_0xbc0500['direction']===_0x0fe7('0x72')?_0xbc0500[_0x0fe7('0x5c')]?_0xbc0500[_0x0fe7('0x5c')][_0x0fe7('0x69')]:_0x0fe7('0x73'):_0xbc0500['Contact'][_0x0fe7('0x5f')]+(_0xbc0500[_0x0fe7('0x5e')][_0x0fe7('0x60')]?'\x20'+_0xbc0500['Contact'][_0x0fe7('0x60')]:''),'body':_0xbc0500['Attachment']?_0xbc0500[_0x0fe7('0x61')][_0x0fe7('0x74')]:_0xbc0500['body'],'direction':_0xbc0500[_0x0fe7('0x63')]==='out'?'A':'C','secret':_0xbc0500[_0x0fe7('0x64')],'read':_0xbc0500[_0x0fe7('0x65')],'attachment':_0xbc0500[_0x0fe7('0x61')]};})[_0x0fe7('0x75')]()};return ejs[_0x0fe7('0x76')](path[_0x0fe7('0x54')](config[_0x0fe7('0x55')],_0x0fe7('0x56'),_0x0fe7('0x77'),_0x0fe7('0x78')),{'interaction':_0x245a16})['then'](function(_0xa5f8ce){var _0x53ca89=path[_0x0fe7('0x54')](_0x27b7c2,'transcript-'+_0xe06ba2['id']+'-'+_0x3a9808+_0x0fe7('0x79'));var _0x5e43b9={'path':_0x53ca89,'channel':'Whatsapp','interactionId':_0xe06ba2['id']};return pdf['createPdfFromHTML'](_0xa5f8ce,_0x5e43b9);})[_0x0fe7('0x1d')](function(){return new BPromise(function(_0x4af088,_0x33ff9b){zipdir(_0x27b7c2,{'saveTo':_0x583f48},function(_0x840296,_0x2cda5a){if(_0x840296)return _0x33ff9b(_0x840296);return _0x4af088(_0x2cda5a);});})[_0x0fe7('0x1d')](function(){return new BPromise(function(_0x3f573b,_0x169b6e){rimraf(_0x27b7c2,function(_0x2ec449){if(_0x2ec449)_0x169b6e(_0x2ec449);return _0x3f573b();});});})[_0x0fe7('0x1d')](function(){return _0x5d4d02[_0x0fe7('0x52')](_0x583f48,_0x279a38,function(_0x525e86){if(_0x525e86){console[_0x0fe7('0x7a')]('err',_0x525e86);}else{fs['unlinkSync'](_0x583f48);}});});});}else{return _0x5d4d02[_0x0fe7('0x12')](0xc8);}})[_0x0fe7('0x38')](handleError(_0x5d4d02,null));};
\ No newline at end of file
+var _0xf536=['existsSync','pipe','createWriteStream','System','out','value','renderFile','views','downloadInteraction.ejs','transcript-','.pdf','createPdfFromHTML','err','sendStatus','zip-dir','bluebird','path','ejs','lodash','../../components/interaction/service','../../components/parsers/qs','../../components/export/pdf','api','../../mysqldb','redis','defaults','localhost','socket.io-emitter','register','end','status','json','offset','undefined','limit','count','set','update','then','destroy','error','stack','name','send','map','WhatsappInteraction','rawAttributes','fieldName','type','key','findAll','query','user','catch','params','model','keys','filters','intersection','attributes','fields','length','includeAll','include','merge','options','create','body','describe','addMessage','WhatsappMessage','find','omit','ids','getMessages','findOne','hasOwnProperty','nolimit','sort','where','pick','filter','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','rows','setTags','spread','Tag','color','emit','whatsappInteractionTags:save','removeTags','download','unix','toString','join','root','server','files','tmp','attachments','transcript-%d-%s.zip','push','User','fullname','Contact','firstName','Attachment','Messages','createdAt','direction','secret','read','WhatsappAccount','Account','Owner','mkdirSync','N.A.','closed','closedAt','disposition','basename'];(function(_0x9e97ca,_0x3ad9db){var _0x5d1649=function(_0xcf9dc7){while(--_0xcf9dc7){_0x9e97ca['push'](_0x9e97ca['shift']());}};_0x5d1649(++_0x3ad9db);}(_0xf536,0x106));var _0x6f53=function(_0x565d34,_0x4176da){_0x565d34=_0x565d34-0x0;var _0x3751e7=_0xf536[_0x565d34];return _0x3751e7;};'use strict';var rimraf=require('rimraf');var zipdir=require(_0x6f53('0x0'));var moment=require('moment');var BPromise=require(_0x6f53('0x1'));var util=require('util');var path=require(_0x6f53('0x2'));var ejs=require(_0x6f53('0x3'));var fs=require('fs');var _=require(_0x6f53('0x4'));var Redis=require('ioredis');var interaction=require(_0x6f53('0x5'));var qs=require(_0x6f53('0x6'));var pdf=require(_0x6f53('0x7'));var logger=require('../../config/logger')(_0x6f53('0x8'));var config=require('../../config/environment');var db=require(_0x6f53('0x9'))['db'];config[_0x6f53('0xa')]=_[_0x6f53('0xb')](config[_0x6f53('0xa')],{'host':_0x6f53('0xc'),'port':0x18eb});var socket=require(_0x6f53('0xd'))(new Redis(config['redis']));require('./whatsappInteraction.socket')[_0x6f53('0xe')](socket);function respondWithStatusCode(_0x2d11b1,_0x2951d9){_0x2951d9=_0x2951d9||0xcc;return function(_0x2dc75d){if(_0x2dc75d){return _0x2d11b1['sendStatus'](_0x2951d9);}return _0x2d11b1['status'](_0x2951d9)[_0x6f53('0xf')]();};}function respondWithResult(_0x376839,_0x2a64da){_0x2a64da=_0x2a64da||0xc8;return function(_0x2aaa19){if(_0x2aaa19){return _0x376839[_0x6f53('0x10')](_0x2a64da)[_0x6f53('0x11')](_0x2aaa19);}};}function respondWithFilteredResult(_0x575fbc,_0x2e49fa){return function(_0x369ea5){if(_0x369ea5){var _0x957ce6=typeof _0x2e49fa[_0x6f53('0x12')]===_0x6f53('0x13')&&typeof _0x2e49fa[_0x6f53('0x14')]===_0x6f53('0x13');var _0x832520=_0x369ea5[_0x6f53('0x15')];var _0xb71688=_0x957ce6?0x0:_0x2e49fa['offset'];var _0x54b0f4=_0x957ce6?_0x369ea5['count']:_0x2e49fa[_0x6f53('0x12')]+_0x2e49fa['limit'];var _0x556730;if(_0x54b0f4>=_0x832520){_0x54b0f4=_0x832520;_0x556730=0xc8;}else{_0x556730=0xce;}_0x575fbc[_0x6f53('0x10')](_0x556730);return _0x575fbc[_0x6f53('0x16')]('Content-Range',_0xb71688+'-'+_0x54b0f4+'/'+_0x832520)[_0x6f53('0x11')](_0x369ea5);}return null;};}function saveUpdates(_0x5f2ac7){return function(_0x345b94){if(_0x345b94){return _0x345b94[_0x6f53('0x17')](_0x5f2ac7)[_0x6f53('0x18')](function(_0x10bf7b){return _0x10bf7b;});}return null;};}function removeEntity(_0x43d2ab){return function(_0x1f0f4f){if(_0x1f0f4f){return _0x1f0f4f[_0x6f53('0x19')]()[_0x6f53('0x18')](function(){_0x43d2ab[_0x6f53('0x10')](0xcc)[_0x6f53('0xf')]();});}};}function handleEntityNotFound(_0x114e24){return function(_0x5381aa){if(!_0x5381aa){_0x114e24['sendStatus'](0x194);}return _0x5381aa;};}function handleError(_0x43cc6b,_0x34525c){_0x34525c=_0x34525c||0x1f4;return function(_0x496d9a){logger[_0x6f53('0x1a')](_0x496d9a[_0x6f53('0x1b')]);if(_0x496d9a[_0x6f53('0x1c')]){delete _0x496d9a[_0x6f53('0x1c')];}_0x43cc6b['status'](_0x34525c)[_0x6f53('0x1d')](_0x496d9a);};}exports['index']=function(_0x580077,_0x439f68){var _0x41cbd6={};var _0x3e8445=_[_0x6f53('0x1e')](db[_0x6f53('0x1f')][_0x6f53('0x20')],function(_0x253194){return{'name':_0x253194[_0x6f53('0x21')],'type':_0x253194[_0x6f53('0x22')][_0x6f53('0x23')]};});_0x41cbd6=qs['getOptions'](_0x3e8445,_0x580077);return interaction[_0x6f53('0x24')]('whatsapp',_0x580077[_0x6f53('0x25')],_0x580077[_0x6f53('0x26')],_0x41cbd6,_0x3e8445)['then'](respondWithFilteredResult(_0x439f68,_0x41cbd6))[_0x6f53('0x27')](handleError(_0x439f68,null));};exports['show']=function(_0x340209,_0x1f27ba){var _0x3de5ab={'raw':![],'where':{'id':_0x340209[_0x6f53('0x28')]['id']}},_0x134bdd={};_0x134bdd[_0x6f53('0x29')]=_[_0x6f53('0x2a')](db[_0x6f53('0x1f')][_0x6f53('0x20')]);_0x134bdd['query']=_[_0x6f53('0x2a')](_0x340209[_0x6f53('0x25')]);_0x134bdd[_0x6f53('0x2b')]=_[_0x6f53('0x2c')](_0x134bdd[_0x6f53('0x29')],_0x134bdd[_0x6f53('0x25')]);_0x3de5ab[_0x6f53('0x2d')]=_[_0x6f53('0x2c')](_0x134bdd['model'],qs[_0x6f53('0x2e')](_0x340209['query'][_0x6f53('0x2e')]));_0x3de5ab[_0x6f53('0x2d')]=_0x3de5ab['attributes'][_0x6f53('0x2f')]?_0x3de5ab[_0x6f53('0x2d')]:_0x134bdd['model'];if(_0x340209['query'][_0x6f53('0x30')]){_0x3de5ab[_0x6f53('0x31')]=[{'all':!![]}];}_0x3de5ab=_[_0x6f53('0x32')]({},_0x3de5ab,_0x340209[_0x6f53('0x33')]);return db['WhatsappInteraction']['find'](_0x3de5ab)[_0x6f53('0x18')](handleEntityNotFound(_0x1f27ba,null))[_0x6f53('0x18')](respondWithResult(_0x1f27ba,null))[_0x6f53('0x27')](handleError(_0x1f27ba,null));};exports['create']=function(_0x143f10,_0x9fdf24){return db['WhatsappInteraction'][_0x6f53('0x34')](_0x143f10[_0x6f53('0x35')],{})[_0x6f53('0x18')](respondWithResult(_0x9fdf24,0xc9))[_0x6f53('0x27')](handleError(_0x9fdf24,null));};exports[_0x6f53('0x17')]=function(_0x3ee064,_0x4edb74){if(_0x3ee064[_0x6f53('0x35')]['id']){delete _0x3ee064[_0x6f53('0x35')]['id'];}return db[_0x6f53('0x1f')]['find']({'where':{'id':_0x3ee064[_0x6f53('0x28')]['id']}})[_0x6f53('0x18')](handleEntityNotFound(_0x4edb74,null))[_0x6f53('0x18')](saveUpdates(_0x3ee064[_0x6f53('0x35')],null))[_0x6f53('0x18')](respondWithResult(_0x4edb74,null))['catch'](handleError(_0x4edb74,null));};exports[_0x6f53('0x19')]=function(_0x16f855,_0x39479d){return db['WhatsappInteraction']['find']({'where':{'id':_0x16f855[_0x6f53('0x28')]['id']}})[_0x6f53('0x18')](handleEntityNotFound(_0x39479d,null))[_0x6f53('0x18')](removeEntity(_0x39479d,null))[_0x6f53('0x27')](handleError(_0x39479d,null));};exports[_0x6f53('0x36')]=function(_0x38b37c,_0xdb7a5c){return db[_0x6f53('0x1f')][_0x6f53('0x36')]()[_0x6f53('0x18')](respondWithResult(_0xdb7a5c,null))[_0x6f53('0x27')](handleError(_0xdb7a5c,null));};exports[_0x6f53('0x37')]=function(_0x271838,_0x82585c){return db[_0x6f53('0x38')][_0x6f53('0x39')]({'where':{'id':_0x271838['params']['id']}})[_0x6f53('0x18')](handleEntityNotFound(_0x82585c,null))['then'](function(_0x5f4887){if(_0x5f4887){return _0x5f4887[_0x6f53('0x37')](_0x271838['body']['ids'],_[_0x6f53('0x3a')](_0x271838['body'],[_0x6f53('0x3b'),'id'])||{});}})[_0x6f53('0x18')](respondWithResult(_0x82585c,null))[_0x6f53('0x27')](handleError(_0x82585c,null));};exports[_0x6f53('0x3c')]=function(_0x5499a9,_0x526e2c){var _0x14ea14={'raw':![],'where':{}};var _0x190082={};var _0x3193ad={'count':0x0,'rows':[]};return db[_0x6f53('0x1f')][_0x6f53('0x3d')]({'where':{'id':_0x5499a9[_0x6f53('0x28')]['id']}})['then'](handleEntityNotFound(_0x526e2c,null))[_0x6f53('0x18')](function(_0x70ccc0){if(_0x70ccc0){_0x190082[_0x6f53('0x29')]=_[_0x6f53('0x2a')](db[_0x6f53('0x38')][_0x6f53('0x20')]);_0x190082[_0x6f53('0x25')]=_['keys'](_0x5499a9[_0x6f53('0x25')]);_0x190082['filters']=_[_0x6f53('0x2c')](_0x190082[_0x6f53('0x29')],_0x190082['query']);_0x14ea14[_0x6f53('0x2d')]=_[_0x6f53('0x2c')](_0x190082[_0x6f53('0x29')],qs['fields'](_0x5499a9[_0x6f53('0x25')]['fields']));_0x14ea14[_0x6f53('0x2d')]=_0x14ea14[_0x6f53('0x2d')][_0x6f53('0x2f')]?_0x14ea14[_0x6f53('0x2d')]:_0x190082[_0x6f53('0x29')];if(!_0x5499a9['query'][_0x6f53('0x3e')](_0x6f53('0x3f'))){_0x14ea14[_0x6f53('0x14')]=qs['limit'](_0x5499a9[_0x6f53('0x25')]['limit']);_0x14ea14['offset']=qs['offset'](_0x5499a9['query']['offset']);}_0x14ea14['order']=qs[_0x6f53('0x40')](_0x5499a9[_0x6f53('0x25')]['sort']);_0x14ea14[_0x6f53('0x41')]=qs[_0x6f53('0x2b')](_[_0x6f53('0x42')](_0x5499a9['query'],_0x190082['filters']));_0x14ea14[_0x6f53('0x41')]['WhatsappInteractionId']=_0x70ccc0['id'];if(_0x5499a9[_0x6f53('0x25')][_0x6f53('0x43')]){_0x14ea14[_0x6f53('0x41')]=_[_0x6f53('0x32')](_0x14ea14['where'],{'$or':_[_0x6f53('0x1e')](_0x14ea14[_0x6f53('0x2d')],function(_0x1a9a4d){var _0x32f2c5={};_0x32f2c5[_0x1a9a4d]={'$like':'%'+_0x5499a9[_0x6f53('0x25')][_0x6f53('0x43')]+'%'};return _0x32f2c5;})});}if(_0x5499a9[_0x6f53('0x25')]['$gte']){var _0x226746=_0x5499a9['query'][_0x6f53('0x44')][_0x6f53('0x45')](',');var _0x40b1db={};_0x40b1db[_0x226746[0x0]]={'$gte':moment(_0x226746[0x1])[_0x6f53('0x46')](_0x6f53('0x47'))};_0x14ea14[_0x6f53('0x41')]=_[_0x6f53('0x32')](_0x14ea14['where'],_0x40b1db);}_0x14ea14=_['merge']({},_0x14ea14,_0x5499a9[_0x6f53('0x33')]);return db[_0x6f53('0x38')][_0x6f53('0x15')]({'where':_0x14ea14[_0x6f53('0x41')]})[_0x6f53('0x18')](function(_0xbabe0f){_0x3193ad[_0x6f53('0x15')]=_0xbabe0f;if(_0x5499a9[_0x6f53('0x25')][_0x6f53('0x30')]){_0x14ea14[_0x6f53('0x31')]=[{'all':!![]}];}return db[_0x6f53('0x38')][_0x6f53('0x24')](_0x14ea14);})[_0x6f53('0x18')](function(_0x43b0a6){_0x3193ad[_0x6f53('0x48')]=_0x43b0a6;return _0x3193ad;});}})['then'](respondWithFilteredResult(_0x526e2c,_0x14ea14))['catch'](handleError(_0x526e2c,null));};exports['addTags']=function(_0x1782df,_0x23b697){return db[_0x6f53('0x1f')][_0x6f53('0x39')]({'where':{'id':_0x1782df[_0x6f53('0x28')]['id']}})[_0x6f53('0x18')](handleEntityNotFound(_0x23b697,null))[_0x6f53('0x18')](function(_0x209fb5){if(_0x209fb5){return _0x209fb5[_0x6f53('0x49')](_0x1782df['body']['ids'],_[_0x6f53('0x3a')](_0x1782df[_0x6f53('0x35')],[_0x6f53('0x3b'),'id'])||{})[_0x6f53('0x4a')](function(){return db[_0x6f53('0x4b')]['findAll']({'attributes':['id',_0x6f53('0x1c'),_0x6f53('0x4c')],'where':{'id':_0x1782df[_0x6f53('0x35')][_0x6f53('0x3b')]}});})[_0x6f53('0x18')](function(_0x3d94f0){socket[_0x6f53('0x4d')](_0x6f53('0x4e'),{'id':Number(_0x1782df[_0x6f53('0x28')]['id']),'tags':_0x3d94f0||[]});return{'id':Number(_0x1782df[_0x6f53('0x28')]['id']),'tags':_0x3d94f0||[]};});}})[_0x6f53('0x18')](respondWithResult(_0x23b697,null))[_0x6f53('0x27')](handleError(_0x23b697,null));};exports[_0x6f53('0x4f')]=function(_0x71109b,_0x14f1d2){return db[_0x6f53('0x1f')][_0x6f53('0x39')]({'where':{'id':_0x71109b['params']['id']}})[_0x6f53('0x18')](handleEntityNotFound(_0x14f1d2,null))['then'](function(_0x31f969){if(_0x31f969){return _0x31f969[_0x6f53('0x4f')](_0x71109b[_0x6f53('0x25')][_0x6f53('0x3b')]);}})['then'](respondWithStatusCode(_0x14f1d2,null))[_0x6f53('0x27')](handleError(_0x14f1d2,null));};exports[_0x6f53('0x50')]=function(_0x490abc,_0x423ab5){var _0x5bd71a=moment()[_0x6f53('0x51')]()[_0x6f53('0x52')]();var _0x27eeaa=path[_0x6f53('0x53')](config[_0x6f53('0x54')],_0x6f53('0x55'),_0x6f53('0x56'),_0x6f53('0x57'));var _0x4e086a=path['join'](config[_0x6f53('0x54')],_0x6f53('0x55'),_0x6f53('0x56'),_0x6f53('0x58'));var _0x215834=path[_0x6f53('0x53')](_0x27eeaa,_0x5bd71a);var _0x1a584b=util[_0x6f53('0x46')](_0x6f53('0x59'),_0x490abc[_0x6f53('0x28')]['id'],_0x5bd71a);var _0x5172df=path[_0x6f53('0x53')](_0x27eeaa,_0x1a584b);var _0x33220d=[];_0x33220d[_0x6f53('0x5a')]({'model':db[_0x6f53('0x5b')],'as':_0x6f53('0x5b'),'attributes':['id',_0x6f53('0x5c')],'raw':!![]});_0x33220d[_0x6f53('0x5a')]({'model':db['CmContact'],'as':_0x6f53('0x5d'),'attributes':['id',_0x6f53('0x5e'),'lastName'],'raw':!![]});if(_0x490abc[_0x6f53('0x25')][_0x6f53('0x58')]){_0x33220d['push']({'model':db[_0x6f53('0x5f')],'as':'Attachment','raw':!![]});}var _0x4fbcf6=[{'model':db[_0x6f53('0x38')],'as':_0x6f53('0x60'),'attributes':['id',_0x6f53('0x35'),_0x6f53('0x61'),_0x6f53('0x62'),_0x6f53('0x63'),_0x6f53('0x64')],'include':_0x33220d}];_0x4fbcf6[_0x6f53('0x5a')]({'model':db[_0x6f53('0x65')],'as':_0x6f53('0x66'),'attributes':['id','name']});_0x4fbcf6[_0x6f53('0x5a')]({'model':db[_0x6f53('0x5b')],'as':_0x6f53('0x67'),'attributes':['id',_0x6f53('0x5c')]});return db[_0x6f53('0x1f')][_0x6f53('0x39')]({'where':{'id':_0x490abc[_0x6f53('0x28')]['id']},'include':_0x4fbcf6})[_0x6f53('0x18')](handleEntityNotFound(_0x423ab5,null))[_0x6f53('0x18')](function(_0x309189){if(_0x309189){var _0x19203e=_0x309189['get']({'plain':!![]});fs[_0x6f53('0x68')](_0x215834);var _0x22f504={'channel':'Whatsapp','account':_0x19203e['Account'][_0x6f53('0x1c')],'agent':_0x19203e['Owner']?_0x19203e[_0x6f53('0x67')][_0x6f53('0x5c')]:_0x6f53('0x69'),'createdAt':moment(_0x19203e['createdAt'])[_0x6f53('0x46')](_0x6f53('0x47'))['toString'](),'closedAt':_0x19203e[_0x6f53('0x6a')]?moment(_0x19203e[_0x6f53('0x6b')])[_0x6f53('0x46')](_0x6f53('0x47'))[_0x6f53('0x52')]():'','disposition':_0x19203e[_0x6f53('0x6c')],'messages':_(_0x19203e[_0x6f53('0x60')])['filter']({'secret':![]})['map'](function(_0x27a1b7){if(_0x27a1b7[_0x6f53('0x5f')]){var _0x5aea7c=path[_0x6f53('0x53')](_0x4e086a,_0x27a1b7[_0x6f53('0x5f')][_0x6f53('0x6d')]);if(fs[_0x6f53('0x6e')](_0x5aea7c)){fs['createReadStream'](_0x5aea7c)[_0x6f53('0x6f')](fs[_0x6f53('0x70')](path[_0x6f53('0x53')](_0x215834,_0x27a1b7[_0x6f53('0x5f')][_0x6f53('0x6d')])));}}return{'date':moment(_0x27a1b7[_0x6f53('0x61')])[_0x6f53('0x46')](_0x6f53('0x47'))[_0x6f53('0x52')](),'sender':_0x27a1b7[_0x6f53('0x62')]==='out'?_0x27a1b7[_0x6f53('0x5b')]?_0x27a1b7[_0x6f53('0x5b')][_0x6f53('0x5c')]:_0x6f53('0x71'):_0x27a1b7['Contact']['firstName']+(_0x27a1b7[_0x6f53('0x5d')]['lastName']?'\x20'+_0x27a1b7[_0x6f53('0x5d')]['lastName']:''),'body':_0x27a1b7[_0x6f53('0x5f')]?_0x27a1b7[_0x6f53('0x5f')][_0x6f53('0x6d')]:_0x27a1b7['body'],'direction':_0x27a1b7[_0x6f53('0x62')]===_0x6f53('0x72')?'A':'C','secret':_0x27a1b7[_0x6f53('0x63')],'read':_0x27a1b7[_0x6f53('0x64')],'attachment':_0x27a1b7[_0x6f53('0x5f')]};})[_0x6f53('0x73')]()};return ejs[_0x6f53('0x74')](path[_0x6f53('0x53')](config[_0x6f53('0x54')],_0x6f53('0x55'),_0x6f53('0x75'),_0x6f53('0x76')),{'interaction':_0x22f504})[_0x6f53('0x18')](function(_0x5df115){var _0x5163b9=path[_0x6f53('0x53')](_0x215834,_0x6f53('0x77')+_0x19203e['id']+'-'+_0x5bd71a+_0x6f53('0x78'));var _0x23b8d1={'path':_0x5163b9,'channel':'Whatsapp','interactionId':_0x19203e['id']};return pdf[_0x6f53('0x79')](_0x5df115,_0x23b8d1);})[_0x6f53('0x18')](function(){return new BPromise(function(_0x33ac95,_0x1ee07b){zipdir(_0x215834,{'saveTo':_0x5172df},function(_0x127fc1,_0x18f50e){if(_0x127fc1)return _0x1ee07b(_0x127fc1);return _0x33ac95(_0x18f50e);});})[_0x6f53('0x18')](function(){return new BPromise(function(_0x5774ef,_0x1ce885){rimraf(_0x215834,function(_0x515a4c){if(_0x515a4c)_0x1ce885(_0x515a4c);return _0x5774ef();});});})['then'](function(){return _0x423ab5[_0x6f53('0x50')](_0x5172df,_0x1a584b,function(_0x808474){if(_0x808474){console['log'](_0x6f53('0x7a'),_0x808474);}else{fs['unlinkSync'](_0x5172df);}});});});}else{return _0x423ab5[_0x6f53('0x7b')](0xc8);}})[_0x6f53('0x27')](handleError(_0x423ab5,null));};
\ No newline at end of file