7248c71e74b3ea59faf4ac5ad4b94e11d240d441
[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 _0x4dc7=['Tag','color','whatsappInteractionTags:save','removeTags','unix','toString','join','root','server','files','attachments','transcript-%d-%s.zip','User','fullname','Contact','lastName','push','Attachment','createdAt','direction','secret','read','WhatsappAccount','Owner','get','mkdirSync','Whatsapp','Account','N.A.','closed','closedAt','disposition','basename','existsSync','createReadStream','pipe','createWriteStream','out','System','value','renderFile','views','downloadInteraction.ejs','transcript-','.pdf','createPdfFromHTML','log','err','unlinkSync','rimraf','zip-dir','moment','bluebird','util','ejs','lodash','ioredis','../../components/interaction/service','../../components/parsers/qs','../../mysqldb','defaults','localhost','socket.io-emitter','redis','./whatsappInteraction.socket','register','sendStatus','status','end','limit','count','offset','set','Content-Range','json','update','then','destroy','stack','name','send','index','map','WhatsappInteraction','rawAttributes','fieldName','type','key','findAll','whatsapp','query','show','model','keys','filters','intersection','attributes','fields','length','options','find','catch','create','body','describe','addMessage','WhatsappMessage','omit','ids','findOne','params','nolimit','order','sort','where','pick','WhatsappInteractionId','filter','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','merge','includeAll','include','rows','setTags','spread'];(function(_0x1690f9,_0x3930ea){var _0x40a4ac=function(_0x19423f){while(--_0x19423f){_0x1690f9['push'](_0x1690f9['shift']());}};_0x40a4ac(++_0x3930ea);}(_0x4dc7,0x131));var _0x74dc=function(_0x57d1e8,_0x2603f2){_0x57d1e8=_0x57d1e8-0x0;var _0x4270fd=_0x4dc7[_0x57d1e8];return _0x4270fd;};'use strict';var rimraf=require(_0x74dc('0x0'));var zipdir=require(_0x74dc('0x1'));var moment=require(_0x74dc('0x2'));var BPromise=require(_0x74dc('0x3'));var util=require(_0x74dc('0x4'));var path=require('path');var ejs=require(_0x74dc('0x5'));var fs=require('fs');var _=require(_0x74dc('0x6'));var Redis=require(_0x74dc('0x7'));var interaction=require(_0x74dc('0x8'));var qs=require(_0x74dc('0x9'));var pdf=require('../../components/export/pdf');var logger=require('../../config/logger')('api');var config=require('../../config/environment');var db=require(_0x74dc('0xa'))['db'];config['redis']=_[_0x74dc('0xb')](config['redis'],{'host':_0x74dc('0xc'),'port':0x18eb});var socket=require(_0x74dc('0xd'))(new Redis(config[_0x74dc('0xe')]));require(_0x74dc('0xf'))[_0x74dc('0x10')](socket);function respondWithStatusCode(_0x2fdc10,_0x3bc658){_0x3bc658=_0x3bc658||0xcc;return function(_0x47671a){if(_0x47671a){return _0x2fdc10[_0x74dc('0x11')](_0x3bc658);}return _0x2fdc10[_0x74dc('0x12')](_0x3bc658)[_0x74dc('0x13')]();};}function respondWithResult(_0x5c7c0b,_0xdd6a1a){_0xdd6a1a=_0xdd6a1a||0xc8;return function(_0x4c886d){if(_0x4c886d){return _0x5c7c0b['status'](_0xdd6a1a)['json'](_0x4c886d);}};}function respondWithFilteredResult(_0x9cd796,_0xdee3c6){return function(_0x491d76){if(_0x491d76){var _0xd46f9a=typeof _0xdee3c6['offset']==='undefined'&&typeof _0xdee3c6[_0x74dc('0x14')]==='undefined';var _0x5eca56=_0x491d76[_0x74dc('0x15')];var _0x5ac0a1=_0xd46f9a?0x0:_0xdee3c6[_0x74dc('0x16')];var _0x1c3f67=_0xd46f9a?_0x491d76[_0x74dc('0x15')]:_0xdee3c6['offset']+_0xdee3c6[_0x74dc('0x14')];var _0x20539c;if(_0x1c3f67>=_0x5eca56){_0x1c3f67=_0x5eca56;_0x20539c=0xc8;}else{_0x20539c=0xce;}_0x9cd796[_0x74dc('0x12')](_0x20539c);return _0x9cd796[_0x74dc('0x17')](_0x74dc('0x18'),_0x5ac0a1+'-'+_0x1c3f67+'/'+_0x5eca56)[_0x74dc('0x19')](_0x491d76);}return null;};}function saveUpdates(_0x16f2f8){return function(_0x4ba9d6){if(_0x4ba9d6){return _0x4ba9d6[_0x74dc('0x1a')](_0x16f2f8)[_0x74dc('0x1b')](function(_0x399e9b){return _0x399e9b;});}return null;};}function removeEntity(_0x3bb162){return function(_0xebfe6e){if(_0xebfe6e){return _0xebfe6e[_0x74dc('0x1c')]()[_0x74dc('0x1b')](function(){_0x3bb162[_0x74dc('0x12')](0xcc)[_0x74dc('0x13')]();});}};}function handleEntityNotFound(_0x4c4b2d){return function(_0x53d6ac){if(!_0x53d6ac){_0x4c4b2d['sendStatus'](0x194);}return _0x53d6ac;};}function handleError(_0x28db87,_0x354b24){_0x354b24=_0x354b24||0x1f4;return function(_0x351beb){logger['error'](_0x351beb[_0x74dc('0x1d')]);if(_0x351beb['name']){delete _0x351beb[_0x74dc('0x1e')];}_0x28db87[_0x74dc('0x12')](_0x354b24)[_0x74dc('0x1f')](_0x351beb);};}exports[_0x74dc('0x20')]=function(_0x34b456,_0x4fafb0){var _0x42d855={};var _0x25e166=_[_0x74dc('0x21')](db[_0x74dc('0x22')][_0x74dc('0x23')],function(_0x36faed){return{'name':_0x36faed[_0x74dc('0x24')],'type':_0x36faed[_0x74dc('0x25')][_0x74dc('0x26')]};});_0x42d855=qs['getOptions'](_0x25e166,_0x34b456);return interaction[_0x74dc('0x27')](_0x74dc('0x28'),_0x34b456[_0x74dc('0x29')],_0x34b456['user'],_0x42d855,_0x25e166)[_0x74dc('0x1b')](respondWithFilteredResult(_0x4fafb0,_0x42d855))['catch'](handleError(_0x4fafb0,null));};exports[_0x74dc('0x2a')]=function(_0x52299c,_0x3bfee5){var _0x1f7a9a={'raw':![],'where':{'id':_0x52299c['params']['id']}},_0x36fe37={};_0x36fe37[_0x74dc('0x2b')]=_[_0x74dc('0x2c')](db[_0x74dc('0x22')][_0x74dc('0x23')]);_0x36fe37[_0x74dc('0x29')]=_[_0x74dc('0x2c')](_0x52299c[_0x74dc('0x29')]);_0x36fe37[_0x74dc('0x2d')]=_[_0x74dc('0x2e')](_0x36fe37[_0x74dc('0x2b')],_0x36fe37['query']);_0x1f7a9a[_0x74dc('0x2f')]=_[_0x74dc('0x2e')](_0x36fe37[_0x74dc('0x2b')],qs[_0x74dc('0x30')](_0x52299c[_0x74dc('0x29')][_0x74dc('0x30')]));_0x1f7a9a['attributes']=_0x1f7a9a[_0x74dc('0x2f')][_0x74dc('0x31')]?_0x1f7a9a[_0x74dc('0x2f')]:_0x36fe37[_0x74dc('0x2b')];if(_0x52299c[_0x74dc('0x29')]['includeAll']){_0x1f7a9a['include']=[{'all':!![]}];}_0x1f7a9a=_['merge']({},_0x1f7a9a,_0x52299c[_0x74dc('0x32')]);return db[_0x74dc('0x22')][_0x74dc('0x33')](_0x1f7a9a)['then'](handleEntityNotFound(_0x3bfee5,null))['then'](respondWithResult(_0x3bfee5,null))[_0x74dc('0x34')](handleError(_0x3bfee5,null));};exports['create']=function(_0x3cb7ef,_0x3d1f66){return db['WhatsappInteraction'][_0x74dc('0x35')](_0x3cb7ef[_0x74dc('0x36')],{})['then'](respondWithResult(_0x3d1f66,0xc9))[_0x74dc('0x34')](handleError(_0x3d1f66,null));};exports[_0x74dc('0x1a')]=function(_0x3fb8be,_0x5b7e3a){if(_0x3fb8be[_0x74dc('0x36')]['id']){delete _0x3fb8be[_0x74dc('0x36')]['id'];}return db[_0x74dc('0x22')]['find']({'where':{'id':_0x3fb8be['params']['id']}})[_0x74dc('0x1b')](handleEntityNotFound(_0x5b7e3a,null))[_0x74dc('0x1b')](saveUpdates(_0x3fb8be[_0x74dc('0x36')],null))['then'](respondWithResult(_0x5b7e3a,null))[_0x74dc('0x34')](handleError(_0x5b7e3a,null));};exports[_0x74dc('0x1c')]=function(_0x567a8c,_0xcc952c){return db[_0x74dc('0x22')]['find']({'where':{'id':_0x567a8c['params']['id']}})[_0x74dc('0x1b')](handleEntityNotFound(_0xcc952c,null))[_0x74dc('0x1b')](removeEntity(_0xcc952c,null))[_0x74dc('0x34')](handleError(_0xcc952c,null));};exports[_0x74dc('0x37')]=function(_0x51ef07,_0x30bdb6){return db['WhatsappInteraction']['describe']()['then'](respondWithResult(_0x30bdb6,null))[_0x74dc('0x34')](handleError(_0x30bdb6,null));};exports[_0x74dc('0x38')]=function(_0x4704c2,_0x2728e5){return db[_0x74dc('0x39')]['find']({'where':{'id':_0x4704c2['params']['id']}})[_0x74dc('0x1b')](handleEntityNotFound(_0x2728e5,null))[_0x74dc('0x1b')](function(_0xb9e549){if(_0xb9e549){return _0xb9e549[_0x74dc('0x38')](_0x4704c2[_0x74dc('0x36')]['ids'],_[_0x74dc('0x3a')](_0x4704c2[_0x74dc('0x36')],[_0x74dc('0x3b'),'id'])||{});}})[_0x74dc('0x1b')](respondWithResult(_0x2728e5,null))[_0x74dc('0x34')](handleError(_0x2728e5,null));};exports['getMessages']=function(_0x1ea191,_0x1f07ba){var _0x14cb08={'raw':![],'where':{}};var _0x40090e={};var _0x576d4d={'count':0x0,'rows':[]};return db[_0x74dc('0x22')][_0x74dc('0x3c')]({'where':{'id':_0x1ea191[_0x74dc('0x3d')]['id']}})[_0x74dc('0x1b')](handleEntityNotFound(_0x1f07ba,null))[_0x74dc('0x1b')](function(_0x468f6b){if(_0x468f6b){_0x40090e[_0x74dc('0x2b')]=_['keys'](db[_0x74dc('0x39')]['rawAttributes']);_0x40090e['query']=_['keys'](_0x1ea191[_0x74dc('0x29')]);_0x40090e[_0x74dc('0x2d')]=_[_0x74dc('0x2e')](_0x40090e[_0x74dc('0x2b')],_0x40090e[_0x74dc('0x29')]);_0x14cb08['attributes']=_[_0x74dc('0x2e')](_0x40090e['model'],qs[_0x74dc('0x30')](_0x1ea191[_0x74dc('0x29')][_0x74dc('0x30')]));_0x14cb08['attributes']=_0x14cb08[_0x74dc('0x2f')][_0x74dc('0x31')]?_0x14cb08['attributes']:_0x40090e['model'];if(!_0x1ea191[_0x74dc('0x29')]['hasOwnProperty'](_0x74dc('0x3e'))){_0x14cb08[_0x74dc('0x14')]=qs[_0x74dc('0x14')](_0x1ea191['query'][_0x74dc('0x14')]);_0x14cb08[_0x74dc('0x16')]=qs['offset'](_0x1ea191[_0x74dc('0x29')]['offset']);}_0x14cb08[_0x74dc('0x3f')]=qs[_0x74dc('0x40')](_0x1ea191[_0x74dc('0x29')]['sort']);_0x14cb08[_0x74dc('0x41')]=qs[_0x74dc('0x2d')](_[_0x74dc('0x42')](_0x1ea191['query'],_0x40090e[_0x74dc('0x2d')]));_0x14cb08['where'][_0x74dc('0x43')]=_0x468f6b['id'];if(_0x1ea191[_0x74dc('0x29')][_0x74dc('0x44')]){_0x14cb08[_0x74dc('0x41')]=_['merge'](_0x14cb08[_0x74dc('0x41')],{'$or':_[_0x74dc('0x21')](_0x14cb08[_0x74dc('0x2f')],function(_0xae82fc){var _0x261182={};_0x261182[_0xae82fc]={'$like':'%'+_0x1ea191[_0x74dc('0x29')][_0x74dc('0x44')]+'%'};return _0x261182;})});}if(_0x1ea191['query'][_0x74dc('0x45')]){var _0x1c55f7=_0x1ea191[_0x74dc('0x29')][_0x74dc('0x45')][_0x74dc('0x46')](',');var _0x2372be={};_0x2372be[_0x1c55f7[0x0]]={'$gte':moment(_0x1c55f7[0x1])[_0x74dc('0x47')](_0x74dc('0x48'))};_0x14cb08[_0x74dc('0x41')]=_[_0x74dc('0x49')](_0x14cb08[_0x74dc('0x41')],_0x2372be);}_0x14cb08=_[_0x74dc('0x49')]({},_0x14cb08,_0x1ea191[_0x74dc('0x32')]);return db[_0x74dc('0x39')]['count']({'where':_0x14cb08[_0x74dc('0x41')]})[_0x74dc('0x1b')](function(_0x159054){_0x576d4d[_0x74dc('0x15')]=_0x159054;if(_0x1ea191['query'][_0x74dc('0x4a')]){_0x14cb08[_0x74dc('0x4b')]=[{'all':!![]}];}return db[_0x74dc('0x39')][_0x74dc('0x27')](_0x14cb08);})[_0x74dc('0x1b')](function(_0x32e6cc){_0x576d4d[_0x74dc('0x4c')]=_0x32e6cc;return _0x576d4d;});}})[_0x74dc('0x1b')](respondWithFilteredResult(_0x1f07ba,_0x14cb08))['catch'](handleError(_0x1f07ba,null));};exports['addTags']=function(_0x21b7e8,_0x342545){return db['WhatsappInteraction'][_0x74dc('0x33')]({'where':{'id':_0x21b7e8['params']['id']}})[_0x74dc('0x1b')](handleEntityNotFound(_0x342545,null))[_0x74dc('0x1b')](function(_0x5d959b){if(_0x5d959b){return _0x5d959b[_0x74dc('0x4d')](_0x21b7e8[_0x74dc('0x36')][_0x74dc('0x3b')],_[_0x74dc('0x3a')](_0x21b7e8['body'],['ids','id'])||{})[_0x74dc('0x4e')](function(){return db[_0x74dc('0x4f')][_0x74dc('0x27')]({'attributes':['id',_0x74dc('0x1e'),_0x74dc('0x50')],'where':{'id':_0x21b7e8[_0x74dc('0x36')][_0x74dc('0x3b')]}});})[_0x74dc('0x1b')](function(_0x108523){socket['emit'](_0x74dc('0x51'),{'id':Number(_0x21b7e8[_0x74dc('0x3d')]['id']),'tags':_0x108523||[]});return{'id':Number(_0x21b7e8[_0x74dc('0x3d')]['id']),'tags':_0x108523||[]};});}})['then'](respondWithResult(_0x342545,null))[_0x74dc('0x34')](handleError(_0x342545,null));};exports[_0x74dc('0x52')]=function(_0x1c5d8c,_0x1a5d4b){return db[_0x74dc('0x22')]['find']({'where':{'id':_0x1c5d8c[_0x74dc('0x3d')]['id']}})[_0x74dc('0x1b')](handleEntityNotFound(_0x1a5d4b,null))[_0x74dc('0x1b')](function(_0x553d6f){if(_0x553d6f){return _0x553d6f['removeTags'](_0x1c5d8c[_0x74dc('0x29')][_0x74dc('0x3b')]);}})[_0x74dc('0x1b')](respondWithStatusCode(_0x1a5d4b,null))[_0x74dc('0x34')](handleError(_0x1a5d4b,null));};exports['download']=function(_0x4b5096,_0x1e1afe){var _0x1146e3=moment()[_0x74dc('0x53')]()[_0x74dc('0x54')]();var _0x4bb3aa=path[_0x74dc('0x55')](config[_0x74dc('0x56')],_0x74dc('0x57'),_0x74dc('0x58'),'tmp');var _0x29ea17=path['join'](config[_0x74dc('0x56')],_0x74dc('0x57'),_0x74dc('0x58'),_0x74dc('0x59'));var _0x212117=path['join'](_0x4bb3aa,_0x1146e3);var _0x5b0f48=util[_0x74dc('0x47')](_0x74dc('0x5a'),_0x4b5096[_0x74dc('0x3d')]['id'],_0x1146e3);var _0x298161=path[_0x74dc('0x55')](_0x4bb3aa,_0x5b0f48);var _0x257d40=[];_0x257d40['push']({'model':db[_0x74dc('0x5b')],'as':'User','attributes':['id',_0x74dc('0x5c')],'raw':!![]});_0x257d40['push']({'model':db['CmContact'],'as':_0x74dc('0x5d'),'attributes':['id','firstName',_0x74dc('0x5e')],'raw':!![]});if(_0x4b5096[_0x74dc('0x29')]['attachments']){_0x257d40[_0x74dc('0x5f')]({'model':db[_0x74dc('0x60')],'as':_0x74dc('0x60'),'raw':!![]});}var _0x1860b9=[{'model':db[_0x74dc('0x39')],'as':'Messages','attributes':['id',_0x74dc('0x36'),_0x74dc('0x61'),_0x74dc('0x62'),_0x74dc('0x63'),_0x74dc('0x64')],'include':_0x257d40}];_0x1860b9[_0x74dc('0x5f')]({'model':db[_0x74dc('0x65')],'as':'Account','attributes':['id',_0x74dc('0x1e')]});_0x1860b9[_0x74dc('0x5f')]({'model':db[_0x74dc('0x5b')],'as':_0x74dc('0x66'),'attributes':['id','fullname']});return db[_0x74dc('0x22')][_0x74dc('0x33')]({'where':{'id':_0x4b5096[_0x74dc('0x3d')]['id']},'include':_0x1860b9})['then'](handleEntityNotFound(_0x1e1afe,null))[_0x74dc('0x1b')](function(_0x50c0be){if(_0x50c0be){var _0x20f578=_0x50c0be[_0x74dc('0x67')]({'plain':!![]});fs[_0x74dc('0x68')](_0x212117);var _0x5ac2e2={'channel':_0x74dc('0x69'),'account':_0x20f578[_0x74dc('0x6a')]['name'],'agent':_0x20f578[_0x74dc('0x66')]?_0x20f578[_0x74dc('0x66')][_0x74dc('0x5c')]:_0x74dc('0x6b'),'createdAt':moment(_0x20f578['createdAt'])[_0x74dc('0x47')](_0x74dc('0x48'))[_0x74dc('0x54')](),'closedAt':_0x20f578[_0x74dc('0x6c')]?moment(_0x20f578[_0x74dc('0x6d')])[_0x74dc('0x47')](_0x74dc('0x48'))[_0x74dc('0x54')]():'','disposition':_0x20f578[_0x74dc('0x6e')],'messages':_(_0x20f578['Messages'])[_0x74dc('0x44')]({'secret':![]})[_0x74dc('0x21')](function(_0x241a84){if(_0x241a84[_0x74dc('0x60')]){var _0x486355=path['join'](_0x29ea17,_0x241a84[_0x74dc('0x60')][_0x74dc('0x6f')]);if(fs[_0x74dc('0x70')](_0x486355)){fs[_0x74dc('0x71')](_0x486355)[_0x74dc('0x72')](fs[_0x74dc('0x73')](path[_0x74dc('0x55')](_0x212117,_0x241a84[_0x74dc('0x60')]['basename'])));}}return{'date':moment(_0x241a84['createdAt'])[_0x74dc('0x47')](_0x74dc('0x48'))[_0x74dc('0x54')](),'sender':_0x241a84[_0x74dc('0x62')]===_0x74dc('0x74')?_0x241a84[_0x74dc('0x5b')]?_0x241a84[_0x74dc('0x5b')]['fullname']:_0x74dc('0x75'):_0x241a84[_0x74dc('0x5d')]['firstName']+(_0x241a84[_0x74dc('0x5d')]['lastName']?'\x20'+_0x241a84['Contact']['lastName']:''),'body':_0x241a84[_0x74dc('0x60')]?_0x241a84[_0x74dc('0x60')][_0x74dc('0x6f')]:_0x241a84[_0x74dc('0x36')],'direction':_0x241a84['direction']==='out'?'A':'C','secret':_0x241a84[_0x74dc('0x63')],'read':_0x241a84[_0x74dc('0x64')],'attachment':_0x241a84[_0x74dc('0x60')]};})[_0x74dc('0x76')]()};return ejs[_0x74dc('0x77')](path[_0x74dc('0x55')](config[_0x74dc('0x56')],'server',_0x74dc('0x78'),_0x74dc('0x79')),{'interaction':_0x5ac2e2})['then'](function(_0x502843){var _0x6f2dab=path[_0x74dc('0x55')](_0x212117,_0x74dc('0x7a')+_0x20f578['id']+'-'+_0x1146e3+_0x74dc('0x7b'));var _0xff410b={'path':_0x6f2dab,'channel':_0x74dc('0x69'),'interactionId':_0x20f578['id']};return pdf[_0x74dc('0x7c')](_0x502843,_0xff410b);})['then'](function(){return new BPromise(function(_0xd66fa7,_0x366fbb){zipdir(_0x212117,{'saveTo':_0x298161},function(_0x19341b,_0x5ee2a1){if(_0x19341b)return _0x366fbb(_0x19341b);return _0xd66fa7(_0x5ee2a1);});})[_0x74dc('0x1b')](function(){return new BPromise(function(_0x1432d2,_0x327e15){rimraf(_0x212117,function(_0x427711){if(_0x427711)_0x327e15(_0x427711);return _0x1432d2();});});})[_0x74dc('0x1b')](function(){return _0x1e1afe['download'](_0x298161,_0x5b0f48,function(_0x5cd324){if(_0x5cd324){console[_0x74dc('0x7d')](_0x74dc('0x7e'),_0x5cd324);}else{fs[_0x74dc('0x7f')](_0x298161);}});});});}else{return _0x1e1afe[_0x74dc('0x11')](0xc8);}})[_0x74dc('0x34')](handleError(_0x1e1afe,null));};