Built motion from commit 39adb707.|2.6.14
[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 _0x8bb5=['findOne','order','sort','where','pick','filter','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','WhatsappMessage','includeAll','rows','setTags','omit','spread','Tag','emit','whatsappInteractionTags:save','removeTags','download','unix','toString','join','root','files','attachments','transcript-%d-%s.zip','push','User','CmContact','Contact','firstName','lastName','Attachment','createdAt','direction','secret','Owner','fullname','get','mkdirSync','Whatsapp','Account','N.A.','closed','closedAt','disposition','Messages','basename','existsSync','createReadStream','pipe','createWriteStream','out','System','read','renderFile','transcript-','.pdf','createPdfFromHTML','log','err','rimraf','moment','bluebird','util','path','ejs','ioredis','../../components/interaction/service','../../components/parsers/qs','../../config/logger','../../config/environment','../../mysqldb','redis','defaults','localhost','socket.io-emitter','sendStatus','status','offset','undefined','limit','count','set','Content-Range','update','then','destroy','end','error','stack','name','index','map','WhatsappInteraction','rawAttributes','fieldName','type','key','getOptions','findAll','whatsapp','user','catch','show','params','keys','query','filters','intersection','model','attributes','fields','length','include','merge','options','find','body','describe','addMessage','ids','getMessages'];(function(_0x3aaf0a,_0x450970){var _0x33e6bc=function(_0x1db54d){while(--_0x1db54d){_0x3aaf0a['push'](_0x3aaf0a['shift']());}};_0x33e6bc(++_0x450970);}(_0x8bb5,0x1b6));var _0x58bb=function(_0x29ac88,_0x33fe85){_0x29ac88=_0x29ac88-0x0;var _0xa23177=_0x8bb5[_0x29ac88];return _0xa23177;};'use strict';var rimraf=require(_0x58bb('0x0'));var zipdir=require('zip-dir');var moment=require(_0x58bb('0x1'));var BPromise=require(_0x58bb('0x2'));var util=require(_0x58bb('0x3'));var path=require(_0x58bb('0x4'));var ejs=require(_0x58bb('0x5'));var fs=require('fs');var _=require('lodash');var Redis=require(_0x58bb('0x6'));var interaction=require(_0x58bb('0x7'));var qs=require(_0x58bb('0x8'));var pdf=require('../../components/export/pdf');var logger=require(_0x58bb('0x9'))('api');var config=require(_0x58bb('0xa'));var db=require(_0x58bb('0xb'))['db'];config[_0x58bb('0xc')]=_[_0x58bb('0xd')](config['redis'],{'host':_0x58bb('0xe'),'port':0x18eb});var socket=require(_0x58bb('0xf'))(new Redis(config[_0x58bb('0xc')]));require('./whatsappInteraction.socket')['register'](socket);function respondWithStatusCode(_0x181839,_0x50b482){_0x50b482=_0x50b482||0xcc;return function(_0x1eade2){if(_0x1eade2){return _0x181839[_0x58bb('0x10')](_0x50b482);}return _0x181839[_0x58bb('0x11')](_0x50b482)['end']();};}function respondWithResult(_0xd4cf66,_0x2e2fc5){_0x2e2fc5=_0x2e2fc5||0xc8;return function(_0x46427d){if(_0x46427d){return _0xd4cf66['status'](_0x2e2fc5)['json'](_0x46427d);}};}function respondWithFilteredResult(_0xd8055d,_0x2e7763){return function(_0x36959e){if(_0x36959e){var _0xea0357=typeof _0x2e7763[_0x58bb('0x12')]===_0x58bb('0x13')&&typeof _0x2e7763[_0x58bb('0x14')]===_0x58bb('0x13');var _0x2ab86b=_0x36959e[_0x58bb('0x15')];var _0x2f8f1e=_0xea0357?0x0:_0x2e7763[_0x58bb('0x12')];var _0x17b08d=_0xea0357?_0x36959e['count']:_0x2e7763['offset']+_0x2e7763['limit'];var _0x44d313;if(_0x17b08d>=_0x2ab86b){_0x17b08d=_0x2ab86b;_0x44d313=0xc8;}else{_0x44d313=0xce;}_0xd8055d[_0x58bb('0x11')](_0x44d313);return _0xd8055d[_0x58bb('0x16')](_0x58bb('0x17'),_0x2f8f1e+'-'+_0x17b08d+'/'+_0x2ab86b)['json'](_0x36959e);}return null;};}function saveUpdates(_0x281ee5){return function(_0x353c69){if(_0x353c69){return _0x353c69[_0x58bb('0x18')](_0x281ee5)[_0x58bb('0x19')](function(_0x4799fb){return _0x4799fb;});}return null;};}function removeEntity(_0x107318){return function(_0x56e04){if(_0x56e04){return _0x56e04[_0x58bb('0x1a')]()[_0x58bb('0x19')](function(){_0x107318[_0x58bb('0x11')](0xcc)[_0x58bb('0x1b')]();});}};}function handleEntityNotFound(_0x52cf9a){return function(_0x5d3800){if(!_0x5d3800){_0x52cf9a[_0x58bb('0x10')](0x194);}return _0x5d3800;};}function handleError(_0x3a65b4,_0x1e697f){_0x1e697f=_0x1e697f||0x1f4;return function(_0x251ea8){logger[_0x58bb('0x1c')](_0x251ea8[_0x58bb('0x1d')]);if(_0x251ea8[_0x58bb('0x1e')]){delete _0x251ea8[_0x58bb('0x1e')];}_0x3a65b4[_0x58bb('0x11')](_0x1e697f)['send'](_0x251ea8);};}exports[_0x58bb('0x1f')]=function(_0x1bf614,_0x518667){var _0x388811={};var _0xda1dc7=_[_0x58bb('0x20')](db[_0x58bb('0x21')][_0x58bb('0x22')],function(_0x4cf556){return{'name':_0x4cf556[_0x58bb('0x23')],'type':_0x4cf556[_0x58bb('0x24')][_0x58bb('0x25')]};});_0x388811=qs[_0x58bb('0x26')](_0xda1dc7,_0x1bf614);return interaction[_0x58bb('0x27')](_0x58bb('0x28'),_0x1bf614['query'],_0x1bf614[_0x58bb('0x29')],_0x388811,_0xda1dc7)[_0x58bb('0x19')](respondWithFilteredResult(_0x518667,_0x388811))[_0x58bb('0x2a')](handleError(_0x518667,null));};exports[_0x58bb('0x2b')]=function(_0x266c5e,_0x5eecad){var _0x406313={'raw':![],'where':{'id':_0x266c5e[_0x58bb('0x2c')]['id']}},_0xe964dd={};_0xe964dd['model']=_[_0x58bb('0x2d')](db[_0x58bb('0x21')]['rawAttributes']);_0xe964dd[_0x58bb('0x2e')]=_[_0x58bb('0x2d')](_0x266c5e[_0x58bb('0x2e')]);_0xe964dd[_0x58bb('0x2f')]=_[_0x58bb('0x30')](_0xe964dd[_0x58bb('0x31')],_0xe964dd[_0x58bb('0x2e')]);_0x406313[_0x58bb('0x32')]=_[_0x58bb('0x30')](_0xe964dd['model'],qs[_0x58bb('0x33')](_0x266c5e[_0x58bb('0x2e')][_0x58bb('0x33')]));_0x406313[_0x58bb('0x32')]=_0x406313[_0x58bb('0x32')][_0x58bb('0x34')]?_0x406313[_0x58bb('0x32')]:_0xe964dd['model'];if(_0x266c5e['query']['includeAll']){_0x406313[_0x58bb('0x35')]=[{'all':!![]}];}_0x406313=_[_0x58bb('0x36')]({},_0x406313,_0x266c5e[_0x58bb('0x37')]);return db[_0x58bb('0x21')][_0x58bb('0x38')](_0x406313)[_0x58bb('0x19')](handleEntityNotFound(_0x5eecad,null))[_0x58bb('0x19')](respondWithResult(_0x5eecad,null))[_0x58bb('0x2a')](handleError(_0x5eecad,null));};exports['create']=function(_0x3e7648,_0x44477b){return db[_0x58bb('0x21')]['create'](_0x3e7648[_0x58bb('0x39')],{})[_0x58bb('0x19')](respondWithResult(_0x44477b,0xc9))[_0x58bb('0x2a')](handleError(_0x44477b,null));};exports[_0x58bb('0x18')]=function(_0x4a66a3,_0x2a9122){if(_0x4a66a3[_0x58bb('0x39')]['id']){delete _0x4a66a3[_0x58bb('0x39')]['id'];}return db[_0x58bb('0x21')][_0x58bb('0x38')]({'where':{'id':_0x4a66a3[_0x58bb('0x2c')]['id']}})[_0x58bb('0x19')](handleEntityNotFound(_0x2a9122,null))[_0x58bb('0x19')](saveUpdates(_0x4a66a3[_0x58bb('0x39')],null))['then'](respondWithResult(_0x2a9122,null))[_0x58bb('0x2a')](handleError(_0x2a9122,null));};exports[_0x58bb('0x1a')]=function(_0x2c307c,_0x4f05ed){return db[_0x58bb('0x21')][_0x58bb('0x38')]({'where':{'id':_0x2c307c[_0x58bb('0x2c')]['id']}})[_0x58bb('0x19')](handleEntityNotFound(_0x4f05ed,null))[_0x58bb('0x19')](removeEntity(_0x4f05ed,null))[_0x58bb('0x2a')](handleError(_0x4f05ed,null));};exports[_0x58bb('0x3a')]=function(_0x433a27,_0x23cb38){return db['WhatsappInteraction'][_0x58bb('0x3a')]()[_0x58bb('0x19')](respondWithResult(_0x23cb38,null))['catch'](handleError(_0x23cb38,null));};exports['addMessage']=function(_0x3f0799,_0x308f74){return db['WhatsappMessage'][_0x58bb('0x38')]({'where':{'id':_0x3f0799['params']['id']}})['then'](handleEntityNotFound(_0x308f74,null))[_0x58bb('0x19')](function(_0x15ff77){if(_0x15ff77){return _0x15ff77[_0x58bb('0x3b')](_0x3f0799[_0x58bb('0x39')]['ids'],_['omit'](_0x3f0799[_0x58bb('0x39')],[_0x58bb('0x3c'),'id'])||{});}})[_0x58bb('0x19')](respondWithResult(_0x308f74,null))['catch'](handleError(_0x308f74,null));};exports[_0x58bb('0x3d')]=function(_0x1e0a29,_0x43e11d){var _0x5159eb={'raw':![],'where':{}};var _0x39ad46={};var _0x28bf2e={'count':0x0,'rows':[]};return db[_0x58bb('0x21')][_0x58bb('0x3e')]({'where':{'id':_0x1e0a29['params']['id']}})[_0x58bb('0x19')](handleEntityNotFound(_0x43e11d,null))[_0x58bb('0x19')](function(_0x5afde6){if(_0x5afde6){_0x39ad46['model']=_[_0x58bb('0x2d')](db['WhatsappMessage'][_0x58bb('0x22')]);_0x39ad46[_0x58bb('0x2e')]=_['keys'](_0x1e0a29[_0x58bb('0x2e')]);_0x39ad46[_0x58bb('0x2f')]=_[_0x58bb('0x30')](_0x39ad46['model'],_0x39ad46[_0x58bb('0x2e')]);_0x5159eb[_0x58bb('0x32')]=_[_0x58bb('0x30')](_0x39ad46['model'],qs['fields'](_0x1e0a29['query']['fields']));_0x5159eb[_0x58bb('0x32')]=_0x5159eb['attributes'][_0x58bb('0x34')]?_0x5159eb[_0x58bb('0x32')]:_0x39ad46['model'];if(!_0x1e0a29[_0x58bb('0x2e')]['hasOwnProperty']('nolimit')){_0x5159eb[_0x58bb('0x14')]=qs['limit'](_0x1e0a29[_0x58bb('0x2e')][_0x58bb('0x14')]);_0x5159eb[_0x58bb('0x12')]=qs[_0x58bb('0x12')](_0x1e0a29[_0x58bb('0x2e')][_0x58bb('0x12')]);}_0x5159eb[_0x58bb('0x3f')]=qs[_0x58bb('0x40')](_0x1e0a29[_0x58bb('0x2e')]['sort']);_0x5159eb[_0x58bb('0x41')]=qs[_0x58bb('0x2f')](_[_0x58bb('0x42')](_0x1e0a29[_0x58bb('0x2e')],_0x39ad46[_0x58bb('0x2f')]));_0x5159eb[_0x58bb('0x41')]['WhatsappInteractionId']=_0x5afde6['id'];if(_0x1e0a29[_0x58bb('0x2e')][_0x58bb('0x43')]){_0x5159eb[_0x58bb('0x41')]=_[_0x58bb('0x36')](_0x5159eb['where'],{'$or':_[_0x58bb('0x20')](_0x5159eb[_0x58bb('0x32')],function(_0x55108c){var _0x232039={};_0x232039[_0x55108c]={'$like':'%'+_0x1e0a29[_0x58bb('0x2e')][_0x58bb('0x43')]+'%'};return _0x232039;})});}if(_0x1e0a29[_0x58bb('0x2e')][_0x58bb('0x44')]){var _0x22baaf=_0x1e0a29[_0x58bb('0x2e')][_0x58bb('0x44')][_0x58bb('0x45')](',');var _0x24ac19={};_0x24ac19[_0x22baaf[0x0]]={'$gte':moment(_0x22baaf[0x1])[_0x58bb('0x46')](_0x58bb('0x47'))};_0x5159eb[_0x58bb('0x41')]=_['merge'](_0x5159eb['where'],_0x24ac19);}_0x5159eb=_['merge']({},_0x5159eb,_0x1e0a29[_0x58bb('0x37')]);return db[_0x58bb('0x48')][_0x58bb('0x15')]({'where':_0x5159eb[_0x58bb('0x41')]})[_0x58bb('0x19')](function(_0x1c5b39){_0x28bf2e[_0x58bb('0x15')]=_0x1c5b39;if(_0x1e0a29[_0x58bb('0x2e')][_0x58bb('0x49')]){_0x5159eb[_0x58bb('0x35')]=[{'all':!![]}];}return db[_0x58bb('0x48')][_0x58bb('0x27')](_0x5159eb);})['then'](function(_0x37bf3c){_0x28bf2e[_0x58bb('0x4a')]=_0x37bf3c;return _0x28bf2e;});}})[_0x58bb('0x19')](respondWithFilteredResult(_0x43e11d,_0x5159eb))[_0x58bb('0x2a')](handleError(_0x43e11d,null));};exports['addTags']=function(_0x1e3a5d,_0x249448){return db[_0x58bb('0x21')][_0x58bb('0x38')]({'where':{'id':_0x1e3a5d['params']['id']}})[_0x58bb('0x19')](handleEntityNotFound(_0x249448,null))[_0x58bb('0x19')](function(_0x2dc44c){if(_0x2dc44c){return _0x2dc44c[_0x58bb('0x4b')](_0x1e3a5d[_0x58bb('0x39')]['ids'],_[_0x58bb('0x4c')](_0x1e3a5d[_0x58bb('0x39')],['ids','id'])||{})[_0x58bb('0x4d')](function(){return db[_0x58bb('0x4e')][_0x58bb('0x27')]({'attributes':['id',_0x58bb('0x1e'),'color'],'where':{'id':_0x1e3a5d[_0x58bb('0x39')][_0x58bb('0x3c')]}});})[_0x58bb('0x19')](function(_0x4263ff){socket[_0x58bb('0x4f')](_0x58bb('0x50'),{'id':Number(_0x1e3a5d[_0x58bb('0x2c')]['id']),'tags':_0x4263ff||[]});return{'id':Number(_0x1e3a5d['params']['id']),'tags':_0x4263ff||[]};});}})[_0x58bb('0x19')](respondWithResult(_0x249448,null))['catch'](handleError(_0x249448,null));};exports['removeTags']=function(_0x4ccf11,_0xf64430){return db[_0x58bb('0x21')][_0x58bb('0x38')]({'where':{'id':_0x4ccf11[_0x58bb('0x2c')]['id']}})[_0x58bb('0x19')](handleEntityNotFound(_0xf64430,null))[_0x58bb('0x19')](function(_0x1c9707){if(_0x1c9707){return _0x1c9707[_0x58bb('0x51')](_0x4ccf11[_0x58bb('0x2e')]['ids']);}})['then'](respondWithStatusCode(_0xf64430,null))[_0x58bb('0x2a')](handleError(_0xf64430,null));};exports[_0x58bb('0x52')]=function(_0x35dacd,_0x4f6cf6){var _0x3a9783=moment()[_0x58bb('0x53')]()[_0x58bb('0x54')]();var _0x4bce94=path[_0x58bb('0x55')](config[_0x58bb('0x56')],'server',_0x58bb('0x57'),'tmp');var _0xc09d11=path[_0x58bb('0x55')](config[_0x58bb('0x56')],'server',_0x58bb('0x57'),_0x58bb('0x58'));var _0x5874b3=path[_0x58bb('0x55')](_0x4bce94,_0x3a9783);var _0x356065=util[_0x58bb('0x46')](_0x58bb('0x59'),_0x35dacd[_0x58bb('0x2c')]['id'],_0x3a9783);var _0x44dd99=path[_0x58bb('0x55')](_0x4bce94,_0x356065);var _0x2459b0=[];_0x2459b0[_0x58bb('0x5a')]({'model':db[_0x58bb('0x5b')],'as':'User','attributes':['id','fullname'],'raw':!![]});_0x2459b0['push']({'model':db[_0x58bb('0x5c')],'as':_0x58bb('0x5d'),'attributes':['id',_0x58bb('0x5e'),_0x58bb('0x5f')],'raw':!![]});if(_0x35dacd[_0x58bb('0x2e')]['attachments']){_0x2459b0[_0x58bb('0x5a')]({'model':db[_0x58bb('0x60')],'as':_0x58bb('0x60'),'raw':!![]});}var _0x514d94=[{'model':db[_0x58bb('0x48')],'as':'Messages','attributes':['id','body',_0x58bb('0x61'),_0x58bb('0x62'),_0x58bb('0x63'),'read'],'include':_0x2459b0}];_0x514d94[_0x58bb('0x5a')]({'model':db['WhatsappAccount'],'as':'Account','attributes':['id',_0x58bb('0x1e')]});_0x514d94['push']({'model':db[_0x58bb('0x5b')],'as':_0x58bb('0x64'),'attributes':['id',_0x58bb('0x65')]});return db['WhatsappInteraction']['find']({'where':{'id':_0x35dacd['params']['id']},'include':_0x514d94})[_0x58bb('0x19')](handleEntityNotFound(_0x4f6cf6,null))[_0x58bb('0x19')](function(_0x59fd23){if(_0x59fd23){var _0x5af0ce=_0x59fd23[_0x58bb('0x66')]({'plain':!![]});fs[_0x58bb('0x67')](_0x5874b3);var _0x403824={'channel':_0x58bb('0x68'),'account':_0x5af0ce[_0x58bb('0x69')]['name'],'agent':_0x5af0ce[_0x58bb('0x64')]?_0x5af0ce['Owner'][_0x58bb('0x65')]:_0x58bb('0x6a'),'createdAt':moment(_0x5af0ce[_0x58bb('0x61')])[_0x58bb('0x46')](_0x58bb('0x47'))[_0x58bb('0x54')](),'closedAt':_0x5af0ce[_0x58bb('0x6b')]?moment(_0x5af0ce[_0x58bb('0x6c')])[_0x58bb('0x46')](_0x58bb('0x47'))['toString']():'','disposition':_0x5af0ce[_0x58bb('0x6d')],'messages':_(_0x5af0ce[_0x58bb('0x6e')])[_0x58bb('0x43')]({'secret':![]})[_0x58bb('0x20')](function(_0x15ab04){if(_0x15ab04[_0x58bb('0x60')]){var _0x848a38=path[_0x58bb('0x55')](_0xc09d11,_0x15ab04[_0x58bb('0x60')][_0x58bb('0x6f')]);if(fs[_0x58bb('0x70')](_0x848a38)){fs[_0x58bb('0x71')](_0x848a38)[_0x58bb('0x72')](fs[_0x58bb('0x73')](path[_0x58bb('0x55')](_0x5874b3,_0x15ab04['Attachment'][_0x58bb('0x6f')])));}}return{'date':moment(_0x15ab04[_0x58bb('0x61')])[_0x58bb('0x46')](_0x58bb('0x47'))[_0x58bb('0x54')](),'sender':_0x15ab04[_0x58bb('0x62')]===_0x58bb('0x74')?_0x15ab04[_0x58bb('0x5b')]?_0x15ab04[_0x58bb('0x5b')][_0x58bb('0x65')]:_0x58bb('0x75'):_0x15ab04[_0x58bb('0x5d')][_0x58bb('0x5e')]+(_0x15ab04[_0x58bb('0x5d')]['lastName']?'\x20'+_0x15ab04['Contact'][_0x58bb('0x5f')]:''),'body':_0x15ab04['Attachment']?_0x15ab04[_0x58bb('0x60')][_0x58bb('0x6f')]:_0x15ab04[_0x58bb('0x39')],'direction':_0x15ab04[_0x58bb('0x62')]==='out'?'A':'C','secret':_0x15ab04['secret'],'read':_0x15ab04[_0x58bb('0x76')],'attachment':_0x15ab04[_0x58bb('0x60')]};})['value']()};return ejs[_0x58bb('0x77')](path[_0x58bb('0x55')](config[_0x58bb('0x56')],'server','views','downloadInteraction.ejs'),{'interaction':_0x403824})['then'](function(_0x50430e){var _0x88e26b=path[_0x58bb('0x55')](_0x5874b3,_0x58bb('0x78')+_0x5af0ce['id']+'-'+_0x3a9783+_0x58bb('0x79'));var _0x41a6d6={'path':_0x88e26b,'channel':'Whatsapp','interactionId':_0x5af0ce['id']};return pdf[_0x58bb('0x7a')](_0x50430e,_0x41a6d6);})[_0x58bb('0x19')](function(){return new BPromise(function(_0x38edf4,_0x724d44){zipdir(_0x5874b3,{'saveTo':_0x44dd99},function(_0x1fed46,_0x32de09){if(_0x1fed46)return _0x724d44(_0x1fed46);return _0x38edf4(_0x32de09);});})[_0x58bb('0x19')](function(){return new BPromise(function(_0x21b103,_0x53828e){rimraf(_0x5874b3,function(_0x54adc6){if(_0x54adc6)_0x53828e(_0x54adc6);return _0x21b103();});});})[_0x58bb('0x19')](function(){return _0x4f6cf6[_0x58bb('0x52')](_0x44dd99,_0x356065,function(_0x2b623e){if(_0x2b623e){console[_0x58bb('0x7b')](_0x58bb('0x7c'),_0x2b623e);}else{fs['unlinkSync'](_0x44dd99);}});});});}else{return _0x4f6cf6[_0x58bb('0x10')](0xc8);}})['catch'](handleError(_0x4f6cf6,null));};