05e5750948cbd2982345caa85c295a39648100db
[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 _0xe421=['Whatsapp','N.A.','format','closed','YYYY-MM-DD\x20HH:mm:ss','disposition','createReadStream','pipe','basename','direction','out','System','firstName','value','renderFile','downloadInteraction.ejs','transcript-','.pdf','createPdfFromHTML','err','unlinkSync','rimraf','zip-dir','bluebird','util','path','ejs','lodash','../../components/interaction/service','../../components/parsers/qs','../../components/export/pdf','../../config/logger','api','../../config/environment','../../mysqldb','redis','defaults','./whatsappInteraction.socket','register','sendStatus','status','end','offset','undefined','limit','set','update','destroy','error','stack','name','index','map','WhatsappInteraction','fieldName','getOptions','findAll','whatsapp','then','catch','show','params','model','keys','rawAttributes','query','filters','intersection','attributes','fields','length','includeAll','include','merge','options','find','create','body','describe','ids','omit','getMessages','WhatsappMessage','hasOwnProperty','nolimit','order','sort','where','pick','WhatsappInteractionId','filter','$gte','split','count','rows','setTags','spread','Tag','color','emit','whatsappInteractionTags:save','removeTags','download','unix','toString','join','server','root','files','attachments','transcript-%d-%s.zip','push','User','Contact','lastName','Attachment','createdAt','secret','read','WhatsappAccount','Account','Owner','fullname','get','mkdirSync'];(function(_0x46a0ef,_0x31e1ec){var _0x182ad2=function(_0x353fde){while(--_0x353fde){_0x46a0ef['push'](_0x46a0ef['shift']());}};_0x182ad2(++_0x31e1ec);}(_0xe421,0x10f));var _0x1e42=function(_0x514ac2,_0xd8f110){_0x514ac2=_0x514ac2-0x0;var _0x2265b7=_0xe421[_0x514ac2];return _0x2265b7;};'use strict';var rimraf=require(_0x1e42('0x0'));var zipdir=require(_0x1e42('0x1'));var moment=require('moment');var BPromise=require(_0x1e42('0x2'));var util=require(_0x1e42('0x3'));var path=require(_0x1e42('0x4'));var ejs=require(_0x1e42('0x5'));var fs=require('fs');var _=require(_0x1e42('0x6'));var Redis=require('ioredis');var interaction=require(_0x1e42('0x7'));var qs=require(_0x1e42('0x8'));var pdf=require(_0x1e42('0x9'));var logger=require(_0x1e42('0xa'))(_0x1e42('0xb'));var config=require(_0x1e42('0xc'));var db=require(_0x1e42('0xd'))['db'];config[_0x1e42('0xe')]=_[_0x1e42('0xf')](config[_0x1e42('0xe')],{'host':'localhost','port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config['redis']));require(_0x1e42('0x10'))[_0x1e42('0x11')](socket);function respondWithStatusCode(_0x273e08,_0x36847f){_0x36847f=_0x36847f||0xcc;return function(_0x1832e7){if(_0x1832e7){return _0x273e08[_0x1e42('0x12')](_0x36847f);}return _0x273e08[_0x1e42('0x13')](_0x36847f)[_0x1e42('0x14')]();};}function respondWithResult(_0x4a65da,_0x46b9fc){_0x46b9fc=_0x46b9fc||0xc8;return function(_0x13d52e){if(_0x13d52e){return _0x4a65da[_0x1e42('0x13')](_0x46b9fc)['json'](_0x13d52e);}};}function respondWithFilteredResult(_0x216e63,_0x33cfc1){return function(_0x3d4bdf){if(_0x3d4bdf){var _0x189acb=typeof _0x33cfc1[_0x1e42('0x15')]===_0x1e42('0x16')&&typeof _0x33cfc1[_0x1e42('0x17')]===_0x1e42('0x16');var _0x679e86=_0x3d4bdf['count'];var _0x17693c=_0x189acb?0x0:_0x33cfc1[_0x1e42('0x15')];var _0x5c66c9=_0x189acb?_0x3d4bdf['count']:_0x33cfc1['offset']+_0x33cfc1['limit'];var _0x2b14f2;if(_0x5c66c9>=_0x679e86){_0x5c66c9=_0x679e86;_0x2b14f2=0xc8;}else{_0x2b14f2=0xce;}_0x216e63[_0x1e42('0x13')](_0x2b14f2);return _0x216e63[_0x1e42('0x18')]('Content-Range',_0x17693c+'-'+_0x5c66c9+'/'+_0x679e86)['json'](_0x3d4bdf);}return null;};}function saveUpdates(_0x471fea){return function(_0x294247){if(_0x294247){return _0x294247[_0x1e42('0x19')](_0x471fea)['then'](function(_0x19cd04){return _0x19cd04;});}return null;};}function removeEntity(_0xdbd3f5){return function(_0x23375e){if(_0x23375e){return _0x23375e[_0x1e42('0x1a')]()['then'](function(){_0xdbd3f5[_0x1e42('0x13')](0xcc)[_0x1e42('0x14')]();});}};}function handleEntityNotFound(_0x342f15){return function(_0x5d4646){if(!_0x5d4646){_0x342f15['sendStatus'](0x194);}return _0x5d4646;};}function handleError(_0x1b2540,_0x27da7b){_0x27da7b=_0x27da7b||0x1f4;return function(_0x19c25f){logger[_0x1e42('0x1b')](_0x19c25f[_0x1e42('0x1c')]);if(_0x19c25f[_0x1e42('0x1d')]){delete _0x19c25f[_0x1e42('0x1d')];}_0x1b2540['status'](_0x27da7b)['send'](_0x19c25f);};}exports[_0x1e42('0x1e')]=function(_0x2fc082,_0x3cc34f){var _0x3d07f2={};var _0x378fae=_[_0x1e42('0x1f')](db[_0x1e42('0x20')]['rawAttributes'],function(_0xdad02){return{'name':_0xdad02[_0x1e42('0x21')],'type':_0xdad02['type']['key']};});_0x3d07f2=qs[_0x1e42('0x22')](_0x378fae,_0x2fc082);return interaction[_0x1e42('0x23')](_0x1e42('0x24'),_0x2fc082['query'],_0x2fc082['user'],_0x3d07f2,_0x378fae)[_0x1e42('0x25')](respondWithFilteredResult(_0x3cc34f,_0x3d07f2))[_0x1e42('0x26')](handleError(_0x3cc34f,null));};exports[_0x1e42('0x27')]=function(_0x222991,_0x4e9179){var _0x33493b={'raw':![],'where':{'id':_0x222991[_0x1e42('0x28')]['id']}},_0x7d43d3={};_0x7d43d3[_0x1e42('0x29')]=_[_0x1e42('0x2a')](db[_0x1e42('0x20')][_0x1e42('0x2b')]);_0x7d43d3[_0x1e42('0x2c')]=_[_0x1e42('0x2a')](_0x222991[_0x1e42('0x2c')]);_0x7d43d3[_0x1e42('0x2d')]=_[_0x1e42('0x2e')](_0x7d43d3['model'],_0x7d43d3[_0x1e42('0x2c')]);_0x33493b[_0x1e42('0x2f')]=_[_0x1e42('0x2e')](_0x7d43d3['model'],qs[_0x1e42('0x30')](_0x222991[_0x1e42('0x2c')]['fields']));_0x33493b['attributes']=_0x33493b[_0x1e42('0x2f')][_0x1e42('0x31')]?_0x33493b['attributes']:_0x7d43d3[_0x1e42('0x29')];if(_0x222991[_0x1e42('0x2c')][_0x1e42('0x32')]){_0x33493b[_0x1e42('0x33')]=[{'all':!![]}];}_0x33493b=_[_0x1e42('0x34')]({},_0x33493b,_0x222991[_0x1e42('0x35')]);return db[_0x1e42('0x20')][_0x1e42('0x36')](_0x33493b)['then'](handleEntityNotFound(_0x4e9179,null))[_0x1e42('0x25')](respondWithResult(_0x4e9179,null))[_0x1e42('0x26')](handleError(_0x4e9179,null));};exports[_0x1e42('0x37')]=function(_0x138fdb,_0x495b19){return db[_0x1e42('0x20')][_0x1e42('0x37')](_0x138fdb[_0x1e42('0x38')],{})['then'](respondWithResult(_0x495b19,0xc9))['catch'](handleError(_0x495b19,null));};exports[_0x1e42('0x19')]=function(_0x348b15,_0x15c97f){if(_0x348b15['body']['id']){delete _0x348b15[_0x1e42('0x38')]['id'];}return db[_0x1e42('0x20')]['find']({'where':{'id':_0x348b15[_0x1e42('0x28')]['id']}})[_0x1e42('0x25')](handleEntityNotFound(_0x15c97f,null))[_0x1e42('0x25')](saveUpdates(_0x348b15[_0x1e42('0x38')],null))[_0x1e42('0x25')](respondWithResult(_0x15c97f,null))[_0x1e42('0x26')](handleError(_0x15c97f,null));};exports[_0x1e42('0x1a')]=function(_0x45ecdd,_0xe13d79){return db['WhatsappInteraction'][_0x1e42('0x36')]({'where':{'id':_0x45ecdd[_0x1e42('0x28')]['id']}})[_0x1e42('0x25')](handleEntityNotFound(_0xe13d79,null))[_0x1e42('0x25')](removeEntity(_0xe13d79,null))[_0x1e42('0x26')](handleError(_0xe13d79,null));};exports[_0x1e42('0x39')]=function(_0x22fa42,_0x62c37d){return db['WhatsappInteraction'][_0x1e42('0x39')]()[_0x1e42('0x25')](respondWithResult(_0x62c37d,null))['catch'](handleError(_0x62c37d,null));};exports['addMessage']=function(_0x28c625,_0x231c38){return db['WhatsappMessage'][_0x1e42('0x36')]({'where':{'id':_0x28c625[_0x1e42('0x28')]['id']}})[_0x1e42('0x25')](handleEntityNotFound(_0x231c38,null))[_0x1e42('0x25')](function(_0x218815){if(_0x218815){return _0x218815['addMessage'](_0x28c625[_0x1e42('0x38')][_0x1e42('0x3a')],_[_0x1e42('0x3b')](_0x28c625[_0x1e42('0x38')],[_0x1e42('0x3a'),'id'])||{});}})['then'](respondWithResult(_0x231c38,null))['catch'](handleError(_0x231c38,null));};exports[_0x1e42('0x3c')]=function(_0x534d46,_0x534398){var _0x2a9ec6={'raw':![],'where':{}};var _0xf8b09b={};var _0x2cdd6b={'count':0x0,'rows':[]};return db[_0x1e42('0x20')]['findOne']({'where':{'id':_0x534d46[_0x1e42('0x28')]['id']}})[_0x1e42('0x25')](handleEntityNotFound(_0x534398,null))[_0x1e42('0x25')](function(_0x18067a){if(_0x18067a){_0xf8b09b[_0x1e42('0x29')]=_[_0x1e42('0x2a')](db[_0x1e42('0x3d')][_0x1e42('0x2b')]);_0xf8b09b[_0x1e42('0x2c')]=_[_0x1e42('0x2a')](_0x534d46[_0x1e42('0x2c')]);_0xf8b09b[_0x1e42('0x2d')]=_[_0x1e42('0x2e')](_0xf8b09b[_0x1e42('0x29')],_0xf8b09b[_0x1e42('0x2c')]);_0x2a9ec6[_0x1e42('0x2f')]=_[_0x1e42('0x2e')](_0xf8b09b[_0x1e42('0x29')],qs[_0x1e42('0x30')](_0x534d46[_0x1e42('0x2c')][_0x1e42('0x30')]));_0x2a9ec6[_0x1e42('0x2f')]=_0x2a9ec6[_0x1e42('0x2f')][_0x1e42('0x31')]?_0x2a9ec6[_0x1e42('0x2f')]:_0xf8b09b[_0x1e42('0x29')];if(!_0x534d46[_0x1e42('0x2c')][_0x1e42('0x3e')](_0x1e42('0x3f'))){_0x2a9ec6['limit']=qs[_0x1e42('0x17')](_0x534d46['query'][_0x1e42('0x17')]);_0x2a9ec6['offset']=qs[_0x1e42('0x15')](_0x534d46[_0x1e42('0x2c')][_0x1e42('0x15')]);}_0x2a9ec6[_0x1e42('0x40')]=qs[_0x1e42('0x41')](_0x534d46[_0x1e42('0x2c')]['sort']);_0x2a9ec6[_0x1e42('0x42')]=qs[_0x1e42('0x2d')](_[_0x1e42('0x43')](_0x534d46[_0x1e42('0x2c')],_0xf8b09b[_0x1e42('0x2d')]));_0x2a9ec6['where'][_0x1e42('0x44')]=_0x18067a['id'];if(_0x534d46[_0x1e42('0x2c')][_0x1e42('0x45')]){_0x2a9ec6[_0x1e42('0x42')]=_[_0x1e42('0x34')](_0x2a9ec6[_0x1e42('0x42')],{'$or':_[_0x1e42('0x1f')](_0x2a9ec6[_0x1e42('0x2f')],function(_0x12d33d){var _0x2699d5={};_0x2699d5[_0x12d33d]={'$like':'%'+_0x534d46[_0x1e42('0x2c')][_0x1e42('0x45')]+'%'};return _0x2699d5;})});}if(_0x534d46['query'][_0x1e42('0x46')]){var _0x241d76=_0x534d46[_0x1e42('0x2c')][_0x1e42('0x46')][_0x1e42('0x47')](',');var _0xb7dc4a={};_0xb7dc4a[_0x241d76[0x0]]={'$gte':moment(_0x241d76[0x1])['format']('YYYY-MM-DD\x20HH:mm:ss')};_0x2a9ec6[_0x1e42('0x42')]=_[_0x1e42('0x34')](_0x2a9ec6[_0x1e42('0x42')],_0xb7dc4a);}_0x2a9ec6=_[_0x1e42('0x34')]({},_0x2a9ec6,_0x534d46[_0x1e42('0x35')]);return db['WhatsappMessage']['count']({'where':_0x2a9ec6[_0x1e42('0x42')]})['then'](function(_0x45aa73){_0x2cdd6b[_0x1e42('0x48')]=_0x45aa73;if(_0x534d46[_0x1e42('0x2c')]['includeAll']){_0x2a9ec6[_0x1e42('0x33')]=[{'all':!![]}];}return db[_0x1e42('0x3d')][_0x1e42('0x23')](_0x2a9ec6);})['then'](function(_0x4d0200){_0x2cdd6b[_0x1e42('0x49')]=_0x4d0200;return _0x2cdd6b;});}})['then'](respondWithFilteredResult(_0x534398,_0x2a9ec6))[_0x1e42('0x26')](handleError(_0x534398,null));};exports['addTags']=function(_0x396d91,_0x2a04bc){return db[_0x1e42('0x20')][_0x1e42('0x36')]({'where':{'id':_0x396d91['params']['id']}})[_0x1e42('0x25')](handleEntityNotFound(_0x2a04bc,null))['then'](function(_0x2f9aca){if(_0x2f9aca){return _0x2f9aca[_0x1e42('0x4a')](_0x396d91['body'][_0x1e42('0x3a')],_[_0x1e42('0x3b')](_0x396d91[_0x1e42('0x38')],[_0x1e42('0x3a'),'id'])||{})[_0x1e42('0x4b')](function(){return db[_0x1e42('0x4c')][_0x1e42('0x23')]({'attributes':['id','name',_0x1e42('0x4d')],'where':{'id':_0x396d91[_0x1e42('0x38')][_0x1e42('0x3a')]}});})['then'](function(_0x1ed838){socket[_0x1e42('0x4e')](_0x1e42('0x4f'),{'id':Number(_0x396d91['params']['id']),'tags':_0x1ed838||[]});return{'id':Number(_0x396d91[_0x1e42('0x28')]['id']),'tags':_0x1ed838||[]};});}})[_0x1e42('0x25')](respondWithResult(_0x2a04bc,null))['catch'](handleError(_0x2a04bc,null));};exports[_0x1e42('0x50')]=function(_0x32f265,_0x283171){return db[_0x1e42('0x20')][_0x1e42('0x36')]({'where':{'id':_0x32f265['params']['id']}})[_0x1e42('0x25')](handleEntityNotFound(_0x283171,null))[_0x1e42('0x25')](function(_0x5e43e2){if(_0x5e43e2){return _0x5e43e2[_0x1e42('0x50')](_0x32f265['query'][_0x1e42('0x3a')]);}})[_0x1e42('0x25')](respondWithStatusCode(_0x283171,null))[_0x1e42('0x26')](handleError(_0x283171,null));};exports[_0x1e42('0x51')]=function(_0xc98977,_0x2c92ea){var _0xbb92eb=moment()[_0x1e42('0x52')]()[_0x1e42('0x53')]();var _0x202b87=path[_0x1e42('0x54')](config['root'],_0x1e42('0x55'),'files','tmp');var _0x1d1a0b=path[_0x1e42('0x54')](config[_0x1e42('0x56')],_0x1e42('0x55'),_0x1e42('0x57'),_0x1e42('0x58'));var _0x2da49a=path[_0x1e42('0x54')](_0x202b87,_0xbb92eb);var _0x237099=util['format'](_0x1e42('0x59'),_0xc98977[_0x1e42('0x28')]['id'],_0xbb92eb);var _0x3bc910=path[_0x1e42('0x54')](_0x202b87,_0x237099);var _0x51546b=[];_0x51546b[_0x1e42('0x5a')]({'model':db[_0x1e42('0x5b')],'as':_0x1e42('0x5b'),'attributes':['id','fullname'],'raw':!![]});_0x51546b[_0x1e42('0x5a')]({'model':db['CmContact'],'as':_0x1e42('0x5c'),'attributes':['id','firstName',_0x1e42('0x5d')],'raw':!![]});if(_0xc98977[_0x1e42('0x2c')][_0x1e42('0x58')]){_0x51546b['push']({'model':db[_0x1e42('0x5e')],'as':'Attachment','raw':!![]});}var _0x1aa483=[{'model':db[_0x1e42('0x3d')],'as':'Messages','attributes':['id',_0x1e42('0x38'),_0x1e42('0x5f'),'direction',_0x1e42('0x60'),_0x1e42('0x61')],'include':_0x51546b}];_0x1aa483[_0x1e42('0x5a')]({'model':db[_0x1e42('0x62')],'as':_0x1e42('0x63'),'attributes':['id',_0x1e42('0x1d')]});_0x1aa483[_0x1e42('0x5a')]({'model':db['User'],'as':_0x1e42('0x64'),'attributes':['id',_0x1e42('0x65')]});return db['WhatsappInteraction'][_0x1e42('0x36')]({'where':{'id':_0xc98977[_0x1e42('0x28')]['id']},'include':_0x1aa483})['then'](handleEntityNotFound(_0x2c92ea,null))[_0x1e42('0x25')](function(_0xb0b8f3){if(_0xb0b8f3){var _0x38e23f=_0xb0b8f3[_0x1e42('0x66')]({'plain':!![]});fs[_0x1e42('0x67')](_0x2da49a);var _0x2a0098={'channel':_0x1e42('0x68'),'account':_0x38e23f[_0x1e42('0x63')][_0x1e42('0x1d')],'agent':_0x38e23f[_0x1e42('0x64')]?_0x38e23f['Owner'][_0x1e42('0x65')]:_0x1e42('0x69'),'createdAt':moment(_0x38e23f['createdAt'])[_0x1e42('0x6a')]('YYYY-MM-DD\x20HH:mm:ss')['toString'](),'closedAt':_0x38e23f[_0x1e42('0x6b')]?moment(_0x38e23f['closedAt'])[_0x1e42('0x6a')](_0x1e42('0x6c'))[_0x1e42('0x53')]():'','disposition':_0x38e23f[_0x1e42('0x6d')],'messages':_(_0x38e23f['Messages'])[_0x1e42('0x45')]({'secret':![]})[_0x1e42('0x1f')](function(_0x23deb9){if(_0x23deb9[_0x1e42('0x5e')]){var _0x4a4bf5=path[_0x1e42('0x54')](_0x1d1a0b,_0x23deb9[_0x1e42('0x5e')]['basename']);if(fs['existsSync'](_0x4a4bf5)){fs[_0x1e42('0x6e')](_0x4a4bf5)[_0x1e42('0x6f')](fs['createWriteStream'](path['join'](_0x2da49a,_0x23deb9[_0x1e42('0x5e')][_0x1e42('0x70')])));}}return{'date':moment(_0x23deb9[_0x1e42('0x5f')])[_0x1e42('0x6a')](_0x1e42('0x6c'))['toString'](),'sender':_0x23deb9[_0x1e42('0x71')]===_0x1e42('0x72')?_0x23deb9[_0x1e42('0x5b')]?_0x23deb9[_0x1e42('0x5b')][_0x1e42('0x65')]:_0x1e42('0x73'):_0x23deb9['Contact'][_0x1e42('0x74')]+(_0x23deb9['Contact'][_0x1e42('0x5d')]?'\x20'+_0x23deb9[_0x1e42('0x5c')][_0x1e42('0x5d')]:''),'body':_0x23deb9[_0x1e42('0x5e')]?_0x23deb9['Attachment']['basename']:_0x23deb9[_0x1e42('0x38')],'direction':_0x23deb9[_0x1e42('0x71')]===_0x1e42('0x72')?'A':'C','secret':_0x23deb9[_0x1e42('0x60')],'read':_0x23deb9['read'],'attachment':_0x23deb9[_0x1e42('0x5e')]};})[_0x1e42('0x75')]()};return ejs[_0x1e42('0x76')](path['join'](config[_0x1e42('0x56')],_0x1e42('0x55'),'views',_0x1e42('0x77')),{'interaction':_0x2a0098})[_0x1e42('0x25')](function(_0x52b434){var _0x441265=path[_0x1e42('0x54')](_0x2da49a,_0x1e42('0x78')+_0x38e23f['id']+'-'+_0xbb92eb+_0x1e42('0x79'));var _0x4a9942={'path':_0x441265,'channel':_0x1e42('0x68'),'interactionId':_0x38e23f['id']};return pdf[_0x1e42('0x7a')](_0x52b434,_0x4a9942);})['then'](function(){return new BPromise(function(_0x1cf70d,_0x3b2ece){zipdir(_0x2da49a,{'saveTo':_0x3bc910},function(_0x1d6f11,_0x1200a3){if(_0x1d6f11)return _0x3b2ece(_0x1d6f11);return _0x1cf70d(_0x1200a3);});})[_0x1e42('0x25')](function(){return new BPromise(function(_0x405d7f,_0x1f84bf){rimraf(_0x2da49a,function(_0x219c36){if(_0x219c36)_0x1f84bf(_0x219c36);return _0x405d7f();});});})[_0x1e42('0x25')](function(){return _0x2c92ea[_0x1e42('0x51')](_0x3bc910,_0x237099,function(_0x4ef845){if(_0x4ef845){console['log'](_0x1e42('0x7b'),_0x4ef845);}else{fs[_0x1e42('0x7c')](_0x3bc910);}});});});}else{return _0x2c92ea[_0x1e42('0x12')](0xc8);}})['catch'](handleError(_0x2c92ea,null));};