Built motion from commit ffdca78c.|2.6.24
[motion2.git] / server / api / whatsappInteraction / whatsappInteraction.controller.js
index c36579b..8f391da 100644 (file)
@@ -15,4 +15,4 @@
 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                        *
 // *                                                                       *
 // *************************************************************************
-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
+var _0x8f58=['moment','bluebird','util','path','ejs','lodash','ioredis','../../components/parsers/qs','../../config/logger','api','defaults','redis','localhost','socket.io-emitter','register','sendStatus','status','end','json','offset','limit','undefined','count','set','Content-Range','then','destroy','stack','name','send','index','map','WhatsappInteraction','rawAttributes','fieldName','type','key','getOptions','findAll','query','catch','show','params','model','keys','filters','intersection','attributes','fields','includeAll','include','merge','find','create','body','describe','addMessage','ids','omit','findOne','WhatsappMessage','length','hasOwnProperty','nolimit','sort','pick','where','filter','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','options','addTags','setTags','spread','Tag','emit','whatsappInteractionTags:save','removeTags','unix','toString','server','files','tmp','join','root','attachments','transcript-%d-%s.zip','push','User','fullname','Contact','firstName','lastName','Attachment','Messages','direction','secret','read','WhatsappAccount','Owner','get','mkdirSync','Whatsapp','Account','N.A.','createdAt','closedAt','basename','existsSync','pipe','out','System','value','renderFile','transcript-','createPdfFromHTML','download','log','unlinkSync'];(function(_0x44d5f2,_0x36f41e){var _0x322e65=function(_0x1f4365){while(--_0x1f4365){_0x44d5f2['push'](_0x44d5f2['shift']());}};_0x322e65(++_0x36f41e);}(_0x8f58,0x79));var _0x88f5=function(_0x1657e2,_0x2e40a6){_0x1657e2=_0x1657e2-0x0;var _0x489415=_0x8f58[_0x1657e2];return _0x489415;};'use strict';var rimraf=require('rimraf');var zipdir=require('zip-dir');var moment=require(_0x88f5('0x0'));var BPromise=require(_0x88f5('0x1'));var util=require(_0x88f5('0x2'));var path=require(_0x88f5('0x3'));var ejs=require(_0x88f5('0x4'));var fs=require('fs');var _=require(_0x88f5('0x5'));var Redis=require(_0x88f5('0x6'));var interaction=require('../../components/interaction/service');var qs=require(_0x88f5('0x7'));var pdf=require('../../components/export/pdf');var logger=require(_0x88f5('0x8'))(_0x88f5('0x9'));var config=require('../../config/environment');var db=require('../../mysqldb')['db'];config['redis']=_[_0x88f5('0xa')](config[_0x88f5('0xb')],{'host':_0x88f5('0xc'),'port':0x18eb});var socket=require(_0x88f5('0xd'))(new Redis(config[_0x88f5('0xb')]));require('./whatsappInteraction.socket')[_0x88f5('0xe')](socket);function respondWithStatusCode(_0x2ea5db,_0x4a3e1c){_0x4a3e1c=_0x4a3e1c||0xcc;return function(_0x3f6aa8){if(_0x3f6aa8){return _0x2ea5db[_0x88f5('0xf')](_0x4a3e1c);}return _0x2ea5db[_0x88f5('0x10')](_0x4a3e1c)[_0x88f5('0x11')]();};}function respondWithResult(_0x38f637,_0x5e9751){_0x5e9751=_0x5e9751||0xc8;return function(_0x53b94a){if(_0x53b94a){return _0x38f637[_0x88f5('0x10')](_0x5e9751)[_0x88f5('0x12')](_0x53b94a);}};}function respondWithFilteredResult(_0x288feb,_0x3b1754){return function(_0x1307a0){if(_0x1307a0){var _0xffe6a5=typeof _0x3b1754[_0x88f5('0x13')]==='undefined'&&typeof _0x3b1754[_0x88f5('0x14')]===_0x88f5('0x15');var _0x38ded4=_0x1307a0[_0x88f5('0x16')];var _0x4009d9=_0xffe6a5?0x0:_0x3b1754['offset'];var _0x56f44d=_0xffe6a5?_0x1307a0[_0x88f5('0x16')]:_0x3b1754[_0x88f5('0x13')]+_0x3b1754['limit'];var _0x34eac6;if(_0x56f44d>=_0x38ded4){_0x56f44d=_0x38ded4;_0x34eac6=0xc8;}else{_0x34eac6=0xce;}_0x288feb[_0x88f5('0x10')](_0x34eac6);return _0x288feb[_0x88f5('0x17')](_0x88f5('0x18'),_0x4009d9+'-'+_0x56f44d+'/'+_0x38ded4)['json'](_0x1307a0);}return null;};}function saveUpdates(_0x115f52){return function(_0x5eb830){if(_0x5eb830){return _0x5eb830['update'](_0x115f52)[_0x88f5('0x19')](function(_0x509a3f){return _0x509a3f;});}return null;};}function removeEntity(_0x17f1f8){return function(_0x21e116){if(_0x21e116){return _0x21e116[_0x88f5('0x1a')]()[_0x88f5('0x19')](function(){_0x17f1f8[_0x88f5('0x10')](0xcc)[_0x88f5('0x11')]();});}};}function handleEntityNotFound(_0x5e513e){return function(_0x51c3b1){if(!_0x51c3b1){_0x5e513e['sendStatus'](0x194);}return _0x51c3b1;};}function handleError(_0x557a96,_0x3708e2){_0x3708e2=_0x3708e2||0x1f4;return function(_0x3c423e){logger['error'](_0x3c423e[_0x88f5('0x1b')]);if(_0x3c423e[_0x88f5('0x1c')]){delete _0x3c423e[_0x88f5('0x1c')];}_0x557a96['status'](_0x3708e2)[_0x88f5('0x1d')](_0x3c423e);};}exports[_0x88f5('0x1e')]=function(_0x2842a8,_0x14ee06){var _0x539447={};var _0x14f159=_[_0x88f5('0x1f')](db[_0x88f5('0x20')][_0x88f5('0x21')],function(_0x3bdd2d){return{'name':_0x3bdd2d[_0x88f5('0x22')],'type':_0x3bdd2d[_0x88f5('0x23')][_0x88f5('0x24')]};});_0x539447=qs[_0x88f5('0x25')](_0x14f159,_0x2842a8);return interaction[_0x88f5('0x26')]('whatsapp',_0x2842a8[_0x88f5('0x27')],_0x2842a8['user'],_0x539447,_0x14f159)[_0x88f5('0x19')](respondWithFilteredResult(_0x14ee06,_0x539447))[_0x88f5('0x28')](handleError(_0x14ee06,null));};exports[_0x88f5('0x29')]=function(_0x1b7e66,_0x535daa){var _0x1209a4={'raw':![],'where':{'id':_0x1b7e66[_0x88f5('0x2a')]['id']}},_0x218bd4={};_0x218bd4[_0x88f5('0x2b')]=_['keys'](db['WhatsappInteraction'][_0x88f5('0x21')]);_0x218bd4[_0x88f5('0x27')]=_[_0x88f5('0x2c')](_0x1b7e66[_0x88f5('0x27')]);_0x218bd4[_0x88f5('0x2d')]=_[_0x88f5('0x2e')](_0x218bd4[_0x88f5('0x2b')],_0x218bd4[_0x88f5('0x27')]);_0x1209a4[_0x88f5('0x2f')]=_[_0x88f5('0x2e')](_0x218bd4['model'],qs[_0x88f5('0x30')](_0x1b7e66[_0x88f5('0x27')][_0x88f5('0x30')]));_0x1209a4[_0x88f5('0x2f')]=_0x1209a4[_0x88f5('0x2f')]['length']?_0x1209a4['attributes']:_0x218bd4[_0x88f5('0x2b')];if(_0x1b7e66[_0x88f5('0x27')][_0x88f5('0x31')]){_0x1209a4[_0x88f5('0x32')]=[{'all':!![]}];}_0x1209a4=_[_0x88f5('0x33')]({},_0x1209a4,_0x1b7e66['options']);return db[_0x88f5('0x20')][_0x88f5('0x34')](_0x1209a4)['then'](handleEntityNotFound(_0x535daa,null))[_0x88f5('0x19')](respondWithResult(_0x535daa,null))['catch'](handleError(_0x535daa,null));};exports[_0x88f5('0x35')]=function(_0x4bfdf6,_0x5a0639){return db[_0x88f5('0x20')][_0x88f5('0x35')](_0x4bfdf6['body'],{})[_0x88f5('0x19')](respondWithResult(_0x5a0639,0xc9))[_0x88f5('0x28')](handleError(_0x5a0639,null));};exports['update']=function(_0x59cc70,_0x7d017a){if(_0x59cc70[_0x88f5('0x36')]['id']){delete _0x59cc70['body']['id'];}return db[_0x88f5('0x20')][_0x88f5('0x34')]({'where':{'id':_0x59cc70[_0x88f5('0x2a')]['id']}})[_0x88f5('0x19')](handleEntityNotFound(_0x7d017a,null))[_0x88f5('0x19')](saveUpdates(_0x59cc70['body'],null))[_0x88f5('0x19')](respondWithResult(_0x7d017a,null))[_0x88f5('0x28')](handleError(_0x7d017a,null));};exports[_0x88f5('0x1a')]=function(_0x58d810,_0x3944ac){return db[_0x88f5('0x20')]['find']({'where':{'id':_0x58d810[_0x88f5('0x2a')]['id']}})['then'](handleEntityNotFound(_0x3944ac,null))[_0x88f5('0x19')](removeEntity(_0x3944ac,null))[_0x88f5('0x28')](handleError(_0x3944ac,null));};exports['describe']=function(_0x5c226a,_0x2d0632){return db[_0x88f5('0x20')][_0x88f5('0x37')]()[_0x88f5('0x19')](respondWithResult(_0x2d0632,null))[_0x88f5('0x28')](handleError(_0x2d0632,null));};exports[_0x88f5('0x38')]=function(_0x353f11,_0x369380){return db['WhatsappMessage']['find']({'where':{'id':_0x353f11['params']['id']}})[_0x88f5('0x19')](handleEntityNotFound(_0x369380,null))[_0x88f5('0x19')](function(_0x20dc21){if(_0x20dc21){return _0x20dc21[_0x88f5('0x38')](_0x353f11[_0x88f5('0x36')][_0x88f5('0x39')],_[_0x88f5('0x3a')](_0x353f11[_0x88f5('0x36')],[_0x88f5('0x39'),'id'])||{});}})[_0x88f5('0x19')](respondWithResult(_0x369380,null))[_0x88f5('0x28')](handleError(_0x369380,null));};exports['getMessages']=function(_0x2cd402,_0x2a0b0c){var _0x4625ea={'raw':![],'where':{}};var _0x2cc0f8={};var _0x4725e9={'count':0x0,'rows':[]};return db['WhatsappInteraction'][_0x88f5('0x3b')]({'where':{'id':_0x2cd402['params']['id']}})[_0x88f5('0x19')](handleEntityNotFound(_0x2a0b0c,null))[_0x88f5('0x19')](function(_0x573c29){if(_0x573c29){_0x2cc0f8[_0x88f5('0x2b')]=_['keys'](db[_0x88f5('0x3c')][_0x88f5('0x21')]);_0x2cc0f8[_0x88f5('0x27')]=_['keys'](_0x2cd402[_0x88f5('0x27')]);_0x2cc0f8[_0x88f5('0x2d')]=_[_0x88f5('0x2e')](_0x2cc0f8[_0x88f5('0x2b')],_0x2cc0f8[_0x88f5('0x27')]);_0x4625ea[_0x88f5('0x2f')]=_[_0x88f5('0x2e')](_0x2cc0f8[_0x88f5('0x2b')],qs[_0x88f5('0x30')](_0x2cd402[_0x88f5('0x27')]['fields']));_0x4625ea[_0x88f5('0x2f')]=_0x4625ea[_0x88f5('0x2f')][_0x88f5('0x3d')]?_0x4625ea['attributes']:_0x2cc0f8[_0x88f5('0x2b')];if(!_0x2cd402[_0x88f5('0x27')][_0x88f5('0x3e')](_0x88f5('0x3f'))){_0x4625ea[_0x88f5('0x14')]=qs[_0x88f5('0x14')](_0x2cd402['query']['limit']);_0x4625ea[_0x88f5('0x13')]=qs[_0x88f5('0x13')](_0x2cd402[_0x88f5('0x27')][_0x88f5('0x13')]);}_0x4625ea['order']=qs[_0x88f5('0x40')](_0x2cd402['query']['sort']);_0x4625ea['where']=qs[_0x88f5('0x2d')](_[_0x88f5('0x41')](_0x2cd402[_0x88f5('0x27')],_0x2cc0f8[_0x88f5('0x2d')]));_0x4625ea[_0x88f5('0x42')]['WhatsappInteractionId']=_0x573c29['id'];if(_0x2cd402[_0x88f5('0x27')][_0x88f5('0x43')]){_0x4625ea['where']=_['merge'](_0x4625ea['where'],{'$or':_[_0x88f5('0x1f')](_0x4625ea[_0x88f5('0x2f')],function(_0x1b3314){var _0x3f0ce8={};_0x3f0ce8[_0x1b3314]={'$like':'%'+_0x2cd402[_0x88f5('0x27')][_0x88f5('0x43')]+'%'};return _0x3f0ce8;})});}if(_0x2cd402[_0x88f5('0x27')][_0x88f5('0x44')]){var _0x556c3d=_0x2cd402[_0x88f5('0x27')][_0x88f5('0x44')][_0x88f5('0x45')](',');var _0x5270c6={};_0x5270c6[_0x556c3d[0x0]]={'$gte':moment(_0x556c3d[0x1])[_0x88f5('0x46')](_0x88f5('0x47'))};_0x4625ea[_0x88f5('0x42')]=_[_0x88f5('0x33')](_0x4625ea[_0x88f5('0x42')],_0x5270c6);}_0x4625ea=_['merge']({},_0x4625ea,_0x2cd402[_0x88f5('0x48')]);return db[_0x88f5('0x3c')][_0x88f5('0x16')]({'where':_0x4625ea[_0x88f5('0x42')]})[_0x88f5('0x19')](function(_0x421da3){_0x4725e9[_0x88f5('0x16')]=_0x421da3;if(_0x2cd402['query']['includeAll']){_0x4625ea[_0x88f5('0x32')]=[{'all':!![]}];}return db[_0x88f5('0x3c')]['findAll'](_0x4625ea);})[_0x88f5('0x19')](function(_0x3d974d){_0x4725e9['rows']=_0x3d974d;return _0x4725e9;});}})[_0x88f5('0x19')](respondWithFilteredResult(_0x2a0b0c,_0x4625ea))['catch'](handleError(_0x2a0b0c,null));};exports[_0x88f5('0x49')]=function(_0x575709,_0x31f77e){return db['WhatsappInteraction'][_0x88f5('0x34')]({'where':{'id':_0x575709[_0x88f5('0x2a')]['id']}})['then'](handleEntityNotFound(_0x31f77e,null))[_0x88f5('0x19')](function(_0x174d44){if(_0x174d44){return _0x174d44[_0x88f5('0x4a')](_0x575709[_0x88f5('0x36')][_0x88f5('0x39')],_[_0x88f5('0x3a')](_0x575709[_0x88f5('0x36')],['ids','id'])||{})[_0x88f5('0x4b')](function(){return db[_0x88f5('0x4c')][_0x88f5('0x26')]({'attributes':['id',_0x88f5('0x1c'),'color'],'where':{'id':_0x575709[_0x88f5('0x36')][_0x88f5('0x39')]}});})[_0x88f5('0x19')](function(_0x188844){socket[_0x88f5('0x4d')](_0x88f5('0x4e'),{'id':Number(_0x575709[_0x88f5('0x2a')]['id']),'tags':_0x188844||[]});return{'id':Number(_0x575709[_0x88f5('0x2a')]['id']),'tags':_0x188844||[]};});}})[_0x88f5('0x19')](respondWithResult(_0x31f77e,null))[_0x88f5('0x28')](handleError(_0x31f77e,null));};exports[_0x88f5('0x4f')]=function(_0x1f1fef,_0x2e2492){return db['WhatsappInteraction']['find']({'where':{'id':_0x1f1fef[_0x88f5('0x2a')]['id']}})[_0x88f5('0x19')](handleEntityNotFound(_0x2e2492,null))[_0x88f5('0x19')](function(_0xc8e854){if(_0xc8e854){return _0xc8e854['removeTags'](_0x1f1fef['query']['ids']);}})[_0x88f5('0x19')](respondWithStatusCode(_0x2e2492,null))[_0x88f5('0x28')](handleError(_0x2e2492,null));};exports['download']=function(_0x22436c,_0x235b09){var _0x4802d1=moment()[_0x88f5('0x50')]()[_0x88f5('0x51')]();var _0x2b8b1d=path['join'](config['root'],_0x88f5('0x52'),_0x88f5('0x53'),_0x88f5('0x54'));var _0x422531=path[_0x88f5('0x55')](config[_0x88f5('0x56')],_0x88f5('0x52'),_0x88f5('0x53'),_0x88f5('0x57'));var _0x17ca79=path[_0x88f5('0x55')](_0x2b8b1d,_0x4802d1);var _0x49fa42=util['format'](_0x88f5('0x58'),_0x22436c[_0x88f5('0x2a')]['id'],_0x4802d1);var _0x184d95=path['join'](_0x2b8b1d,_0x49fa42);var _0x1bca68=[];_0x1bca68[_0x88f5('0x59')]({'model':db[_0x88f5('0x5a')],'as':'User','attributes':['id',_0x88f5('0x5b')],'raw':!![]});_0x1bca68['push']({'model':db['CmContact'],'as':_0x88f5('0x5c'),'attributes':['id',_0x88f5('0x5d'),_0x88f5('0x5e')],'raw':!![]});if(_0x22436c['query'][_0x88f5('0x57')]){_0x1bca68[_0x88f5('0x59')]({'model':db[_0x88f5('0x5f')],'as':_0x88f5('0x5f'),'raw':!![]});}var _0x4ccab9=[{'model':db[_0x88f5('0x3c')],'as':_0x88f5('0x60'),'attributes':['id',_0x88f5('0x36'),'createdAt',_0x88f5('0x61'),_0x88f5('0x62'),_0x88f5('0x63')],'include':_0x1bca68}];_0x4ccab9['push']({'model':db[_0x88f5('0x64')],'as':'Account','attributes':['id',_0x88f5('0x1c')]});_0x4ccab9['push']({'model':db[_0x88f5('0x5a')],'as':_0x88f5('0x65'),'attributes':['id','fullname']});return db[_0x88f5('0x20')]['find']({'where':{'id':_0x22436c[_0x88f5('0x2a')]['id']},'include':_0x4ccab9})[_0x88f5('0x19')](handleEntityNotFound(_0x235b09,null))[_0x88f5('0x19')](function(_0x1f263e){if(_0x1f263e){var _0x2f9a5b=_0x1f263e[_0x88f5('0x66')]({'plain':!![]});fs[_0x88f5('0x67')](_0x17ca79);var _0x171cda={'channel':_0x88f5('0x68'),'account':_0x2f9a5b[_0x88f5('0x69')]['name'],'agent':_0x2f9a5b[_0x88f5('0x65')]?_0x2f9a5b['Owner']['fullname']:_0x88f5('0x6a'),'createdAt':moment(_0x2f9a5b[_0x88f5('0x6b')])[_0x88f5('0x46')](_0x88f5('0x47'))['toString'](),'closedAt':_0x2f9a5b['closed']?moment(_0x2f9a5b[_0x88f5('0x6c')])[_0x88f5('0x46')]('YYYY-MM-DD\x20HH:mm:ss')[_0x88f5('0x51')]():'','disposition':_0x2f9a5b['disposition'],'messages':_(_0x2f9a5b['Messages'])['filter']({'secret':![]})['map'](function(_0x5baefa){if(_0x5baefa['Attachment']){var _0x31ff36=path[_0x88f5('0x55')](_0x422531,_0x5baefa[_0x88f5('0x5f')][_0x88f5('0x6d')]);if(fs[_0x88f5('0x6e')](_0x31ff36)){fs['createReadStream'](_0x31ff36)[_0x88f5('0x6f')](fs['createWriteStream'](path[_0x88f5('0x55')](_0x17ca79,_0x5baefa['Attachment'][_0x88f5('0x6d')])));}}return{'date':moment(_0x5baefa['createdAt'])[_0x88f5('0x46')]('YYYY-MM-DD\x20HH:mm:ss')[_0x88f5('0x51')](),'sender':_0x5baefa['direction']===_0x88f5('0x70')?_0x5baefa[_0x88f5('0x5a')]?_0x5baefa[_0x88f5('0x5a')][_0x88f5('0x5b')]:_0x88f5('0x71'):_0x5baefa[_0x88f5('0x5c')][_0x88f5('0x5d')]+(_0x5baefa[_0x88f5('0x5c')]['lastName']?'\x20'+_0x5baefa[_0x88f5('0x5c')][_0x88f5('0x5e')]:''),'body':_0x5baefa['Attachment']?_0x5baefa[_0x88f5('0x5f')][_0x88f5('0x6d')]:_0x5baefa['body'],'direction':_0x5baefa[_0x88f5('0x61')]==='out'?'A':'C','secret':_0x5baefa[_0x88f5('0x62')],'read':_0x5baefa[_0x88f5('0x63')],'attachment':_0x5baefa['Attachment']};})[_0x88f5('0x72')]()};return ejs[_0x88f5('0x73')](path['join'](config['root'],_0x88f5('0x52'),'views','downloadInteraction.ejs'),{'interaction':_0x171cda})[_0x88f5('0x19')](function(_0x8aaebd){var _0x5714e6=path[_0x88f5('0x55')](_0x17ca79,_0x88f5('0x74')+_0x2f9a5b['id']+'-'+_0x4802d1+'.pdf');var _0x13341c={'path':_0x5714e6,'channel':_0x88f5('0x68'),'interactionId':_0x2f9a5b['id']};return pdf[_0x88f5('0x75')](_0x8aaebd,_0x13341c);})[_0x88f5('0x19')](function(){return new BPromise(function(_0x19ea55,_0x36e703){zipdir(_0x17ca79,{'saveTo':_0x184d95},function(_0x2fcbcb,_0xecbac){if(_0x2fcbcb)return _0x36e703(_0x2fcbcb);return _0x19ea55(_0xecbac);});})[_0x88f5('0x19')](function(){return new BPromise(function(_0x14f87e,_0x26582a){rimraf(_0x17ca79,function(_0x586952){if(_0x586952)_0x26582a(_0x586952);return _0x14f87e();});});})[_0x88f5('0x19')](function(){return _0x235b09[_0x88f5('0x76')](_0x184d95,_0x49fa42,function(_0x5c06b7){if(_0x5c06b7){console[_0x88f5('0x77')]('err',_0x5c06b7);}else{fs[_0x88f5('0x78')](_0x184d95);}});});});}else{return _0x235b09[_0x88f5('0xf')](0xc8);}})[_0x88f5('0x28')](handleError(_0x235b09,null));};
\ No newline at end of file