6c7946a7f501b677c90514aab5d90506475e977b
[motion2.git] / server / config / schedule / transcribe.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 _0x6198=['data2','transcribeRegion','then','[RECORDING:%d]\x20Status\x20of\x20transcript\x20job\x20(%s):\x20%s','TranscriptionJob','TranscriptionJobStatus','Transcript','TranscriptFileUri','[RECORDING:%d]\x20Get\x20transcript','[RECORDING:%d]\x20Transcript\x20downloaded','results','transcripts','length','fileText','transcript','update','[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated','tempSentiment','Sequelize','Sentiment\x20Analysis\x20not\x20requested','sentiment','language','ValidationError','Transcript\x20not\x20available','sentimentAccountId','findOne','Cloud\x20Provider\x20not\x20available','data1','Cloud\x20Provider\x20not\x20configured','[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis','[RECORDING:%d]\x20Sentiment\x20Analysis\x20available','Sentiment','SentimentScore','Positive','Negative','Mixed','FAILED','[RECORDING:%d]\x20Transcript\x20Job\x20status\x20not\x20managed','resolve','sentimentRegion','bucket','Sentiment\x20Analysis\x20not\x20configured','CloudProvider','Neutral','COMPLETED','name','SequelizeValidationError','[RECORDING:%d]\x20Error\x20running\x20Transcribe\x20&\x20Sentiment\x20Analysis','inspect','VoiceRecording','findAll','No\x20transcribe\x20job\x20to\x20check','Setting','Settings\x20not\x20available','transcribe','transcribeAccountId','Transcribe\x20not\x20configured','forEach','push','all','error','start','lodash','aws-sdk','request-promise','util','bluebird','../../mysqldb','../../config/logger','TranscribeService','getTranscriptionJob','Comprehend','detectSentiment','info','format','transcribeName'];(function(_0x5f0fbd,_0x36b2ae){var _0x2dfde0=function(_0x3ab74f){while(--_0x3ab74f){_0x5f0fbd['push'](_0x5f0fbd['shift']());}};_0x2dfde0(++_0x36b2ae);}(_0x6198,0x1ba));var _0x8619=function(_0x54640c,_0x2a7887){_0x54640c=_0x54640c-0x0;var _0x2fa24d=_0x6198[_0x54640c];return _0x2fa24d;};var _=require(_0x8619('0x0'));var AWS=require(_0x8619('0x1'));var rp=require(_0x8619('0x2'));var util=require(_0x8619('0x3'));var BPromise=require(_0x8619('0x4'));var db=require(_0x8619('0x5'))['db'];var logger=require(_0x8619('0x6'))('transcribe');function getTranscriptionJob(_0xe4145f,_0x30bb99,_0x36a2f6,_0x5a4feb){return new Promise(function(_0x4b2109,_0x45070f){var _0x47e7e8=new AWS[(_0x8619('0x7'))]({'accessKeyId':_0x30bb99,'secretAccessKey':_0xe4145f,'region':_0x36a2f6});_0x47e7e8[_0x8619('0x8')]({'TranscriptionJobName':_0x5a4feb},function(_0x50705a,_0x50d039){if(_0x50705a){return _0x45070f(_0x50705a);}else{return _0x4b2109(_0x50d039);}});});}function sentiment(_0x51854c,_0x4ea8f8,_0x1a4446,_0x4d9c34,_0x562365){return new Promise(function(_0x29b06d,_0x54cb0e){var _0x539f4b=new AWS[(_0x8619('0x9'))]({'accessKeyId':_0x4ea8f8,'secretAccessKey':_0x51854c,'region':_0x1a4446});_0x539f4b[_0x8619('0xa')]({'Text':_0x4d9c34,'LanguageCode':_0x562365['substring'](0x0,0x2)},function(_0x47ef22,_0x557136){if(_0x47ef22){return _0x54cb0e(_0x47ef22);}else{return _0x29b06d(_0x557136);}});});}function checkTranscribeJob(_0x5e155d,_0x114a4d,_0x3b1881){var _0x2cd291;return new BPromise(function(_0xd575bf,_0x35b5f9){logger[_0x8619('0xb')](util[_0x8619('0xc')]('[RECORDING:%d]\x20Get\x20status\x20for\x20transcript\x20job\x20(%s)',_0x5e155d['id'],_0x5e155d[_0x8619('0xd')]));return getTranscriptionJob(_0x3b1881['data1'],_0x3b1881[_0x8619('0xe')],_0x114a4d[_0x8619('0xf')],_0x5e155d[_0x8619('0xd')])[_0x8619('0x10')](function(_0x426302){logger[_0x8619('0xb')](util['format'](_0x8619('0x11'),_0x5e155d['id'],_0x5e155d[_0x8619('0xd')],_0x426302[_0x8619('0x12')]['TranscriptionJobStatus']));if(!_0x5e155d['fileText']){switch(_0x426302[_0x8619('0x12')][_0x8619('0x13')]){case'COMPLETED':var _0x335dfb={'transcribeStatus':_0x426302[_0x8619('0x12')]['TranscriptionJobStatus'],'fileUri':_0x426302[_0x8619('0x12')][_0x8619('0x14')][_0x8619('0x15')]};logger[_0x8619('0xb')](util[_0x8619('0xc')](_0x8619('0x16'),_0x5e155d['id']));return rp({'uri':_0x426302[_0x8619('0x12')][_0x8619('0x14')][_0x8619('0x15')],'json':!![]})[_0x8619('0x10')](function(_0x3f45b1){logger[_0x8619('0xb')](util['format'](_0x8619('0x17'),_0x5e155d['id']));if(_0x3f45b1&&_0x3f45b1[_0x8619('0x18')]&&_0x3f45b1[_0x8619('0x18')][_0x8619('0x19')]&&_0x3f45b1[_0x8619('0x18')]['transcripts'][_0x8619('0x1a')]){_0x335dfb[_0x8619('0x1b')]='';for(var _0x4712a8=0x0;_0x4712a8<_0x3f45b1[_0x8619('0x18')][_0x8619('0x19')]['length'];_0x4712a8++){_0x335dfb['fileText']+=_0x3f45b1[_0x8619('0x18')][_0x8619('0x19')][_0x4712a8][_0x8619('0x1c')]+'';}}return _0x5e155d[_0x8619('0x1d')](_0x335dfb);})[_0x8619('0x10')](function(){logger[_0x8619('0xb')](util[_0x8619('0xc')](_0x8619('0x1e'),_0x5e155d['id']));if(!_0x5e155d[_0x8619('0x1f')]){throw new db[(_0x8619('0x20'))]['ValidationError'](_0x8619('0x21'));}if(!_0x114a4d[_0x8619('0x22')]||!_0x114a4d['sentimentAccountId']||!_0x114a4d['sentimentRegion']||!_0x114a4d[_0x8619('0x23')]||!_0x114a4d['bucket']){throw new db['Sequelize'][(_0x8619('0x24'))]('Sentiment\x20Analysis\x20not\x20configured');}if(!_0x335dfb[_0x8619('0x1b')]){throw new Error(_0x8619('0x25'));}if(_0x114a4d[_0x8619('0x26')]===_0x3b1881['id']){return _0x3b1881;}else{return db['CloudProvider'][_0x8619('0x27')]({'where':{'id':_settings[_0x8619('0x26')]}});}})[_0x8619('0x10')](function(_0x378e1e){if(!_0x378e1e){throw new db['Sequelize']['ValidationError'](_0x8619('0x28'));}if(!_0x378e1e[_0x8619('0x29')]||!_0x378e1e[_0x8619('0xe')]){throw new db[(_0x8619('0x20'))][(_0x8619('0x24'))](_0x8619('0x2a'));}_0x2cd291=_0x378e1e;logger[_0x8619('0xb')](util[_0x8619('0xc')](_0x8619('0x2b'),_0x5e155d['id']));return sentiment(_0x2cd291[_0x8619('0x29')],_0x2cd291['data2'],_0x114a4d['sentimentRegion'],_0x335dfb[_0x8619('0x1b')],_0x114a4d['language']);})['then'](function(_0x432ed4){logger[_0x8619('0xb')](util['format'](_0x8619('0x2c'),_0x5e155d['id']));return _0x5e155d['update']({'sentiment':_0x432ed4[_0x8619('0x2d')],'sPositive':_0x432ed4[_0x8619('0x2e')][_0x8619('0x2f')],'sNegative':_0x432ed4[_0x8619('0x2e')][_0x8619('0x30')],'sMixed':_0x432ed4[_0x8619('0x2e')][_0x8619('0x31')],'sNeutral':_0x432ed4[_0x8619('0x2e')]['Neutral']});})['then'](function(){logger[_0x8619('0xb')](util[_0x8619('0xc')]('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x5e155d['id']));_0xd575bf();});case _0x8619('0x32'):return _0x5e155d[_0x8619('0x1d')]({'transcribeStatus':_0x8619('0x32'),'failureReason':_0x426302[_0x8619('0x12')]['FailureReason']})[_0x8619('0x10')](function(){logger['info'](util[_0x8619('0xc')](_0x8619('0x1e'),_0x5e155d['id']));_0xd575bf();});default:logger[_0x8619('0xb')](util[_0x8619('0xc')](_0x8619('0x33'),_0x5e155d['id']));break;}}else{return BPromise[_0x8619('0x34')]()[_0x8619('0x10')](function(){if(!_0x5e155d['tempSentiment']){throw new db['Sequelize']['ValidationError'](_0x8619('0x21'));}if(!_0x114a4d[_0x8619('0x22')]||!_0x114a4d['sentimentAccountId']||!_0x114a4d[_0x8619('0x35')]||!_0x114a4d['language']||!_0x114a4d[_0x8619('0x36')]){throw new db[(_0x8619('0x20'))][(_0x8619('0x24'))](_0x8619('0x37'));}if(!_0x5e155d[_0x8619('0x1b')]){throw new Error(_0x8619('0x25'));}if(_0x114a4d[_0x8619('0x26')]===_0x3b1881['id']){return _0x3b1881;}else{return db[_0x8619('0x38')][_0x8619('0x27')]({'where':{'id':_settings[_0x8619('0x26')]}});}})[_0x8619('0x10')](function(_0x5061d2){if(!_0x5061d2){throw new db[(_0x8619('0x20'))][(_0x8619('0x24'))](_0x8619('0x28'));}if(!_0x5061d2[_0x8619('0x29')]||!_0x5061d2[_0x8619('0xe')]){throw new db['Sequelize'][(_0x8619('0x24'))](_0x8619('0x2a'));}_0x2cd291=_0x5061d2;logger[_0x8619('0xb')](util['format'](_0x8619('0x2b'),_0x5e155d['id']));return sentiment(_0x2cd291['data1'],_0x2cd291[_0x8619('0xe')],_0x114a4d[_0x8619('0x35')],_0x5e155d[_0x8619('0x1b')],_0x114a4d[_0x8619('0x23')]);})[_0x8619('0x10')](function(_0xdc14ef){logger[_0x8619('0xb')](util[_0x8619('0xc')](_0x8619('0x2c'),_0x5e155d['id']));return _0x5e155d[_0x8619('0x1d')]({'sentiment':_0xdc14ef['Sentiment'],'sPositive':_0xdc14ef['SentimentScore'][_0x8619('0x2f')],'sNegative':_0xdc14ef[_0x8619('0x2e')]['Negative'],'sMixed':_0xdc14ef[_0x8619('0x2e')][_0x8619('0x31')],'sNeutral':_0xdc14ef['SentimentScore'][_0x8619('0x39')],'transcribeStatus':_0x8619('0x3a')});})[_0x8619('0x10')](function(){logger[_0x8619('0xb')](util[_0x8619('0xc')](_0x8619('0x1e'),_0x5e155d['id']));_0xd575bf();});}})['catch'](function(_0x5f0e41){if(_0x5f0e41){if(!_0x5f0e41['name']||_0x5f0e41[_0x8619('0x3b')]!==_0x8619('0x3c')){logger['error'](util[_0x8619('0xc')](_0x8619('0x3d'),_0x5e155d['id']),util[_0x8619('0x3e')](_0x5f0e41,{'showHidden':![],'depth':null}));}}_0xd575bf();});});}function checkTranscribe(){var _0x5a7a9e;var _0x48b3c7;var _0xe093c5;return db[_0x8619('0x3f')][_0x8619('0x40')]({'where':{'transcribeStatus':'IN_PROGRESS'}})['then'](function(_0x1b3404){if(!_0x1b3404||!_0x1b3404[_0x8619('0x1a')]){throw new db[(_0x8619('0x20'))][(_0x8619('0x24'))](_0x8619('0x41'));}_0x5a7a9e=_0x1b3404;return db[_0x8619('0x42')][_0x8619('0x27')]({'where':{'id':0x1}});})[_0x8619('0x10')](function(_0x255b47){if(!_0x255b47){throw new db['Sequelize']['ValidationError'](_0x8619('0x43'));}_0x48b3c7=_0x255b47;if(!_0x48b3c7[_0x8619('0x44')]||!_0x48b3c7[_0x8619('0x45')]||!_0x48b3c7[_0x8619('0xf')]||!_0x48b3c7[_0x8619('0x23')]||!_0x48b3c7[_0x8619('0x36')]){throw new db['Sequelize'][(_0x8619('0x24'))](_0x8619('0x46'));}return db['CloudProvider'][_0x8619('0x27')]({'where':{'id':_0x48b3c7['transcribeAccountId']}});})[_0x8619('0x10')](function(_0x102472){if(!_0x102472){throw new db[(_0x8619('0x20'))]['ValidationError'](_0x8619('0x28'));}if(!_0x102472[_0x8619('0x29')]||!_0x102472[_0x8619('0xe')]){throw new db[(_0x8619('0x20'))][(_0x8619('0x24'))](_0x8619('0x2a'));}_0xe093c5=_0x102472;var _0x1d8c06=[];_0x5a7a9e[_0x8619('0x47')](function(_0x18dd1e){_0x1d8c06[_0x8619('0x48')](checkTranscribeJob(_0x18dd1e,_0x48b3c7,_0xe093c5));});return BPromise[_0x8619('0x49')](_0x1d8c06);})['catch'](function(_0x54fbb2){if(_0x54fbb2){if(!_0x54fbb2[_0x8619('0x3b')]||_0x54fbb2[_0x8619('0x3b')]!=='SequelizeValidationError'){logger[_0x8619('0x4a')](util['inspect'](_0x54fbb2,{'showHidden':![],'depth':null}));}}});}exports[_0x8619('0x4b')]=function(){setInterval(function(){return checkTranscribe();},0x3c*0x3e8);return checkTranscribe();};