9e4301f627b3f8ca2e283a6a50e01050884a1a27
[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 _0x9e24=['attributes','fields','length','merge','options','find','then','create','update','body','describe','addMessage','WhatsappMessage','ids','findOne','filters','hasOwnProperty','nolimit','sort','where','pick','WhatsappInteractionId','filter','$gte','split','format','include','rows','addTags','omit','spread','Tag','color','emit','whatsappInteractionTags:save','removeTags','download','unix','toString','join','root','server','files','transcript-%d-%s.zip','push','User','fullname','Contact','lastName','attachments','Attachment','Messages','createdAt','direction','secret','read','WhatsappAccount','Account','Owner','Whatsapp','N.A.','closed','closedAt','disposition','pipe','basename','out','System','firstName','value','renderFile','views','downloadInteraction.ejs','.pdf','createPdfFromHTML','err','unlinkSync','rimraf','moment','bluebird','path','ejs','lodash','ioredis','../../components/interaction/service','../../components/export/pdf','../../config/logger','redis','localhost','./whatsappInteraction.socket','register','sendStatus','status','end','undefined','limit','count','offset','set','Content-Range','json','destroy','error','name','index','map','WhatsappInteraction','fieldName','type','key','findAll','query','catch','show','params','keys','rawAttributes','intersection','model'];(function(_0x2503a0,_0x1df296){var _0x283467=function(_0xa2c16){while(--_0xa2c16){_0x2503a0['push'](_0x2503a0['shift']());}};_0x283467(++_0x1df296);}(_0x9e24,0xc4));var _0x49e2=function(_0x2f9467,_0x40576f){_0x2f9467=_0x2f9467-0x0;var _0x39894c=_0x9e24[_0x2f9467];return _0x39894c;};'use strict';var rimraf=require(_0x49e2('0x0'));var zipdir=require('zip-dir');var moment=require(_0x49e2('0x1'));var BPromise=require(_0x49e2('0x2'));var util=require('util');var path=require(_0x49e2('0x3'));var ejs=require(_0x49e2('0x4'));var fs=require('fs');var _=require(_0x49e2('0x5'));var Redis=require(_0x49e2('0x6'));var interaction=require(_0x49e2('0x7'));var qs=require('../../components/parsers/qs');var pdf=require(_0x49e2('0x8'));var logger=require(_0x49e2('0x9'))('api');var config=require('../../config/environment');var db=require('../../mysqldb')['db'];config[_0x49e2('0xa')]=_['defaults'](config[_0x49e2('0xa')],{'host':_0x49e2('0xb'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config['redis']));require(_0x49e2('0xc'))[_0x49e2('0xd')](socket);function respondWithStatusCode(_0x1ff6fc,_0x12d743){_0x12d743=_0x12d743||0xcc;return function(_0x2b1611){if(_0x2b1611){return _0x1ff6fc[_0x49e2('0xe')](_0x12d743);}return _0x1ff6fc[_0x49e2('0xf')](_0x12d743)[_0x49e2('0x10')]();};}function respondWithResult(_0x1166a1,_0x30d9d8){_0x30d9d8=_0x30d9d8||0xc8;return function(_0x103372){if(_0x103372){return _0x1166a1['status'](_0x30d9d8)['json'](_0x103372);}};}function respondWithFilteredResult(_0x43a666,_0x553120){return function(_0x91d7b9){if(_0x91d7b9){var _0x324507=typeof _0x553120['offset']===_0x49e2('0x11')&&typeof _0x553120[_0x49e2('0x12')]===_0x49e2('0x11');var _0x25192f=_0x91d7b9[_0x49e2('0x13')];var _0x3de693=_0x324507?0x0:_0x553120[_0x49e2('0x14')];var _0x2a97b3=_0x324507?_0x91d7b9['count']:_0x553120[_0x49e2('0x14')]+_0x553120[_0x49e2('0x12')];var _0x21c9b6;if(_0x2a97b3>=_0x25192f){_0x2a97b3=_0x25192f;_0x21c9b6=0xc8;}else{_0x21c9b6=0xce;}_0x43a666[_0x49e2('0xf')](_0x21c9b6);return _0x43a666[_0x49e2('0x15')](_0x49e2('0x16'),_0x3de693+'-'+_0x2a97b3+'/'+_0x25192f)[_0x49e2('0x17')](_0x91d7b9);}return null;};}function saveUpdates(_0x45e042){return function(_0x1ba30a){if(_0x1ba30a){return _0x1ba30a['update'](_0x45e042)['then'](function(_0xd34244){return _0xd34244;});}return null;};}function removeEntity(_0x3ada85){return function(_0x5d8384){if(_0x5d8384){return _0x5d8384[_0x49e2('0x18')]()['then'](function(){_0x3ada85['status'](0xcc)[_0x49e2('0x10')]();});}};}function handleEntityNotFound(_0x50c262){return function(_0x4a1d76){if(!_0x4a1d76){_0x50c262['sendStatus'](0x194);}return _0x4a1d76;};}function handleError(_0x5e3145,_0x285311){_0x285311=_0x285311||0x1f4;return function(_0x1e40f2){logger[_0x49e2('0x19')](_0x1e40f2['stack']);if(_0x1e40f2[_0x49e2('0x1a')]){delete _0x1e40f2[_0x49e2('0x1a')];}_0x5e3145['status'](_0x285311)['send'](_0x1e40f2);};}exports[_0x49e2('0x1b')]=function(_0xae301,_0x448086){var _0xb8743f={};var _0x3f4be2=_[_0x49e2('0x1c')](db[_0x49e2('0x1d')]['rawAttributes'],function(_0x514a6d){return{'name':_0x514a6d[_0x49e2('0x1e')],'type':_0x514a6d[_0x49e2('0x1f')][_0x49e2('0x20')]};});_0xb8743f=qs['getOptions'](_0x3f4be2,_0xae301);return interaction[_0x49e2('0x21')]('whatsapp',_0xae301[_0x49e2('0x22')],_0xae301['user'],_0xb8743f,_0x3f4be2)['then'](respondWithFilteredResult(_0x448086,_0xb8743f))[_0x49e2('0x23')](handleError(_0x448086,null));};exports[_0x49e2('0x24')]=function(_0x228c5e,_0x56f910){var _0x53e168={'raw':![],'where':{'id':_0x228c5e[_0x49e2('0x25')]['id']}},_0x450119={};_0x450119['model']=_[_0x49e2('0x26')](db['WhatsappInteraction'][_0x49e2('0x27')]);_0x450119[_0x49e2('0x22')]=_[_0x49e2('0x26')](_0x228c5e[_0x49e2('0x22')]);_0x450119['filters']=_[_0x49e2('0x28')](_0x450119[_0x49e2('0x29')],_0x450119['query']);_0x53e168[_0x49e2('0x2a')]=_[_0x49e2('0x28')](_0x450119[_0x49e2('0x29')],qs[_0x49e2('0x2b')](_0x228c5e[_0x49e2('0x22')][_0x49e2('0x2b')]));_0x53e168[_0x49e2('0x2a')]=_0x53e168[_0x49e2('0x2a')][_0x49e2('0x2c')]?_0x53e168[_0x49e2('0x2a')]:_0x450119['model'];if(_0x228c5e['query']['includeAll']){_0x53e168['include']=[{'all':!![]}];}_0x53e168=_[_0x49e2('0x2d')]({},_0x53e168,_0x228c5e[_0x49e2('0x2e')]);return db['WhatsappInteraction'][_0x49e2('0x2f')](_0x53e168)[_0x49e2('0x30')](handleEntityNotFound(_0x56f910,null))['then'](respondWithResult(_0x56f910,null))['catch'](handleError(_0x56f910,null));};exports[_0x49e2('0x31')]=function(_0x3df45d,_0x4d0f2b){return db[_0x49e2('0x1d')][_0x49e2('0x31')](_0x3df45d['body'],{})[_0x49e2('0x30')](respondWithResult(_0x4d0f2b,0xc9))[_0x49e2('0x23')](handleError(_0x4d0f2b,null));};exports[_0x49e2('0x32')]=function(_0x5087ca,_0x55bca4){if(_0x5087ca[_0x49e2('0x33')]['id']){delete _0x5087ca[_0x49e2('0x33')]['id'];}return db['WhatsappInteraction']['find']({'where':{'id':_0x5087ca[_0x49e2('0x25')]['id']}})[_0x49e2('0x30')](handleEntityNotFound(_0x55bca4,null))[_0x49e2('0x30')](saveUpdates(_0x5087ca[_0x49e2('0x33')],null))['then'](respondWithResult(_0x55bca4,null))['catch'](handleError(_0x55bca4,null));};exports[_0x49e2('0x18')]=function(_0x441dff,_0x5812ec){return db[_0x49e2('0x1d')]['find']({'where':{'id':_0x441dff[_0x49e2('0x25')]['id']}})[_0x49e2('0x30')](handleEntityNotFound(_0x5812ec,null))['then'](removeEntity(_0x5812ec,null))['catch'](handleError(_0x5812ec,null));};exports[_0x49e2('0x34')]=function(_0x30d15f,_0x2f5c01){return db['WhatsappInteraction']['describe']()[_0x49e2('0x30')](respondWithResult(_0x2f5c01,null))[_0x49e2('0x23')](handleError(_0x2f5c01,null));};exports[_0x49e2('0x35')]=function(_0x1f2b35,_0x30fbe3){return db[_0x49e2('0x36')]['find']({'where':{'id':_0x1f2b35['params']['id']}})[_0x49e2('0x30')](handleEntityNotFound(_0x30fbe3,null))[_0x49e2('0x30')](function(_0x59229f){if(_0x59229f){return _0x59229f['addMessage'](_0x1f2b35[_0x49e2('0x33')][_0x49e2('0x37')],_['omit'](_0x1f2b35[_0x49e2('0x33')],[_0x49e2('0x37'),'id'])||{});}})[_0x49e2('0x30')](respondWithResult(_0x30fbe3,null))[_0x49e2('0x23')](handleError(_0x30fbe3,null));};exports['getMessages']=function(_0x469bc9,_0x209d6e){var _0x100012={'raw':![],'where':{}};var _0x110ddd={};var _0x1b1e73={'count':0x0,'rows':[]};return db[_0x49e2('0x1d')][_0x49e2('0x38')]({'where':{'id':_0x469bc9['params']['id']}})[_0x49e2('0x30')](handleEntityNotFound(_0x209d6e,null))['then'](function(_0x47535f){if(_0x47535f){_0x110ddd[_0x49e2('0x29')]=_[_0x49e2('0x26')](db[_0x49e2('0x36')]['rawAttributes']);_0x110ddd['query']=_[_0x49e2('0x26')](_0x469bc9[_0x49e2('0x22')]);_0x110ddd[_0x49e2('0x39')]=_['intersection'](_0x110ddd['model'],_0x110ddd[_0x49e2('0x22')]);_0x100012[_0x49e2('0x2a')]=_[_0x49e2('0x28')](_0x110ddd[_0x49e2('0x29')],qs[_0x49e2('0x2b')](_0x469bc9[_0x49e2('0x22')][_0x49e2('0x2b')]));_0x100012[_0x49e2('0x2a')]=_0x100012['attributes'][_0x49e2('0x2c')]?_0x100012[_0x49e2('0x2a')]:_0x110ddd[_0x49e2('0x29')];if(!_0x469bc9[_0x49e2('0x22')][_0x49e2('0x3a')](_0x49e2('0x3b'))){_0x100012[_0x49e2('0x12')]=qs[_0x49e2('0x12')](_0x469bc9[_0x49e2('0x22')][_0x49e2('0x12')]);_0x100012['offset']=qs[_0x49e2('0x14')](_0x469bc9['query'][_0x49e2('0x14')]);}_0x100012['order']=qs[_0x49e2('0x3c')](_0x469bc9[_0x49e2('0x22')][_0x49e2('0x3c')]);_0x100012[_0x49e2('0x3d')]=qs[_0x49e2('0x39')](_[_0x49e2('0x3e')](_0x469bc9[_0x49e2('0x22')],_0x110ddd[_0x49e2('0x39')]));_0x100012[_0x49e2('0x3d')][_0x49e2('0x3f')]=_0x47535f['id'];if(_0x469bc9[_0x49e2('0x22')]['filter']){_0x100012[_0x49e2('0x3d')]=_[_0x49e2('0x2d')](_0x100012[_0x49e2('0x3d')],{'$or':_['map'](_0x100012[_0x49e2('0x2a')],function(_0x21357e){var _0x426f6e={};_0x426f6e[_0x21357e]={'$like':'%'+_0x469bc9[_0x49e2('0x22')][_0x49e2('0x40')]+'%'};return _0x426f6e;})});}if(_0x469bc9[_0x49e2('0x22')]['$gte']){var _0x25f1f1=_0x469bc9[_0x49e2('0x22')][_0x49e2('0x41')][_0x49e2('0x42')](',');var _0x5e3e18={};_0x5e3e18[_0x25f1f1[0x0]]={'$gte':moment(_0x25f1f1[0x1])[_0x49e2('0x43')]('YYYY-MM-DD\x20HH:mm:ss')};_0x100012[_0x49e2('0x3d')]=_[_0x49e2('0x2d')](_0x100012[_0x49e2('0x3d')],_0x5e3e18);}_0x100012=_[_0x49e2('0x2d')]({},_0x100012,_0x469bc9[_0x49e2('0x2e')]);return db[_0x49e2('0x36')][_0x49e2('0x13')]({'where':_0x100012[_0x49e2('0x3d')]})[_0x49e2('0x30')](function(_0x3aff26){_0x1b1e73['count']=_0x3aff26;if(_0x469bc9[_0x49e2('0x22')]['includeAll']){_0x100012[_0x49e2('0x44')]=[{'all':!![]}];}return db[_0x49e2('0x36')]['findAll'](_0x100012);})[_0x49e2('0x30')](function(_0x3edf7a){_0x1b1e73[_0x49e2('0x45')]=_0x3edf7a;return _0x1b1e73;});}})[_0x49e2('0x30')](respondWithFilteredResult(_0x209d6e,_0x100012))['catch'](handleError(_0x209d6e,null));};exports[_0x49e2('0x46')]=function(_0x5a2b38,_0x462f46){return db[_0x49e2('0x1d')][_0x49e2('0x2f')]({'where':{'id':_0x5a2b38['params']['id']}})[_0x49e2('0x30')](handleEntityNotFound(_0x462f46,null))[_0x49e2('0x30')](function(_0x152c7b){if(_0x152c7b){return _0x152c7b['setTags'](_0x5a2b38[_0x49e2('0x33')][_0x49e2('0x37')],_[_0x49e2('0x47')](_0x5a2b38[_0x49e2('0x33')],['ids','id'])||{})[_0x49e2('0x48')](function(){return db[_0x49e2('0x49')]['findAll']({'attributes':['id',_0x49e2('0x1a'),_0x49e2('0x4a')],'where':{'id':_0x5a2b38['body'][_0x49e2('0x37')]}});})['then'](function(_0xdf6908){socket[_0x49e2('0x4b')](_0x49e2('0x4c'),{'id':Number(_0x5a2b38[_0x49e2('0x25')]['id']),'tags':_0xdf6908||[]});return{'id':Number(_0x5a2b38[_0x49e2('0x25')]['id']),'tags':_0xdf6908||[]};});}})[_0x49e2('0x30')](respondWithResult(_0x462f46,null))[_0x49e2('0x23')](handleError(_0x462f46,null));};exports[_0x49e2('0x4d')]=function(_0x26bae3,_0x42d290){return db[_0x49e2('0x1d')][_0x49e2('0x2f')]({'where':{'id':_0x26bae3['params']['id']}})[_0x49e2('0x30')](handleEntityNotFound(_0x42d290,null))[_0x49e2('0x30')](function(_0x36af48){if(_0x36af48){return _0x36af48['removeTags'](_0x26bae3[_0x49e2('0x22')][_0x49e2('0x37')]);}})[_0x49e2('0x30')](respondWithStatusCode(_0x42d290,null))[_0x49e2('0x23')](handleError(_0x42d290,null));};exports[_0x49e2('0x4e')]=function(_0x40ed8a,_0x57b48e){var _0x14dcb0=moment()[_0x49e2('0x4f')]()[_0x49e2('0x50')]();var _0x39a26c=path[_0x49e2('0x51')](config[_0x49e2('0x52')],_0x49e2('0x53'),_0x49e2('0x54'),'tmp');var _0x532975=path[_0x49e2('0x51')](config['root'],'server','files','attachments');var _0x1dd696=path[_0x49e2('0x51')](_0x39a26c,_0x14dcb0);var _0x236319=util[_0x49e2('0x43')](_0x49e2('0x55'),_0x40ed8a[_0x49e2('0x25')]['id'],_0x14dcb0);var _0x4eb4b7=path['join'](_0x39a26c,_0x236319);var _0x745869=[];_0x745869[_0x49e2('0x56')]({'model':db['User'],'as':_0x49e2('0x57'),'attributes':['id',_0x49e2('0x58')],'raw':!![]});_0x745869[_0x49e2('0x56')]({'model':db['CmContact'],'as':_0x49e2('0x59'),'attributes':['id','firstName',_0x49e2('0x5a')],'raw':!![]});if(_0x40ed8a[_0x49e2('0x22')][_0x49e2('0x5b')]){_0x745869[_0x49e2('0x56')]({'model':db[_0x49e2('0x5c')],'as':'Attachment','raw':!![]});}var _0x47bb5f=[{'model':db[_0x49e2('0x36')],'as':_0x49e2('0x5d'),'attributes':['id',_0x49e2('0x33'),_0x49e2('0x5e'),_0x49e2('0x5f'),_0x49e2('0x60'),_0x49e2('0x61')],'include':_0x745869}];_0x47bb5f[_0x49e2('0x56')]({'model':db[_0x49e2('0x62')],'as':_0x49e2('0x63'),'attributes':['id','name']});_0x47bb5f['push']({'model':db['User'],'as':_0x49e2('0x64'),'attributes':['id',_0x49e2('0x58')]});return db[_0x49e2('0x1d')][_0x49e2('0x2f')]({'where':{'id':_0x40ed8a[_0x49e2('0x25')]['id']},'include':_0x47bb5f})['then'](handleEntityNotFound(_0x57b48e,null))['then'](function(_0x1fe66b){if(_0x1fe66b){var _0x13668e=_0x1fe66b['get']({'plain':!![]});fs['mkdirSync'](_0x1dd696);var _0x462173={'channel':_0x49e2('0x65'),'account':_0x13668e['Account'][_0x49e2('0x1a')],'agent':_0x13668e[_0x49e2('0x64')]?_0x13668e[_0x49e2('0x64')]['fullname']:_0x49e2('0x66'),'createdAt':moment(_0x13668e[_0x49e2('0x5e')])[_0x49e2('0x43')]('YYYY-MM-DD\x20HH:mm:ss')[_0x49e2('0x50')](),'closedAt':_0x13668e[_0x49e2('0x67')]?moment(_0x13668e[_0x49e2('0x68')])[_0x49e2('0x43')]('YYYY-MM-DD\x20HH:mm:ss')[_0x49e2('0x50')]():'','disposition':_0x13668e[_0x49e2('0x69')],'messages':_(_0x13668e[_0x49e2('0x5d')])[_0x49e2('0x40')]({'secret':![]})[_0x49e2('0x1c')](function(_0xb3ea4){if(_0xb3ea4['Attachment']){var _0x2fd2a6=path[_0x49e2('0x51')](_0x532975,_0xb3ea4[_0x49e2('0x5c')]['basename']);if(fs['existsSync'](_0x2fd2a6)){fs['createReadStream'](_0x2fd2a6)[_0x49e2('0x6a')](fs['createWriteStream'](path[_0x49e2('0x51')](_0x1dd696,_0xb3ea4['Attachment'][_0x49e2('0x6b')])));}}return{'date':moment(_0xb3ea4[_0x49e2('0x5e')])['format']('YYYY-MM-DD\x20HH:mm:ss')[_0x49e2('0x50')](),'sender':_0xb3ea4[_0x49e2('0x5f')]===_0x49e2('0x6c')?_0xb3ea4[_0x49e2('0x57')]?_0xb3ea4[_0x49e2('0x57')][_0x49e2('0x58')]:_0x49e2('0x6d'):_0xb3ea4[_0x49e2('0x59')][_0x49e2('0x6e')]+(_0xb3ea4[_0x49e2('0x59')][_0x49e2('0x5a')]?'\x20'+_0xb3ea4[_0x49e2('0x59')]['lastName']:''),'body':_0xb3ea4['Attachment']?_0xb3ea4[_0x49e2('0x5c')][_0x49e2('0x6b')]:_0xb3ea4['body'],'direction':_0xb3ea4['direction']===_0x49e2('0x6c')?'A':'C','secret':_0xb3ea4[_0x49e2('0x60')],'read':_0xb3ea4[_0x49e2('0x61')],'attachment':_0xb3ea4[_0x49e2('0x5c')]};})[_0x49e2('0x6f')]()};return ejs[_0x49e2('0x70')](path[_0x49e2('0x51')](config[_0x49e2('0x52')],_0x49e2('0x53'),_0x49e2('0x71'),_0x49e2('0x72')),{'interaction':_0x462173})['then'](function(_0x3974d8){var _0x9e7e68=path[_0x49e2('0x51')](_0x1dd696,'transcript-'+_0x13668e['id']+'-'+_0x14dcb0+_0x49e2('0x73'));var _0x140403={'path':_0x9e7e68,'channel':_0x49e2('0x65'),'interactionId':_0x13668e['id']};return pdf[_0x49e2('0x74')](_0x3974d8,_0x140403);})[_0x49e2('0x30')](function(){return new BPromise(function(_0x2623d6,_0x5751f9){zipdir(_0x1dd696,{'saveTo':_0x4eb4b7},function(_0x4d6938,_0xd901d2){if(_0x4d6938)return _0x5751f9(_0x4d6938);return _0x2623d6(_0xd901d2);});})[_0x49e2('0x30')](function(){return new BPromise(function(_0x20273c,_0x5c271f){rimraf(_0x1dd696,function(_0x5c7d71){if(_0x5c7d71)_0x5c271f(_0x5c7d71);return _0x20273c();});});})[_0x49e2('0x30')](function(){return _0x57b48e['download'](_0x4eb4b7,_0x236319,function(_0x50e70f){if(_0x50e70f){console['log'](_0x49e2('0x75'),_0x50e70f);}else{fs[_0x49e2('0x76')](_0x4eb4b7);}});});});}else{return _0x57b48e[_0x49e2('0xe')](0xc8);}})[_0x49e2('0x23')](handleError(_0x57b48e,null));};