Built motion from commit c116dc8b.|2.6.30
[motion2.git] / server / api / whatsappInteraction / whatsappInteraction.controller.js
index bed5bd6..52d0686 100644 (file)
@@ -15,4 +15,4 @@
 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                        *
 // *                                                                       *
 // *************************************************************************
-var _0xfcb2=['includeAll','include','merge','create','update','body','addMessage','WhatsappMessage','find','ids','nolimit','limit','sort','where','WhatsappInteractionId','filter','map','$gte','YYYY-MM-DD\x20HH:mm:ss','options','rows','addTags','setTags','omit','spread','Tag','emit','removeTags','download','unix','toString','join','root','server','files','format','User','fullname','push','CmContact','firstName','attachments','Attachment','Messages','direction','read','WhatsappAccount','Account','Owner','get','mkdirSync','Whatsapp','N.A.','createdAt','closedAt','disposition','createReadStream','pipe','createWriteStream','out','System','Contact','lastName','basename','secret','value','renderFile','views','transcript-','.pdf','createPdfFromHTML','log','err','unlinkSync','rimraf','zip-dir','moment','bluebird','path','ejs','lodash','ioredis','../../components/interaction/service','../../components/parsers/qs','../../components/export/pdf','../../config/logger','api','defaults','redis','localhost','socket.io-emitter','./whatsappInteraction.socket','register','sendStatus','status','end','json','offset','count','Content-Range','destroy','then','error','stack','name','index','rawAttributes','fieldName','key','getOptions','findAll','whatsapp','query','catch','show','params','model','keys','WhatsappInteraction','filters','intersection','attributes','fields','length'];(function(_0x260223,_0x1deaf3){var _0x4ee3bb=function(_0x35fc9f){while(--_0x35fc9f){_0x260223['push'](_0x260223['shift']());}};_0x4ee3bb(++_0x1deaf3);}(_0xfcb2,0x1be));var _0x2fcb=function(_0x59884f,_0x266770){_0x59884f=_0x59884f-0x0;var _0xcb7fd1=_0xfcb2[_0x59884f];return _0xcb7fd1;};'use strict';var rimraf=require(_0x2fcb('0x0'));var zipdir=require(_0x2fcb('0x1'));var moment=require(_0x2fcb('0x2'));var BPromise=require(_0x2fcb('0x3'));var util=require('util');var path=require(_0x2fcb('0x4'));var ejs=require(_0x2fcb('0x5'));var fs=require('fs');var _=require(_0x2fcb('0x6'));var Redis=require(_0x2fcb('0x7'));var interaction=require(_0x2fcb('0x8'));var qs=require(_0x2fcb('0x9'));var pdf=require(_0x2fcb('0xa'));var logger=require(_0x2fcb('0xb'))(_0x2fcb('0xc'));var config=require('../../config/environment');var db=require('../../mysqldb')['db'];config['redis']=_[_0x2fcb('0xd')](config[_0x2fcb('0xe')],{'host':_0x2fcb('0xf'),'port':0x18eb});var socket=require(_0x2fcb('0x10'))(new Redis(config['redis']));require(_0x2fcb('0x11'))[_0x2fcb('0x12')](socket);function respondWithStatusCode(_0x444bcb,_0x2172ca){_0x2172ca=_0x2172ca||0xcc;return function(_0x1ffbdd){if(_0x1ffbdd){return _0x444bcb[_0x2fcb('0x13')](_0x2172ca);}return _0x444bcb[_0x2fcb('0x14')](_0x2172ca)[_0x2fcb('0x15')]();};}function respondWithResult(_0x3c47d9,_0x3284dc){_0x3284dc=_0x3284dc||0xc8;return function(_0x38f93c){if(_0x38f93c){return _0x3c47d9[_0x2fcb('0x14')](_0x3284dc)[_0x2fcb('0x16')](_0x38f93c);}};}function respondWithFilteredResult(_0x559a5d,_0x5d94f1){return function(_0x52aae4){if(_0x52aae4){var _0xc68a4e=typeof _0x5d94f1[_0x2fcb('0x17')]==='undefined'&&typeof _0x5d94f1['limit']==='undefined';var _0xb42905=_0x52aae4['count'];var _0x48c2bf=_0xc68a4e?0x0:_0x5d94f1[_0x2fcb('0x17')];var _0x65eb40=_0xc68a4e?_0x52aae4[_0x2fcb('0x18')]:_0x5d94f1[_0x2fcb('0x17')]+_0x5d94f1['limit'];var _0x16deb6;if(_0x65eb40>=_0xb42905){_0x65eb40=_0xb42905;_0x16deb6=0xc8;}else{_0x16deb6=0xce;}_0x559a5d[_0x2fcb('0x14')](_0x16deb6);return _0x559a5d['set'](_0x2fcb('0x19'),_0x48c2bf+'-'+_0x65eb40+'/'+_0xb42905)[_0x2fcb('0x16')](_0x52aae4);}return null;};}function saveUpdates(_0x3f7bdb){return function(_0x8d4f80){if(_0x8d4f80){return _0x8d4f80['update'](_0x3f7bdb)['then'](function(_0x35e0ce){return _0x35e0ce;});}return null;};}function removeEntity(_0x5a1c0c){return function(_0xb74aa7){if(_0xb74aa7){return _0xb74aa7[_0x2fcb('0x1a')]()[_0x2fcb('0x1b')](function(){_0x5a1c0c[_0x2fcb('0x14')](0xcc)[_0x2fcb('0x15')]();});}};}function handleEntityNotFound(_0x15a828){return function(_0xd37b0f){if(!_0xd37b0f){_0x15a828['sendStatus'](0x194);}return _0xd37b0f;};}function handleError(_0x238cfd,_0x52245e){_0x52245e=_0x52245e||0x1f4;return function(_0x43c311){logger[_0x2fcb('0x1c')](_0x43c311[_0x2fcb('0x1d')]);if(_0x43c311[_0x2fcb('0x1e')]){delete _0x43c311[_0x2fcb('0x1e')];}_0x238cfd[_0x2fcb('0x14')](_0x52245e)['send'](_0x43c311);};}exports[_0x2fcb('0x1f')]=function(_0x57f475,_0x250d8e){var _0x175c23={};var _0x40bdf7=_['map'](db['WhatsappInteraction'][_0x2fcb('0x20')],function(_0x4dbcaa){return{'name':_0x4dbcaa[_0x2fcb('0x21')],'type':_0x4dbcaa['type'][_0x2fcb('0x22')]};});_0x175c23=qs[_0x2fcb('0x23')](_0x40bdf7,_0x57f475);return interaction[_0x2fcb('0x24')](_0x2fcb('0x25'),_0x57f475[_0x2fcb('0x26')],_0x57f475['user'],_0x175c23,_0x40bdf7)[_0x2fcb('0x1b')](respondWithFilteredResult(_0x250d8e,_0x175c23))[_0x2fcb('0x27')](handleError(_0x250d8e,null));};exports[_0x2fcb('0x28')]=function(_0x4c772d,_0x5cde93){var _0xe8e05={'raw':![],'where':{'id':_0x4c772d[_0x2fcb('0x29')]['id']}},_0x2e8e03={};_0x2e8e03[_0x2fcb('0x2a')]=_[_0x2fcb('0x2b')](db[_0x2fcb('0x2c')][_0x2fcb('0x20')]);_0x2e8e03[_0x2fcb('0x26')]=_['keys'](_0x4c772d[_0x2fcb('0x26')]);_0x2e8e03[_0x2fcb('0x2d')]=_[_0x2fcb('0x2e')](_0x2e8e03[_0x2fcb('0x2a')],_0x2e8e03[_0x2fcb('0x26')]);_0xe8e05[_0x2fcb('0x2f')]=_[_0x2fcb('0x2e')](_0x2e8e03[_0x2fcb('0x2a')],qs[_0x2fcb('0x30')](_0x4c772d[_0x2fcb('0x26')][_0x2fcb('0x30')]));_0xe8e05[_0x2fcb('0x2f')]=_0xe8e05[_0x2fcb('0x2f')][_0x2fcb('0x31')]?_0xe8e05[_0x2fcb('0x2f')]:_0x2e8e03['model'];if(_0x4c772d[_0x2fcb('0x26')][_0x2fcb('0x32')]){_0xe8e05[_0x2fcb('0x33')]=[{'all':!![]}];}_0xe8e05=_[_0x2fcb('0x34')]({},_0xe8e05,_0x4c772d['options']);return db[_0x2fcb('0x2c')]['find'](_0xe8e05)[_0x2fcb('0x1b')](handleEntityNotFound(_0x5cde93,null))[_0x2fcb('0x1b')](respondWithResult(_0x5cde93,null))['catch'](handleError(_0x5cde93,null));};exports[_0x2fcb('0x35')]=function(_0x32d0ba,_0x1fbcb5){return db[_0x2fcb('0x2c')][_0x2fcb('0x35')](_0x32d0ba['body'],{})['then'](respondWithResult(_0x1fbcb5,0xc9))[_0x2fcb('0x27')](handleError(_0x1fbcb5,null));};exports[_0x2fcb('0x36')]=function(_0x433b7c,_0xe96131){if(_0x433b7c[_0x2fcb('0x37')]['id']){delete _0x433b7c['body']['id'];}return db['WhatsappInteraction']['find']({'where':{'id':_0x433b7c['params']['id']}})[_0x2fcb('0x1b')](handleEntityNotFound(_0xe96131,null))[_0x2fcb('0x1b')](saveUpdates(_0x433b7c[_0x2fcb('0x37')],null))[_0x2fcb('0x1b')](respondWithResult(_0xe96131,null))[_0x2fcb('0x27')](handleError(_0xe96131,null));};exports['destroy']=function(_0x9380a5,_0x4235cf){return db['WhatsappInteraction']['find']({'where':{'id':_0x9380a5['params']['id']}})[_0x2fcb('0x1b')](handleEntityNotFound(_0x4235cf,null))[_0x2fcb('0x1b')](removeEntity(_0x4235cf,null))['catch'](handleError(_0x4235cf,null));};exports['describe']=function(_0x1bb594,_0x568ac6){return db[_0x2fcb('0x2c')]['describe']()['then'](respondWithResult(_0x568ac6,null))[_0x2fcb('0x27')](handleError(_0x568ac6,null));};exports[_0x2fcb('0x38')]=function(_0x49a192,_0x7204e2){return db[_0x2fcb('0x39')][_0x2fcb('0x3a')]({'where':{'id':_0x49a192[_0x2fcb('0x29')]['id']}})[_0x2fcb('0x1b')](handleEntityNotFound(_0x7204e2,null))[_0x2fcb('0x1b')](function(_0x4e0a84){if(_0x4e0a84){return _0x4e0a84[_0x2fcb('0x38')](_0x49a192[_0x2fcb('0x37')][_0x2fcb('0x3b')],_['omit'](_0x49a192['body'],['ids','id'])||{});}})['then'](respondWithResult(_0x7204e2,null))[_0x2fcb('0x27')](handleError(_0x7204e2,null));};exports['getMessages']=function(_0x36d09f,_0x3e78f0){var _0x18680f={'raw':![],'where':{}};var _0x1e033f={};var _0x5028a0={'count':0x0,'rows':[]};return db['WhatsappInteraction']['findOne']({'where':{'id':_0x36d09f['params']['id']}})[_0x2fcb('0x1b')](handleEntityNotFound(_0x3e78f0,null))[_0x2fcb('0x1b')](function(_0x1fc275){if(_0x1fc275){_0x1e033f[_0x2fcb('0x2a')]=_['keys'](db[_0x2fcb('0x39')]['rawAttributes']);_0x1e033f['query']=_[_0x2fcb('0x2b')](_0x36d09f[_0x2fcb('0x26')]);_0x1e033f[_0x2fcb('0x2d')]=_['intersection'](_0x1e033f[_0x2fcb('0x2a')],_0x1e033f['query']);_0x18680f[_0x2fcb('0x2f')]=_[_0x2fcb('0x2e')](_0x1e033f[_0x2fcb('0x2a')],qs[_0x2fcb('0x30')](_0x36d09f[_0x2fcb('0x26')]['fields']));_0x18680f[_0x2fcb('0x2f')]=_0x18680f[_0x2fcb('0x2f')][_0x2fcb('0x31')]?_0x18680f['attributes']:_0x1e033f[_0x2fcb('0x2a')];if(!_0x36d09f[_0x2fcb('0x26')]['hasOwnProperty'](_0x2fcb('0x3c'))){_0x18680f[_0x2fcb('0x3d')]=qs[_0x2fcb('0x3d')](_0x36d09f['query'][_0x2fcb('0x3d')]);_0x18680f['offset']=qs[_0x2fcb('0x17')](_0x36d09f[_0x2fcb('0x26')][_0x2fcb('0x17')]);}_0x18680f['order']=qs['sort'](_0x36d09f[_0x2fcb('0x26')][_0x2fcb('0x3e')]);_0x18680f['where']=qs['filters'](_['pick'](_0x36d09f[_0x2fcb('0x26')],_0x1e033f['filters']));_0x18680f[_0x2fcb('0x3f')][_0x2fcb('0x40')]=_0x1fc275['id'];if(_0x36d09f['query'][_0x2fcb('0x41')]){_0x18680f[_0x2fcb('0x3f')]=_[_0x2fcb('0x34')](_0x18680f[_0x2fcb('0x3f')],{'$or':_[_0x2fcb('0x42')](_0x18680f[_0x2fcb('0x2f')],function(_0x2d6451){var _0x38bb9c={};_0x38bb9c[_0x2d6451]={'$like':'%'+_0x36d09f[_0x2fcb('0x26')][_0x2fcb('0x41')]+'%'};return _0x38bb9c;})});}if(_0x36d09f['query'][_0x2fcb('0x43')]){var _0x1b83b2=_0x36d09f[_0x2fcb('0x26')][_0x2fcb('0x43')]['split'](',');var _0x33d4fd={};_0x33d4fd[_0x1b83b2[0x0]]={'$gte':moment(_0x1b83b2[0x1])['format'](_0x2fcb('0x44'))};_0x18680f[_0x2fcb('0x3f')]=_['merge'](_0x18680f[_0x2fcb('0x3f')],_0x33d4fd);}_0x18680f=_[_0x2fcb('0x34')]({},_0x18680f,_0x36d09f[_0x2fcb('0x45')]);return db['WhatsappMessage']['count']({'where':_0x18680f[_0x2fcb('0x3f')]})[_0x2fcb('0x1b')](function(_0x4d6ce8){_0x5028a0[_0x2fcb('0x18')]=_0x4d6ce8;if(_0x36d09f['query'][_0x2fcb('0x32')]){_0x18680f[_0x2fcb('0x33')]=[{'all':!![]}];}return db[_0x2fcb('0x39')][_0x2fcb('0x24')](_0x18680f);})[_0x2fcb('0x1b')](function(_0x4badc9){_0x5028a0[_0x2fcb('0x46')]=_0x4badc9;return _0x5028a0;});}})[_0x2fcb('0x1b')](respondWithFilteredResult(_0x3e78f0,_0x18680f))['catch'](handleError(_0x3e78f0,null));};exports[_0x2fcb('0x47')]=function(_0x107023,_0x1e0562){return db[_0x2fcb('0x2c')]['find']({'where':{'id':_0x107023['params']['id']}})['then'](handleEntityNotFound(_0x1e0562,null))['then'](function(_0x579f51){if(_0x579f51){return _0x579f51[_0x2fcb('0x48')](_0x107023[_0x2fcb('0x37')]['ids'],_[_0x2fcb('0x49')](_0x107023['body'],[_0x2fcb('0x3b'),'id'])||{})[_0x2fcb('0x4a')](function(){return db[_0x2fcb('0x4b')]['findAll']({'attributes':['id','name','color'],'where':{'id':_0x107023['body']['ids']}});})[_0x2fcb('0x1b')](function(_0x12a3ca){socket[_0x2fcb('0x4c')]('whatsappInteractionTags:save',{'id':Number(_0x107023['params']['id']),'tags':_0x12a3ca||[]});return{'id':Number(_0x107023[_0x2fcb('0x29')]['id']),'tags':_0x12a3ca||[]};});}})[_0x2fcb('0x1b')](respondWithResult(_0x1e0562,null))[_0x2fcb('0x27')](handleError(_0x1e0562,null));};exports['removeTags']=function(_0x3b7d70,_0xf1f56){return db['WhatsappInteraction']['find']({'where':{'id':_0x3b7d70[_0x2fcb('0x29')]['id']}})[_0x2fcb('0x1b')](handleEntityNotFound(_0xf1f56,null))[_0x2fcb('0x1b')](function(_0xcd88d5){if(_0xcd88d5){return _0xcd88d5[_0x2fcb('0x4d')](_0x3b7d70[_0x2fcb('0x26')][_0x2fcb('0x3b')]);}})[_0x2fcb('0x1b')](respondWithStatusCode(_0xf1f56,null))[_0x2fcb('0x27')](handleError(_0xf1f56,null));};exports[_0x2fcb('0x4e')]=function(_0x54322e,_0x22d931){var _0x528a79=moment()[_0x2fcb('0x4f')]()[_0x2fcb('0x50')]();var _0x4e1e97=path[_0x2fcb('0x51')](config[_0x2fcb('0x52')],_0x2fcb('0x53'),_0x2fcb('0x54'),'tmp');var _0x3a40e5=path[_0x2fcb('0x51')](config[_0x2fcb('0x52')],_0x2fcb('0x53'),_0x2fcb('0x54'),'attachments');var _0x5847a5=path['join'](_0x4e1e97,_0x528a79);var _0xdc0e61=util[_0x2fcb('0x55')]('transcript-%d-%s.zip',_0x54322e[_0x2fcb('0x29')]['id'],_0x528a79);var _0x2bc744=path[_0x2fcb('0x51')](_0x4e1e97,_0xdc0e61);var _0x1a2014=[];_0x1a2014['push']({'model':db[_0x2fcb('0x56')],'as':'User','attributes':['id',_0x2fcb('0x57')],'raw':!![]});_0x1a2014[_0x2fcb('0x58')]({'model':db[_0x2fcb('0x59')],'as':'Contact','attributes':['id',_0x2fcb('0x5a'),'lastName'],'raw':!![]});if(_0x54322e[_0x2fcb('0x26')][_0x2fcb('0x5b')]){_0x1a2014['push']({'model':db['Attachment'],'as':_0x2fcb('0x5c'),'raw':!![]});}var _0x5b86ac=[{'model':db[_0x2fcb('0x39')],'as':_0x2fcb('0x5d'),'attributes':['id',_0x2fcb('0x37'),'createdAt',_0x2fcb('0x5e'),'secret',_0x2fcb('0x5f')],'include':_0x1a2014}];_0x5b86ac['push']({'model':db[_0x2fcb('0x60')],'as':_0x2fcb('0x61'),'attributes':['id',_0x2fcb('0x1e')]});_0x5b86ac['push']({'model':db[_0x2fcb('0x56')],'as':_0x2fcb('0x62'),'attributes':['id','fullname']});return db[_0x2fcb('0x2c')][_0x2fcb('0x3a')]({'where':{'id':_0x54322e[_0x2fcb('0x29')]['id']},'include':_0x5b86ac})[_0x2fcb('0x1b')](handleEntityNotFound(_0x22d931,null))[_0x2fcb('0x1b')](function(_0x1bb583){if(_0x1bb583){var _0x3bfa86=_0x1bb583[_0x2fcb('0x63')]({'plain':!![]});fs[_0x2fcb('0x64')](_0x5847a5);var _0x182c4d={'channel':_0x2fcb('0x65'),'account':_0x3bfa86[_0x2fcb('0x61')][_0x2fcb('0x1e')],'agent':_0x3bfa86[_0x2fcb('0x62')]?_0x3bfa86[_0x2fcb('0x62')][_0x2fcb('0x57')]:_0x2fcb('0x66'),'createdAt':moment(_0x3bfa86[_0x2fcb('0x67')])[_0x2fcb('0x55')](_0x2fcb('0x44'))['toString'](),'closedAt':_0x3bfa86['closed']?moment(_0x3bfa86[_0x2fcb('0x68')])['format'](_0x2fcb('0x44'))[_0x2fcb('0x50')]():'','disposition':_0x3bfa86[_0x2fcb('0x69')],'messages':_(_0x3bfa86['Messages'])['filter']({'secret':![]})['map'](function(_0x491c88){if(_0x491c88[_0x2fcb('0x5c')]){var _0xf4c4eb=path[_0x2fcb('0x51')](_0x3a40e5,_0x491c88['Attachment']['basename']);if(fs['existsSync'](_0xf4c4eb)){fs[_0x2fcb('0x6a')](_0xf4c4eb)[_0x2fcb('0x6b')](fs[_0x2fcb('0x6c')](path[_0x2fcb('0x51')](_0x5847a5,_0x491c88[_0x2fcb('0x5c')]['basename'])));}}return{'date':moment(_0x491c88[_0x2fcb('0x67')])[_0x2fcb('0x55')](_0x2fcb('0x44'))[_0x2fcb('0x50')](),'sender':_0x491c88['direction']===_0x2fcb('0x6d')?_0x491c88[_0x2fcb('0x56')]?_0x491c88['User'][_0x2fcb('0x57')]:_0x2fcb('0x6e'):_0x491c88[_0x2fcb('0x6f')][_0x2fcb('0x5a')]+(_0x491c88[_0x2fcb('0x6f')][_0x2fcb('0x70')]?'\x20'+_0x491c88[_0x2fcb('0x6f')][_0x2fcb('0x70')]:''),'body':_0x491c88[_0x2fcb('0x5c')]?_0x491c88[_0x2fcb('0x5c')][_0x2fcb('0x71')]:_0x491c88['body'],'direction':_0x491c88[_0x2fcb('0x5e')]===_0x2fcb('0x6d')?'A':'C','secret':_0x491c88[_0x2fcb('0x72')],'read':_0x491c88[_0x2fcb('0x5f')],'attachment':_0x491c88[_0x2fcb('0x5c')]};})[_0x2fcb('0x73')]()};return ejs[_0x2fcb('0x74')](path[_0x2fcb('0x51')](config[_0x2fcb('0x52')],_0x2fcb('0x53'),_0x2fcb('0x75'),'downloadInteraction.ejs'),{'interaction':_0x182c4d})[_0x2fcb('0x1b')](function(_0x28d5ec){var _0x5f0035=path[_0x2fcb('0x51')](_0x5847a5,_0x2fcb('0x76')+_0x3bfa86['id']+'-'+_0x528a79+_0x2fcb('0x77'));var _0x2a4091={'path':_0x5f0035,'channel':_0x2fcb('0x65'),'interactionId':_0x3bfa86['id']};return pdf[_0x2fcb('0x78')](_0x28d5ec,_0x2a4091);})[_0x2fcb('0x1b')](function(){return new BPromise(function(_0x576c1b,_0xf3d19){zipdir(_0x5847a5,{'saveTo':_0x2bc744},function(_0xabe564,_0x56d876){if(_0xabe564)return _0xf3d19(_0xabe564);return _0x576c1b(_0x56d876);});})[_0x2fcb('0x1b')](function(){return new BPromise(function(_0x17b10c,_0x3cde11){rimraf(_0x5847a5,function(_0x5b19d8){if(_0x5b19d8)_0x3cde11(_0x5b19d8);return _0x17b10c();});});})[_0x2fcb('0x1b')](function(){return _0x22d931['download'](_0x2bc744,_0xdc0e61,function(_0x2e1a1d){if(_0x2e1a1d){console[_0x2fcb('0x79')](_0x2fcb('0x7a'),_0x2e1a1d);}else{fs[_0x2fcb('0x7b')](_0x2bc744);}});});});}else{return _0x22d931['sendStatus'](0xc8);}})[_0x2fcb('0x27')](handleError(_0x22d931,null));};
\ No newline at end of file
+var _0x4d4a=['CmContact','lastName','Attachment','Messages','createdAt','direction','secret','WhatsappAccount','Account','Owner','fullname','get','mkdirSync','Whatsapp','N.A.','closed','closedAt','existsSync','createReadStream','createWriteStream','basename','out','System','Contact','firstName','read','renderFile','views','downloadInteraction.ejs','.pdf','createPdfFromHTML','log','err','unlinkSync','zip-dir','moment','bluebird','util','path','../../components/interaction/service','../../components/parsers/qs','../../components/export/pdf','api','../../config/environment','../../mysqldb','redis','localhost','socket.io-emitter','./whatsappInteraction.socket','register','sendStatus','status','end','json','undefined','limit','count','offset','set','Content-Range','update','then','error','stack','name','send','index','map','WhatsappInteraction','fieldName','type','key','getOptions','findAll','whatsapp','query','user','show','params','keys','filters','intersection','model','attributes','fields','includeAll','options','find','catch','create','body','destroy','describe','addMessage','WhatsappMessage','ids','findOne','hasOwnProperty','nolimit','order','sort','where','pick','WhatsappInteractionId','filter','merge','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','include','rows','addTags','setTags','omit','Tag','color','whatsappInteractionTags:save','removeTags','download','unix','toString','join','server','files','root','attachments','push','User'];(function(_0x204a28,_0x1c2f71){var _0x3388a2=function(_0x4836e9){while(--_0x4836e9){_0x204a28['push'](_0x204a28['shift']());}};_0x3388a2(++_0x1c2f71);}(_0x4d4a,0x124));var _0xa4d4=function(_0x413feb,_0x10477f){_0x413feb=_0x413feb-0x0;var _0x40ee88=_0x4d4a[_0x413feb];return _0x40ee88;};'use strict';var rimraf=require('rimraf');var zipdir=require(_0xa4d4('0x0'));var moment=require(_0xa4d4('0x1'));var BPromise=require(_0xa4d4('0x2'));var util=require(_0xa4d4('0x3'));var path=require(_0xa4d4('0x4'));var ejs=require('ejs');var fs=require('fs');var _=require('lodash');var Redis=require('ioredis');var interaction=require(_0xa4d4('0x5'));var qs=require(_0xa4d4('0x6'));var pdf=require(_0xa4d4('0x7'));var logger=require('../../config/logger')(_0xa4d4('0x8'));var config=require(_0xa4d4('0x9'));var db=require(_0xa4d4('0xa'))['db'];config[_0xa4d4('0xb')]=_['defaults'](config['redis'],{'host':_0xa4d4('0xc'),'port':0x18eb});var socket=require(_0xa4d4('0xd'))(new Redis(config[_0xa4d4('0xb')]));require(_0xa4d4('0xe'))[_0xa4d4('0xf')](socket);function respondWithStatusCode(_0x33c1bf,_0x4b914b){_0x4b914b=_0x4b914b||0xcc;return function(_0x26c167){if(_0x26c167){return _0x33c1bf[_0xa4d4('0x10')](_0x4b914b);}return _0x33c1bf[_0xa4d4('0x11')](_0x4b914b)[_0xa4d4('0x12')]();};}function respondWithResult(_0x2cc6a6,_0x2b212d){_0x2b212d=_0x2b212d||0xc8;return function(_0x5f3f37){if(_0x5f3f37){return _0x2cc6a6[_0xa4d4('0x11')](_0x2b212d)[_0xa4d4('0x13')](_0x5f3f37);}};}function respondWithFilteredResult(_0x1dc46e,_0x192f26){return function(_0x3bb391){if(_0x3bb391){var _0x2e648b=typeof _0x192f26['offset']===_0xa4d4('0x14')&&typeof _0x192f26[_0xa4d4('0x15')]===_0xa4d4('0x14');var _0x170b55=_0x3bb391[_0xa4d4('0x16')];var _0x1326b5=_0x2e648b?0x0:_0x192f26[_0xa4d4('0x17')];var _0x375975=_0x2e648b?_0x3bb391[_0xa4d4('0x16')]:_0x192f26[_0xa4d4('0x17')]+_0x192f26[_0xa4d4('0x15')];var _0x329f88;if(_0x375975>=_0x170b55){_0x375975=_0x170b55;_0x329f88=0xc8;}else{_0x329f88=0xce;}_0x1dc46e[_0xa4d4('0x11')](_0x329f88);return _0x1dc46e[_0xa4d4('0x18')](_0xa4d4('0x19'),_0x1326b5+'-'+_0x375975+'/'+_0x170b55)[_0xa4d4('0x13')](_0x3bb391);}return null;};}function saveUpdates(_0x41c7e6){return function(_0x5515dd){if(_0x5515dd){return _0x5515dd[_0xa4d4('0x1a')](_0x41c7e6)[_0xa4d4('0x1b')](function(_0x1cf1f6){return _0x1cf1f6;});}return null;};}function removeEntity(_0x5e5027){return function(_0x180b37){if(_0x180b37){return _0x180b37['destroy']()[_0xa4d4('0x1b')](function(){_0x5e5027[_0xa4d4('0x11')](0xcc)[_0xa4d4('0x12')]();});}};}function handleEntityNotFound(_0xcc7aea){return function(_0xb7df0f){if(!_0xb7df0f){_0xcc7aea[_0xa4d4('0x10')](0x194);}return _0xb7df0f;};}function handleError(_0x434513,_0x5ab6f1){_0x5ab6f1=_0x5ab6f1||0x1f4;return function(_0x3c024d){logger[_0xa4d4('0x1c')](_0x3c024d[_0xa4d4('0x1d')]);if(_0x3c024d[_0xa4d4('0x1e')]){delete _0x3c024d['name'];}_0x434513['status'](_0x5ab6f1)[_0xa4d4('0x1f')](_0x3c024d);};}exports[_0xa4d4('0x20')]=function(_0xbef48e,_0x3abe5f){var _0x189aa5={};var _0x171756=_[_0xa4d4('0x21')](db[_0xa4d4('0x22')]['rawAttributes'],function(_0x4836ff){return{'name':_0x4836ff[_0xa4d4('0x23')],'type':_0x4836ff[_0xa4d4('0x24')][_0xa4d4('0x25')]};});_0x189aa5=qs[_0xa4d4('0x26')](_0x171756,_0xbef48e);return interaction[_0xa4d4('0x27')](_0xa4d4('0x28'),_0xbef48e[_0xa4d4('0x29')],_0xbef48e[_0xa4d4('0x2a')],_0x189aa5,_0x171756)[_0xa4d4('0x1b')](respondWithFilteredResult(_0x3abe5f,_0x189aa5))['catch'](handleError(_0x3abe5f,null));};exports[_0xa4d4('0x2b')]=function(_0x90c401,_0x1df1e7){var _0x22453d={'raw':![],'where':{'id':_0x90c401[_0xa4d4('0x2c')]['id']}},_0x4fcc13={};_0x4fcc13['model']=_[_0xa4d4('0x2d')](db[_0xa4d4('0x22')]['rawAttributes']);_0x4fcc13['query']=_['keys'](_0x90c401[_0xa4d4('0x29')]);_0x4fcc13[_0xa4d4('0x2e')]=_[_0xa4d4('0x2f')](_0x4fcc13[_0xa4d4('0x30')],_0x4fcc13['query']);_0x22453d[_0xa4d4('0x31')]=_[_0xa4d4('0x2f')](_0x4fcc13[_0xa4d4('0x30')],qs[_0xa4d4('0x32')](_0x90c401[_0xa4d4('0x29')][_0xa4d4('0x32')]));_0x22453d[_0xa4d4('0x31')]=_0x22453d[_0xa4d4('0x31')]['length']?_0x22453d[_0xa4d4('0x31')]:_0x4fcc13[_0xa4d4('0x30')];if(_0x90c401[_0xa4d4('0x29')][_0xa4d4('0x33')]){_0x22453d['include']=[{'all':!![]}];}_0x22453d=_['merge']({},_0x22453d,_0x90c401[_0xa4d4('0x34')]);return db['WhatsappInteraction'][_0xa4d4('0x35')](_0x22453d)[_0xa4d4('0x1b')](handleEntityNotFound(_0x1df1e7,null))[_0xa4d4('0x1b')](respondWithResult(_0x1df1e7,null))[_0xa4d4('0x36')](handleError(_0x1df1e7,null));};exports[_0xa4d4('0x37')]=function(_0x15ba8d,_0x4288de){return db[_0xa4d4('0x22')][_0xa4d4('0x37')](_0x15ba8d[_0xa4d4('0x38')],{})['then'](respondWithResult(_0x4288de,0xc9))[_0xa4d4('0x36')](handleError(_0x4288de,null));};exports['update']=function(_0x2310c2,_0x516443){if(_0x2310c2[_0xa4d4('0x38')]['id']){delete _0x2310c2[_0xa4d4('0x38')]['id'];}return db[_0xa4d4('0x22')][_0xa4d4('0x35')]({'where':{'id':_0x2310c2[_0xa4d4('0x2c')]['id']}})[_0xa4d4('0x1b')](handleEntityNotFound(_0x516443,null))[_0xa4d4('0x1b')](saveUpdates(_0x2310c2['body'],null))[_0xa4d4('0x1b')](respondWithResult(_0x516443,null))[_0xa4d4('0x36')](handleError(_0x516443,null));};exports[_0xa4d4('0x39')]=function(_0x425815,_0x5e0afa){return db[_0xa4d4('0x22')][_0xa4d4('0x35')]({'where':{'id':_0x425815['params']['id']}})[_0xa4d4('0x1b')](handleEntityNotFound(_0x5e0afa,null))[_0xa4d4('0x1b')](removeEntity(_0x5e0afa,null))[_0xa4d4('0x36')](handleError(_0x5e0afa,null));};exports[_0xa4d4('0x3a')]=function(_0xbf9013,_0x47dfb2){return db[_0xa4d4('0x22')][_0xa4d4('0x3a')]()[_0xa4d4('0x1b')](respondWithResult(_0x47dfb2,null))[_0xa4d4('0x36')](handleError(_0x47dfb2,null));};exports[_0xa4d4('0x3b')]=function(_0x5be48d,_0x520ce3){return db[_0xa4d4('0x3c')][_0xa4d4('0x35')]({'where':{'id':_0x5be48d[_0xa4d4('0x2c')]['id']}})[_0xa4d4('0x1b')](handleEntityNotFound(_0x520ce3,null))[_0xa4d4('0x1b')](function(_0x409eb8){if(_0x409eb8){return _0x409eb8[_0xa4d4('0x3b')](_0x5be48d['body'][_0xa4d4('0x3d')],_['omit'](_0x5be48d[_0xa4d4('0x38')],[_0xa4d4('0x3d'),'id'])||{});}})[_0xa4d4('0x1b')](respondWithResult(_0x520ce3,null))[_0xa4d4('0x36')](handleError(_0x520ce3,null));};exports['getMessages']=function(_0x25f6da,_0x1e47cf){var _0x5a46e6={'raw':![],'where':{}};var _0x375811={};var _0x2662e3={'count':0x0,'rows':[]};return db[_0xa4d4('0x22')][_0xa4d4('0x3e')]({'where':{'id':_0x25f6da[_0xa4d4('0x2c')]['id']}})[_0xa4d4('0x1b')](handleEntityNotFound(_0x1e47cf,null))[_0xa4d4('0x1b')](function(_0x54c35c){if(_0x54c35c){_0x375811['model']=_[_0xa4d4('0x2d')](db['WhatsappMessage']['rawAttributes']);_0x375811[_0xa4d4('0x29')]=_['keys'](_0x25f6da[_0xa4d4('0x29')]);_0x375811[_0xa4d4('0x2e')]=_[_0xa4d4('0x2f')](_0x375811['model'],_0x375811[_0xa4d4('0x29')]);_0x5a46e6[_0xa4d4('0x31')]=_['intersection'](_0x375811['model'],qs[_0xa4d4('0x32')](_0x25f6da['query']['fields']));_0x5a46e6['attributes']=_0x5a46e6[_0xa4d4('0x31')]['length']?_0x5a46e6[_0xa4d4('0x31')]:_0x375811[_0xa4d4('0x30')];if(!_0x25f6da[_0xa4d4('0x29')][_0xa4d4('0x3f')](_0xa4d4('0x40'))){_0x5a46e6[_0xa4d4('0x15')]=qs[_0xa4d4('0x15')](_0x25f6da['query'][_0xa4d4('0x15')]);_0x5a46e6[_0xa4d4('0x17')]=qs['offset'](_0x25f6da[_0xa4d4('0x29')][_0xa4d4('0x17')]);}_0x5a46e6[_0xa4d4('0x41')]=qs[_0xa4d4('0x42')](_0x25f6da['query']['sort']);_0x5a46e6[_0xa4d4('0x43')]=qs[_0xa4d4('0x2e')](_[_0xa4d4('0x44')](_0x25f6da[_0xa4d4('0x29')],_0x375811[_0xa4d4('0x2e')]));_0x5a46e6[_0xa4d4('0x43')][_0xa4d4('0x45')]=_0x54c35c['id'];if(_0x25f6da[_0xa4d4('0x29')][_0xa4d4('0x46')]){_0x5a46e6['where']=_[_0xa4d4('0x47')](_0x5a46e6['where'],{'$or':_[_0xa4d4('0x21')](_0x5a46e6[_0xa4d4('0x31')],function(_0x1dafde){var _0x3c3338={};_0x3c3338[_0x1dafde]={'$like':'%'+_0x25f6da[_0xa4d4('0x29')]['filter']+'%'};return _0x3c3338;})});}if(_0x25f6da[_0xa4d4('0x29')][_0xa4d4('0x48')]){var _0x246baa=_0x25f6da[_0xa4d4('0x29')][_0xa4d4('0x48')][_0xa4d4('0x49')](',');var _0x2656f3={};_0x2656f3[_0x246baa[0x0]]={'$gte':moment(_0x246baa[0x1])[_0xa4d4('0x4a')](_0xa4d4('0x4b'))};_0x5a46e6[_0xa4d4('0x43')]=_['merge'](_0x5a46e6[_0xa4d4('0x43')],_0x2656f3);}_0x5a46e6=_['merge']({},_0x5a46e6,_0x25f6da[_0xa4d4('0x34')]);return db[_0xa4d4('0x3c')][_0xa4d4('0x16')]({'where':_0x5a46e6[_0xa4d4('0x43')]})[_0xa4d4('0x1b')](function(_0x3dc9ea){_0x2662e3[_0xa4d4('0x16')]=_0x3dc9ea;if(_0x25f6da[_0xa4d4('0x29')][_0xa4d4('0x33')]){_0x5a46e6[_0xa4d4('0x4c')]=[{'all':!![]}];}return db[_0xa4d4('0x3c')][_0xa4d4('0x27')](_0x5a46e6);})['then'](function(_0x1f76f7){_0x2662e3[_0xa4d4('0x4d')]=_0x1f76f7;return _0x2662e3;});}})[_0xa4d4('0x1b')](respondWithFilteredResult(_0x1e47cf,_0x5a46e6))[_0xa4d4('0x36')](handleError(_0x1e47cf,null));};exports[_0xa4d4('0x4e')]=function(_0x49ddf3,_0x41eb19){return db[_0xa4d4('0x22')]['find']({'where':{'id':_0x49ddf3['params']['id']}})[_0xa4d4('0x1b')](handleEntityNotFound(_0x41eb19,null))[_0xa4d4('0x1b')](function(_0x299024){if(_0x299024){return _0x299024[_0xa4d4('0x4f')](_0x49ddf3[_0xa4d4('0x38')][_0xa4d4('0x3d')],_[_0xa4d4('0x50')](_0x49ddf3[_0xa4d4('0x38')],[_0xa4d4('0x3d'),'id'])||{})['spread'](function(){return db[_0xa4d4('0x51')]['findAll']({'attributes':['id',_0xa4d4('0x1e'),_0xa4d4('0x52')],'where':{'id':_0x49ddf3['body'][_0xa4d4('0x3d')]}});})['then'](function(_0x2ed397){socket['emit'](_0xa4d4('0x53'),{'id':Number(_0x49ddf3[_0xa4d4('0x2c')]['id']),'tags':_0x2ed397||[]});return{'id':Number(_0x49ddf3[_0xa4d4('0x2c')]['id']),'tags':_0x2ed397||[]};});}})[_0xa4d4('0x1b')](respondWithResult(_0x41eb19,null))['catch'](handleError(_0x41eb19,null));};exports[_0xa4d4('0x54')]=function(_0x152f09,_0x859c25){return db[_0xa4d4('0x22')][_0xa4d4('0x35')]({'where':{'id':_0x152f09[_0xa4d4('0x2c')]['id']}})[_0xa4d4('0x1b')](handleEntityNotFound(_0x859c25,null))['then'](function(_0x2b27d8){if(_0x2b27d8){return _0x2b27d8[_0xa4d4('0x54')](_0x152f09[_0xa4d4('0x29')][_0xa4d4('0x3d')]);}})[_0xa4d4('0x1b')](respondWithStatusCode(_0x859c25,null))[_0xa4d4('0x36')](handleError(_0x859c25,null));};exports[_0xa4d4('0x55')]=function(_0x383b33,_0xcbaaaa){var _0x5d7c4d=moment()[_0xa4d4('0x56')]()[_0xa4d4('0x57')]();var _0x33150b=path[_0xa4d4('0x58')](config['root'],_0xa4d4('0x59'),_0xa4d4('0x5a'),'tmp');var _0x51bcb0=path[_0xa4d4('0x58')](config[_0xa4d4('0x5b')],_0xa4d4('0x59'),'files',_0xa4d4('0x5c'));var _0x57808c=path[_0xa4d4('0x58')](_0x33150b,_0x5d7c4d);var _0x102fe9=util[_0xa4d4('0x4a')]('transcript-%d-%s.zip',_0x383b33[_0xa4d4('0x2c')]['id'],_0x5d7c4d);var _0x2da00a=path['join'](_0x33150b,_0x102fe9);var _0x199c43=[];_0x199c43[_0xa4d4('0x5d')]({'model':db[_0xa4d4('0x5e')],'as':_0xa4d4('0x5e'),'attributes':['id','fullname'],'raw':!![]});_0x199c43[_0xa4d4('0x5d')]({'model':db[_0xa4d4('0x5f')],'as':'Contact','attributes':['id','firstName',_0xa4d4('0x60')],'raw':!![]});if(_0x383b33[_0xa4d4('0x29')][_0xa4d4('0x5c')]){_0x199c43['push']({'model':db[_0xa4d4('0x61')],'as':'Attachment','raw':!![]});}var _0x41ccd7=[{'model':db[_0xa4d4('0x3c')],'as':_0xa4d4('0x62'),'attributes':['id','body',_0xa4d4('0x63'),_0xa4d4('0x64'),_0xa4d4('0x65'),'read'],'include':_0x199c43}];_0x41ccd7[_0xa4d4('0x5d')]({'model':db[_0xa4d4('0x66')],'as':_0xa4d4('0x67'),'attributes':['id',_0xa4d4('0x1e')]});_0x41ccd7['push']({'model':db[_0xa4d4('0x5e')],'as':_0xa4d4('0x68'),'attributes':['id',_0xa4d4('0x69')]});return db[_0xa4d4('0x22')][_0xa4d4('0x35')]({'where':{'id':_0x383b33['params']['id']},'include':_0x41ccd7})[_0xa4d4('0x1b')](handleEntityNotFound(_0xcbaaaa,null))['then'](function(_0xd72089){if(_0xd72089){var _0x31856b=_0xd72089[_0xa4d4('0x6a')]({'plain':!![]});fs[_0xa4d4('0x6b')](_0x57808c);var _0x139631={'channel':_0xa4d4('0x6c'),'account':_0x31856b[_0xa4d4('0x67')]['name'],'agent':_0x31856b['Owner']?_0x31856b[_0xa4d4('0x68')][_0xa4d4('0x69')]:_0xa4d4('0x6d'),'createdAt':moment(_0x31856b[_0xa4d4('0x63')])[_0xa4d4('0x4a')](_0xa4d4('0x4b'))[_0xa4d4('0x57')](),'closedAt':_0x31856b[_0xa4d4('0x6e')]?moment(_0x31856b[_0xa4d4('0x6f')])[_0xa4d4('0x4a')](_0xa4d4('0x4b'))['toString']():'','disposition':_0x31856b['disposition'],'messages':_(_0x31856b[_0xa4d4('0x62')])['filter']({'secret':![]})[_0xa4d4('0x21')](function(_0x3c9a04){if(_0x3c9a04['Attachment']){var _0x3a4e5f=path[_0xa4d4('0x58')](_0x51bcb0,_0x3c9a04['Attachment']['basename']);if(fs[_0xa4d4('0x70')](_0x3a4e5f)){fs[_0xa4d4('0x71')](_0x3a4e5f)['pipe'](fs[_0xa4d4('0x72')](path[_0xa4d4('0x58')](_0x57808c,_0x3c9a04['Attachment'][_0xa4d4('0x73')])));}}return{'date':moment(_0x3c9a04[_0xa4d4('0x63')])[_0xa4d4('0x4a')](_0xa4d4('0x4b'))[_0xa4d4('0x57')](),'sender':_0x3c9a04[_0xa4d4('0x64')]===_0xa4d4('0x74')?_0x3c9a04[_0xa4d4('0x5e')]?_0x3c9a04[_0xa4d4('0x5e')][_0xa4d4('0x69')]:_0xa4d4('0x75'):_0x3c9a04[_0xa4d4('0x76')][_0xa4d4('0x77')]+(_0x3c9a04['Contact'][_0xa4d4('0x60')]?'\x20'+_0x3c9a04['Contact'][_0xa4d4('0x60')]:''),'body':_0x3c9a04[_0xa4d4('0x61')]?_0x3c9a04[_0xa4d4('0x61')][_0xa4d4('0x73')]:_0x3c9a04[_0xa4d4('0x38')],'direction':_0x3c9a04[_0xa4d4('0x64')]==='out'?'A':'C','secret':_0x3c9a04[_0xa4d4('0x65')],'read':_0x3c9a04[_0xa4d4('0x78')],'attachment':_0x3c9a04[_0xa4d4('0x61')]};})['value']()};return ejs[_0xa4d4('0x79')](path['join'](config[_0xa4d4('0x5b')],'server',_0xa4d4('0x7a'),_0xa4d4('0x7b')),{'interaction':_0x139631})['then'](function(_0x3fbff1){var _0x210772=path[_0xa4d4('0x58')](_0x57808c,'transcript-'+_0x31856b['id']+'-'+_0x5d7c4d+_0xa4d4('0x7c'));var _0x595e61={'path':_0x210772,'channel':'Whatsapp','interactionId':_0x31856b['id']};return pdf[_0xa4d4('0x7d')](_0x3fbff1,_0x595e61);})['then'](function(){return new BPromise(function(_0x175a42,_0x337760){zipdir(_0x57808c,{'saveTo':_0x2da00a},function(_0x5c2a17,_0x5dc68b){if(_0x5c2a17)return _0x337760(_0x5c2a17);return _0x175a42(_0x5dc68b);});})[_0xa4d4('0x1b')](function(){return new BPromise(function(_0x2ad63d,_0x3cc7ad){rimraf(_0x57808c,function(_0xee37f6){if(_0xee37f6)_0x3cc7ad(_0xee37f6);return _0x2ad63d();});});})[_0xa4d4('0x1b')](function(){return _0xcbaaaa[_0xa4d4('0x55')](_0x2da00a,_0x102fe9,function(_0x2283fc){if(_0x2283fc){console[_0xa4d4('0x7e')](_0xa4d4('0x7f'),_0x2283fc);}else{fs[_0xa4d4('0x80')](_0x2da00a);}});});});}else{return _0xcbaaaa[_0xa4d4('0x10')](0xc8);}})[_0xa4d4('0x36')](handleError(_0xcbaaaa,null));};
\ No newline at end of file