4635347633270538a885c8eb6e31a7ba62e19f87
[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 _0x2f28=['lodash','../../components/interaction/service','../../components/parsers/qs','../../config/logger','api','../../config/environment','../../mysqldb','defaults','redis','localhost','./whatsappInteraction.socket','register','sendStatus','status','end','json','offset','undefined','count','set','Content-Range','update','destroy','then','name','send','index','map','fieldName','type','key','getOptions','findAll','whatsapp','query','user','params','model','WhatsappInteraction','rawAttributes','keys','attributes','length','includeAll','include','merge','options','find','create','body','catch','addMessage','WhatsappMessage','omit','ids','findOne','intersection','fields','hasOwnProperty','limit','order','sort','where','filters','filter','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','rows','addTags','Tag','color','whatsappInteractionTags:save','removeTags','download','unix','toString','join','root','server','files','transcript-%d-%s.zip','User','fullname','push','Contact','firstName','lastName','attachments','Attachment','createdAt','direction','secret','WhatsappAccount','Account','Owner','mkdirSync','Whatsapp','N.A.','closed','closedAt','disposition','Messages','createReadStream','pipe','out','System','basename','read','value','downloadInteraction.ejs','transcript-','.pdf','createPdfFromHTML','log','zip-dir','moment','bluebird'];(function(_0x12ff57,_0x343e6b){var _0x5f0255=function(_0x4cecd4){while(--_0x4cecd4){_0x12ff57['push'](_0x12ff57['shift']());}};_0x5f0255(++_0x343e6b);}(_0x2f28,0x162));var _0x82f2=function(_0x51dada,_0x4ddb59){_0x51dada=_0x51dada-0x0;var _0x209d74=_0x2f28[_0x51dada];return _0x209d74;};'use strict';var rimraf=require('rimraf');var zipdir=require(_0x82f2('0x0'));var moment=require(_0x82f2('0x1'));var BPromise=require(_0x82f2('0x2'));var util=require('util');var path=require('path');var ejs=require('ejs');var fs=require('fs');var _=require(_0x82f2('0x3'));var Redis=require('ioredis');var interaction=require(_0x82f2('0x4'));var qs=require(_0x82f2('0x5'));var pdf=require('../../components/export/pdf');var logger=require(_0x82f2('0x6'))(_0x82f2('0x7'));var config=require(_0x82f2('0x8'));var db=require(_0x82f2('0x9'))['db'];config['redis']=_[_0x82f2('0xa')](config[_0x82f2('0xb')],{'host':_0x82f2('0xc'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x82f2('0xb')]));require(_0x82f2('0xd'))[_0x82f2('0xe')](socket);function respondWithStatusCode(_0x206691,_0x5bdb86){_0x5bdb86=_0x5bdb86||0xcc;return function(_0x178ffe){if(_0x178ffe){return _0x206691[_0x82f2('0xf')](_0x5bdb86);}return _0x206691[_0x82f2('0x10')](_0x5bdb86)[_0x82f2('0x11')]();};}function respondWithResult(_0x40bb04,_0x4bf59f){_0x4bf59f=_0x4bf59f||0xc8;return function(_0x2cf56a){if(_0x2cf56a){return _0x40bb04[_0x82f2('0x10')](_0x4bf59f)[_0x82f2('0x12')](_0x2cf56a);}};}function respondWithFilteredResult(_0x53349d,_0x595f99){return function(_0x3515a0){if(_0x3515a0){var _0x3e9695=typeof _0x595f99[_0x82f2('0x13')]==='undefined'&&typeof _0x595f99['limit']===_0x82f2('0x14');var _0x17a2ed=_0x3515a0[_0x82f2('0x15')];var _0x518880=_0x3e9695?0x0:_0x595f99[_0x82f2('0x13')];var _0x23ad6c=_0x3e9695?_0x3515a0['count']:_0x595f99['offset']+_0x595f99['limit'];var _0x1d0f6b;if(_0x23ad6c>=_0x17a2ed){_0x23ad6c=_0x17a2ed;_0x1d0f6b=0xc8;}else{_0x1d0f6b=0xce;}_0x53349d[_0x82f2('0x10')](_0x1d0f6b);return _0x53349d[_0x82f2('0x16')](_0x82f2('0x17'),_0x518880+'-'+_0x23ad6c+'/'+_0x17a2ed)['json'](_0x3515a0);}return null;};}function saveUpdates(_0x40fdce){return function(_0x310111){if(_0x310111){return _0x310111[_0x82f2('0x18')](_0x40fdce)['then'](function(_0x2e7d93){return _0x2e7d93;});}return null;};}function removeEntity(_0x2b248f){return function(_0x5034ac){if(_0x5034ac){return _0x5034ac[_0x82f2('0x19')]()[_0x82f2('0x1a')](function(){_0x2b248f[_0x82f2('0x10')](0xcc)[_0x82f2('0x11')]();});}};}function handleEntityNotFound(_0x366d56){return function(_0x48fc2e){if(!_0x48fc2e){_0x366d56['sendStatus'](0x194);}return _0x48fc2e;};}function handleError(_0x3ef13e,_0x57c68c){_0x57c68c=_0x57c68c||0x1f4;return function(_0x38c0f6){logger['error'](_0x38c0f6['stack']);if(_0x38c0f6['name']){delete _0x38c0f6[_0x82f2('0x1b')];}_0x3ef13e['status'](_0x57c68c)[_0x82f2('0x1c')](_0x38c0f6);};}exports[_0x82f2('0x1d')]=function(_0xd59d94,_0x40b696){var _0x41231d={};var _0x5b651d=_[_0x82f2('0x1e')](db['WhatsappInteraction']['rawAttributes'],function(_0x3d4a86){return{'name':_0x3d4a86[_0x82f2('0x1f')],'type':_0x3d4a86[_0x82f2('0x20')][_0x82f2('0x21')]};});_0x41231d=qs[_0x82f2('0x22')](_0x5b651d,_0xd59d94);return interaction[_0x82f2('0x23')](_0x82f2('0x24'),_0xd59d94[_0x82f2('0x25')],_0xd59d94[_0x82f2('0x26')],_0x41231d,_0x5b651d)[_0x82f2('0x1a')](respondWithFilteredResult(_0x40b696,_0x41231d))['catch'](handleError(_0x40b696,null));};exports['show']=function(_0x134760,_0x5673ce){var _0x2797df={'raw':![],'where':{'id':_0x134760[_0x82f2('0x27')]['id']}},_0x141bef={};_0x141bef[_0x82f2('0x28')]=_['keys'](db[_0x82f2('0x29')][_0x82f2('0x2a')]);_0x141bef[_0x82f2('0x25')]=_[_0x82f2('0x2b')](_0x134760[_0x82f2('0x25')]);_0x141bef['filters']=_['intersection'](_0x141bef[_0x82f2('0x28')],_0x141bef[_0x82f2('0x25')]);_0x2797df[_0x82f2('0x2c')]=_['intersection'](_0x141bef[_0x82f2('0x28')],qs['fields'](_0x134760[_0x82f2('0x25')]['fields']));_0x2797df[_0x82f2('0x2c')]=_0x2797df['attributes'][_0x82f2('0x2d')]?_0x2797df[_0x82f2('0x2c')]:_0x141bef[_0x82f2('0x28')];if(_0x134760[_0x82f2('0x25')][_0x82f2('0x2e')]){_0x2797df[_0x82f2('0x2f')]=[{'all':!![]}];}_0x2797df=_[_0x82f2('0x30')]({},_0x2797df,_0x134760[_0x82f2('0x31')]);return db[_0x82f2('0x29')][_0x82f2('0x32')](_0x2797df)[_0x82f2('0x1a')](handleEntityNotFound(_0x5673ce,null))[_0x82f2('0x1a')](respondWithResult(_0x5673ce,null))['catch'](handleError(_0x5673ce,null));};exports[_0x82f2('0x33')]=function(_0x54c64d,_0x4bbae9){return db[_0x82f2('0x29')][_0x82f2('0x33')](_0x54c64d[_0x82f2('0x34')],{})['then'](respondWithResult(_0x4bbae9,0xc9))[_0x82f2('0x35')](handleError(_0x4bbae9,null));};exports['update']=function(_0x64353e,_0x3ba9df){if(_0x64353e['body']['id']){delete _0x64353e[_0x82f2('0x34')]['id'];}return db[_0x82f2('0x29')][_0x82f2('0x32')]({'where':{'id':_0x64353e['params']['id']}})[_0x82f2('0x1a')](handleEntityNotFound(_0x3ba9df,null))[_0x82f2('0x1a')](saveUpdates(_0x64353e[_0x82f2('0x34')],null))[_0x82f2('0x1a')](respondWithResult(_0x3ba9df,null))[_0x82f2('0x35')](handleError(_0x3ba9df,null));};exports[_0x82f2('0x19')]=function(_0x9299d9,_0xb8c5d0){return db[_0x82f2('0x29')][_0x82f2('0x32')]({'where':{'id':_0x9299d9[_0x82f2('0x27')]['id']}})[_0x82f2('0x1a')](handleEntityNotFound(_0xb8c5d0,null))[_0x82f2('0x1a')](removeEntity(_0xb8c5d0,null))['catch'](handleError(_0xb8c5d0,null));};exports['describe']=function(_0x148d63,_0x444785){return db['WhatsappInteraction']['describe']()[_0x82f2('0x1a')](respondWithResult(_0x444785,null))[_0x82f2('0x35')](handleError(_0x444785,null));};exports[_0x82f2('0x36')]=function(_0x1888a8,_0x17891f){return db[_0x82f2('0x37')][_0x82f2('0x32')]({'where':{'id':_0x1888a8[_0x82f2('0x27')]['id']}})[_0x82f2('0x1a')](handleEntityNotFound(_0x17891f,null))[_0x82f2('0x1a')](function(_0x1dff17){if(_0x1dff17){return _0x1dff17[_0x82f2('0x36')](_0x1888a8[_0x82f2('0x34')]['ids'],_[_0x82f2('0x38')](_0x1888a8[_0x82f2('0x34')],[_0x82f2('0x39'),'id'])||{});}})[_0x82f2('0x1a')](respondWithResult(_0x17891f,null))[_0x82f2('0x35')](handleError(_0x17891f,null));};exports['getMessages']=function(_0x2fbd07,_0x4e51d9){var _0x14f9f7={'raw':![],'where':{}};var _0x1bfd73={};var _0x5589ce={'count':0x0,'rows':[]};return db[_0x82f2('0x29')][_0x82f2('0x3a')]({'where':{'id':_0x2fbd07[_0x82f2('0x27')]['id']}})[_0x82f2('0x1a')](handleEntityNotFound(_0x4e51d9,null))[_0x82f2('0x1a')](function(_0x7c352b){if(_0x7c352b){_0x1bfd73['model']=_[_0x82f2('0x2b')](db[_0x82f2('0x37')][_0x82f2('0x2a')]);_0x1bfd73[_0x82f2('0x25')]=_[_0x82f2('0x2b')](_0x2fbd07[_0x82f2('0x25')]);_0x1bfd73['filters']=_[_0x82f2('0x3b')](_0x1bfd73['model'],_0x1bfd73[_0x82f2('0x25')]);_0x14f9f7[_0x82f2('0x2c')]=_[_0x82f2('0x3b')](_0x1bfd73[_0x82f2('0x28')],qs[_0x82f2('0x3c')](_0x2fbd07['query'][_0x82f2('0x3c')]));_0x14f9f7[_0x82f2('0x2c')]=_0x14f9f7['attributes'][_0x82f2('0x2d')]?_0x14f9f7[_0x82f2('0x2c')]:_0x1bfd73['model'];if(!_0x2fbd07[_0x82f2('0x25')][_0x82f2('0x3d')]('nolimit')){_0x14f9f7[_0x82f2('0x3e')]=qs[_0x82f2('0x3e')](_0x2fbd07[_0x82f2('0x25')][_0x82f2('0x3e')]);_0x14f9f7['offset']=qs[_0x82f2('0x13')](_0x2fbd07[_0x82f2('0x25')][_0x82f2('0x13')]);}_0x14f9f7[_0x82f2('0x3f')]=qs[_0x82f2('0x40')](_0x2fbd07[_0x82f2('0x25')]['sort']);_0x14f9f7[_0x82f2('0x41')]=qs[_0x82f2('0x42')](_['pick'](_0x2fbd07[_0x82f2('0x25')],_0x1bfd73[_0x82f2('0x42')]));_0x14f9f7[_0x82f2('0x41')]['WhatsappInteractionId']=_0x7c352b['id'];if(_0x2fbd07[_0x82f2('0x25')][_0x82f2('0x43')]){_0x14f9f7[_0x82f2('0x41')]=_[_0x82f2('0x30')](_0x14f9f7[_0x82f2('0x41')],{'$or':_[_0x82f2('0x1e')](_0x14f9f7['attributes'],function(_0x262d21){var _0x1119b5={};_0x1119b5[_0x262d21]={'$like':'%'+_0x2fbd07[_0x82f2('0x25')]['filter']+'%'};return _0x1119b5;})});}if(_0x2fbd07['query'][_0x82f2('0x44')]){var _0x37e4da=_0x2fbd07[_0x82f2('0x25')][_0x82f2('0x44')][_0x82f2('0x45')](',');var _0x12a4d3={};_0x12a4d3[_0x37e4da[0x0]]={'$gte':moment(_0x37e4da[0x1])[_0x82f2('0x46')](_0x82f2('0x47'))};_0x14f9f7[_0x82f2('0x41')]=_[_0x82f2('0x30')](_0x14f9f7[_0x82f2('0x41')],_0x12a4d3);}_0x14f9f7=_[_0x82f2('0x30')]({},_0x14f9f7,_0x2fbd07[_0x82f2('0x31')]);return db[_0x82f2('0x37')][_0x82f2('0x15')]({'where':_0x14f9f7[_0x82f2('0x41')]})[_0x82f2('0x1a')](function(_0x456469){_0x5589ce[_0x82f2('0x15')]=_0x456469;if(_0x2fbd07['query']['includeAll']){_0x14f9f7[_0x82f2('0x2f')]=[{'all':!![]}];}return db['WhatsappMessage']['findAll'](_0x14f9f7);})['then'](function(_0x5e8a44){_0x5589ce[_0x82f2('0x48')]=_0x5e8a44;return _0x5589ce;});}})['then'](respondWithFilteredResult(_0x4e51d9,_0x14f9f7))[_0x82f2('0x35')](handleError(_0x4e51d9,null));};exports[_0x82f2('0x49')]=function(_0x44b224,_0x433ac0){return db[_0x82f2('0x29')][_0x82f2('0x32')]({'where':{'id':_0x44b224[_0x82f2('0x27')]['id']}})[_0x82f2('0x1a')](handleEntityNotFound(_0x433ac0,null))[_0x82f2('0x1a')](function(_0xacc2ff){if(_0xacc2ff){return _0xacc2ff['setTags'](_0x44b224[_0x82f2('0x34')]['ids'],_[_0x82f2('0x38')](_0x44b224[_0x82f2('0x34')],['ids','id'])||{})['spread'](function(){return db[_0x82f2('0x4a')][_0x82f2('0x23')]({'attributes':['id',_0x82f2('0x1b'),_0x82f2('0x4b')],'where':{'id':_0x44b224[_0x82f2('0x34')][_0x82f2('0x39')]}});})['then'](function(_0xfc3ce8){socket['emit'](_0x82f2('0x4c'),{'id':Number(_0x44b224[_0x82f2('0x27')]['id']),'tags':_0xfc3ce8||[]});return{'id':Number(_0x44b224[_0x82f2('0x27')]['id']),'tags':_0xfc3ce8||[]};});}})[_0x82f2('0x1a')](respondWithResult(_0x433ac0,null))[_0x82f2('0x35')](handleError(_0x433ac0,null));};exports[_0x82f2('0x4d')]=function(_0x5cd102,_0x2d9cbb){return db[_0x82f2('0x29')][_0x82f2('0x32')]({'where':{'id':_0x5cd102[_0x82f2('0x27')]['id']}})[_0x82f2('0x1a')](handleEntityNotFound(_0x2d9cbb,null))['then'](function(_0x2eef32){if(_0x2eef32){return _0x2eef32['removeTags'](_0x5cd102[_0x82f2('0x25')][_0x82f2('0x39')]);}})[_0x82f2('0x1a')](respondWithStatusCode(_0x2d9cbb,null))[_0x82f2('0x35')](handleError(_0x2d9cbb,null));};exports[_0x82f2('0x4e')]=function(_0x1b2c37,_0x42efb7){var _0x24b7bf=moment()[_0x82f2('0x4f')]()[_0x82f2('0x50')]();var _0x40debb=path[_0x82f2('0x51')](config[_0x82f2('0x52')],_0x82f2('0x53'),'files','tmp');var _0x1da9da=path[_0x82f2('0x51')](config[_0x82f2('0x52')],_0x82f2('0x53'),_0x82f2('0x54'),'attachments');var _0x512c29=path[_0x82f2('0x51')](_0x40debb,_0x24b7bf);var _0xdb2321=util['format'](_0x82f2('0x55'),_0x1b2c37[_0x82f2('0x27')]['id'],_0x24b7bf);var _0x24adb7=path[_0x82f2('0x51')](_0x40debb,_0xdb2321);var _0x1fd2c4=[];_0x1fd2c4['push']({'model':db[_0x82f2('0x56')],'as':'User','attributes':['id',_0x82f2('0x57')],'raw':!![]});_0x1fd2c4[_0x82f2('0x58')]({'model':db['CmContact'],'as':_0x82f2('0x59'),'attributes':['id',_0x82f2('0x5a'),_0x82f2('0x5b')],'raw':!![]});if(_0x1b2c37[_0x82f2('0x25')][_0x82f2('0x5c')]){_0x1fd2c4[_0x82f2('0x58')]({'model':db[_0x82f2('0x5d')],'as':_0x82f2('0x5d'),'raw':!![]});}var _0x5b4918=[{'model':db[_0x82f2('0x37')],'as':'Messages','attributes':['id','body',_0x82f2('0x5e'),_0x82f2('0x5f'),_0x82f2('0x60'),'read'],'include':_0x1fd2c4}];_0x5b4918[_0x82f2('0x58')]({'model':db[_0x82f2('0x61')],'as':_0x82f2('0x62'),'attributes':['id','name']});_0x5b4918[_0x82f2('0x58')]({'model':db[_0x82f2('0x56')],'as':_0x82f2('0x63'),'attributes':['id',_0x82f2('0x57')]});return db[_0x82f2('0x29')]['find']({'where':{'id':_0x1b2c37[_0x82f2('0x27')]['id']},'include':_0x5b4918})['then'](handleEntityNotFound(_0x42efb7,null))[_0x82f2('0x1a')](function(_0x319bf1){if(_0x319bf1){var _0x1e5ec3=_0x319bf1['get']({'plain':!![]});fs[_0x82f2('0x64')](_0x512c29);var _0x4f0136={'channel':_0x82f2('0x65'),'account':_0x1e5ec3[_0x82f2('0x62')][_0x82f2('0x1b')],'agent':_0x1e5ec3[_0x82f2('0x63')]?_0x1e5ec3[_0x82f2('0x63')][_0x82f2('0x57')]:_0x82f2('0x66'),'createdAt':moment(_0x1e5ec3[_0x82f2('0x5e')])[_0x82f2('0x46')](_0x82f2('0x47'))[_0x82f2('0x50')](),'closedAt':_0x1e5ec3[_0x82f2('0x67')]?moment(_0x1e5ec3[_0x82f2('0x68')])[_0x82f2('0x46')](_0x82f2('0x47'))[_0x82f2('0x50')]():'','disposition':_0x1e5ec3[_0x82f2('0x69')],'messages':_(_0x1e5ec3[_0x82f2('0x6a')])[_0x82f2('0x43')]({'secret':![]})[_0x82f2('0x1e')](function(_0xb1f597){if(_0xb1f597[_0x82f2('0x5d')]){var _0xc3d488=path['join'](_0x1da9da,_0xb1f597['Attachment']['basename']);if(fs['existsSync'](_0xc3d488)){fs[_0x82f2('0x6b')](_0xc3d488)[_0x82f2('0x6c')](fs['createWriteStream'](path[_0x82f2('0x51')](_0x512c29,_0xb1f597[_0x82f2('0x5d')]['basename'])));}}return{'date':moment(_0xb1f597[_0x82f2('0x5e')])[_0x82f2('0x46')]('YYYY-MM-DD\x20HH:mm:ss')['toString'](),'sender':_0xb1f597['direction']===_0x82f2('0x6d')?_0xb1f597['User']?_0xb1f597[_0x82f2('0x56')][_0x82f2('0x57')]:_0x82f2('0x6e'):_0xb1f597[_0x82f2('0x59')]['firstName']+(_0xb1f597[_0x82f2('0x59')]['lastName']?'\x20'+_0xb1f597[_0x82f2('0x59')][_0x82f2('0x5b')]:''),'body':_0xb1f597[_0x82f2('0x5d')]?_0xb1f597[_0x82f2('0x5d')][_0x82f2('0x6f')]:_0xb1f597[_0x82f2('0x34')],'direction':_0xb1f597[_0x82f2('0x5f')]===_0x82f2('0x6d')?'A':'C','secret':_0xb1f597['secret'],'read':_0xb1f597[_0x82f2('0x70')],'attachment':_0xb1f597[_0x82f2('0x5d')]};})[_0x82f2('0x71')]()};return ejs['renderFile'](path[_0x82f2('0x51')](config[_0x82f2('0x52')],_0x82f2('0x53'),'views',_0x82f2('0x72')),{'interaction':_0x4f0136})[_0x82f2('0x1a')](function(_0x25a4e1){var _0x55d6af=path[_0x82f2('0x51')](_0x512c29,_0x82f2('0x73')+_0x1e5ec3['id']+'-'+_0x24b7bf+_0x82f2('0x74'));var _0x3fa315={'path':_0x55d6af,'channel':_0x82f2('0x65'),'interactionId':_0x1e5ec3['id']};return pdf[_0x82f2('0x75')](_0x25a4e1,_0x3fa315);})[_0x82f2('0x1a')](function(){return new BPromise(function(_0xdbd794,_0x3364fe){zipdir(_0x512c29,{'saveTo':_0x24adb7},function(_0x42da79,_0x3ba4f6){if(_0x42da79)return _0x3364fe(_0x42da79);return _0xdbd794(_0x3ba4f6);});})[_0x82f2('0x1a')](function(){return new BPromise(function(_0x5a24f7,_0x1ffb17){rimraf(_0x512c29,function(_0x5d147e){if(_0x5d147e)_0x1ffb17(_0x5d147e);return _0x5a24f7();});});})['then'](function(){return _0x42efb7['download'](_0x24adb7,_0xdb2321,function(_0x524583){if(_0x524583){console[_0x82f2('0x76')]('err',_0x524583);}else{fs['unlinkSync'](_0x24adb7);}});});});}else{return _0x42efb7[_0x82f2('0xf')](0xc8);}})[_0x82f2('0x35')](handleError(_0x42efb7,null));};