acde98973ccfd8ffba5a15d4eeefec86a9b31340
[motion2.git] / server / api / openchannelInteraction / openchannelInteraction.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 _0x692b=['index','map','OpenchannelInteraction','rawAttributes','fieldName','type','key','findAll','query','catch','show','from','model','keys','intersection','attributes','fields','length','includeAll','include','options','create','body','find','params','describe','addMessage','ids','omit','getMessages','findOne','filters','hasOwnProperty','where','pick','filter','merge','$gte','OpenchannelMessage','rows','addTags','spread','color','openchannelInteractionTags:save','removeTags','download','unix','toString','join','root','server','files','attachments','format','push','User','fullname','CmContact','Contact','firstName','lastName','Attachment','Messages','createdAt','direction','read','OpenchannelAccount','Account','Owner','get','mkdirSync','N.A.','closedAt','YYYY-MM-DD\x20HH:mm:ss','disposition','threadId','Public','Private','basename','existsSync','createReadStream','pipe','createWriteStream','System','renderFile','views','downloadInteraction.ejs','transcript-','Openchannel','createPdfFromHTML','Unable\x20to\x20delete\x20zip\x20file','unlinkSync','rimraf','zip-dir','moment','util','path','lodash','ioredis','../../components/interaction/service','../../components/parsers/qs','../../components/export/pdf','../../config/logger','api','../../config/environment','../../mysqldb','redis','localhost','socket.io-emitter','register','sendStatus','status','json','offset','limit','undefined','count','update','then','destroy','end','error','stack','name','send'];(function(_0x14206d,_0x8c7313){var _0x17bf4f=function(_0x18c67a){while(--_0x18c67a){_0x14206d['push'](_0x14206d['shift']());}};_0x17bf4f(++_0x8c7313);}(_0x692b,0xd9));var _0xb692=function(_0x1031e6,_0x3e5b64){_0x1031e6=_0x1031e6-0x0;var _0x224542=_0x692b[_0x1031e6];return _0x224542;};'use strict';var rimraf=require(_0xb692('0x0'));var zipdir=require(_0xb692('0x1'));var moment=require(_0xb692('0x2'));var util=require(_0xb692('0x3'));var path=require(_0xb692('0x4'));var ejs=require('ejs');var fs=require('fs');var _=require(_0xb692('0x5'));var Redis=require(_0xb692('0x6'));var interaction=require(_0xb692('0x7'));var qs=require(_0xb692('0x8'));var pdf=require(_0xb692('0x9'));var logger=require(_0xb692('0xa'))(_0xb692('0xb'));var config=require(_0xb692('0xc'));var db=require(_0xb692('0xd'))['db'];config[_0xb692('0xe')]=_['defaults'](config[_0xb692('0xe')],{'host':_0xb692('0xf'),'port':0x18eb});var socket=require(_0xb692('0x10'))(new Redis(config[_0xb692('0xe')]));require('./openchannelInteraction.socket')[_0xb692('0x11')](socket);function respondWithStatusCode(_0x36f8e4,_0x3af507){_0x3af507=_0x3af507||0xcc;return function(_0x689c18){if(_0x689c18){return _0x36f8e4[_0xb692('0x12')](_0x3af507);}return _0x36f8e4['status'](_0x3af507)['end']();};}function respondWithResult(_0x3564e3,_0x54902b){_0x54902b=_0x54902b||0xc8;return function(_0x1ecb62){if(_0x1ecb62){return _0x3564e3[_0xb692('0x13')](_0x54902b)[_0xb692('0x14')](_0x1ecb62);}};}function respondWithFilteredResult(_0x5e2346,_0x9ce6f6){return function(_0x1448f5){if(_0x1448f5){var _0x34e96a=typeof _0x9ce6f6[_0xb692('0x15')]==='undefined'&&typeof _0x9ce6f6[_0xb692('0x16')]===_0xb692('0x17');var _0x2765b8=_0x1448f5['count'];var _0x28f601=_0x34e96a?0x0:_0x9ce6f6[_0xb692('0x15')];var _0x1e5c5b=_0x34e96a?_0x1448f5[_0xb692('0x18')]:_0x9ce6f6[_0xb692('0x15')]+_0x9ce6f6['limit'];var _0x5258b7;if(_0x1e5c5b>=_0x2765b8){_0x1e5c5b=_0x2765b8;_0x5258b7=0xc8;}else{_0x5258b7=0xce;}_0x5e2346[_0xb692('0x13')](_0x5258b7);return _0x5e2346['set']('Content-Range',_0x28f601+'-'+_0x1e5c5b+'/'+_0x2765b8)['json'](_0x1448f5);}return null;};}function saveUpdates(_0x11d0ee){return function(_0x3d19dc){if(_0x3d19dc){return _0x3d19dc[_0xb692('0x19')](_0x11d0ee)[_0xb692('0x1a')](function(_0x197e75){return _0x197e75;});}return null;};}function removeEntity(_0x170dda){return function(_0x2dc55b){if(_0x2dc55b){return _0x2dc55b[_0xb692('0x1b')]()[_0xb692('0x1a')](function(){_0x170dda['status'](0xcc)[_0xb692('0x1c')]();});}};}function handleEntityNotFound(_0x244aba){return function(_0x2f602a){if(!_0x2f602a){_0x244aba[_0xb692('0x12')](0x194);}return _0x2f602a;};}function handleError(_0x473ada,_0x34a5b0){_0x34a5b0=_0x34a5b0||0x1f4;return function(_0x403ed3){logger[_0xb692('0x1d')](_0x403ed3[_0xb692('0x1e')]);if(_0x403ed3[_0xb692('0x1f')]){delete _0x403ed3[_0xb692('0x1f')];}_0x473ada['status'](_0x34a5b0)[_0xb692('0x20')](_0x403ed3);};}exports[_0xb692('0x21')]=function(_0x516569,_0x49f146){var _0x13b316={};var _0x1c92b2=_[_0xb692('0x22')](db[_0xb692('0x23')][_0xb692('0x24')],function(_0x3f8959){return{'name':_0x3f8959[_0xb692('0x25')],'type':_0x3f8959[_0xb692('0x26')][_0xb692('0x27')]};});_0x13b316=qs['getOptions'](_0x1c92b2,_0x516569);return interaction[_0xb692('0x28')]('openchannel',_0x516569[_0xb692('0x29')],_0x516569['user'],_0x13b316,_0x1c92b2)[_0xb692('0x1a')](respondWithFilteredResult(_0x49f146,_0x13b316))[_0xb692('0x2a')](handleError(_0x49f146,null));};exports[_0xb692('0x2b')]=function(_0x492bc9,_0x2d73e2){var _0x4aeb00={};if(_0x492bc9[_0xb692('0x29')][_0xb692('0x26')]&&_0x492bc9[_0xb692('0x29')][_0xb692('0x26')]===_0xb692('0x2c')){_0x4aeb00[_0xb692('0x2c')]=_0x492bc9['params']['id'];}else{_0x4aeb00['id']=_0x492bc9['params']['id'];}var _0xa021b2={'raw':![],'where':_0x4aeb00},_0x3d6ae5={};_0x3d6ae5[_0xb692('0x2d')]=_[_0xb692('0x2e')](db[_0xb692('0x23')][_0xb692('0x24')]);_0x3d6ae5[_0xb692('0x29')]=_[_0xb692('0x2e')](_0x492bc9[_0xb692('0x29')]);_0x3d6ae5['filters']=_[_0xb692('0x2f')](_0x3d6ae5[_0xb692('0x2d')],_0x3d6ae5[_0xb692('0x29')]);_0xa021b2[_0xb692('0x30')]=_[_0xb692('0x2f')](_0x3d6ae5[_0xb692('0x2d')],qs[_0xb692('0x31')](_0x492bc9[_0xb692('0x29')][_0xb692('0x31')]));_0xa021b2[_0xb692('0x30')]=_0xa021b2[_0xb692('0x30')][_0xb692('0x32')]?_0xa021b2[_0xb692('0x30')]:_0x3d6ae5['model'];if(_0x492bc9[_0xb692('0x29')][_0xb692('0x33')]){_0xa021b2[_0xb692('0x34')]=[{'all':!![]}];}_0xa021b2=_['merge']({},_0xa021b2,_0x492bc9[_0xb692('0x35')]);return db[_0xb692('0x23')]['find'](_0xa021b2)[_0xb692('0x1a')](handleEntityNotFound(_0x2d73e2,null))[_0xb692('0x1a')](respondWithResult(_0x2d73e2,null))[_0xb692('0x2a')](handleError(_0x2d73e2,null));};exports[_0xb692('0x36')]=function(_0x53741c,_0x3afae5){return db[_0xb692('0x23')][_0xb692('0x36')](_0x53741c[_0xb692('0x37')],{})[_0xb692('0x1a')](respondWithResult(_0x3afae5,0xc9))['catch'](handleError(_0x3afae5,null));};exports['update']=function(_0x2e7cc2,_0x4226ea){if(_0x2e7cc2['body']['id']){delete _0x2e7cc2[_0xb692('0x37')]['id'];}return db[_0xb692('0x23')][_0xb692('0x38')]({'where':{'id':_0x2e7cc2[_0xb692('0x39')]['id']}})['then'](handleEntityNotFound(_0x4226ea,null))[_0xb692('0x1a')](saveUpdates(_0x2e7cc2[_0xb692('0x37')],null))[_0xb692('0x1a')](respondWithResult(_0x4226ea,null))[_0xb692('0x2a')](handleError(_0x4226ea,null));};exports[_0xb692('0x1b')]=function(_0x545f89,_0x548ea4){return db['OpenchannelInteraction']['find']({'where':{'id':_0x545f89['params']['id']}})[_0xb692('0x1a')](handleEntityNotFound(_0x548ea4,null))[_0xb692('0x1a')](removeEntity(_0x548ea4,null))[_0xb692('0x2a')](handleError(_0x548ea4,null));};exports[_0xb692('0x3a')]=function(_0x161b41,_0x353ecf){return db[_0xb692('0x23')][_0xb692('0x3a')]()['then'](respondWithResult(_0x353ecf,null))['catch'](handleError(_0x353ecf,null));};exports['addMessage']=function(_0x22e3cc,_0x4ddfd6){return db['OpenchannelMessage'][_0xb692('0x38')]({'where':{'id':_0x22e3cc[_0xb692('0x39')]['id']}})[_0xb692('0x1a')](handleEntityNotFound(_0x4ddfd6,null))[_0xb692('0x1a')](function(_0x5e975b){if(_0x5e975b){return _0x5e975b[_0xb692('0x3b')](_0x22e3cc['body'][_0xb692('0x3c')],_[_0xb692('0x3d')](_0x22e3cc[_0xb692('0x37')],[_0xb692('0x3c'),'id'])||{});}})[_0xb692('0x1a')](respondWithResult(_0x4ddfd6,null))[_0xb692('0x2a')](handleError(_0x4ddfd6,null));};exports[_0xb692('0x3e')]=function(_0x2c9916,_0x3cf43d){var _0x568280={'raw':![],'where':{}};var _0x5635e8={};var _0x378c20={'count':0x0,'rows':[]};return db[_0xb692('0x23')][_0xb692('0x3f')]({'where':{'id':_0x2c9916['params']['id']}})[_0xb692('0x1a')](handleEntityNotFound(_0x3cf43d,null))[_0xb692('0x1a')](function(_0x3e99ee){if(_0x3e99ee){_0x5635e8[_0xb692('0x2d')]=_[_0xb692('0x2e')](db['OpenchannelMessage'][_0xb692('0x24')]);_0x5635e8[_0xb692('0x29')]=_['keys'](_0x2c9916[_0xb692('0x29')]);_0x5635e8[_0xb692('0x40')]=_[_0xb692('0x2f')](_0x5635e8[_0xb692('0x2d')],_0x5635e8[_0xb692('0x29')]);_0x568280[_0xb692('0x30')]=_[_0xb692('0x2f')](_0x5635e8['model'],qs[_0xb692('0x31')](_0x2c9916[_0xb692('0x29')][_0xb692('0x31')]));_0x568280['attributes']=_0x568280[_0xb692('0x30')][_0xb692('0x32')]?_0x568280[_0xb692('0x30')]:_0x5635e8[_0xb692('0x2d')];if(!_0x2c9916[_0xb692('0x29')][_0xb692('0x41')]('nolimit')){_0x568280['limit']=qs[_0xb692('0x16')](_0x2c9916[_0xb692('0x29')][_0xb692('0x16')]);_0x568280[_0xb692('0x15')]=qs['offset'](_0x2c9916[_0xb692('0x29')][_0xb692('0x15')]);}_0x568280['order']=qs['sort'](_0x2c9916['query']['sort']);_0x568280[_0xb692('0x42')]=qs[_0xb692('0x40')](_[_0xb692('0x43')](_0x2c9916['query'],_0x5635e8[_0xb692('0x40')]));_0x568280[_0xb692('0x42')]['OpenchannelInteractionId']=_0x3e99ee['id'];if(_0x2c9916[_0xb692('0x29')][_0xb692('0x44')]){_0x568280[_0xb692('0x42')]=_[_0xb692('0x45')](_0x568280[_0xb692('0x42')],{'$or':_['map'](_0x568280['attributes'],function(_0x4d9917){var _0x3c0fa1={};_0x3c0fa1[_0x4d9917]={'$like':'%'+_0x2c9916[_0xb692('0x29')][_0xb692('0x44')]+'%'};return _0x3c0fa1;})});}if(_0x2c9916[_0xb692('0x29')][_0xb692('0x46')]){var _0x8d10f8=_0x2c9916[_0xb692('0x29')][_0xb692('0x46')]['split'](',');var _0x5591ff={};_0x5591ff[_0x8d10f8[0x0]]={'$gte':moment(_0x8d10f8[0x1])['format']('YYYY-MM-DD\x20HH:mm:ss')};_0x568280[_0xb692('0x42')]=_[_0xb692('0x45')](_0x568280[_0xb692('0x42')],_0x5591ff);}_0x568280=_[_0xb692('0x45')]({},_0x568280,_0x2c9916['options']);return db[_0xb692('0x47')][_0xb692('0x18')]({'where':_0x568280[_0xb692('0x42')]})['then'](function(_0x3c6a63){_0x378c20[_0xb692('0x18')]=_0x3c6a63;if(_0x2c9916['query'][_0xb692('0x33')]){_0x568280[_0xb692('0x34')]=[{'all':!![]}];}return db[_0xb692('0x47')][_0xb692('0x28')](_0x568280);})['then'](function(_0x4f7f2b){_0x378c20[_0xb692('0x48')]=_0x4f7f2b;return _0x378c20;});}})[_0xb692('0x1a')](respondWithFilteredResult(_0x3cf43d,_0x568280))[_0xb692('0x2a')](handleError(_0x3cf43d,null));};exports[_0xb692('0x49')]=function(_0x17a0e3,_0x4cc04c){return db[_0xb692('0x23')][_0xb692('0x38')]({'where':{'id':_0x17a0e3[_0xb692('0x39')]['id']}})[_0xb692('0x1a')](handleEntityNotFound(_0x4cc04c,null))[_0xb692('0x1a')](function(_0x4f52f7){if(_0x4f52f7){return _0x4f52f7['setTags'](_0x17a0e3['body'][_0xb692('0x3c')],_[_0xb692('0x3d')](_0x17a0e3[_0xb692('0x37')],[_0xb692('0x3c'),'id'])||{})[_0xb692('0x4a')](function(){return db['Tag'][_0xb692('0x28')]({'attributes':['id',_0xb692('0x1f'),_0xb692('0x4b')],'where':{'id':_0x17a0e3[_0xb692('0x37')]['ids']}});})[_0xb692('0x1a')](function(_0x9017ac){socket['emit'](_0xb692('0x4c'),{'id':Number(_0x17a0e3[_0xb692('0x39')]['id']),'tags':_0x9017ac||[]});return{'id':Number(_0x17a0e3[_0xb692('0x39')]['id']),'tags':_0x9017ac||[]};});}})[_0xb692('0x1a')](respondWithResult(_0x4cc04c,null))[_0xb692('0x2a')](handleError(_0x4cc04c,null));};exports[_0xb692('0x4d')]=function(_0x50b091,_0x482fca){return db['OpenchannelInteraction']['find']({'where':{'id':_0x50b091[_0xb692('0x39')]['id']}})['then'](handleEntityNotFound(_0x482fca,null))[_0xb692('0x1a')](function(_0x14adda){if(_0x14adda){return _0x14adda[_0xb692('0x4d')](_0x50b091[_0xb692('0x29')][_0xb692('0x3c')]);}})[_0xb692('0x1a')](respondWithStatusCode(_0x482fca,null))[_0xb692('0x2a')](handleError(_0x482fca,null));};exports[_0xb692('0x4e')]=function(_0x17a328,_0x3dceb4){var _0x15d3af=moment()[_0xb692('0x4f')]()[_0xb692('0x50')]();var _0x5aed1a=path[_0xb692('0x51')](config[_0xb692('0x52')],_0xb692('0x53'),_0xb692('0x54'),'tmp');var _0x5b1749=path['join'](config[_0xb692('0x52')],'server',_0xb692('0x54'),_0xb692('0x55'));var _0x495291=path['join'](_0x5aed1a,_0x15d3af);var _0x250ff6=util[_0xb692('0x56')]('transcript-%d-%s.zip',_0x17a328['params']['id'],_0x15d3af);var _0x558c57=path[_0xb692('0x51')](_0x5aed1a,_0x250ff6);var _0x540a8d=[];_0x540a8d[_0xb692('0x57')]({'model':db[_0xb692('0x58')],'as':_0xb692('0x58'),'attributes':['id',_0xb692('0x59')],'raw':!![]});_0x540a8d['push']({'model':db[_0xb692('0x5a')],'as':_0xb692('0x5b'),'attributes':['id',_0xb692('0x5c'),_0xb692('0x5d')],'raw':!![]});if(_0x17a328[_0xb692('0x29')][_0xb692('0x55')]){_0x540a8d['push']({'model':db[_0xb692('0x5e')],'as':'Attachment','raw':!![]});}var _0x2a9eec=[{'model':db['OpenchannelMessage'],'as':_0xb692('0x5f'),'attributes':['id',_0xb692('0x37'),_0xb692('0x60'),_0xb692('0x61'),'secret',_0xb692('0x62')],'include':_0x540a8d}];_0x2a9eec['push']({'model':db[_0xb692('0x63')],'as':_0xb692('0x64'),'attributes':['id',_0xb692('0x1f')]});_0x2a9eec[_0xb692('0x57')]({'model':db[_0xb692('0x58')],'as':_0xb692('0x65'),'attributes':['id',_0xb692('0x59')]});return db[_0xb692('0x23')][_0xb692('0x38')]({'where':{'id':_0x17a328[_0xb692('0x39')]['id']},'include':_0x2a9eec})['then'](handleEntityNotFound(_0x3dceb4,null))[_0xb692('0x1a')](function(_0x52226e){if(_0x52226e){var _0xabe87b=_0x52226e[_0xb692('0x66')]({'plain':!![]});fs[_0xb692('0x67')](_0x495291);var _0x10ac22={'channel':'Openchannel','account':_0xabe87b[_0xb692('0x64')][_0xb692('0x1f')],'agent':_0xabe87b[_0xb692('0x65')]?_0xabe87b[_0xb692('0x65')][_0xb692('0x59')]:_0xb692('0x68'),'createdAt':moment(_0xabe87b[_0xb692('0x60')])[_0xb692('0x56')]('YYYY-MM-DD\x20HH:mm:ss')['toString'](),'closedAt':_0xabe87b['closed']?moment(_0xabe87b[_0xb692('0x69')])[_0xb692('0x56')](_0xb692('0x6a'))['toString']():'','disposition':_0xabe87b[_0xb692('0x6b')],'visibility':_0xabe87b[_0xb692('0x6c')]?_0xb692('0x6d'):_0xb692('0x6e'),'messages':_(_0xabe87b[_0xb692('0x5f')])[_0xb692('0x44')]({'secret':![]})[_0xb692('0x22')](function(_0x2b469c){if(_0x2b469c['Attachment']){var _0x59b274=path[_0xb692('0x51')](_0x5b1749,_0x2b469c[_0xb692('0x5e')][_0xb692('0x6f')]);if(fs[_0xb692('0x70')](_0x59b274)){fs[_0xb692('0x71')](_0x59b274)[_0xb692('0x72')](fs[_0xb692('0x73')](path['join'](_0x495291,_0x2b469c[_0xb692('0x5e')][_0xb692('0x6f')])));}}return{'date':moment(_0x2b469c[_0xb692('0x60')])['format']('YYYY-MM-DD\x20HH:mm:ss')[_0xb692('0x50')](),'sender':_0x2b469c[_0xb692('0x61')]==='out'?_0x2b469c[_0xb692('0x58')]?_0x2b469c[_0xb692('0x58')][_0xb692('0x59')]:_0xb692('0x74'):_0x2b469c['Contact']['firstName']+(_0x2b469c[_0xb692('0x5b')][_0xb692('0x5d')]?'\x20'+_0x2b469c[_0xb692('0x5b')][_0xb692('0x5d')]:''),'body':_0x2b469c[_0xb692('0x5e')]?_0x2b469c[_0xb692('0x5e')][_0xb692('0x6f')]:_0x2b469c['body'],'direction':_0x2b469c[_0xb692('0x61')]==='out'?'A':'C','secret':_0x2b469c['secret'],'read':_0x2b469c[_0xb692('0x62')],'attachment':_0x2b469c[_0xb692('0x5e')]};})['value']()};return ejs[_0xb692('0x75')](path[_0xb692('0x51')](config[_0xb692('0x52')],_0xb692('0x53'),_0xb692('0x76'),_0xb692('0x77')),{'interaction':_0x10ac22})[_0xb692('0x1a')](function(_0x2593d3){var _0x5e3465=path[_0xb692('0x51')](_0x495291,_0xb692('0x78')+_0xabe87b['id']+'-'+_0x15d3af+'.pdf');var _0x20cb00={'path':_0x5e3465,'channel':_0xb692('0x79'),'interactionId':_0xabe87b['id']};return pdf[_0xb692('0x7a')](_0x2593d3,_0x20cb00);})[_0xb692('0x1a')](function(){return new Promise(function(_0xbdf6c,_0x560aea){zipdir(_0x495291,{'saveTo':_0x558c57},function(_0x2ef03c,_0x279fe6){if(_0x2ef03c)return _0x560aea(_0x2ef03c);return _0xbdf6c(_0x279fe6);});})[_0xb692('0x1a')](function(){return new Promise(function(_0x5dbc34,_0x2e7dc7){rimraf(_0x495291,function(_0x3e60a2){if(_0x3e60a2)_0x2e7dc7(_0x3e60a2);return _0x5dbc34();});});})[_0xb692('0x1a')](function(){return _0x3dceb4[_0xb692('0x4e')](_0x558c57,_0x250ff6,function(_0x44274f){if(_0x44274f){logger[_0xb692('0x1d')](_0xb692('0x7b'),_0x44274f);}else{fs[_0xb692('0x7c')](_0x558c57);}});});});}else{return _0x3dceb4['sendStatus'](0xc8);}})[_0xb692('0x2a')](handleError(_0x3dceb4,null));};