b5707324ea43997a1002f964834217bc7d19a6bd
[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 _0x24bb=['filter','$gte','split','format','YYYY-MM-DD\x20HH:mm:ss','rows','addTags','setTags','omit','spread','Tag','emit','removeTags','download','unix','toString','join','root','files','tmp','server','attachments','transcript-%d-%s.zip','push','User','fullname','Contact','lastName','Attachment','Messages','createdAt','read','WhatsappAccount','Account','Owner','get','mkdirSync','Whatsapp','N.A.','closed','closedAt','map','existsSync','pipe','createWriteStream','basename','direction','firstName','secret','value','views','transcript-','.pdf','err','unlinkSync','rimraf','moment','util','path','lodash','../../components/parsers/qs','../../components/export/pdf','../../config/logger','../../mysqldb','redis','defaults','localhost','socket.io-emitter','./whatsappInteraction.socket','sendStatus','status','json','undefined','limit','count','offset','Content-Range','update','then','destroy','end','stack','name','index','WhatsappInteraction','rawAttributes','fieldName','findAll','whatsapp','query','catch','show','model','keys','intersection','attributes','fields','length','includeAll','include','merge','options','find','create','body','params','describe','addMessage','ids','getMessages','findOne','WhatsappMessage','filters','hasOwnProperty','nolimit','order','sort','pick','where','WhatsappInteractionId'];(function(_0x2abe95,_0x2c6820){var _0x3f296f=function(_0xb4546c){while(--_0xb4546c){_0x2abe95['push'](_0x2abe95['shift']());}};_0x3f296f(++_0x2c6820);}(_0x24bb,0x19f));var _0xb24b=function(_0x53419f,_0xc00c3b){_0x53419f=_0x53419f-0x0;var _0x1a3ff7=_0x24bb[_0x53419f];return _0x1a3ff7;};'use strict';var rimraf=require(_0xb24b('0x0'));var zipdir=require('zip-dir');var moment=require(_0xb24b('0x1'));var BPromise=require('bluebird');var util=require(_0xb24b('0x2'));var path=require(_0xb24b('0x3'));var ejs=require('ejs');var fs=require('fs');var _=require(_0xb24b('0x4'));var Redis=require('ioredis');var interaction=require('../../components/interaction/service');var qs=require(_0xb24b('0x5'));var pdf=require(_0xb24b('0x6'));var logger=require(_0xb24b('0x7'))('api');var config=require('../../config/environment');var db=require(_0xb24b('0x8'))['db'];config[_0xb24b('0x9')]=_[_0xb24b('0xa')](config[_0xb24b('0x9')],{'host':_0xb24b('0xb'),'port':0x18eb});var socket=require(_0xb24b('0xc'))(new Redis(config[_0xb24b('0x9')]));require(_0xb24b('0xd'))['register'](socket);function respondWithStatusCode(_0x3bb468,_0x450c9c){_0x450c9c=_0x450c9c||0xcc;return function(_0x3d68d3){if(_0x3d68d3){return _0x3bb468[_0xb24b('0xe')](_0x450c9c);}return _0x3bb468['status'](_0x450c9c)['end']();};}function respondWithResult(_0x1b6c09,_0x2044f1){_0x2044f1=_0x2044f1||0xc8;return function(_0x561297){if(_0x561297){return _0x1b6c09[_0xb24b('0xf')](_0x2044f1)[_0xb24b('0x10')](_0x561297);}};}function respondWithFilteredResult(_0x323ecf,_0x53ba78){return function(_0xb08418){if(_0xb08418){var _0x1d7894=typeof _0x53ba78['offset']===_0xb24b('0x11')&&typeof _0x53ba78[_0xb24b('0x12')]===_0xb24b('0x11');var _0x2f89f5=_0xb08418[_0xb24b('0x13')];var _0x4e95c8=_0x1d7894?0x0:_0x53ba78[_0xb24b('0x14')];var _0x33e000=_0x1d7894?_0xb08418[_0xb24b('0x13')]:_0x53ba78[_0xb24b('0x14')]+_0x53ba78[_0xb24b('0x12')];var _0x3d5a32;if(_0x33e000>=_0x2f89f5){_0x33e000=_0x2f89f5;_0x3d5a32=0xc8;}else{_0x3d5a32=0xce;}_0x323ecf[_0xb24b('0xf')](_0x3d5a32);return _0x323ecf['set'](_0xb24b('0x15'),_0x4e95c8+'-'+_0x33e000+'/'+_0x2f89f5)[_0xb24b('0x10')](_0xb08418);}return null;};}function saveUpdates(_0x292c4a){return function(_0x594b38){if(_0x594b38){return _0x594b38[_0xb24b('0x16')](_0x292c4a)[_0xb24b('0x17')](function(_0x54eb8a){return _0x54eb8a;});}return null;};}function removeEntity(_0x45f623){return function(_0x3f4339){if(_0x3f4339){return _0x3f4339[_0xb24b('0x18')]()[_0xb24b('0x17')](function(){_0x45f623[_0xb24b('0xf')](0xcc)[_0xb24b('0x19')]();});}};}function handleEntityNotFound(_0x4ec7c4){return function(_0x163fd0){if(!_0x163fd0){_0x4ec7c4[_0xb24b('0xe')](0x194);}return _0x163fd0;};}function handleError(_0x16bcbf,_0x2e8664){_0x2e8664=_0x2e8664||0x1f4;return function(_0xfc656d){logger['error'](_0xfc656d[_0xb24b('0x1a')]);if(_0xfc656d[_0xb24b('0x1b')]){delete _0xfc656d[_0xb24b('0x1b')];}_0x16bcbf[_0xb24b('0xf')](_0x2e8664)['send'](_0xfc656d);};}exports[_0xb24b('0x1c')]=function(_0x5f560f,_0x5d1f52){var _0x3981c4={};var _0x360c3e=_['map'](db[_0xb24b('0x1d')][_0xb24b('0x1e')],function(_0x1f7045){return{'name':_0x1f7045[_0xb24b('0x1f')],'type':_0x1f7045['type']['key']};});_0x3981c4=qs['getOptions'](_0x360c3e,_0x5f560f);return interaction[_0xb24b('0x20')](_0xb24b('0x21'),_0x5f560f[_0xb24b('0x22')],_0x5f560f['user'],_0x3981c4,_0x360c3e)[_0xb24b('0x17')](respondWithFilteredResult(_0x5d1f52,_0x3981c4))[_0xb24b('0x23')](handleError(_0x5d1f52,null));};exports[_0xb24b('0x24')]=function(_0x53888d,_0x112c4d){var _0x12d30a={'raw':![],'where':{'id':_0x53888d['params']['id']}},_0xf950fe={};_0xf950fe[_0xb24b('0x25')]=_[_0xb24b('0x26')](db[_0xb24b('0x1d')][_0xb24b('0x1e')]);_0xf950fe[_0xb24b('0x22')]=_[_0xb24b('0x26')](_0x53888d[_0xb24b('0x22')]);_0xf950fe['filters']=_[_0xb24b('0x27')](_0xf950fe[_0xb24b('0x25')],_0xf950fe[_0xb24b('0x22')]);_0x12d30a[_0xb24b('0x28')]=_[_0xb24b('0x27')](_0xf950fe[_0xb24b('0x25')],qs[_0xb24b('0x29')](_0x53888d[_0xb24b('0x22')]['fields']));_0x12d30a[_0xb24b('0x28')]=_0x12d30a[_0xb24b('0x28')][_0xb24b('0x2a')]?_0x12d30a['attributes']:_0xf950fe['model'];if(_0x53888d[_0xb24b('0x22')][_0xb24b('0x2b')]){_0x12d30a[_0xb24b('0x2c')]=[{'all':!![]}];}_0x12d30a=_[_0xb24b('0x2d')]({},_0x12d30a,_0x53888d[_0xb24b('0x2e')]);return db[_0xb24b('0x1d')][_0xb24b('0x2f')](_0x12d30a)['then'](handleEntityNotFound(_0x112c4d,null))[_0xb24b('0x17')](respondWithResult(_0x112c4d,null))['catch'](handleError(_0x112c4d,null));};exports[_0xb24b('0x30')]=function(_0x81e4d9,_0x237cc8){return db['WhatsappInteraction'][_0xb24b('0x30')](_0x81e4d9['body'],{})[_0xb24b('0x17')](respondWithResult(_0x237cc8,0xc9))[_0xb24b('0x23')](handleError(_0x237cc8,null));};exports[_0xb24b('0x16')]=function(_0x6c89d5,_0x1dc5e8){if(_0x6c89d5['body']['id']){delete _0x6c89d5[_0xb24b('0x31')]['id'];}return db[_0xb24b('0x1d')]['find']({'where':{'id':_0x6c89d5[_0xb24b('0x32')]['id']}})[_0xb24b('0x17')](handleEntityNotFound(_0x1dc5e8,null))[_0xb24b('0x17')](saveUpdates(_0x6c89d5['body'],null))['then'](respondWithResult(_0x1dc5e8,null))[_0xb24b('0x23')](handleError(_0x1dc5e8,null));};exports[_0xb24b('0x18')]=function(_0x1d1030,_0x3c6446){return db[_0xb24b('0x1d')][_0xb24b('0x2f')]({'where':{'id':_0x1d1030[_0xb24b('0x32')]['id']}})[_0xb24b('0x17')](handleEntityNotFound(_0x3c6446,null))[_0xb24b('0x17')](removeEntity(_0x3c6446,null))[_0xb24b('0x23')](handleError(_0x3c6446,null));};exports[_0xb24b('0x33')]=function(_0x3bb563,_0x509ccd){return db['WhatsappInteraction']['describe']()[_0xb24b('0x17')](respondWithResult(_0x509ccd,null))[_0xb24b('0x23')](handleError(_0x509ccd,null));};exports['addMessage']=function(_0x242444,_0x277420){return db['WhatsappMessage'][_0xb24b('0x2f')]({'where':{'id':_0x242444['params']['id']}})[_0xb24b('0x17')](handleEntityNotFound(_0x277420,null))['then'](function(_0x3a8ec0){if(_0x3a8ec0){return _0x3a8ec0[_0xb24b('0x34')](_0x242444[_0xb24b('0x31')]['ids'],_['omit'](_0x242444[_0xb24b('0x31')],[_0xb24b('0x35'),'id'])||{});}})[_0xb24b('0x17')](respondWithResult(_0x277420,null))[_0xb24b('0x23')](handleError(_0x277420,null));};exports[_0xb24b('0x36')]=function(_0x57f830,_0x470e94){var _0x545166={'raw':![],'where':{}};var _0x6878ae={};var _0x415da1={'count':0x0,'rows':[]};return db[_0xb24b('0x1d')][_0xb24b('0x37')]({'where':{'id':_0x57f830['params']['id']}})[_0xb24b('0x17')](handleEntityNotFound(_0x470e94,null))['then'](function(_0x445478){if(_0x445478){_0x6878ae[_0xb24b('0x25')]=_['keys'](db[_0xb24b('0x38')][_0xb24b('0x1e')]);_0x6878ae[_0xb24b('0x22')]=_[_0xb24b('0x26')](_0x57f830['query']);_0x6878ae[_0xb24b('0x39')]=_[_0xb24b('0x27')](_0x6878ae['model'],_0x6878ae[_0xb24b('0x22')]);_0x545166[_0xb24b('0x28')]=_[_0xb24b('0x27')](_0x6878ae[_0xb24b('0x25')],qs[_0xb24b('0x29')](_0x57f830[_0xb24b('0x22')]['fields']));_0x545166['attributes']=_0x545166[_0xb24b('0x28')]['length']?_0x545166[_0xb24b('0x28')]:_0x6878ae['model'];if(!_0x57f830[_0xb24b('0x22')][_0xb24b('0x3a')](_0xb24b('0x3b'))){_0x545166[_0xb24b('0x12')]=qs[_0xb24b('0x12')](_0x57f830[_0xb24b('0x22')][_0xb24b('0x12')]);_0x545166[_0xb24b('0x14')]=qs[_0xb24b('0x14')](_0x57f830[_0xb24b('0x22')][_0xb24b('0x14')]);}_0x545166[_0xb24b('0x3c')]=qs[_0xb24b('0x3d')](_0x57f830[_0xb24b('0x22')][_0xb24b('0x3d')]);_0x545166['where']=qs[_0xb24b('0x39')](_[_0xb24b('0x3e')](_0x57f830[_0xb24b('0x22')],_0x6878ae['filters']));_0x545166[_0xb24b('0x3f')][_0xb24b('0x40')]=_0x445478['id'];if(_0x57f830[_0xb24b('0x22')][_0xb24b('0x41')]){_0x545166[_0xb24b('0x3f')]=_[_0xb24b('0x2d')](_0x545166['where'],{'$or':_['map'](_0x545166[_0xb24b('0x28')],function(_0xbfcc8a){var _0x532c77={};_0x532c77[_0xbfcc8a]={'$like':'%'+_0x57f830[_0xb24b('0x22')][_0xb24b('0x41')]+'%'};return _0x532c77;})});}if(_0x57f830[_0xb24b('0x22')][_0xb24b('0x42')]){var _0x1aad0f=_0x57f830['query'][_0xb24b('0x42')][_0xb24b('0x43')](',');var _0x3ad41a={};_0x3ad41a[_0x1aad0f[0x0]]={'$gte':moment(_0x1aad0f[0x1])[_0xb24b('0x44')](_0xb24b('0x45'))};_0x545166[_0xb24b('0x3f')]=_[_0xb24b('0x2d')](_0x545166['where'],_0x3ad41a);}_0x545166=_[_0xb24b('0x2d')]({},_0x545166,_0x57f830[_0xb24b('0x2e')]);return db[_0xb24b('0x38')]['count']({'where':_0x545166[_0xb24b('0x3f')]})['then'](function(_0x25d8c2){_0x415da1[_0xb24b('0x13')]=_0x25d8c2;if(_0x57f830[_0xb24b('0x22')][_0xb24b('0x2b')]){_0x545166[_0xb24b('0x2c')]=[{'all':!![]}];}return db['WhatsappMessage'][_0xb24b('0x20')](_0x545166);})[_0xb24b('0x17')](function(_0x39f5bd){_0x415da1[_0xb24b('0x46')]=_0x39f5bd;return _0x415da1;});}})[_0xb24b('0x17')](respondWithFilteredResult(_0x470e94,_0x545166))['catch'](handleError(_0x470e94,null));};exports[_0xb24b('0x47')]=function(_0x73eeeb,_0x1b8a30){return db['WhatsappInteraction'][_0xb24b('0x2f')]({'where':{'id':_0x73eeeb[_0xb24b('0x32')]['id']}})[_0xb24b('0x17')](handleEntityNotFound(_0x1b8a30,null))[_0xb24b('0x17')](function(_0xc6be76){if(_0xc6be76){return _0xc6be76[_0xb24b('0x48')](_0x73eeeb[_0xb24b('0x31')][_0xb24b('0x35')],_[_0xb24b('0x49')](_0x73eeeb[_0xb24b('0x31')],['ids','id'])||{})[_0xb24b('0x4a')](function(){return db[_0xb24b('0x4b')][_0xb24b('0x20')]({'attributes':['id',_0xb24b('0x1b'),'color'],'where':{'id':_0x73eeeb[_0xb24b('0x31')][_0xb24b('0x35')]}});})[_0xb24b('0x17')](function(_0xab3bbb){socket[_0xb24b('0x4c')]('whatsappInteractionTags:save',{'id':Number(_0x73eeeb['params']['id']),'tags':_0xab3bbb||[]});return{'id':Number(_0x73eeeb['params']['id']),'tags':_0xab3bbb||[]};});}})['then'](respondWithResult(_0x1b8a30,null))['catch'](handleError(_0x1b8a30,null));};exports[_0xb24b('0x4d')]=function(_0x2f14b3,_0x5485c5){return db[_0xb24b('0x1d')][_0xb24b('0x2f')]({'where':{'id':_0x2f14b3[_0xb24b('0x32')]['id']}})[_0xb24b('0x17')](handleEntityNotFound(_0x5485c5,null))[_0xb24b('0x17')](function(_0x906fc0){if(_0x906fc0){return _0x906fc0['removeTags'](_0x2f14b3[_0xb24b('0x22')][_0xb24b('0x35')]);}})['then'](respondWithStatusCode(_0x5485c5,null))['catch'](handleError(_0x5485c5,null));};exports[_0xb24b('0x4e')]=function(_0x554867,_0x10c982){var _0x430293=moment()[_0xb24b('0x4f')]()[_0xb24b('0x50')]();var _0x2e6f99=path[_0xb24b('0x51')](config[_0xb24b('0x52')],'server',_0xb24b('0x53'),_0xb24b('0x54'));var _0x243d51=path[_0xb24b('0x51')](config[_0xb24b('0x52')],_0xb24b('0x55'),'files',_0xb24b('0x56'));var _0xbe2767=path['join'](_0x2e6f99,_0x430293);var _0xcabab6=util[_0xb24b('0x44')](_0xb24b('0x57'),_0x554867[_0xb24b('0x32')]['id'],_0x430293);var _0x385740=path[_0xb24b('0x51')](_0x2e6f99,_0xcabab6);var _0x26f89a=[];_0x26f89a[_0xb24b('0x58')]({'model':db[_0xb24b('0x59')],'as':_0xb24b('0x59'),'attributes':['id',_0xb24b('0x5a')],'raw':!![]});_0x26f89a[_0xb24b('0x58')]({'model':db['CmContact'],'as':_0xb24b('0x5b'),'attributes':['id','firstName',_0xb24b('0x5c')],'raw':!![]});if(_0x554867[_0xb24b('0x22')]['attachments']){_0x26f89a[_0xb24b('0x58')]({'model':db[_0xb24b('0x5d')],'as':'Attachment','raw':!![]});}var _0x43f998=[{'model':db[_0xb24b('0x38')],'as':_0xb24b('0x5e'),'attributes':['id',_0xb24b('0x31'),_0xb24b('0x5f'),'direction','secret',_0xb24b('0x60')],'include':_0x26f89a}];_0x43f998[_0xb24b('0x58')]({'model':db[_0xb24b('0x61')],'as':_0xb24b('0x62'),'attributes':['id',_0xb24b('0x1b')]});_0x43f998['push']({'model':db[_0xb24b('0x59')],'as':_0xb24b('0x63'),'attributes':['id',_0xb24b('0x5a')]});return db[_0xb24b('0x1d')][_0xb24b('0x2f')]({'where':{'id':_0x554867[_0xb24b('0x32')]['id']},'include':_0x43f998})[_0xb24b('0x17')](handleEntityNotFound(_0x10c982,null))[_0xb24b('0x17')](function(_0xd12728){if(_0xd12728){var _0xd05840=_0xd12728[_0xb24b('0x64')]({'plain':!![]});fs[_0xb24b('0x65')](_0xbe2767);var _0x14e1f8={'channel':_0xb24b('0x66'),'account':_0xd05840[_0xb24b('0x62')][_0xb24b('0x1b')],'agent':_0xd05840[_0xb24b('0x63')]?_0xd05840[_0xb24b('0x63')]['fullname']:_0xb24b('0x67'),'createdAt':moment(_0xd05840['createdAt'])[_0xb24b('0x44')](_0xb24b('0x45'))[_0xb24b('0x50')](),'closedAt':_0xd05840[_0xb24b('0x68')]?moment(_0xd05840[_0xb24b('0x69')])[_0xb24b('0x44')]('YYYY-MM-DD\x20HH:mm:ss')[_0xb24b('0x50')]():'','disposition':_0xd05840['disposition'],'messages':_(_0xd05840['Messages'])[_0xb24b('0x41')]({'secret':![]})[_0xb24b('0x6a')](function(_0x1266a8){if(_0x1266a8[_0xb24b('0x5d')]){var _0x5aad3d=path[_0xb24b('0x51')](_0x243d51,_0x1266a8[_0xb24b('0x5d')]['basename']);if(fs[_0xb24b('0x6b')](_0x5aad3d)){fs['createReadStream'](_0x5aad3d)[_0xb24b('0x6c')](fs[_0xb24b('0x6d')](path['join'](_0xbe2767,_0x1266a8[_0xb24b('0x5d')][_0xb24b('0x6e')])));}}return{'date':moment(_0x1266a8[_0xb24b('0x5f')])[_0xb24b('0x44')](_0xb24b('0x45'))[_0xb24b('0x50')](),'sender':_0x1266a8[_0xb24b('0x6f')]==='out'?_0x1266a8['User']?_0x1266a8[_0xb24b('0x59')][_0xb24b('0x5a')]:'System':_0x1266a8[_0xb24b('0x5b')][_0xb24b('0x70')]+(_0x1266a8[_0xb24b('0x5b')][_0xb24b('0x5c')]?'\x20'+_0x1266a8[_0xb24b('0x5b')]['lastName']:''),'body':_0x1266a8[_0xb24b('0x5d')]?_0x1266a8['Attachment'][_0xb24b('0x6e')]:_0x1266a8[_0xb24b('0x31')],'direction':_0x1266a8[_0xb24b('0x6f')]==='out'?'A':'C','secret':_0x1266a8[_0xb24b('0x71')],'read':_0x1266a8[_0xb24b('0x60')],'attachment':_0x1266a8['Attachment']};})[_0xb24b('0x72')]()};return ejs['renderFile'](path[_0xb24b('0x51')](config['root'],_0xb24b('0x55'),_0xb24b('0x73'),'downloadInteraction.ejs'),{'interaction':_0x14e1f8})['then'](function(_0x5da5f1){var _0x3322f7=path['join'](_0xbe2767,_0xb24b('0x74')+_0xd05840['id']+'-'+_0x430293+_0xb24b('0x75'));var _0x27ecca={'path':_0x3322f7,'channel':_0xb24b('0x66'),'interactionId':_0xd05840['id']};return pdf['createPdfFromHTML'](_0x5da5f1,_0x27ecca);})[_0xb24b('0x17')](function(){return new BPromise(function(_0x245bed,_0x14fef3){zipdir(_0xbe2767,{'saveTo':_0x385740},function(_0x6d4bf2,_0x327e92){if(_0x6d4bf2)return _0x14fef3(_0x6d4bf2);return _0x245bed(_0x327e92);});})[_0xb24b('0x17')](function(){return new BPromise(function(_0x12ddce,_0x1bb82e){rimraf(_0xbe2767,function(_0x2370c0){if(_0x2370c0)_0x1bb82e(_0x2370c0);return _0x12ddce();});});})[_0xb24b('0x17')](function(){return _0x10c982[_0xb24b('0x4e')](_0x385740,_0xcabab6,function(_0x173d49){if(_0x173d49){console['log'](_0xb24b('0x76'),_0x173d49);}else{fs[_0xb24b('0x77')](_0x385740);}});});});}else{return _0x10c982['sendStatus'](0xc8);}})[_0xb24b('0x23')](handleError(_0x10c982,null));};