b2cdc1c77b05417f44fe59caaec07d8a0d0b2f18
[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 _0x56d9=['redis','localhost','socket.io-emitter','./openchannelInteraction.socket','sendStatus','status','end','limit','undefined','count','offset','set','Content-Range','update','then','destroy','index','map','rawAttributes','type','getOptions','findAll','openchannel','user','catch','query','from','params','model','keys','filters','intersection','fields','attributes','includeAll','include','options','OpenchannelInteraction','find','create','body','describe','addMessage','ids','getMessages','findOne','OpenchannelMessage','length','hasOwnProperty','nolimit','sort','where','OpenchannelInteractionId','$gte','format','YYYY-MM-DD\x20HH:mm:ss','merge','rows','setTags','omit','spread','Tag','name','color','emit','removeTags','download','unix','toString','join','server','files','tmp','root','transcript-%d-%s.zip','push','User','fullname','CmContact','Contact','lastName','attachments','Attachment','createdAt','direction','secret','read','Account','Owner','get','mkdirSync','Openchannel','closed','disposition','Public','Private','Messages','basename','existsSync','createReadStream','pipe','createWriteStream','out','System','firstName','downloadInteraction.ejs','transcript-','createPdfFromHTML','error','Unable\x20to\x20delete\x20zip\x20file','rimraf','zip-dir','moment','util','path','lodash','../../components/parsers/qs','../../config/logger','api','../../mysqldb','defaults'];(function(_0x45c173,_0x1f9585){var _0x16260a=function(_0x396179){while(--_0x396179){_0x45c173['push'](_0x45c173['shift']());}};_0x16260a(++_0x1f9585);}(_0x56d9,0x6e));var _0x956d=function(_0x450e22,_0x3ba125){_0x450e22=_0x450e22-0x0;var _0x5e3b1d=_0x56d9[_0x450e22];return _0x5e3b1d;};'use strict';var rimraf=require(_0x956d('0x0'));var zipdir=require(_0x956d('0x1'));var moment=require(_0x956d('0x2'));var util=require(_0x956d('0x3'));var path=require(_0x956d('0x4'));var ejs=require('ejs');var fs=require('fs');var _=require(_0x956d('0x5'));var Redis=require('ioredis');var interaction=require('../../components/interaction/service');var qs=require(_0x956d('0x6'));var pdf=require('../../components/export/pdf');var logger=require(_0x956d('0x7'))(_0x956d('0x8'));var config=require('../../config/environment');var db=require(_0x956d('0x9'))['db'];config['redis']=_[_0x956d('0xa')](config[_0x956d('0xb')],{'host':_0x956d('0xc'),'port':0x18eb});var socket=require(_0x956d('0xd'))(new Redis(config[_0x956d('0xb')]));require(_0x956d('0xe'))['register'](socket);function respondWithStatusCode(_0x34d4ac,_0x459465){_0x459465=_0x459465||0xcc;return function(_0x875fe8){if(_0x875fe8){return _0x34d4ac[_0x956d('0xf')](_0x459465);}return _0x34d4ac[_0x956d('0x10')](_0x459465)[_0x956d('0x11')]();};}function respondWithResult(_0x8d6665,_0x2be5f7){_0x2be5f7=_0x2be5f7||0xc8;return function(_0x2581c9){if(_0x2581c9){return _0x8d6665[_0x956d('0x10')](_0x2be5f7)['json'](_0x2581c9);}};}function respondWithFilteredResult(_0x4dff30,_0x558954){return function(_0x51ed37){if(_0x51ed37){var _0x5d7083=typeof _0x558954['offset']==='undefined'&&typeof _0x558954[_0x956d('0x12')]===_0x956d('0x13');var _0x387fc2=_0x51ed37[_0x956d('0x14')];var _0x59ac71=_0x5d7083?0x0:_0x558954[_0x956d('0x15')];var _0x432c87=_0x5d7083?_0x51ed37[_0x956d('0x14')]:_0x558954[_0x956d('0x15')]+_0x558954['limit'];var _0x165168;if(_0x432c87>=_0x387fc2){_0x432c87=_0x387fc2;_0x165168=0xc8;}else{_0x165168=0xce;}_0x4dff30[_0x956d('0x10')](_0x165168);return _0x4dff30[_0x956d('0x16')](_0x956d('0x17'),_0x59ac71+'-'+_0x432c87+'/'+_0x387fc2)['json'](_0x51ed37);}return null;};}function saveUpdates(_0x482d29){return function(_0x42320d){if(_0x42320d){return _0x42320d[_0x956d('0x18')](_0x482d29)[_0x956d('0x19')](function(_0x44f18e){return _0x44f18e;});}return null;};}function removeEntity(_0x26074f){return function(_0x4610ef){if(_0x4610ef){return _0x4610ef[_0x956d('0x1a')]()[_0x956d('0x19')](function(){_0x26074f['status'](0xcc)[_0x956d('0x11')]();});}};}function handleEntityNotFound(_0x44dd01){return function(_0x589475){if(!_0x589475){_0x44dd01[_0x956d('0xf')](0x194);}return _0x589475;};}function handleError(_0x5663f9,_0x24edfb){_0x24edfb=_0x24edfb||0x1f4;return function(_0x66dbe5){logger['error'](_0x66dbe5['stack']);if(_0x66dbe5['name']){delete _0x66dbe5['name'];}_0x5663f9[_0x956d('0x10')](_0x24edfb)['send'](_0x66dbe5);};}exports[_0x956d('0x1b')]=function(_0x90b94f,_0x4d4765){var _0x427373={};var _0x1616cc=_[_0x956d('0x1c')](db['OpenchannelInteraction'][_0x956d('0x1d')],function(_0x21f06a){return{'name':_0x21f06a['fieldName'],'type':_0x21f06a[_0x956d('0x1e')]['key']};});_0x427373=qs[_0x956d('0x1f')](_0x1616cc,_0x90b94f);return interaction[_0x956d('0x20')](_0x956d('0x21'),_0x90b94f['query'],_0x90b94f[_0x956d('0x22')],_0x427373,_0x1616cc)[_0x956d('0x19')](respondWithFilteredResult(_0x4d4765,_0x427373))[_0x956d('0x23')](handleError(_0x4d4765,null));};exports['show']=function(_0x2a81d7,_0x589238){var _0x33da03={};if(_0x2a81d7[_0x956d('0x24')][_0x956d('0x1e')]&&_0x2a81d7[_0x956d('0x24')][_0x956d('0x1e')]===_0x956d('0x25')){_0x33da03[_0x956d('0x25')]=_0x2a81d7[_0x956d('0x26')]['id'];}else{_0x33da03['id']=_0x2a81d7[_0x956d('0x26')]['id'];}var _0x3b41ab={'raw':![],'where':_0x33da03},_0x241767={};_0x241767[_0x956d('0x27')]=_[_0x956d('0x28')](db['OpenchannelInteraction'][_0x956d('0x1d')]);_0x241767[_0x956d('0x24')]=_[_0x956d('0x28')](_0x2a81d7['query']);_0x241767[_0x956d('0x29')]=_[_0x956d('0x2a')](_0x241767['model'],_0x241767[_0x956d('0x24')]);_0x3b41ab['attributes']=_[_0x956d('0x2a')](_0x241767[_0x956d('0x27')],qs[_0x956d('0x2b')](_0x2a81d7[_0x956d('0x24')]['fields']));_0x3b41ab[_0x956d('0x2c')]=_0x3b41ab[_0x956d('0x2c')]['length']?_0x3b41ab[_0x956d('0x2c')]:_0x241767['model'];if(_0x2a81d7[_0x956d('0x24')][_0x956d('0x2d')]){_0x3b41ab[_0x956d('0x2e')]=[{'all':!![]}];}_0x3b41ab=_['merge']({},_0x3b41ab,_0x2a81d7[_0x956d('0x2f')]);return db[_0x956d('0x30')][_0x956d('0x31')](_0x3b41ab)[_0x956d('0x19')](handleEntityNotFound(_0x589238,null))[_0x956d('0x19')](respondWithResult(_0x589238,null))['catch'](handleError(_0x589238,null));};exports[_0x956d('0x32')]=function(_0x568a44,_0x47a478){return db[_0x956d('0x30')][_0x956d('0x32')](_0x568a44[_0x956d('0x33')],{})[_0x956d('0x19')](respondWithResult(_0x47a478,0xc9))['catch'](handleError(_0x47a478,null));};exports[_0x956d('0x18')]=function(_0x287c72,_0x38bb4a){if(_0x287c72[_0x956d('0x33')]['id']){delete _0x287c72[_0x956d('0x33')]['id'];}return db[_0x956d('0x30')]['find']({'where':{'id':_0x287c72['params']['id']}})[_0x956d('0x19')](handleEntityNotFound(_0x38bb4a,null))['then'](saveUpdates(_0x287c72['body'],null))[_0x956d('0x19')](respondWithResult(_0x38bb4a,null))[_0x956d('0x23')](handleError(_0x38bb4a,null));};exports['destroy']=function(_0x430bea,_0x18b1d4){return db[_0x956d('0x30')][_0x956d('0x31')]({'where':{'id':_0x430bea[_0x956d('0x26')]['id']}})[_0x956d('0x19')](handleEntityNotFound(_0x18b1d4,null))['then'](removeEntity(_0x18b1d4,null))['catch'](handleError(_0x18b1d4,null));};exports[_0x956d('0x34')]=function(_0xcd3b2d,_0x82aeca){return db['OpenchannelInteraction'][_0x956d('0x34')]()[_0x956d('0x19')](respondWithResult(_0x82aeca,null))[_0x956d('0x23')](handleError(_0x82aeca,null));};exports[_0x956d('0x35')]=function(_0x373fcd,_0x22fc81){return db['OpenchannelMessage'][_0x956d('0x31')]({'where':{'id':_0x373fcd[_0x956d('0x26')]['id']}})['then'](handleEntityNotFound(_0x22fc81,null))[_0x956d('0x19')](function(_0x331e31){if(_0x331e31){return _0x331e31['addMessage'](_0x373fcd[_0x956d('0x33')][_0x956d('0x36')],_['omit'](_0x373fcd[_0x956d('0x33')],[_0x956d('0x36'),'id'])||{});}})[_0x956d('0x19')](respondWithResult(_0x22fc81,null))[_0x956d('0x23')](handleError(_0x22fc81,null));};exports[_0x956d('0x37')]=function(_0x462822,_0x25bc91){var _0x56dc6c={'raw':![],'where':{}};var _0x2ddb09={};var _0x53cfe9={'count':0x0,'rows':[]};return db[_0x956d('0x30')][_0x956d('0x38')]({'where':{'id':_0x462822['params']['id']}})['then'](handleEntityNotFound(_0x25bc91,null))[_0x956d('0x19')](function(_0x883d2b){if(_0x883d2b){_0x2ddb09[_0x956d('0x27')]=_[_0x956d('0x28')](db[_0x956d('0x39')][_0x956d('0x1d')]);_0x2ddb09['query']=_[_0x956d('0x28')](_0x462822[_0x956d('0x24')]);_0x2ddb09[_0x956d('0x29')]=_[_0x956d('0x2a')](_0x2ddb09[_0x956d('0x27')],_0x2ddb09['query']);_0x56dc6c[_0x956d('0x2c')]=_['intersection'](_0x2ddb09[_0x956d('0x27')],qs[_0x956d('0x2b')](_0x462822[_0x956d('0x24')][_0x956d('0x2b')]));_0x56dc6c['attributes']=_0x56dc6c[_0x956d('0x2c')][_0x956d('0x3a')]?_0x56dc6c[_0x956d('0x2c')]:_0x2ddb09['model'];if(!_0x462822[_0x956d('0x24')][_0x956d('0x3b')](_0x956d('0x3c'))){_0x56dc6c[_0x956d('0x12')]=qs[_0x956d('0x12')](_0x462822['query']['limit']);_0x56dc6c[_0x956d('0x15')]=qs['offset'](_0x462822['query'][_0x956d('0x15')]);}_0x56dc6c['order']=qs[_0x956d('0x3d')](_0x462822['query']['sort']);_0x56dc6c[_0x956d('0x3e')]=qs[_0x956d('0x29')](_['pick'](_0x462822[_0x956d('0x24')],_0x2ddb09[_0x956d('0x29')]));_0x56dc6c['where'][_0x956d('0x3f')]=_0x883d2b['id'];if(_0x462822[_0x956d('0x24')]['filter']){_0x56dc6c[_0x956d('0x3e')]=_['merge'](_0x56dc6c[_0x956d('0x3e')],{'$or':_['map'](_0x56dc6c[_0x956d('0x2c')],function(_0x4fb76a){var _0x25bf8b={};_0x25bf8b[_0x4fb76a]={'$like':'%'+_0x462822[_0x956d('0x24')]['filter']+'%'};return _0x25bf8b;})});}if(_0x462822[_0x956d('0x24')][_0x956d('0x40')]){var _0x3b4507=_0x462822[_0x956d('0x24')]['$gte']['split'](',');var _0x3a319e={};_0x3a319e[_0x3b4507[0x0]]={'$gte':moment(_0x3b4507[0x1])[_0x956d('0x41')](_0x956d('0x42'))};_0x56dc6c['where']=_[_0x956d('0x43')](_0x56dc6c[_0x956d('0x3e')],_0x3a319e);}_0x56dc6c=_['merge']({},_0x56dc6c,_0x462822[_0x956d('0x2f')]);return db[_0x956d('0x39')]['count']({'where':_0x56dc6c['where']})[_0x956d('0x19')](function(_0x25f69d){_0x53cfe9[_0x956d('0x14')]=_0x25f69d;if(_0x462822['query']['includeAll']){_0x56dc6c[_0x956d('0x2e')]=[{'all':!![]}];}return db[_0x956d('0x39')]['findAll'](_0x56dc6c);})[_0x956d('0x19')](function(_0x376946){_0x53cfe9[_0x956d('0x44')]=_0x376946;return _0x53cfe9;});}})['then'](respondWithFilteredResult(_0x25bc91,_0x56dc6c))['catch'](handleError(_0x25bc91,null));};exports['addTags']=function(_0x26181f,_0x1e2c8b){return db['OpenchannelInteraction'][_0x956d('0x31')]({'where':{'id':_0x26181f[_0x956d('0x26')]['id']}})[_0x956d('0x19')](handleEntityNotFound(_0x1e2c8b,null))[_0x956d('0x19')](function(_0x50e58a){if(_0x50e58a){return _0x50e58a[_0x956d('0x45')](_0x26181f[_0x956d('0x33')]['ids'],_[_0x956d('0x46')](_0x26181f[_0x956d('0x33')],[_0x956d('0x36'),'id'])||{})[_0x956d('0x47')](function(){return db[_0x956d('0x48')][_0x956d('0x20')]({'attributes':['id',_0x956d('0x49'),_0x956d('0x4a')],'where':{'id':_0x26181f[_0x956d('0x33')][_0x956d('0x36')]}});})['then'](function(_0x4884f7){socket[_0x956d('0x4b')]('openchannelInteractionTags:save',{'id':Number(_0x26181f[_0x956d('0x26')]['id']),'tags':_0x4884f7||[]});return{'id':Number(_0x26181f['params']['id']),'tags':_0x4884f7||[]};});}})[_0x956d('0x19')](respondWithResult(_0x1e2c8b,null))[_0x956d('0x23')](handleError(_0x1e2c8b,null));};exports['removeTags']=function(_0x1fe07,_0x2a5779){return db['OpenchannelInteraction'][_0x956d('0x31')]({'where':{'id':_0x1fe07[_0x956d('0x26')]['id']}})['then'](handleEntityNotFound(_0x2a5779,null))[_0x956d('0x19')](function(_0x38ee9d){if(_0x38ee9d){return _0x38ee9d[_0x956d('0x4c')](_0x1fe07[_0x956d('0x24')][_0x956d('0x36')]);}})[_0x956d('0x19')](respondWithStatusCode(_0x2a5779,null))['catch'](handleError(_0x2a5779,null));};exports[_0x956d('0x4d')]=function(_0x496a57,_0x11757a){var _0x2f7be0=moment()[_0x956d('0x4e')]()[_0x956d('0x4f')]();var _0x9be996=path[_0x956d('0x50')](config['root'],_0x956d('0x51'),_0x956d('0x52'),_0x956d('0x53'));var _0x3ac4a9=path[_0x956d('0x50')](config[_0x956d('0x54')],'server',_0x956d('0x52'),'attachments');var _0x16ceba=path[_0x956d('0x50')](_0x9be996,_0x2f7be0);var _0x450062=util[_0x956d('0x41')](_0x956d('0x55'),_0x496a57[_0x956d('0x26')]['id'],_0x2f7be0);var _0x4725ab=path[_0x956d('0x50')](_0x9be996,_0x450062);var _0x28e753=[];_0x28e753[_0x956d('0x56')]({'model':db[_0x956d('0x57')],'as':_0x956d('0x57'),'attributes':['id',_0x956d('0x58')],'raw':!![]});_0x28e753[_0x956d('0x56')]({'model':db[_0x956d('0x59')],'as':_0x956d('0x5a'),'attributes':['id','firstName',_0x956d('0x5b')],'raw':!![]});if(_0x496a57[_0x956d('0x24')][_0x956d('0x5c')]){_0x28e753['push']({'model':db[_0x956d('0x5d')],'as':_0x956d('0x5d'),'raw':!![]});}var _0x346a70=[{'model':db[_0x956d('0x39')],'as':'Messages','attributes':['id','body',_0x956d('0x5e'),_0x956d('0x5f'),_0x956d('0x60'),_0x956d('0x61')],'include':_0x28e753}];_0x346a70[_0x956d('0x56')]({'model':db['OpenchannelAccount'],'as':_0x956d('0x62'),'attributes':['id',_0x956d('0x49')]});_0x346a70[_0x956d('0x56')]({'model':db[_0x956d('0x57')],'as':_0x956d('0x63'),'attributes':['id',_0x956d('0x58')]});return db[_0x956d('0x30')][_0x956d('0x31')]({'where':{'id':_0x496a57[_0x956d('0x26')]['id']},'include':_0x346a70})['then'](handleEntityNotFound(_0x11757a,null))[_0x956d('0x19')](function(_0x3372e1){if(_0x3372e1){var _0x3296de=_0x3372e1[_0x956d('0x64')]({'plain':!![]});fs[_0x956d('0x65')](_0x16ceba);var _0x9a2eea={'channel':_0x956d('0x66'),'account':_0x3296de[_0x956d('0x62')]['name'],'agent':_0x3296de['Owner']?_0x3296de[_0x956d('0x63')][_0x956d('0x58')]:'N.A.','createdAt':moment(_0x3296de[_0x956d('0x5e')])[_0x956d('0x41')](_0x956d('0x42'))[_0x956d('0x4f')](),'closedAt':_0x3296de[_0x956d('0x67')]?moment(_0x3296de['closedAt'])[_0x956d('0x41')](_0x956d('0x42'))['toString']():'','disposition':_0x3296de[_0x956d('0x68')],'visibility':_0x3296de['threadId']?_0x956d('0x69'):_0x956d('0x6a'),'messages':_(_0x3296de[_0x956d('0x6b')])['filter']({'secret':![]})[_0x956d('0x1c')](function(_0x4e18d4){if(_0x4e18d4[_0x956d('0x5d')]){var _0x353493=path[_0x956d('0x50')](_0x3ac4a9,_0x4e18d4[_0x956d('0x5d')][_0x956d('0x6c')]);if(fs[_0x956d('0x6d')](_0x353493)){fs[_0x956d('0x6e')](_0x353493)[_0x956d('0x6f')](fs[_0x956d('0x70')](path['join'](_0x16ceba,_0x4e18d4[_0x956d('0x5d')][_0x956d('0x6c')])));}}return{'date':moment(_0x4e18d4[_0x956d('0x5e')])['format'](_0x956d('0x42'))['toString'](),'sender':_0x4e18d4['direction']===_0x956d('0x71')?_0x4e18d4['User']?_0x4e18d4[_0x956d('0x57')][_0x956d('0x58')]:_0x956d('0x72'):_0x4e18d4[_0x956d('0x5a')][_0x956d('0x73')]+(_0x4e18d4[_0x956d('0x5a')][_0x956d('0x5b')]?'\x20'+_0x4e18d4['Contact'][_0x956d('0x5b')]:''),'body':_0x4e18d4['Attachment']?_0x4e18d4[_0x956d('0x5d')][_0x956d('0x6c')]:_0x4e18d4[_0x956d('0x33')],'direction':_0x4e18d4[_0x956d('0x5f')]==='out'?'A':'C','secret':_0x4e18d4[_0x956d('0x60')],'read':_0x4e18d4[_0x956d('0x61')],'attachment':_0x4e18d4['Attachment']};})['value']()};return ejs['renderFile'](path[_0x956d('0x50')](config[_0x956d('0x54')],'server','views',_0x956d('0x74')),{'interaction':_0x9a2eea})['then'](function(_0x372102){var _0x2d3f58=path['join'](_0x16ceba,_0x956d('0x75')+_0x3296de['id']+'-'+_0x2f7be0+'.pdf');var _0x4fb14d={'path':_0x2d3f58,'channel':_0x956d('0x66'),'interactionId':_0x3296de['id']};return pdf[_0x956d('0x76')](_0x372102,_0x4fb14d);})[_0x956d('0x19')](function(){return new Promise(function(_0x4cc972,_0x2e9be4){zipdir(_0x16ceba,{'saveTo':_0x4725ab},function(_0x183815,_0x2bd424){if(_0x183815)return _0x2e9be4(_0x183815);return _0x4cc972(_0x2bd424);});})['then'](function(){return new Promise(function(_0x3417e6,_0x417f82){rimraf(_0x16ceba,function(_0x5f35f3){if(_0x5f35f3)_0x417f82(_0x5f35f3);return _0x3417e6();});});})[_0x956d('0x19')](function(){return _0x11757a[_0x956d('0x4d')](_0x4725ab,_0x450062,function(_0x5efc15){if(_0x5efc15){logger[_0x956d('0x77')](_0x956d('0x78'),_0x5efc15);}else{fs['unlinkSync'](_0x4725ab);}});});});}else{return _0x11757a[_0x956d('0xf')](0xc8);}})[_0x956d('0x23')](handleError(_0x11757a,null));};