Built motion from commit 4ca75f1b.|2.6.28
[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 _0x8f67=['unlinkSync','rimraf','zip-dir','moment','bluebird','util','ejs','ioredis','../../components/interaction/service','../../components/parsers/qs','../../components/export/pdf','../../config/logger','api','../../config/environment','../../mysqldb','redis','localhost','socket.io-emitter','./whatsappInteraction.socket','register','sendStatus','status','json','offset','limit','undefined','count','set','Content-Range','then','name','send','index','map','WhatsappInteraction','rawAttributes','type','key','getOptions','findAll','whatsapp','query','user','show','params','keys','intersection','model','fields','attributes','length','includeAll','find','catch','create','body','update','destroy','describe','WhatsappMessage','addMessage','ids','omit','findOne','filters','nolimit','order','sort','where','WhatsappInteractionId','filter','merge','$gte','split','format','options','include','rows','Tag','color','emit','removeTags','download','unix','root','tmp','join','server','files','attachments','User','fullname','push','CmContact','Contact','firstName','lastName','Attachment','Messages','createdAt','direction','read','WhatsappAccount','Account','Owner','get','mkdirSync','Whatsapp','YYYY-MM-DD\x20HH:mm:ss','toString','closed','closedAt','disposition','basename','existsSync','createReadStream','pipe','createWriteStream','out','secret','value','.pdf','createPdfFromHTML','log','err'];(function(_0x56dc0a,_0x34b6b5){var _0x265418=function(_0x5c0694){while(--_0x5c0694){_0x56dc0a['push'](_0x56dc0a['shift']());}};_0x265418(++_0x34b6b5);}(_0x8f67,0xfb));var _0x78f6=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0x8f67[_0x3dd15e];return _0x231fd0;};'use strict';var rimraf=require(_0x78f6('0x0'));var zipdir=require(_0x78f6('0x1'));var moment=require(_0x78f6('0x2'));var BPromise=require(_0x78f6('0x3'));var util=require(_0x78f6('0x4'));var path=require('path');var ejs=require(_0x78f6('0x5'));var fs=require('fs');var _=require('lodash');var Redis=require(_0x78f6('0x6'));var interaction=require(_0x78f6('0x7'));var qs=require(_0x78f6('0x8'));var pdf=require(_0x78f6('0x9'));var logger=require(_0x78f6('0xa'))(_0x78f6('0xb'));var config=require(_0x78f6('0xc'));var db=require(_0x78f6('0xd'))['db'];config[_0x78f6('0xe')]=_['defaults'](config[_0x78f6('0xe')],{'host':_0x78f6('0xf'),'port':0x18eb});var socket=require(_0x78f6('0x10'))(new Redis(config[_0x78f6('0xe')]));require(_0x78f6('0x11'))[_0x78f6('0x12')](socket);function respondWithStatusCode(_0x46a018,_0x4a03b1){_0x4a03b1=_0x4a03b1||0xcc;return function(_0x2cc0f4){if(_0x2cc0f4){return _0x46a018[_0x78f6('0x13')](_0x4a03b1);}return _0x46a018[_0x78f6('0x14')](_0x4a03b1)['end']();};}function respondWithResult(_0x5df31b,_0x574dcd){_0x574dcd=_0x574dcd||0xc8;return function(_0x2d2406){if(_0x2d2406){return _0x5df31b[_0x78f6('0x14')](_0x574dcd)[_0x78f6('0x15')](_0x2d2406);}};}function respondWithFilteredResult(_0x13797c,_0x121184){return function(_0x5dcd15){if(_0x5dcd15){var _0x1702c3=typeof _0x121184[_0x78f6('0x16')]==='undefined'&&typeof _0x121184[_0x78f6('0x17')]===_0x78f6('0x18');var _0x246b4a=_0x5dcd15[_0x78f6('0x19')];var _0x2dcbfc=_0x1702c3?0x0:_0x121184['offset'];var _0xeb7675=_0x1702c3?_0x5dcd15[_0x78f6('0x19')]:_0x121184['offset']+_0x121184[_0x78f6('0x17')];var _0x38b17e;if(_0xeb7675>=_0x246b4a){_0xeb7675=_0x246b4a;_0x38b17e=0xc8;}else{_0x38b17e=0xce;}_0x13797c[_0x78f6('0x14')](_0x38b17e);return _0x13797c[_0x78f6('0x1a')](_0x78f6('0x1b'),_0x2dcbfc+'-'+_0xeb7675+'/'+_0x246b4a)[_0x78f6('0x15')](_0x5dcd15);}return null;};}function saveUpdates(_0x43f11f){return function(_0x1e16fe){if(_0x1e16fe){return _0x1e16fe['update'](_0x43f11f)[_0x78f6('0x1c')](function(_0x41ada3){return _0x41ada3;});}return null;};}function removeEntity(_0x3c4714){return function(_0x580098){if(_0x580098){return _0x580098['destroy']()[_0x78f6('0x1c')](function(){_0x3c4714[_0x78f6('0x14')](0xcc)['end']();});}};}function handleEntityNotFound(_0x3b0f2b){return function(_0x39bb8b){if(!_0x39bb8b){_0x3b0f2b['sendStatus'](0x194);}return _0x39bb8b;};}function handleError(_0x2b9646,_0x3cf8b3){_0x3cf8b3=_0x3cf8b3||0x1f4;return function(_0x3f8326){logger['error'](_0x3f8326['stack']);if(_0x3f8326[_0x78f6('0x1d')]){delete _0x3f8326['name'];}_0x2b9646[_0x78f6('0x14')](_0x3cf8b3)[_0x78f6('0x1e')](_0x3f8326);};}exports[_0x78f6('0x1f')]=function(_0x459a87,_0x4c1944){var _0x1df807={};var _0x1e9266=_[_0x78f6('0x20')](db[_0x78f6('0x21')][_0x78f6('0x22')],function(_0x226d45){return{'name':_0x226d45['fieldName'],'type':_0x226d45[_0x78f6('0x23')][_0x78f6('0x24')]};});_0x1df807=qs[_0x78f6('0x25')](_0x1e9266,_0x459a87);return interaction[_0x78f6('0x26')](_0x78f6('0x27'),_0x459a87[_0x78f6('0x28')],_0x459a87[_0x78f6('0x29')],_0x1df807,_0x1e9266)[_0x78f6('0x1c')](respondWithFilteredResult(_0x4c1944,_0x1df807))['catch'](handleError(_0x4c1944,null));};exports[_0x78f6('0x2a')]=function(_0x510f96,_0x5cf76a){var _0x56c9d7={'raw':![],'where':{'id':_0x510f96[_0x78f6('0x2b')]['id']}},_0x3bbad7={};_0x3bbad7['model']=_[_0x78f6('0x2c')](db[_0x78f6('0x21')][_0x78f6('0x22')]);_0x3bbad7[_0x78f6('0x28')]=_['keys'](_0x510f96[_0x78f6('0x28')]);_0x3bbad7['filters']=_[_0x78f6('0x2d')](_0x3bbad7[_0x78f6('0x2e')],_0x3bbad7[_0x78f6('0x28')]);_0x56c9d7['attributes']=_['intersection'](_0x3bbad7[_0x78f6('0x2e')],qs[_0x78f6('0x2f')](_0x510f96[_0x78f6('0x28')][_0x78f6('0x2f')]));_0x56c9d7[_0x78f6('0x30')]=_0x56c9d7[_0x78f6('0x30')][_0x78f6('0x31')]?_0x56c9d7['attributes']:_0x3bbad7['model'];if(_0x510f96[_0x78f6('0x28')][_0x78f6('0x32')]){_0x56c9d7['include']=[{'all':!![]}];}_0x56c9d7=_['merge']({},_0x56c9d7,_0x510f96['options']);return db['WhatsappInteraction'][_0x78f6('0x33')](_0x56c9d7)[_0x78f6('0x1c')](handleEntityNotFound(_0x5cf76a,null))[_0x78f6('0x1c')](respondWithResult(_0x5cf76a,null))[_0x78f6('0x34')](handleError(_0x5cf76a,null));};exports[_0x78f6('0x35')]=function(_0x43545a,_0x4eea0c){return db['WhatsappInteraction'][_0x78f6('0x35')](_0x43545a[_0x78f6('0x36')],{})[_0x78f6('0x1c')](respondWithResult(_0x4eea0c,0xc9))[_0x78f6('0x34')](handleError(_0x4eea0c,null));};exports[_0x78f6('0x37')]=function(_0x1b2eab,_0x33bda0){if(_0x1b2eab[_0x78f6('0x36')]['id']){delete _0x1b2eab[_0x78f6('0x36')]['id'];}return db['WhatsappInteraction'][_0x78f6('0x33')]({'where':{'id':_0x1b2eab[_0x78f6('0x2b')]['id']}})['then'](handleEntityNotFound(_0x33bda0,null))[_0x78f6('0x1c')](saveUpdates(_0x1b2eab['body'],null))['then'](respondWithResult(_0x33bda0,null))[_0x78f6('0x34')](handleError(_0x33bda0,null));};exports[_0x78f6('0x38')]=function(_0x10d2ca,_0x44c3db){return db[_0x78f6('0x21')]['find']({'where':{'id':_0x10d2ca[_0x78f6('0x2b')]['id']}})['then'](handleEntityNotFound(_0x44c3db,null))[_0x78f6('0x1c')](removeEntity(_0x44c3db,null))[_0x78f6('0x34')](handleError(_0x44c3db,null));};exports[_0x78f6('0x39')]=function(_0x2b842d,_0x5f0a6c){return db[_0x78f6('0x21')][_0x78f6('0x39')]()['then'](respondWithResult(_0x5f0a6c,null))[_0x78f6('0x34')](handleError(_0x5f0a6c,null));};exports['addMessage']=function(_0x2bbb58,_0x5870fb){return db[_0x78f6('0x3a')][_0x78f6('0x33')]({'where':{'id':_0x2bbb58[_0x78f6('0x2b')]['id']}})[_0x78f6('0x1c')](handleEntityNotFound(_0x5870fb,null))[_0x78f6('0x1c')](function(_0x1e1dd7){if(_0x1e1dd7){return _0x1e1dd7[_0x78f6('0x3b')](_0x2bbb58[_0x78f6('0x36')][_0x78f6('0x3c')],_[_0x78f6('0x3d')](_0x2bbb58[_0x78f6('0x36')],[_0x78f6('0x3c'),'id'])||{});}})[_0x78f6('0x1c')](respondWithResult(_0x5870fb,null))[_0x78f6('0x34')](handleError(_0x5870fb,null));};exports['getMessages']=function(_0x2e6548,_0x5e9fec){var _0x179458={'raw':![],'where':{}};var _0x476d4c={};var _0x4ba6a4={'count':0x0,'rows':[]};return db['WhatsappInteraction'][_0x78f6('0x3e')]({'where':{'id':_0x2e6548[_0x78f6('0x2b')]['id']}})[_0x78f6('0x1c')](handleEntityNotFound(_0x5e9fec,null))[_0x78f6('0x1c')](function(_0xf9564d){if(_0xf9564d){_0x476d4c[_0x78f6('0x2e')]=_['keys'](db[_0x78f6('0x3a')][_0x78f6('0x22')]);_0x476d4c[_0x78f6('0x28')]=_['keys'](_0x2e6548[_0x78f6('0x28')]);_0x476d4c[_0x78f6('0x3f')]=_[_0x78f6('0x2d')](_0x476d4c['model'],_0x476d4c[_0x78f6('0x28')]);_0x179458[_0x78f6('0x30')]=_[_0x78f6('0x2d')](_0x476d4c[_0x78f6('0x2e')],qs[_0x78f6('0x2f')](_0x2e6548[_0x78f6('0x28')][_0x78f6('0x2f')]));_0x179458['attributes']=_0x179458[_0x78f6('0x30')][_0x78f6('0x31')]?_0x179458[_0x78f6('0x30')]:_0x476d4c[_0x78f6('0x2e')];if(!_0x2e6548[_0x78f6('0x28')]['hasOwnProperty'](_0x78f6('0x40'))){_0x179458[_0x78f6('0x17')]=qs['limit'](_0x2e6548[_0x78f6('0x28')][_0x78f6('0x17')]);_0x179458[_0x78f6('0x16')]=qs[_0x78f6('0x16')](_0x2e6548[_0x78f6('0x28')][_0x78f6('0x16')]);}_0x179458[_0x78f6('0x41')]=qs[_0x78f6('0x42')](_0x2e6548[_0x78f6('0x28')][_0x78f6('0x42')]);_0x179458[_0x78f6('0x43')]=qs[_0x78f6('0x3f')](_['pick'](_0x2e6548[_0x78f6('0x28')],_0x476d4c['filters']));_0x179458[_0x78f6('0x43')][_0x78f6('0x44')]=_0xf9564d['id'];if(_0x2e6548['query'][_0x78f6('0x45')]){_0x179458[_0x78f6('0x43')]=_[_0x78f6('0x46')](_0x179458[_0x78f6('0x43')],{'$or':_[_0x78f6('0x20')](_0x179458['attributes'],function(_0xb388c0){var _0x501d28={};_0x501d28[_0xb388c0]={'$like':'%'+_0x2e6548[_0x78f6('0x28')][_0x78f6('0x45')]+'%'};return _0x501d28;})});}if(_0x2e6548[_0x78f6('0x28')][_0x78f6('0x47')]){var _0x12be2c=_0x2e6548[_0x78f6('0x28')]['$gte'][_0x78f6('0x48')](',');var _0x1f55f8={};_0x1f55f8[_0x12be2c[0x0]]={'$gte':moment(_0x12be2c[0x1])[_0x78f6('0x49')]('YYYY-MM-DD\x20HH:mm:ss')};_0x179458[_0x78f6('0x43')]=_['merge'](_0x179458[_0x78f6('0x43')],_0x1f55f8);}_0x179458=_[_0x78f6('0x46')]({},_0x179458,_0x2e6548[_0x78f6('0x4a')]);return db[_0x78f6('0x3a')][_0x78f6('0x19')]({'where':_0x179458[_0x78f6('0x43')]})[_0x78f6('0x1c')](function(_0x5c4284){_0x4ba6a4[_0x78f6('0x19')]=_0x5c4284;if(_0x2e6548[_0x78f6('0x28')]['includeAll']){_0x179458[_0x78f6('0x4b')]=[{'all':!![]}];}return db[_0x78f6('0x3a')][_0x78f6('0x26')](_0x179458);})[_0x78f6('0x1c')](function(_0x4053af){_0x4ba6a4[_0x78f6('0x4c')]=_0x4053af;return _0x4ba6a4;});}})['then'](respondWithFilteredResult(_0x5e9fec,_0x179458))['catch'](handleError(_0x5e9fec,null));};exports['addTags']=function(_0x297952,_0x5f5795){return db[_0x78f6('0x21')][_0x78f6('0x33')]({'where':{'id':_0x297952[_0x78f6('0x2b')]['id']}})[_0x78f6('0x1c')](handleEntityNotFound(_0x5f5795,null))[_0x78f6('0x1c')](function(_0x1894fb){if(_0x1894fb){return _0x1894fb['setTags'](_0x297952[_0x78f6('0x36')][_0x78f6('0x3c')],_[_0x78f6('0x3d')](_0x297952[_0x78f6('0x36')],[_0x78f6('0x3c'),'id'])||{})['spread'](function(){return db[_0x78f6('0x4d')][_0x78f6('0x26')]({'attributes':['id',_0x78f6('0x1d'),_0x78f6('0x4e')],'where':{'id':_0x297952[_0x78f6('0x36')][_0x78f6('0x3c')]}});})['then'](function(_0x1149b3){socket[_0x78f6('0x4f')]('whatsappInteractionTags:save',{'id':Number(_0x297952[_0x78f6('0x2b')]['id']),'tags':_0x1149b3||[]});return{'id':Number(_0x297952[_0x78f6('0x2b')]['id']),'tags':_0x1149b3||[]};});}})[_0x78f6('0x1c')](respondWithResult(_0x5f5795,null))['catch'](handleError(_0x5f5795,null));};exports[_0x78f6('0x50')]=function(_0x782b5d,_0x1745e4){return db['WhatsappInteraction'][_0x78f6('0x33')]({'where':{'id':_0x782b5d[_0x78f6('0x2b')]['id']}})[_0x78f6('0x1c')](handleEntityNotFound(_0x1745e4,null))['then'](function(_0x4f5972){if(_0x4f5972){return _0x4f5972[_0x78f6('0x50')](_0x782b5d['query'][_0x78f6('0x3c')]);}})['then'](respondWithStatusCode(_0x1745e4,null))[_0x78f6('0x34')](handleError(_0x1745e4,null));};exports[_0x78f6('0x51')]=function(_0xb7887b,_0x99b5ea){var _0x5dcd28=moment()[_0x78f6('0x52')]()['toString']();var _0x4c3601=path['join'](config[_0x78f6('0x53')],'server','files',_0x78f6('0x54'));var _0x3071d0=path[_0x78f6('0x55')](config[_0x78f6('0x53')],_0x78f6('0x56'),_0x78f6('0x57'),_0x78f6('0x58'));var _0x241915=path[_0x78f6('0x55')](_0x4c3601,_0x5dcd28);var _0x57cc73=util[_0x78f6('0x49')]('transcript-%d-%s.zip',_0xb7887b[_0x78f6('0x2b')]['id'],_0x5dcd28);var _0x4c95a8=path['join'](_0x4c3601,_0x57cc73);var _0x11ffd3=[];_0x11ffd3['push']({'model':db[_0x78f6('0x59')],'as':_0x78f6('0x59'),'attributes':['id',_0x78f6('0x5a')],'raw':!![]});_0x11ffd3[_0x78f6('0x5b')]({'model':db[_0x78f6('0x5c')],'as':_0x78f6('0x5d'),'attributes':['id',_0x78f6('0x5e'),_0x78f6('0x5f')],'raw':!![]});if(_0xb7887b['query'][_0x78f6('0x58')]){_0x11ffd3['push']({'model':db[_0x78f6('0x60')],'as':_0x78f6('0x60'),'raw':!![]});}var _0x5a68b3=[{'model':db['WhatsappMessage'],'as':_0x78f6('0x61'),'attributes':['id',_0x78f6('0x36'),_0x78f6('0x62'),_0x78f6('0x63'),'secret',_0x78f6('0x64')],'include':_0x11ffd3}];_0x5a68b3[_0x78f6('0x5b')]({'model':db[_0x78f6('0x65')],'as':_0x78f6('0x66'),'attributes':['id','name']});_0x5a68b3[_0x78f6('0x5b')]({'model':db[_0x78f6('0x59')],'as':_0x78f6('0x67'),'attributes':['id',_0x78f6('0x5a')]});return db['WhatsappInteraction']['find']({'where':{'id':_0xb7887b[_0x78f6('0x2b')]['id']},'include':_0x5a68b3})['then'](handleEntityNotFound(_0x99b5ea,null))[_0x78f6('0x1c')](function(_0x245f43){if(_0x245f43){var _0x2a0894=_0x245f43[_0x78f6('0x68')]({'plain':!![]});fs[_0x78f6('0x69')](_0x241915);var _0x24bcd4={'channel':_0x78f6('0x6a'),'account':_0x2a0894[_0x78f6('0x66')]['name'],'agent':_0x2a0894[_0x78f6('0x67')]?_0x2a0894[_0x78f6('0x67')][_0x78f6('0x5a')]:'N.A.','createdAt':moment(_0x2a0894['createdAt'])[_0x78f6('0x49')](_0x78f6('0x6b'))[_0x78f6('0x6c')](),'closedAt':_0x2a0894[_0x78f6('0x6d')]?moment(_0x2a0894[_0x78f6('0x6e')])['format'](_0x78f6('0x6b'))[_0x78f6('0x6c')]():'','disposition':_0x2a0894[_0x78f6('0x6f')],'messages':_(_0x2a0894[_0x78f6('0x61')])['filter']({'secret':![]})['map'](function(_0x57216a){if(_0x57216a[_0x78f6('0x60')]){var _0x184fb2=path[_0x78f6('0x55')](_0x3071d0,_0x57216a['Attachment'][_0x78f6('0x70')]);if(fs[_0x78f6('0x71')](_0x184fb2)){fs[_0x78f6('0x72')](_0x184fb2)[_0x78f6('0x73')](fs[_0x78f6('0x74')](path[_0x78f6('0x55')](_0x241915,_0x57216a['Attachment'][_0x78f6('0x70')])));}}return{'date':moment(_0x57216a[_0x78f6('0x62')])[_0x78f6('0x49')]('YYYY-MM-DD\x20HH:mm:ss')[_0x78f6('0x6c')](),'sender':_0x57216a[_0x78f6('0x63')]===_0x78f6('0x75')?_0x57216a['User']?_0x57216a[_0x78f6('0x59')]['fullname']:'System':_0x57216a['Contact'][_0x78f6('0x5e')]+(_0x57216a['Contact'][_0x78f6('0x5f')]?'\x20'+_0x57216a['Contact'][_0x78f6('0x5f')]:''),'body':_0x57216a[_0x78f6('0x60')]?_0x57216a[_0x78f6('0x60')]['basename']:_0x57216a[_0x78f6('0x36')],'direction':_0x57216a[_0x78f6('0x63')]==='out'?'A':'C','secret':_0x57216a[_0x78f6('0x76')],'read':_0x57216a[_0x78f6('0x64')],'attachment':_0x57216a[_0x78f6('0x60')]};})[_0x78f6('0x77')]()};return ejs['renderFile'](path['join'](config[_0x78f6('0x53')],_0x78f6('0x56'),'views','downloadInteraction.ejs'),{'interaction':_0x24bcd4})[_0x78f6('0x1c')](function(_0x1fd5e1){var _0x446b72=path[_0x78f6('0x55')](_0x241915,'transcript-'+_0x2a0894['id']+'-'+_0x5dcd28+_0x78f6('0x78'));var _0x48e691={'path':_0x446b72,'channel':_0x78f6('0x6a'),'interactionId':_0x2a0894['id']};return pdf[_0x78f6('0x79')](_0x1fd5e1,_0x48e691);})['then'](function(){return new BPromise(function(_0x283126,_0x5c3a72){zipdir(_0x241915,{'saveTo':_0x4c95a8},function(_0x41f12b,_0x575b63){if(_0x41f12b)return _0x5c3a72(_0x41f12b);return _0x283126(_0x575b63);});})[_0x78f6('0x1c')](function(){return new BPromise(function(_0x24a8d4,_0x2c8741){rimraf(_0x241915,function(_0x1bfeec){if(_0x1bfeec)_0x2c8741(_0x1bfeec);return _0x24a8d4();});});})[_0x78f6('0x1c')](function(){return _0x99b5ea[_0x78f6('0x51')](_0x4c95a8,_0x57cc73,function(_0x22706b){if(_0x22706b){console[_0x78f6('0x7a')](_0x78f6('0x7b'),_0x22706b);}else{fs[_0x78f6('0x7c')](_0x4c95a8);}});});});}else{return _0x99b5ea[_0x78f6('0x13')](0xc8);}})['catch'](handleError(_0x99b5ea,null));};