Built motion from commit 10af8726.|2.6.34
[motion2.git] / server / api / whatsappInteraction / whatsappInteraction.controller.js
index c36579b..36629f4 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 _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