Built motion from commit 00bf23f6.|2.6.16
[motion2.git] / server / api / whatsappInteraction / whatsappInteraction.controller.js
index bbf64e1..7a84eaa 100644 (file)
@@ -15,4 +15,4 @@
 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                        *
 // *                                                                       *
 // *************************************************************************
-var _0x977f=['limit','undefined','count','offset','set','Content-Range','error','name','send','rawAttributes','type','key','getOptions','findAll','query','user','then','catch','show','params','model','keys','WhatsappInteraction','filters','intersection','attributes','fields','length','includeAll','merge','options','create','body','find','destroy','describe','addMessage','WhatsappMessage','ids','omit','getMessages','hasOwnProperty','nolimit','order','sort','where','pick','WhatsappInteractionId','filter','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','include','addTags','spread','Tag','color','emit','whatsappInteractionTags:save','removeTags','download','unix','toString','root','server','tmp','files','join','transcript-%d-%s.zip','push','User','fullname','Contact','firstName','lastName','attachments','Attachment','Messages','createdAt','secret','read','WhatsappAccount','Account','mkdirSync','Whatsapp','Owner','closed','closedAt','basename','existsSync','pipe','createWriteStream','direction','System','out','value','views','downloadInteraction.ejs','transcript-','err','unlinkSync','rimraf','zip-dir','moment','util','path','lodash','ioredis','../../components/parsers/qs','../../config/logger','api','../../config/environment','../../mysqldb','redis','defaults','localhost','socket.io-emitter','register','sendStatus','status','end','json'];(function(_0x10d8bb,_0x1b9a0a){var _0x1fb518=function(_0x594478){while(--_0x594478){_0x10d8bb['push'](_0x10d8bb['shift']());}};_0x1fb518(++_0x1b9a0a);}(_0x977f,0x1d7));var _0xf977=function(_0x59fc22,_0x4256a9){_0x59fc22=_0x59fc22-0x0;var _0x442363=_0x977f[_0x59fc22];return _0x442363;};'use strict';var rimraf=require(_0xf977('0x0'));var zipdir=require(_0xf977('0x1'));var moment=require(_0xf977('0x2'));var BPromise=require('bluebird');var util=require(_0xf977('0x3'));var path=require(_0xf977('0x4'));var ejs=require('ejs');var fs=require('fs');var _=require(_0xf977('0x5'));var Redis=require(_0xf977('0x6'));var interaction=require('../../components/interaction/service');var qs=require(_0xf977('0x7'));var pdf=require('../../components/export/pdf');var logger=require(_0xf977('0x8'))(_0xf977('0x9'));var config=require(_0xf977('0xa'));var db=require(_0xf977('0xb'))['db'];config[_0xf977('0xc')]=_[_0xf977('0xd')](config[_0xf977('0xc')],{'host':_0xf977('0xe'),'port':0x18eb});var socket=require(_0xf977('0xf'))(new Redis(config[_0xf977('0xc')]));require('./whatsappInteraction.socket')[_0xf977('0x10')](socket);function respondWithStatusCode(_0x2bbeb1,_0x3060f2){_0x3060f2=_0x3060f2||0xcc;return function(_0x1f78af){if(_0x1f78af){return _0x2bbeb1[_0xf977('0x11')](_0x3060f2);}return _0x2bbeb1[_0xf977('0x12')](_0x3060f2)[_0xf977('0x13')]();};}function respondWithResult(_0xb733b0,_0x173f39){_0x173f39=_0x173f39||0xc8;return function(_0x5394e9){if(_0x5394e9){return _0xb733b0['status'](_0x173f39)[_0xf977('0x14')](_0x5394e9);}};}function respondWithFilteredResult(_0x44e8dd,_0x4bdc7c){return function(_0x1ee804){if(_0x1ee804){var _0x5bea6f=typeof _0x4bdc7c['offset']==='undefined'&&typeof _0x4bdc7c[_0xf977('0x15')]===_0xf977('0x16');var _0x348afb=_0x1ee804[_0xf977('0x17')];var _0x5ae6df=_0x5bea6f?0x0:_0x4bdc7c[_0xf977('0x18')];var _0x3b34f6=_0x5bea6f?_0x1ee804[_0xf977('0x17')]:_0x4bdc7c[_0xf977('0x18')]+_0x4bdc7c[_0xf977('0x15')];var _0x2d9f14;if(_0x3b34f6>=_0x348afb){_0x3b34f6=_0x348afb;_0x2d9f14=0xc8;}else{_0x2d9f14=0xce;}_0x44e8dd['status'](_0x2d9f14);return _0x44e8dd[_0xf977('0x19')](_0xf977('0x1a'),_0x5ae6df+'-'+_0x3b34f6+'/'+_0x348afb)['json'](_0x1ee804);}return null;};}function saveUpdates(_0x5b1568){return function(_0x2ce345){if(_0x2ce345){return _0x2ce345['update'](_0x5b1568)['then'](function(_0x202e2a){return _0x202e2a;});}return null;};}function removeEntity(_0x5eb2f1){return function(_0x1e64d0){if(_0x1e64d0){return _0x1e64d0['destroy']()['then'](function(){_0x5eb2f1[_0xf977('0x12')](0xcc)[_0xf977('0x13')]();});}};}function handleEntityNotFound(_0x5d9b12){return function(_0x45ac11){if(!_0x45ac11){_0x5d9b12['sendStatus'](0x194);}return _0x45ac11;};}function handleError(_0x453aa0,_0x46f2e7){_0x46f2e7=_0x46f2e7||0x1f4;return function(_0x17ad84){logger[_0xf977('0x1b')](_0x17ad84['stack']);if(_0x17ad84[_0xf977('0x1c')]){delete _0x17ad84[_0xf977('0x1c')];}_0x453aa0[_0xf977('0x12')](_0x46f2e7)[_0xf977('0x1d')](_0x17ad84);};}exports['index']=function(_0x397274,_0x56eab7){var _0x46172c={};var _0x36fc32=_['map'](db['WhatsappInteraction'][_0xf977('0x1e')],function(_0xb28d79){return{'name':_0xb28d79['fieldName'],'type':_0xb28d79[_0xf977('0x1f')][_0xf977('0x20')]};});_0x46172c=qs[_0xf977('0x21')](_0x36fc32,_0x397274);return interaction[_0xf977('0x22')]('whatsapp',_0x397274[_0xf977('0x23')],_0x397274[_0xf977('0x24')],_0x46172c,_0x36fc32)[_0xf977('0x25')](respondWithFilteredResult(_0x56eab7,_0x46172c))[_0xf977('0x26')](handleError(_0x56eab7,null));};exports[_0xf977('0x27')]=function(_0x14c492,_0x5e74dc){var _0x35f786={'raw':![],'where':{'id':_0x14c492[_0xf977('0x28')]['id']}},_0x3de8e9={};_0x3de8e9[_0xf977('0x29')]=_[_0xf977('0x2a')](db[_0xf977('0x2b')][_0xf977('0x1e')]);_0x3de8e9[_0xf977('0x23')]=_['keys'](_0x14c492[_0xf977('0x23')]);_0x3de8e9[_0xf977('0x2c')]=_[_0xf977('0x2d')](_0x3de8e9[_0xf977('0x29')],_0x3de8e9['query']);_0x35f786[_0xf977('0x2e')]=_[_0xf977('0x2d')](_0x3de8e9['model'],qs[_0xf977('0x2f')](_0x14c492['query'][_0xf977('0x2f')]));_0x35f786[_0xf977('0x2e')]=_0x35f786[_0xf977('0x2e')][_0xf977('0x30')]?_0x35f786[_0xf977('0x2e')]:_0x3de8e9['model'];if(_0x14c492['query'][_0xf977('0x31')]){_0x35f786['include']=[{'all':!![]}];}_0x35f786=_[_0xf977('0x32')]({},_0x35f786,_0x14c492[_0xf977('0x33')]);return db['WhatsappInteraction']['find'](_0x35f786)[_0xf977('0x25')](handleEntityNotFound(_0x5e74dc,null))[_0xf977('0x25')](respondWithResult(_0x5e74dc,null))['catch'](handleError(_0x5e74dc,null));};exports['create']=function(_0x32659d,_0xdb00bb){return db['WhatsappInteraction'][_0xf977('0x34')](_0x32659d[_0xf977('0x35')],{})['then'](respondWithResult(_0xdb00bb,0xc9))[_0xf977('0x26')](handleError(_0xdb00bb,null));};exports['update']=function(_0x2dc8bb,_0x52d5da){if(_0x2dc8bb[_0xf977('0x35')]['id']){delete _0x2dc8bb[_0xf977('0x35')]['id'];}return db[_0xf977('0x2b')][_0xf977('0x36')]({'where':{'id':_0x2dc8bb['params']['id']}})[_0xf977('0x25')](handleEntityNotFound(_0x52d5da,null))[_0xf977('0x25')](saveUpdates(_0x2dc8bb[_0xf977('0x35')],null))['then'](respondWithResult(_0x52d5da,null))[_0xf977('0x26')](handleError(_0x52d5da,null));};exports[_0xf977('0x37')]=function(_0x2425ce,_0x5bb4c3){return db['WhatsappInteraction'][_0xf977('0x36')]({'where':{'id':_0x2425ce['params']['id']}})[_0xf977('0x25')](handleEntityNotFound(_0x5bb4c3,null))[_0xf977('0x25')](removeEntity(_0x5bb4c3,null))[_0xf977('0x26')](handleError(_0x5bb4c3,null));};exports[_0xf977('0x38')]=function(_0x2472b1,_0x30d32a){return db[_0xf977('0x2b')][_0xf977('0x38')]()['then'](respondWithResult(_0x30d32a,null))[_0xf977('0x26')](handleError(_0x30d32a,null));};exports[_0xf977('0x39')]=function(_0x48c03a,_0x4216d0){return db[_0xf977('0x3a')][_0xf977('0x36')]({'where':{'id':_0x48c03a[_0xf977('0x28')]['id']}})[_0xf977('0x25')](handleEntityNotFound(_0x4216d0,null))[_0xf977('0x25')](function(_0x4921e5){if(_0x4921e5){return _0x4921e5[_0xf977('0x39')](_0x48c03a[_0xf977('0x35')][_0xf977('0x3b')],_[_0xf977('0x3c')](_0x48c03a['body'],[_0xf977('0x3b'),'id'])||{});}})['then'](respondWithResult(_0x4216d0,null))[_0xf977('0x26')](handleError(_0x4216d0,null));};exports[_0xf977('0x3d')]=function(_0x1a11a3,_0x569f44){var _0x2faa4d={'raw':![],'where':{}};var _0x4e5b90={};var _0x443a95={'count':0x0,'rows':[]};return db['WhatsappInteraction']['findOne']({'where':{'id':_0x1a11a3['params']['id']}})[_0xf977('0x25')](handleEntityNotFound(_0x569f44,null))['then'](function(_0x333517){if(_0x333517){_0x4e5b90['model']=_[_0xf977('0x2a')](db[_0xf977('0x3a')][_0xf977('0x1e')]);_0x4e5b90[_0xf977('0x23')]=_[_0xf977('0x2a')](_0x1a11a3[_0xf977('0x23')]);_0x4e5b90['filters']=_[_0xf977('0x2d')](_0x4e5b90[_0xf977('0x29')],_0x4e5b90['query']);_0x2faa4d[_0xf977('0x2e')]=_[_0xf977('0x2d')](_0x4e5b90[_0xf977('0x29')],qs[_0xf977('0x2f')](_0x1a11a3[_0xf977('0x23')]['fields']));_0x2faa4d[_0xf977('0x2e')]=_0x2faa4d[_0xf977('0x2e')][_0xf977('0x30')]?_0x2faa4d['attributes']:_0x4e5b90[_0xf977('0x29')];if(!_0x1a11a3[_0xf977('0x23')][_0xf977('0x3e')](_0xf977('0x3f'))){_0x2faa4d[_0xf977('0x15')]=qs[_0xf977('0x15')](_0x1a11a3[_0xf977('0x23')][_0xf977('0x15')]);_0x2faa4d['offset']=qs[_0xf977('0x18')](_0x1a11a3[_0xf977('0x23')][_0xf977('0x18')]);}_0x2faa4d[_0xf977('0x40')]=qs['sort'](_0x1a11a3[_0xf977('0x23')][_0xf977('0x41')]);_0x2faa4d[_0xf977('0x42')]=qs[_0xf977('0x2c')](_[_0xf977('0x43')](_0x1a11a3[_0xf977('0x23')],_0x4e5b90['filters']));_0x2faa4d[_0xf977('0x42')][_0xf977('0x44')]=_0x333517['id'];if(_0x1a11a3[_0xf977('0x23')][_0xf977('0x45')]){_0x2faa4d['where']=_[_0xf977('0x32')](_0x2faa4d['where'],{'$or':_['map'](_0x2faa4d[_0xf977('0x2e')],function(_0x8e2b6c){var _0x31b092={};_0x31b092[_0x8e2b6c]={'$like':'%'+_0x1a11a3[_0xf977('0x23')][_0xf977('0x45')]+'%'};return _0x31b092;})});}if(_0x1a11a3[_0xf977('0x23')][_0xf977('0x46')]){var _0x2ddba6=_0x1a11a3['query']['$gte'][_0xf977('0x47')](',');var _0x21661b={};_0x21661b[_0x2ddba6[0x0]]={'$gte':moment(_0x2ddba6[0x1])[_0xf977('0x48')](_0xf977('0x49'))};_0x2faa4d['where']=_[_0xf977('0x32')](_0x2faa4d[_0xf977('0x42')],_0x21661b);}_0x2faa4d=_[_0xf977('0x32')]({},_0x2faa4d,_0x1a11a3['options']);return db[_0xf977('0x3a')][_0xf977('0x17')]({'where':_0x2faa4d[_0xf977('0x42')]})[_0xf977('0x25')](function(_0x1d81b3){_0x443a95['count']=_0x1d81b3;if(_0x1a11a3[_0xf977('0x23')][_0xf977('0x31')]){_0x2faa4d[_0xf977('0x4a')]=[{'all':!![]}];}return db[_0xf977('0x3a')][_0xf977('0x22')](_0x2faa4d);})['then'](function(_0x23806a){_0x443a95['rows']=_0x23806a;return _0x443a95;});}})['then'](respondWithFilteredResult(_0x569f44,_0x2faa4d))[_0xf977('0x26')](handleError(_0x569f44,null));};exports[_0xf977('0x4b')]=function(_0x5cf69d,_0x754386){return db['WhatsappInteraction'][_0xf977('0x36')]({'where':{'id':_0x5cf69d[_0xf977('0x28')]['id']}})[_0xf977('0x25')](handleEntityNotFound(_0x754386,null))[_0xf977('0x25')](function(_0x56a2c1){if(_0x56a2c1){return _0x56a2c1['setTags'](_0x5cf69d[_0xf977('0x35')][_0xf977('0x3b')],_['omit'](_0x5cf69d[_0xf977('0x35')],['ids','id'])||{})[_0xf977('0x4c')](function(){return db[_0xf977('0x4d')][_0xf977('0x22')]({'attributes':['id','name',_0xf977('0x4e')],'where':{'id':_0x5cf69d[_0xf977('0x35')][_0xf977('0x3b')]}});})[_0xf977('0x25')](function(_0x575f7f){socket[_0xf977('0x4f')](_0xf977('0x50'),{'id':Number(_0x5cf69d['params']['id']),'tags':_0x575f7f||[]});return{'id':Number(_0x5cf69d[_0xf977('0x28')]['id']),'tags':_0x575f7f||[]};});}})[_0xf977('0x25')](respondWithResult(_0x754386,null))[_0xf977('0x26')](handleError(_0x754386,null));};exports[_0xf977('0x51')]=function(_0x2893d6,_0x21277c){return db[_0xf977('0x2b')][_0xf977('0x36')]({'where':{'id':_0x2893d6[_0xf977('0x28')]['id']}})[_0xf977('0x25')](handleEntityNotFound(_0x21277c,null))[_0xf977('0x25')](function(_0x3bb17a){if(_0x3bb17a){return _0x3bb17a[_0xf977('0x51')](_0x2893d6[_0xf977('0x23')][_0xf977('0x3b')]);}})[_0xf977('0x25')](respondWithStatusCode(_0x21277c,null))['catch'](handleError(_0x21277c,null));};exports[_0xf977('0x52')]=function(_0x440125,_0x37501d){var _0x1a4123=moment()[_0xf977('0x53')]()[_0xf977('0x54')]();var _0x4d5e3e=path['join'](config[_0xf977('0x55')],_0xf977('0x56'),'files',_0xf977('0x57'));var _0x546d1f=path['join'](config['root'],_0xf977('0x56'),_0xf977('0x58'),'attachments');var _0x54cbae=path[_0xf977('0x59')](_0x4d5e3e,_0x1a4123);var _0x1ecd88=util['format'](_0xf977('0x5a'),_0x440125[_0xf977('0x28')]['id'],_0x1a4123);var _0x47456f=path[_0xf977('0x59')](_0x4d5e3e,_0x1ecd88);var _0x563244=[];_0x563244[_0xf977('0x5b')]({'model':db[_0xf977('0x5c')],'as':_0xf977('0x5c'),'attributes':['id',_0xf977('0x5d')],'raw':!![]});_0x563244[_0xf977('0x5b')]({'model':db['CmContact'],'as':_0xf977('0x5e'),'attributes':['id',_0xf977('0x5f'),_0xf977('0x60')],'raw':!![]});if(_0x440125[_0xf977('0x23')][_0xf977('0x61')]){_0x563244['push']({'model':db[_0xf977('0x62')],'as':_0xf977('0x62'),'raw':!![]});}var _0x5d339e=[{'model':db[_0xf977('0x3a')],'as':_0xf977('0x63'),'attributes':['id',_0xf977('0x35'),_0xf977('0x64'),'direction',_0xf977('0x65'),_0xf977('0x66')],'include':_0x563244}];_0x5d339e[_0xf977('0x5b')]({'model':db[_0xf977('0x67')],'as':_0xf977('0x68'),'attributes':['id',_0xf977('0x1c')]});_0x5d339e[_0xf977('0x5b')]({'model':db[_0xf977('0x5c')],'as':'Owner','attributes':['id',_0xf977('0x5d')]});return db['WhatsappInteraction']['find']({'where':{'id':_0x440125[_0xf977('0x28')]['id']},'include':_0x5d339e})[_0xf977('0x25')](handleEntityNotFound(_0x37501d,null))[_0xf977('0x25')](function(_0x4c3f06){if(_0x4c3f06){var _0x3ba8bd=_0x4c3f06['get']({'plain':!![]});fs[_0xf977('0x69')](_0x54cbae);var _0x3384d5={'channel':_0xf977('0x6a'),'account':_0x3ba8bd[_0xf977('0x68')]['name'],'agent':_0x3ba8bd['Owner']?_0x3ba8bd[_0xf977('0x6b')][_0xf977('0x5d')]:'N.A.','createdAt':moment(_0x3ba8bd['createdAt'])[_0xf977('0x48')](_0xf977('0x49'))['toString'](),'closedAt':_0x3ba8bd[_0xf977('0x6c')]?moment(_0x3ba8bd[_0xf977('0x6d')])[_0xf977('0x48')](_0xf977('0x49'))[_0xf977('0x54')]():'','disposition':_0x3ba8bd['disposition'],'messages':_(_0x3ba8bd[_0xf977('0x63')])[_0xf977('0x45')]({'secret':![]})['map'](function(_0x4ebae0){if(_0x4ebae0[_0xf977('0x62')]){var _0x25aa16=path[_0xf977('0x59')](_0x546d1f,_0x4ebae0['Attachment'][_0xf977('0x6e')]);if(fs[_0xf977('0x6f')](_0x25aa16)){fs['createReadStream'](_0x25aa16)[_0xf977('0x70')](fs[_0xf977('0x71')](path['join'](_0x54cbae,_0x4ebae0[_0xf977('0x62')][_0xf977('0x6e')])));}}return{'date':moment(_0x4ebae0[_0xf977('0x64')])[_0xf977('0x48')]('YYYY-MM-DD\x20HH:mm:ss')[_0xf977('0x54')](),'sender':_0x4ebae0[_0xf977('0x72')]==='out'?_0x4ebae0[_0xf977('0x5c')]?_0x4ebae0[_0xf977('0x5c')][_0xf977('0x5d')]:_0xf977('0x73'):_0x4ebae0['Contact'][_0xf977('0x5f')]+(_0x4ebae0[_0xf977('0x5e')][_0xf977('0x60')]?'\x20'+_0x4ebae0['Contact'][_0xf977('0x60')]:''),'body':_0x4ebae0['Attachment']?_0x4ebae0[_0xf977('0x62')][_0xf977('0x6e')]:_0x4ebae0[_0xf977('0x35')],'direction':_0x4ebae0[_0xf977('0x72')]===_0xf977('0x74')?'A':'C','secret':_0x4ebae0[_0xf977('0x65')],'read':_0x4ebae0[_0xf977('0x66')],'attachment':_0x4ebae0[_0xf977('0x62')]};})[_0xf977('0x75')]()};return ejs['renderFile'](path[_0xf977('0x59')](config['root'],_0xf977('0x56'),_0xf977('0x76'),_0xf977('0x77')),{'interaction':_0x3384d5})['then'](function(_0x1f68dc){var _0x432b7d=path['join'](_0x54cbae,_0xf977('0x78')+_0x3ba8bd['id']+'-'+_0x1a4123+'.pdf');var _0x5959b4={'path':_0x432b7d,'channel':_0xf977('0x6a'),'interactionId':_0x3ba8bd['id']};return pdf['createPdfFromHTML'](_0x1f68dc,_0x5959b4);})[_0xf977('0x25')](function(){return new BPromise(function(_0x6607a9,_0x23a503){zipdir(_0x54cbae,{'saveTo':_0x47456f},function(_0x40c1c0,_0x2d6b66){if(_0x40c1c0)return _0x23a503(_0x40c1c0);return _0x6607a9(_0x2d6b66);});})[_0xf977('0x25')](function(){return new BPromise(function(_0xd4f620,_0x308d82){rimraf(_0x54cbae,function(_0x502a43){if(_0x502a43)_0x308d82(_0x502a43);return _0xd4f620();});});})['then'](function(){return _0x37501d[_0xf977('0x52')](_0x47456f,_0x1ecd88,function(_0x776cea){if(_0x776cea){console['log'](_0xf977('0x79'),_0x776cea);}else{fs[_0xf977('0x7a')](_0x47456f);}});});});}else{return _0x37501d[_0xf977('0x11')](0xc8);}})[_0xf977('0x26')](handleError(_0x37501d,null));};
\ No newline at end of file
+var _0xb7b4=['offset','sort','pick','where','WhatsappInteractionId','filter','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','includeAll','rows','addTags','setTags','spread','Tag','color','emit','removeTags','download','join','root','server','files','tmp','transcript-%d-%s.zip','push','User','fullname','CmContact','Contact','firstName','lastName','attachments','Attachment','Messages','createdAt','direction','read','Account','Owner','N.A.','toString','closed','closedAt','disposition','basename','existsSync','pipe','out','System','secret','renderFile','views','.pdf','Whatsapp','createPdfFromHTML','log','err','unlinkSync','rimraf','zip-dir','moment','path','ejs','ioredis','../../components/interaction/service','../../components/parsers/qs','../../components/export/pdf','../../config/logger','api','../../config/environment','../../mysqldb','redis','defaults','localhost','register','sendStatus','status','end','json','undefined','limit','count','set','Content-Range','update','then','destroy','stack','name','index','map','rawAttributes','fieldName','type','findAll','whatsapp','catch','show','params','keys','WhatsappInteraction','query','filters','intersection','model','attributes','fields','include','merge','options','find','create','body','addMessage','WhatsappMessage','ids','omit','getMessages','length','hasOwnProperty','nolimit'];(function(_0x2808e2,_0x2570fe){var _0x57088f=function(_0x488583){while(--_0x488583){_0x2808e2['push'](_0x2808e2['shift']());}};_0x57088f(++_0x2570fe);}(_0xb7b4,0x1ad));var _0x4b7b=function(_0x385f0e,_0xa10008){_0x385f0e=_0x385f0e-0x0;var _0x41d183=_0xb7b4[_0x385f0e];return _0x41d183;};'use strict';var rimraf=require(_0x4b7b('0x0'));var zipdir=require(_0x4b7b('0x1'));var moment=require(_0x4b7b('0x2'));var BPromise=require('bluebird');var util=require('util');var path=require(_0x4b7b('0x3'));var ejs=require(_0x4b7b('0x4'));var fs=require('fs');var _=require('lodash');var Redis=require(_0x4b7b('0x5'));var interaction=require(_0x4b7b('0x6'));var qs=require(_0x4b7b('0x7'));var pdf=require(_0x4b7b('0x8'));var logger=require(_0x4b7b('0x9'))(_0x4b7b('0xa'));var config=require(_0x4b7b('0xb'));var db=require(_0x4b7b('0xc'))['db'];config[_0x4b7b('0xd')]=_[_0x4b7b('0xe')](config[_0x4b7b('0xd')],{'host':_0x4b7b('0xf'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x4b7b('0xd')]));require('./whatsappInteraction.socket')[_0x4b7b('0x10')](socket);function respondWithStatusCode(_0x873075,_0x27414b){_0x27414b=_0x27414b||0xcc;return function(_0x5b457f){if(_0x5b457f){return _0x873075[_0x4b7b('0x11')](_0x27414b);}return _0x873075[_0x4b7b('0x12')](_0x27414b)[_0x4b7b('0x13')]();};}function respondWithResult(_0x31f453,_0x469122){_0x469122=_0x469122||0xc8;return function(_0x272280){if(_0x272280){return _0x31f453[_0x4b7b('0x12')](_0x469122)[_0x4b7b('0x14')](_0x272280);}};}function respondWithFilteredResult(_0x24a13b,_0x5e3510){return function(_0x477bb6){if(_0x477bb6){var _0x136e86=typeof _0x5e3510['offset']===_0x4b7b('0x15')&&typeof _0x5e3510[_0x4b7b('0x16')]===_0x4b7b('0x15');var _0x33a7f8=_0x477bb6[_0x4b7b('0x17')];var _0x35a13b=_0x136e86?0x0:_0x5e3510['offset'];var _0x411284=_0x136e86?_0x477bb6['count']:_0x5e3510['offset']+_0x5e3510[_0x4b7b('0x16')];var _0x216b12;if(_0x411284>=_0x33a7f8){_0x411284=_0x33a7f8;_0x216b12=0xc8;}else{_0x216b12=0xce;}_0x24a13b[_0x4b7b('0x12')](_0x216b12);return _0x24a13b[_0x4b7b('0x18')](_0x4b7b('0x19'),_0x35a13b+'-'+_0x411284+'/'+_0x33a7f8)[_0x4b7b('0x14')](_0x477bb6);}return null;};}function saveUpdates(_0x590f83){return function(_0x209215){if(_0x209215){return _0x209215[_0x4b7b('0x1a')](_0x590f83)[_0x4b7b('0x1b')](function(_0x54ef79){return _0x54ef79;});}return null;};}function removeEntity(_0x1a4365){return function(_0x5dcf02){if(_0x5dcf02){return _0x5dcf02[_0x4b7b('0x1c')]()[_0x4b7b('0x1b')](function(){_0x1a4365[_0x4b7b('0x12')](0xcc)[_0x4b7b('0x13')]();});}};}function handleEntityNotFound(_0x918d1b){return function(_0x4bf891){if(!_0x4bf891){_0x918d1b[_0x4b7b('0x11')](0x194);}return _0x4bf891;};}function handleError(_0x59c6bd,_0x4660ea){_0x4660ea=_0x4660ea||0x1f4;return function(_0x70f4fa){logger['error'](_0x70f4fa[_0x4b7b('0x1d')]);if(_0x70f4fa[_0x4b7b('0x1e')]){delete _0x70f4fa[_0x4b7b('0x1e')];}_0x59c6bd[_0x4b7b('0x12')](_0x4660ea)['send'](_0x70f4fa);};}exports[_0x4b7b('0x1f')]=function(_0xfa7452,_0x44925d){var _0x2cf184={};var _0x10ec6b=_[_0x4b7b('0x20')](db['WhatsappInteraction'][_0x4b7b('0x21')],function(_0xa683b4){return{'name':_0xa683b4[_0x4b7b('0x22')],'type':_0xa683b4[_0x4b7b('0x23')]['key']};});_0x2cf184=qs['getOptions'](_0x10ec6b,_0xfa7452);return interaction[_0x4b7b('0x24')](_0x4b7b('0x25'),_0xfa7452['query'],_0xfa7452['user'],_0x2cf184,_0x10ec6b)[_0x4b7b('0x1b')](respondWithFilteredResult(_0x44925d,_0x2cf184))[_0x4b7b('0x26')](handleError(_0x44925d,null));};exports[_0x4b7b('0x27')]=function(_0x4d0df2,_0x59c9e2){var _0x228e96={'raw':![],'where':{'id':_0x4d0df2[_0x4b7b('0x28')]['id']}},_0x1c61d0={};_0x1c61d0['model']=_[_0x4b7b('0x29')](db[_0x4b7b('0x2a')]['rawAttributes']);_0x1c61d0['query']=_[_0x4b7b('0x29')](_0x4d0df2[_0x4b7b('0x2b')]);_0x1c61d0[_0x4b7b('0x2c')]=_[_0x4b7b('0x2d')](_0x1c61d0[_0x4b7b('0x2e')],_0x1c61d0[_0x4b7b('0x2b')]);_0x228e96[_0x4b7b('0x2f')]=_[_0x4b7b('0x2d')](_0x1c61d0[_0x4b7b('0x2e')],qs[_0x4b7b('0x30')](_0x4d0df2[_0x4b7b('0x2b')][_0x4b7b('0x30')]));_0x228e96[_0x4b7b('0x2f')]=_0x228e96[_0x4b7b('0x2f')]['length']?_0x228e96['attributes']:_0x1c61d0[_0x4b7b('0x2e')];if(_0x4d0df2[_0x4b7b('0x2b')]['includeAll']){_0x228e96[_0x4b7b('0x31')]=[{'all':!![]}];}_0x228e96=_[_0x4b7b('0x32')]({},_0x228e96,_0x4d0df2[_0x4b7b('0x33')]);return db[_0x4b7b('0x2a')][_0x4b7b('0x34')](_0x228e96)[_0x4b7b('0x1b')](handleEntityNotFound(_0x59c9e2,null))[_0x4b7b('0x1b')](respondWithResult(_0x59c9e2,null))['catch'](handleError(_0x59c9e2,null));};exports['create']=function(_0x4d611e,_0x5253d0){return db['WhatsappInteraction'][_0x4b7b('0x35')](_0x4d611e[_0x4b7b('0x36')],{})[_0x4b7b('0x1b')](respondWithResult(_0x5253d0,0xc9))[_0x4b7b('0x26')](handleError(_0x5253d0,null));};exports[_0x4b7b('0x1a')]=function(_0x1c1c63,_0xa2c747){if(_0x1c1c63[_0x4b7b('0x36')]['id']){delete _0x1c1c63[_0x4b7b('0x36')]['id'];}return db[_0x4b7b('0x2a')][_0x4b7b('0x34')]({'where':{'id':_0x1c1c63[_0x4b7b('0x28')]['id']}})[_0x4b7b('0x1b')](handleEntityNotFound(_0xa2c747,null))[_0x4b7b('0x1b')](saveUpdates(_0x1c1c63[_0x4b7b('0x36')],null))[_0x4b7b('0x1b')](respondWithResult(_0xa2c747,null))[_0x4b7b('0x26')](handleError(_0xa2c747,null));};exports[_0x4b7b('0x1c')]=function(_0x5edc33,_0x59f0df){return db['WhatsappInteraction'][_0x4b7b('0x34')]({'where':{'id':_0x5edc33[_0x4b7b('0x28')]['id']}})['then'](handleEntityNotFound(_0x59f0df,null))[_0x4b7b('0x1b')](removeEntity(_0x59f0df,null))[_0x4b7b('0x26')](handleError(_0x59f0df,null));};exports['describe']=function(_0x6a496e,_0x14d02d){return db['WhatsappInteraction']['describe']()[_0x4b7b('0x1b')](respondWithResult(_0x14d02d,null))[_0x4b7b('0x26')](handleError(_0x14d02d,null));};exports[_0x4b7b('0x37')]=function(_0x5d9516,_0x509ff8){return db[_0x4b7b('0x38')][_0x4b7b('0x34')]({'where':{'id':_0x5d9516[_0x4b7b('0x28')]['id']}})[_0x4b7b('0x1b')](handleEntityNotFound(_0x509ff8,null))['then'](function(_0x18bb41){if(_0x18bb41){return _0x18bb41[_0x4b7b('0x37')](_0x5d9516[_0x4b7b('0x36')][_0x4b7b('0x39')],_[_0x4b7b('0x3a')](_0x5d9516[_0x4b7b('0x36')],[_0x4b7b('0x39'),'id'])||{});}})[_0x4b7b('0x1b')](respondWithResult(_0x509ff8,null))['catch'](handleError(_0x509ff8,null));};exports[_0x4b7b('0x3b')]=function(_0x2a60b0,_0x112cd1){var _0x3d4716={'raw':![],'where':{}};var _0x50dd4f={};var _0x4bd6a3={'count':0x0,'rows':[]};return db[_0x4b7b('0x2a')]['findOne']({'where':{'id':_0x2a60b0[_0x4b7b('0x28')]['id']}})[_0x4b7b('0x1b')](handleEntityNotFound(_0x112cd1,null))[_0x4b7b('0x1b')](function(_0x387ce2){if(_0x387ce2){_0x50dd4f['model']=_[_0x4b7b('0x29')](db[_0x4b7b('0x38')]['rawAttributes']);_0x50dd4f[_0x4b7b('0x2b')]=_[_0x4b7b('0x29')](_0x2a60b0['query']);_0x50dd4f[_0x4b7b('0x2c')]=_[_0x4b7b('0x2d')](_0x50dd4f[_0x4b7b('0x2e')],_0x50dd4f['query']);_0x3d4716[_0x4b7b('0x2f')]=_[_0x4b7b('0x2d')](_0x50dd4f['model'],qs['fields'](_0x2a60b0['query'][_0x4b7b('0x30')]));_0x3d4716[_0x4b7b('0x2f')]=_0x3d4716[_0x4b7b('0x2f')][_0x4b7b('0x3c')]?_0x3d4716[_0x4b7b('0x2f')]:_0x50dd4f['model'];if(!_0x2a60b0[_0x4b7b('0x2b')][_0x4b7b('0x3d')](_0x4b7b('0x3e'))){_0x3d4716[_0x4b7b('0x16')]=qs['limit'](_0x2a60b0[_0x4b7b('0x2b')][_0x4b7b('0x16')]);_0x3d4716[_0x4b7b('0x3f')]=qs[_0x4b7b('0x3f')](_0x2a60b0['query'][_0x4b7b('0x3f')]);}_0x3d4716['order']=qs['sort'](_0x2a60b0[_0x4b7b('0x2b')][_0x4b7b('0x40')]);_0x3d4716['where']=qs[_0x4b7b('0x2c')](_[_0x4b7b('0x41')](_0x2a60b0[_0x4b7b('0x2b')],_0x50dd4f[_0x4b7b('0x2c')]));_0x3d4716[_0x4b7b('0x42')][_0x4b7b('0x43')]=_0x387ce2['id'];if(_0x2a60b0[_0x4b7b('0x2b')]['filter']){_0x3d4716[_0x4b7b('0x42')]=_[_0x4b7b('0x32')](_0x3d4716[_0x4b7b('0x42')],{'$or':_[_0x4b7b('0x20')](_0x3d4716[_0x4b7b('0x2f')],function(_0x4dd54a){var _0x83e4e4={};_0x83e4e4[_0x4dd54a]={'$like':'%'+_0x2a60b0[_0x4b7b('0x2b')][_0x4b7b('0x44')]+'%'};return _0x83e4e4;})});}if(_0x2a60b0['query'][_0x4b7b('0x45')]){var _0xf62d79=_0x2a60b0[_0x4b7b('0x2b')]['$gte'][_0x4b7b('0x46')](',');var _0xdd0c0f={};_0xdd0c0f[_0xf62d79[0x0]]={'$gte':moment(_0xf62d79[0x1])[_0x4b7b('0x47')](_0x4b7b('0x48'))};_0x3d4716[_0x4b7b('0x42')]=_[_0x4b7b('0x32')](_0x3d4716['where'],_0xdd0c0f);}_0x3d4716=_['merge']({},_0x3d4716,_0x2a60b0[_0x4b7b('0x33')]);return db['WhatsappMessage'][_0x4b7b('0x17')]({'where':_0x3d4716[_0x4b7b('0x42')]})[_0x4b7b('0x1b')](function(_0x25caf1){_0x4bd6a3[_0x4b7b('0x17')]=_0x25caf1;if(_0x2a60b0[_0x4b7b('0x2b')][_0x4b7b('0x49')]){_0x3d4716[_0x4b7b('0x31')]=[{'all':!![]}];}return db[_0x4b7b('0x38')][_0x4b7b('0x24')](_0x3d4716);})[_0x4b7b('0x1b')](function(_0x10002e){_0x4bd6a3[_0x4b7b('0x4a')]=_0x10002e;return _0x4bd6a3;});}})[_0x4b7b('0x1b')](respondWithFilteredResult(_0x112cd1,_0x3d4716))[_0x4b7b('0x26')](handleError(_0x112cd1,null));};exports[_0x4b7b('0x4b')]=function(_0x53888d,_0x42c481){return db['WhatsappInteraction'][_0x4b7b('0x34')]({'where':{'id':_0x53888d[_0x4b7b('0x28')]['id']}})[_0x4b7b('0x1b')](handleEntityNotFound(_0x42c481,null))[_0x4b7b('0x1b')](function(_0x51e7da){if(_0x51e7da){return _0x51e7da[_0x4b7b('0x4c')](_0x53888d['body']['ids'],_['omit'](_0x53888d[_0x4b7b('0x36')],[_0x4b7b('0x39'),'id'])||{})[_0x4b7b('0x4d')](function(){return db[_0x4b7b('0x4e')]['findAll']({'attributes':['id','name',_0x4b7b('0x4f')],'where':{'id':_0x53888d[_0x4b7b('0x36')][_0x4b7b('0x39')]}});})[_0x4b7b('0x1b')](function(_0x43fd8b){socket[_0x4b7b('0x50')]('whatsappInteractionTags:save',{'id':Number(_0x53888d[_0x4b7b('0x28')]['id']),'tags':_0x43fd8b||[]});return{'id':Number(_0x53888d[_0x4b7b('0x28')]['id']),'tags':_0x43fd8b||[]};});}})[_0x4b7b('0x1b')](respondWithResult(_0x42c481,null))[_0x4b7b('0x26')](handleError(_0x42c481,null));};exports[_0x4b7b('0x51')]=function(_0x5bd219,_0x339543){return db['WhatsappInteraction'][_0x4b7b('0x34')]({'where':{'id':_0x5bd219['params']['id']}})[_0x4b7b('0x1b')](handleEntityNotFound(_0x339543,null))[_0x4b7b('0x1b')](function(_0x2a53bf){if(_0x2a53bf){return _0x2a53bf[_0x4b7b('0x51')](_0x5bd219[_0x4b7b('0x2b')][_0x4b7b('0x39')]);}})['then'](respondWithStatusCode(_0x339543,null))[_0x4b7b('0x26')](handleError(_0x339543,null));};exports[_0x4b7b('0x52')]=function(_0x4b2e11,_0x1db915){var _0x24dbe2=moment()['unix']()['toString']();var _0x44a893=path[_0x4b7b('0x53')](config[_0x4b7b('0x54')],_0x4b7b('0x55'),_0x4b7b('0x56'),_0x4b7b('0x57'));var _0x43a93e=path[_0x4b7b('0x53')](config[_0x4b7b('0x54')],_0x4b7b('0x55'),_0x4b7b('0x56'),'attachments');var _0x58a96c=path[_0x4b7b('0x53')](_0x44a893,_0x24dbe2);var _0x515f6e=util[_0x4b7b('0x47')](_0x4b7b('0x58'),_0x4b2e11['params']['id'],_0x24dbe2);var _0x35148a=path[_0x4b7b('0x53')](_0x44a893,_0x515f6e);var _0x2a1270=[];_0x2a1270[_0x4b7b('0x59')]({'model':db[_0x4b7b('0x5a')],'as':_0x4b7b('0x5a'),'attributes':['id',_0x4b7b('0x5b')],'raw':!![]});_0x2a1270['push']({'model':db[_0x4b7b('0x5c')],'as':_0x4b7b('0x5d'),'attributes':['id',_0x4b7b('0x5e'),_0x4b7b('0x5f')],'raw':!![]});if(_0x4b2e11[_0x4b7b('0x2b')][_0x4b7b('0x60')]){_0x2a1270['push']({'model':db['Attachment'],'as':_0x4b7b('0x61'),'raw':!![]});}var _0x40bb8d=[{'model':db[_0x4b7b('0x38')],'as':_0x4b7b('0x62'),'attributes':['id',_0x4b7b('0x36'),_0x4b7b('0x63'),_0x4b7b('0x64'),'secret',_0x4b7b('0x65')],'include':_0x2a1270}];_0x40bb8d[_0x4b7b('0x59')]({'model':db['WhatsappAccount'],'as':_0x4b7b('0x66'),'attributes':['id',_0x4b7b('0x1e')]});_0x40bb8d['push']({'model':db['User'],'as':_0x4b7b('0x67'),'attributes':['id','fullname']});return db[_0x4b7b('0x2a')][_0x4b7b('0x34')]({'where':{'id':_0x4b2e11[_0x4b7b('0x28')]['id']},'include':_0x40bb8d})[_0x4b7b('0x1b')](handleEntityNotFound(_0x1db915,null))['then'](function(_0x258ed5){if(_0x258ed5){var _0x157097=_0x258ed5['get']({'plain':!![]});fs['mkdirSync'](_0x58a96c);var _0x532807={'channel':'Whatsapp','account':_0x157097['Account'][_0x4b7b('0x1e')],'agent':_0x157097['Owner']?_0x157097[_0x4b7b('0x67')][_0x4b7b('0x5b')]:_0x4b7b('0x68'),'createdAt':moment(_0x157097['createdAt'])[_0x4b7b('0x47')]('YYYY-MM-DD\x20HH:mm:ss')[_0x4b7b('0x69')](),'closedAt':_0x157097[_0x4b7b('0x6a')]?moment(_0x157097[_0x4b7b('0x6b')])[_0x4b7b('0x47')]('YYYY-MM-DD\x20HH:mm:ss')['toString']():'','disposition':_0x157097[_0x4b7b('0x6c')],'messages':_(_0x157097[_0x4b7b('0x62')])[_0x4b7b('0x44')]({'secret':![]})['map'](function(_0x169058){if(_0x169058['Attachment']){var _0x36ace4=path['join'](_0x43a93e,_0x169058[_0x4b7b('0x61')][_0x4b7b('0x6d')]);if(fs[_0x4b7b('0x6e')](_0x36ace4)){fs['createReadStream'](_0x36ace4)[_0x4b7b('0x6f')](fs['createWriteStream'](path['join'](_0x58a96c,_0x169058[_0x4b7b('0x61')][_0x4b7b('0x6d')])));}}return{'date':moment(_0x169058[_0x4b7b('0x63')])['format']('YYYY-MM-DD\x20HH:mm:ss')['toString'](),'sender':_0x169058[_0x4b7b('0x64')]===_0x4b7b('0x70')?_0x169058[_0x4b7b('0x5a')]?_0x169058['User']['fullname']:_0x4b7b('0x71'):_0x169058[_0x4b7b('0x5d')][_0x4b7b('0x5e')]+(_0x169058[_0x4b7b('0x5d')][_0x4b7b('0x5f')]?'\x20'+_0x169058['Contact'][_0x4b7b('0x5f')]:''),'body':_0x169058[_0x4b7b('0x61')]?_0x169058[_0x4b7b('0x61')]['basename']:_0x169058[_0x4b7b('0x36')],'direction':_0x169058[_0x4b7b('0x64')]===_0x4b7b('0x70')?'A':'C','secret':_0x169058[_0x4b7b('0x72')],'read':_0x169058[_0x4b7b('0x65')],'attachment':_0x169058[_0x4b7b('0x61')]};})['value']()};return ejs[_0x4b7b('0x73')](path[_0x4b7b('0x53')](config[_0x4b7b('0x54')],_0x4b7b('0x55'),_0x4b7b('0x74'),'downloadInteraction.ejs'),{'interaction':_0x532807})[_0x4b7b('0x1b')](function(_0x607106){var _0x508946=path[_0x4b7b('0x53')](_0x58a96c,'transcript-'+_0x157097['id']+'-'+_0x24dbe2+_0x4b7b('0x75'));var _0x382fdb={'path':_0x508946,'channel':_0x4b7b('0x76'),'interactionId':_0x157097['id']};return pdf[_0x4b7b('0x77')](_0x607106,_0x382fdb);})[_0x4b7b('0x1b')](function(){return new BPromise(function(_0xa2cc74,_0x43ffc6){zipdir(_0x58a96c,{'saveTo':_0x35148a},function(_0x10e810,_0x335583){if(_0x10e810)return _0x43ffc6(_0x10e810);return _0xa2cc74(_0x335583);});})[_0x4b7b('0x1b')](function(){return new BPromise(function(_0x26988e,_0x50e139){rimraf(_0x58a96c,function(_0x21cf63){if(_0x21cf63)_0x50e139(_0x21cf63);return _0x26988e();});});})[_0x4b7b('0x1b')](function(){return _0x1db915[_0x4b7b('0x52')](_0x35148a,_0x515f6e,function(_0x282ec9){if(_0x282ec9){console[_0x4b7b('0x78')](_0x4b7b('0x79'),_0x282ec9);}else{fs[_0x4b7b('0x7a')](_0x35148a);}});});});}else{return _0x1db915['sendStatus'](0xc8);}})[_0x4b7b('0x26')](handleError(_0x1db915,null));};
\ No newline at end of file