f07e516b28ba24f9c3fdedafcc89e5aaf29a2b46
[motion2.git] / server / api / whatsappInteraction / whatsappInteraction.controller.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0xb21b=['../../components/export/pdf','api','../../config/environment','../../mysqldb','redis','defaults','localhost','./whatsappInteraction.socket','sendStatus','status','end','json','undefined','limit','count','offset','set','Content-Range','update','then','destroy','error','stack','name','send','index','rawAttributes','fieldName','type','key','findAll','query','catch','params','model','keys','filters','intersection','attributes','fields','length','include','merge','options','WhatsappInteraction','find','create','body','describe','addMessage','WhatsappMessage','ids','omit','getMessages','hasOwnProperty','nolimit','order','sort','where','pick','filter','map','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','includeAll','rows','addTags','setTags','spread','color','whatsappInteractionTags:save','removeTags','download','toString','root','server','files','tmp','attachments','join','transcript-%d-%s.zip','User','push','CmContact','Contact','firstName','Attachment','direction','secret','WhatsappAccount','Owner','get','mkdirSync','fullname','N.A.','createdAt','closedAt','Messages','existsSync','createReadStream','pipe','out','System','lastName','basename','read','value','renderFile','views','downloadInteraction.ejs','transcript-','.pdf','Whatsapp','createPdfFromHTML','unlinkSync','rimraf','moment','util','path','ejs','lodash','ioredis','../../components/interaction/service'];(function(_0x13c491,_0x2909a9){var _0x24bf67=function(_0x3514cf){while(--_0x3514cf){_0x13c491['push'](_0x13c491['shift']());}};_0x24bf67(++_0x2909a9);}(_0xb21b,0x1ec));var _0xbb21=function(_0x2d4e1b,_0x58a1d3){_0x2d4e1b=_0x2d4e1b-0x0;var _0x2a0fde=_0xb21b[_0x2d4e1b];return _0x2a0fde;};'use strict';var rimraf=require(_0xbb21('0x0'));var zipdir=require('zip-dir');var moment=require(_0xbb21('0x1'));var BPromise=require('bluebird');var util=require(_0xbb21('0x2'));var path=require(_0xbb21('0x3'));var ejs=require(_0xbb21('0x4'));var fs=require('fs');var _=require(_0xbb21('0x5'));var Redis=require(_0xbb21('0x6'));var interaction=require(_0xbb21('0x7'));var qs=require('../../components/parsers/qs');var pdf=require(_0xbb21('0x8'));var logger=require('../../config/logger')(_0xbb21('0x9'));var config=require(_0xbb21('0xa'));var db=require(_0xbb21('0xb'))['db'];config[_0xbb21('0xc')]=_[_0xbb21('0xd')](config[_0xbb21('0xc')],{'host':_0xbb21('0xe'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0xbb21('0xc')]));require(_0xbb21('0xf'))['register'](socket);function respondWithStatusCode(_0x2ec9a7,_0x40ede5){_0x40ede5=_0x40ede5||0xcc;return function(_0x1ac89d){if(_0x1ac89d){return _0x2ec9a7[_0xbb21('0x10')](_0x40ede5);}return _0x2ec9a7[_0xbb21('0x11')](_0x40ede5)[_0xbb21('0x12')]();};}function respondWithResult(_0x54258e,_0xbaeabb){_0xbaeabb=_0xbaeabb||0xc8;return function(_0x4e6844){if(_0x4e6844){return _0x54258e[_0xbb21('0x11')](_0xbaeabb)[_0xbb21('0x13')](_0x4e6844);}};}function respondWithFilteredResult(_0x4c0b6b,_0xb8cce2){return function(_0x31537c){if(_0x31537c){var _0x48d7cd=typeof _0xb8cce2['offset']===_0xbb21('0x14')&&typeof _0xb8cce2[_0xbb21('0x15')]===_0xbb21('0x14');var _0x460b6f=_0x31537c[_0xbb21('0x16')];var _0x51c9b8=_0x48d7cd?0x0:_0xb8cce2[_0xbb21('0x17')];var _0x58902f=_0x48d7cd?_0x31537c[_0xbb21('0x16')]:_0xb8cce2[_0xbb21('0x17')]+_0xb8cce2[_0xbb21('0x15')];var _0x1c226b;if(_0x58902f>=_0x460b6f){_0x58902f=_0x460b6f;_0x1c226b=0xc8;}else{_0x1c226b=0xce;}_0x4c0b6b[_0xbb21('0x11')](_0x1c226b);return _0x4c0b6b[_0xbb21('0x18')](_0xbb21('0x19'),_0x51c9b8+'-'+_0x58902f+'/'+_0x460b6f)['json'](_0x31537c);}return null;};}function saveUpdates(_0x192849){return function(_0xb7d773){if(_0xb7d773){return _0xb7d773[_0xbb21('0x1a')](_0x192849)[_0xbb21('0x1b')](function(_0x576efb){return _0x576efb;});}return null;};}function removeEntity(_0x452475){return function(_0x4dadcc){if(_0x4dadcc){return _0x4dadcc[_0xbb21('0x1c')]()[_0xbb21('0x1b')](function(){_0x452475[_0xbb21('0x11')](0xcc)[_0xbb21('0x12')]();});}};}function handleEntityNotFound(_0x552bca){return function(_0x5b5429){if(!_0x5b5429){_0x552bca[_0xbb21('0x10')](0x194);}return _0x5b5429;};}function handleError(_0x33005b,_0xdd8d8e){_0xdd8d8e=_0xdd8d8e||0x1f4;return function(_0x33b5fb){logger[_0xbb21('0x1d')](_0x33b5fb[_0xbb21('0x1e')]);if(_0x33b5fb[_0xbb21('0x1f')]){delete _0x33b5fb[_0xbb21('0x1f')];}_0x33005b[_0xbb21('0x11')](_0xdd8d8e)[_0xbb21('0x20')](_0x33b5fb);};}exports[_0xbb21('0x21')]=function(_0x322e8c,_0x303c1c){var _0x4016c3={};var _0x4e18c3=_['map'](db['WhatsappInteraction'][_0xbb21('0x22')],function(_0x1b76b5){return{'name':_0x1b76b5[_0xbb21('0x23')],'type':_0x1b76b5[_0xbb21('0x24')][_0xbb21('0x25')]};});_0x4016c3=qs['getOptions'](_0x4e18c3,_0x322e8c);return interaction[_0xbb21('0x26')]('whatsapp',_0x322e8c[_0xbb21('0x27')],_0x322e8c['user'],_0x4016c3,_0x4e18c3)[_0xbb21('0x1b')](respondWithFilteredResult(_0x303c1c,_0x4016c3))[_0xbb21('0x28')](handleError(_0x303c1c,null));};exports['show']=function(_0xe13e31,_0x4e28fe){var _0x52d014={'raw':![],'where':{'id':_0xe13e31[_0xbb21('0x29')]['id']}},_0x108519={};_0x108519[_0xbb21('0x2a')]=_[_0xbb21('0x2b')](db['WhatsappInteraction'][_0xbb21('0x22')]);_0x108519[_0xbb21('0x27')]=_[_0xbb21('0x2b')](_0xe13e31['query']);_0x108519[_0xbb21('0x2c')]=_[_0xbb21('0x2d')](_0x108519[_0xbb21('0x2a')],_0x108519[_0xbb21('0x27')]);_0x52d014[_0xbb21('0x2e')]=_[_0xbb21('0x2d')](_0x108519['model'],qs['fields'](_0xe13e31[_0xbb21('0x27')][_0xbb21('0x2f')]));_0x52d014[_0xbb21('0x2e')]=_0x52d014[_0xbb21('0x2e')][_0xbb21('0x30')]?_0x52d014['attributes']:_0x108519[_0xbb21('0x2a')];if(_0xe13e31['query']['includeAll']){_0x52d014[_0xbb21('0x31')]=[{'all':!![]}];}_0x52d014=_[_0xbb21('0x32')]({},_0x52d014,_0xe13e31[_0xbb21('0x33')]);return db[_0xbb21('0x34')][_0xbb21('0x35')](_0x52d014)[_0xbb21('0x1b')](handleEntityNotFound(_0x4e28fe,null))[_0xbb21('0x1b')](respondWithResult(_0x4e28fe,null))[_0xbb21('0x28')](handleError(_0x4e28fe,null));};exports[_0xbb21('0x36')]=function(_0x26aaf1,_0x567c5c){return db[_0xbb21('0x34')][_0xbb21('0x36')](_0x26aaf1[_0xbb21('0x37')],{})[_0xbb21('0x1b')](respondWithResult(_0x567c5c,0xc9))[_0xbb21('0x28')](handleError(_0x567c5c,null));};exports[_0xbb21('0x1a')]=function(_0x380333,_0x1fce9d){if(_0x380333[_0xbb21('0x37')]['id']){delete _0x380333[_0xbb21('0x37')]['id'];}return db[_0xbb21('0x34')][_0xbb21('0x35')]({'where':{'id':_0x380333[_0xbb21('0x29')]['id']}})[_0xbb21('0x1b')](handleEntityNotFound(_0x1fce9d,null))[_0xbb21('0x1b')](saveUpdates(_0x380333[_0xbb21('0x37')],null))[_0xbb21('0x1b')](respondWithResult(_0x1fce9d,null))[_0xbb21('0x28')](handleError(_0x1fce9d,null));};exports[_0xbb21('0x1c')]=function(_0x11ac7f,_0x3887b4){return db[_0xbb21('0x34')][_0xbb21('0x35')]({'where':{'id':_0x11ac7f[_0xbb21('0x29')]['id']}})['then'](handleEntityNotFound(_0x3887b4,null))[_0xbb21('0x1b')](removeEntity(_0x3887b4,null))[_0xbb21('0x28')](handleError(_0x3887b4,null));};exports[_0xbb21('0x38')]=function(_0x5059ff,_0x53667d){return db[_0xbb21('0x34')][_0xbb21('0x38')]()[_0xbb21('0x1b')](respondWithResult(_0x53667d,null))[_0xbb21('0x28')](handleError(_0x53667d,null));};exports[_0xbb21('0x39')]=function(_0x4cde66,_0x4d4419){return db[_0xbb21('0x3a')][_0xbb21('0x35')]({'where':{'id':_0x4cde66[_0xbb21('0x29')]['id']}})[_0xbb21('0x1b')](handleEntityNotFound(_0x4d4419,null))['then'](function(_0x191b44){if(_0x191b44){return _0x191b44[_0xbb21('0x39')](_0x4cde66['body'][_0xbb21('0x3b')],_[_0xbb21('0x3c')](_0x4cde66['body'],['ids','id'])||{});}})[_0xbb21('0x1b')](respondWithResult(_0x4d4419,null))[_0xbb21('0x28')](handleError(_0x4d4419,null));};exports[_0xbb21('0x3d')]=function(_0x36a486,_0x19c202){var _0xf467bc={'raw':![],'where':{}};var _0x56c658={};var _0x47e8b1={'count':0x0,'rows':[]};return db[_0xbb21('0x34')]['findOne']({'where':{'id':_0x36a486[_0xbb21('0x29')]['id']}})[_0xbb21('0x1b')](handleEntityNotFound(_0x19c202,null))[_0xbb21('0x1b')](function(_0x575ce3){if(_0x575ce3){_0x56c658[_0xbb21('0x2a')]=_[_0xbb21('0x2b')](db[_0xbb21('0x3a')]['rawAttributes']);_0x56c658[_0xbb21('0x27')]=_['keys'](_0x36a486[_0xbb21('0x27')]);_0x56c658[_0xbb21('0x2c')]=_[_0xbb21('0x2d')](_0x56c658[_0xbb21('0x2a')],_0x56c658[_0xbb21('0x27')]);_0xf467bc['attributes']=_[_0xbb21('0x2d')](_0x56c658['model'],qs[_0xbb21('0x2f')](_0x36a486[_0xbb21('0x27')][_0xbb21('0x2f')]));_0xf467bc['attributes']=_0xf467bc[_0xbb21('0x2e')][_0xbb21('0x30')]?_0xf467bc['attributes']:_0x56c658[_0xbb21('0x2a')];if(!_0x36a486['query'][_0xbb21('0x3e')](_0xbb21('0x3f'))){_0xf467bc['limit']=qs['limit'](_0x36a486[_0xbb21('0x27')][_0xbb21('0x15')]);_0xf467bc[_0xbb21('0x17')]=qs[_0xbb21('0x17')](_0x36a486[_0xbb21('0x27')][_0xbb21('0x17')]);}_0xf467bc[_0xbb21('0x40')]=qs[_0xbb21('0x41')](_0x36a486['query']['sort']);_0xf467bc[_0xbb21('0x42')]=qs[_0xbb21('0x2c')](_[_0xbb21('0x43')](_0x36a486[_0xbb21('0x27')],_0x56c658[_0xbb21('0x2c')]));_0xf467bc[_0xbb21('0x42')]['WhatsappInteractionId']=_0x575ce3['id'];if(_0x36a486[_0xbb21('0x27')][_0xbb21('0x44')]){_0xf467bc[_0xbb21('0x42')]=_[_0xbb21('0x32')](_0xf467bc[_0xbb21('0x42')],{'$or':_[_0xbb21('0x45')](_0xf467bc[_0xbb21('0x2e')],function(_0x1c9aae){var _0x73b9a3={};_0x73b9a3[_0x1c9aae]={'$like':'%'+_0x36a486[_0xbb21('0x27')][_0xbb21('0x44')]+'%'};return _0x73b9a3;})});}if(_0x36a486[_0xbb21('0x27')][_0xbb21('0x46')]){var _0x12d9fe=_0x36a486[_0xbb21('0x27')]['$gte'][_0xbb21('0x47')](',');var _0x2be382={};_0x2be382[_0x12d9fe[0x0]]={'$gte':moment(_0x12d9fe[0x1])[_0xbb21('0x48')](_0xbb21('0x49'))};_0xf467bc[_0xbb21('0x42')]=_[_0xbb21('0x32')](_0xf467bc['where'],_0x2be382);}_0xf467bc=_['merge']({},_0xf467bc,_0x36a486[_0xbb21('0x33')]);return db['WhatsappMessage']['count']({'where':_0xf467bc['where']})[_0xbb21('0x1b')](function(_0x96e9da){_0x47e8b1[_0xbb21('0x16')]=_0x96e9da;if(_0x36a486[_0xbb21('0x27')][_0xbb21('0x4a')]){_0xf467bc[_0xbb21('0x31')]=[{'all':!![]}];}return db[_0xbb21('0x3a')][_0xbb21('0x26')](_0xf467bc);})[_0xbb21('0x1b')](function(_0x4bff1f){_0x47e8b1[_0xbb21('0x4b')]=_0x4bff1f;return _0x47e8b1;});}})[_0xbb21('0x1b')](respondWithFilteredResult(_0x19c202,_0xf467bc))['catch'](handleError(_0x19c202,null));};exports[_0xbb21('0x4c')]=function(_0x30a80a,_0x52a20e){return db[_0xbb21('0x34')][_0xbb21('0x35')]({'where':{'id':_0x30a80a[_0xbb21('0x29')]['id']}})[_0xbb21('0x1b')](handleEntityNotFound(_0x52a20e,null))['then'](function(_0x3096bb){if(_0x3096bb){return _0x3096bb[_0xbb21('0x4d')](_0x30a80a[_0xbb21('0x37')][_0xbb21('0x3b')],_[_0xbb21('0x3c')](_0x30a80a[_0xbb21('0x37')],['ids','id'])||{})[_0xbb21('0x4e')](function(){return db['Tag']['findAll']({'attributes':['id',_0xbb21('0x1f'),_0xbb21('0x4f')],'where':{'id':_0x30a80a[_0xbb21('0x37')]['ids']}});})[_0xbb21('0x1b')](function(_0xe46c44){socket['emit'](_0xbb21('0x50'),{'id':Number(_0x30a80a[_0xbb21('0x29')]['id']),'tags':_0xe46c44||[]});return{'id':Number(_0x30a80a['params']['id']),'tags':_0xe46c44||[]};});}})[_0xbb21('0x1b')](respondWithResult(_0x52a20e,null))[_0xbb21('0x28')](handleError(_0x52a20e,null));};exports[_0xbb21('0x51')]=function(_0x4bc07f,_0x3c07a8){return db[_0xbb21('0x34')][_0xbb21('0x35')]({'where':{'id':_0x4bc07f[_0xbb21('0x29')]['id']}})[_0xbb21('0x1b')](handleEntityNotFound(_0x3c07a8,null))[_0xbb21('0x1b')](function(_0x115d42){if(_0x115d42){return _0x115d42[_0xbb21('0x51')](_0x4bc07f['query'][_0xbb21('0x3b')]);}})[_0xbb21('0x1b')](respondWithStatusCode(_0x3c07a8,null))['catch'](handleError(_0x3c07a8,null));};exports[_0xbb21('0x52')]=function(_0x42f712,_0x21a115){var _0x31ecc4=moment()['unix']()[_0xbb21('0x53')]();var _0x3d451a=path['join'](config[_0xbb21('0x54')],_0xbb21('0x55'),_0xbb21('0x56'),_0xbb21('0x57'));var _0x4d334a=path['join'](config[_0xbb21('0x54')],_0xbb21('0x55'),_0xbb21('0x56'),_0xbb21('0x58'));var _0x8fbfdc=path[_0xbb21('0x59')](_0x3d451a,_0x31ecc4);var _0x61af2f=util[_0xbb21('0x48')](_0xbb21('0x5a'),_0x42f712[_0xbb21('0x29')]['id'],_0x31ecc4);var _0x553467=path[_0xbb21('0x59')](_0x3d451a,_0x61af2f);var _0x201445=[];_0x201445['push']({'model':db['User'],'as':_0xbb21('0x5b'),'attributes':['id','fullname'],'raw':!![]});_0x201445[_0xbb21('0x5c')]({'model':db[_0xbb21('0x5d')],'as':_0xbb21('0x5e'),'attributes':['id',_0xbb21('0x5f'),'lastName'],'raw':!![]});if(_0x42f712['query'][_0xbb21('0x58')]){_0x201445[_0xbb21('0x5c')]({'model':db['Attachment'],'as':_0xbb21('0x60'),'raw':!![]});}var _0x5f13f8=[{'model':db['WhatsappMessage'],'as':'Messages','attributes':['id','body','createdAt',_0xbb21('0x61'),_0xbb21('0x62'),'read'],'include':_0x201445}];_0x5f13f8[_0xbb21('0x5c')]({'model':db[_0xbb21('0x63')],'as':'Account','attributes':['id',_0xbb21('0x1f')]});_0x5f13f8['push']({'model':db[_0xbb21('0x5b')],'as':_0xbb21('0x64'),'attributes':['id','fullname']});return db[_0xbb21('0x34')][_0xbb21('0x35')]({'where':{'id':_0x42f712[_0xbb21('0x29')]['id']},'include':_0x5f13f8})[_0xbb21('0x1b')](handleEntityNotFound(_0x21a115,null))[_0xbb21('0x1b')](function(_0x361e34){if(_0x361e34){var _0x1d6bbf=_0x361e34[_0xbb21('0x65')]({'plain':!![]});fs[_0xbb21('0x66')](_0x8fbfdc);var _0x1900e1={'channel':'Whatsapp','account':_0x1d6bbf['Account'][_0xbb21('0x1f')],'agent':_0x1d6bbf['Owner']?_0x1d6bbf[_0xbb21('0x64')][_0xbb21('0x67')]:_0xbb21('0x68'),'createdAt':moment(_0x1d6bbf[_0xbb21('0x69')])[_0xbb21('0x48')]('YYYY-MM-DD\x20HH:mm:ss')[_0xbb21('0x53')](),'closedAt':_0x1d6bbf['closed']?moment(_0x1d6bbf[_0xbb21('0x6a')])[_0xbb21('0x48')](_0xbb21('0x49'))[_0xbb21('0x53')]():'','disposition':_0x1d6bbf['disposition'],'messages':_(_0x1d6bbf[_0xbb21('0x6b')])[_0xbb21('0x44')]({'secret':![]})[_0xbb21('0x45')](function(_0x564e4a){if(_0x564e4a[_0xbb21('0x60')]){var _0x4ea415=path[_0xbb21('0x59')](_0x4d334a,_0x564e4a[_0xbb21('0x60')]['basename']);if(fs[_0xbb21('0x6c')](_0x4ea415)){fs[_0xbb21('0x6d')](_0x4ea415)[_0xbb21('0x6e')](fs['createWriteStream'](path[_0xbb21('0x59')](_0x8fbfdc,_0x564e4a['Attachment']['basename'])));}}return{'date':moment(_0x564e4a['createdAt'])[_0xbb21('0x48')](_0xbb21('0x49'))['toString'](),'sender':_0x564e4a[_0xbb21('0x61')]===_0xbb21('0x6f')?_0x564e4a[_0xbb21('0x5b')]?_0x564e4a[_0xbb21('0x5b')]['fullname']:_0xbb21('0x70'):_0x564e4a['Contact'][_0xbb21('0x5f')]+(_0x564e4a['Contact'][_0xbb21('0x71')]?'\x20'+_0x564e4a[_0xbb21('0x5e')][_0xbb21('0x71')]:''),'body':_0x564e4a[_0xbb21('0x60')]?_0x564e4a[_0xbb21('0x60')][_0xbb21('0x72')]:_0x564e4a[_0xbb21('0x37')],'direction':_0x564e4a[_0xbb21('0x61')]==='out'?'A':'C','secret':_0x564e4a[_0xbb21('0x62')],'read':_0x564e4a[_0xbb21('0x73')],'attachment':_0x564e4a[_0xbb21('0x60')]};})[_0xbb21('0x74')]()};return ejs[_0xbb21('0x75')](path[_0xbb21('0x59')](config[_0xbb21('0x54')],'server',_0xbb21('0x76'),_0xbb21('0x77')),{'interaction':_0x1900e1})[_0xbb21('0x1b')](function(_0x5ad775){var _0x5d2caa=path[_0xbb21('0x59')](_0x8fbfdc,_0xbb21('0x78')+_0x1d6bbf['id']+'-'+_0x31ecc4+_0xbb21('0x79'));var _0x210a20={'path':_0x5d2caa,'channel':_0xbb21('0x7a'),'interactionId':_0x1d6bbf['id']};return pdf[_0xbb21('0x7b')](_0x5ad775,_0x210a20);})[_0xbb21('0x1b')](function(){return new BPromise(function(_0x4d6f80,_0x3cc2eb){zipdir(_0x8fbfdc,{'saveTo':_0x553467},function(_0x463b4f,_0x1b9cea){if(_0x463b4f)return _0x3cc2eb(_0x463b4f);return _0x4d6f80(_0x1b9cea);});})[_0xbb21('0x1b')](function(){return new BPromise(function(_0x1aa63c,_0x211e96){rimraf(_0x8fbfdc,function(_0x2959fd){if(_0x2959fd)_0x211e96(_0x2959fd);return _0x1aa63c();});});})[_0xbb21('0x1b')](function(){return _0x21a115[_0xbb21('0x52')](_0x553467,_0x61af2f,function(_0x57cddc){if(_0x57cddc){console['log']('err',_0x57cddc);}else{fs[_0xbb21('0x7c')](_0x553467);}});});});}else{return _0x21a115[_0xbb21('0x10')](0xc8);}})[_0xbb21('0x28')](handleError(_0x21a115,null));};