19f2b1ccda69f29c749ce019d63a27b74f7d7634
[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 _0x22a4=['util','bluebird','../../mysqldb','../../config/logger','transcribe','TranscribeService','getTranscriptionJob','Comprehend','detectSentiment','substring','info','format','[RECORDING:%d]\x20Get\x20status\x20for\x20transcript\x20job\x20(%s)','transcribeName','data1','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','ValidationError','sentiment','sentimentAccountId','bucket','CloudProvider','findOne','Cloud\x20Provider\x20not\x20available','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','Sentiment\x20Analysis\x20not\x20requested','sentimentRegion','language','Transcript\x20not\x20available','Neutral','COMPLETED','catch','SequelizeValidationError','error','[RECORDING:%d]\x20Error\x20running\x20Transcribe\x20&\x20Sentiment\x20Analysis','inspect','VoiceRecording','findAll','IN_PROGRESS','No\x20transcribe\x20job\x20to\x20check','Settings\x20not\x20available','Transcribe\x20not\x20configured','transcribeAccountId','forEach','push','all','name','lodash','aws-sdk','request-promise'];(function(_0x216a28,_0x4f021e){var _0x4f4901=function(_0x285775){while(--_0x285775){_0x216a28['push'](_0x216a28['shift']());}};_0x4f4901(++_0x4f021e);}(_0x22a4,0x1cb));var _0x422a=function(_0x130ef2,_0x3cc896){_0x130ef2=_0x130ef2-0x0;var _0x1e7442=_0x22a4[_0x130ef2];return _0x1e7442;};var _=require(_0x422a('0x0'));var AWS=require(_0x422a('0x1'));var rp=require(_0x422a('0x2'));var util=require(_0x422a('0x3'));var BPromise=require(_0x422a('0x4'));var db=require(_0x422a('0x5'))['db'];var logger=require(_0x422a('0x6'))(_0x422a('0x7'));function getTranscriptionJob(_0x5457aa,_0x32dc33,_0x10cc65,_0x1d5d59){return new Promise(function(_0x33e51c,_0x28deaa){var _0x5e8fe2=new AWS[(_0x422a('0x8'))]({'accessKeyId':_0x32dc33,'secretAccessKey':_0x5457aa,'region':_0x10cc65});_0x5e8fe2[_0x422a('0x9')]({'TranscriptionJobName':_0x1d5d59},function(_0x23b1b0,_0x392224){if(_0x23b1b0){return _0x28deaa(_0x23b1b0);}else{return _0x33e51c(_0x392224);}});});}function sentiment(_0x3f785b,_0x3ad391,_0x317466,_0x29caa4,_0x4b76b7){return new Promise(function(_0x4fef05,_0x336959){var _0x2eed9c=new AWS[(_0x422a('0xa'))]({'accessKeyId':_0x3ad391,'secretAccessKey':_0x3f785b,'region':_0x317466});_0x2eed9c[_0x422a('0xb')]({'Text':_0x29caa4,'LanguageCode':_0x4b76b7[_0x422a('0xc')](0x0,0x2)},function(_0x1aa133,_0x48c0a2){if(_0x1aa133){return _0x336959(_0x1aa133);}else{return _0x4fef05(_0x48c0a2);}});});}function checkTranscribeJob(_0x489953,_0xebe70d,_0x4dbed1){var _0x5a0c59;return new BPromise(function(_0x5a1200,_0x30f24c){logger[_0x422a('0xd')](util[_0x422a('0xe')](_0x422a('0xf'),_0x489953['id'],_0x489953[_0x422a('0x10')]));return getTranscriptionJob(_0x4dbed1[_0x422a('0x11')],_0x4dbed1[_0x422a('0x12')],_0xebe70d[_0x422a('0x13')],_0x489953[_0x422a('0x10')])[_0x422a('0x14')](function(_0x30b6e9){logger[_0x422a('0xd')](util[_0x422a('0xe')](_0x422a('0x15'),_0x489953['id'],_0x489953[_0x422a('0x10')],_0x30b6e9[_0x422a('0x16')][_0x422a('0x17')]));if(!_0x489953['fileText']){switch(_0x30b6e9[_0x422a('0x16')][_0x422a('0x17')]){case'COMPLETED':var _0x26bce9={'transcribeStatus':_0x30b6e9['TranscriptionJob']['TranscriptionJobStatus'],'fileUri':_0x30b6e9[_0x422a('0x16')][_0x422a('0x18')][_0x422a('0x19')]};logger[_0x422a('0xd')](util[_0x422a('0xe')](_0x422a('0x1a'),_0x489953['id']));return rp({'uri':_0x30b6e9[_0x422a('0x16')][_0x422a('0x18')]['TranscriptFileUri'],'json':!![]})[_0x422a('0x14')](function(_0x3284ed){logger[_0x422a('0xd')](util[_0x422a('0xe')](_0x422a('0x1b'),_0x489953['id']));if(_0x3284ed&&_0x3284ed['results']&&_0x3284ed[_0x422a('0x1c')][_0x422a('0x1d')]&&_0x3284ed[_0x422a('0x1c')]['transcripts'][_0x422a('0x1e')]){_0x26bce9[_0x422a('0x1f')]='';for(var _0x1fd126=0x0;_0x1fd126<_0x3284ed[_0x422a('0x1c')][_0x422a('0x1d')][_0x422a('0x1e')];_0x1fd126++){_0x26bce9[_0x422a('0x1f')]+=_0x3284ed[_0x422a('0x1c')]['transcripts'][_0x1fd126][_0x422a('0x20')]+'';}}return _0x489953[_0x422a('0x21')](_0x26bce9);})['then'](function(){logger[_0x422a('0xd')](util['format'](_0x422a('0x22'),_0x489953['id']));if(!_0x489953[_0x422a('0x23')]){throw new db[(_0x422a('0x24'))][(_0x422a('0x25'))]('Sentiment\x20Analysis\x20not\x20requested');}if(!_0xebe70d[_0x422a('0x26')]||!_0xebe70d[_0x422a('0x27')]||!_0xebe70d['sentimentRegion']||!_0xebe70d['language']||!_0xebe70d[_0x422a('0x28')]){throw new db[(_0x422a('0x24'))]['ValidationError']('Sentiment\x20Analysis\x20not\x20configured');}if(!_0x26bce9['fileText']){throw new Error('Transcript\x20not\x20available');}if(_0xebe70d['sentimentAccountId']===_0x4dbed1['id']){return _0x4dbed1;}else{return db[_0x422a('0x29')][_0x422a('0x2a')]({'where':{'id':_settings[_0x422a('0x27')]}});}})[_0x422a('0x14')](function(_0x30a1e3){if(!_0x30a1e3){throw new db['Sequelize'][(_0x422a('0x25'))](_0x422a('0x2b'));}if(!_0x30a1e3[_0x422a('0x11')]||!_0x30a1e3[_0x422a('0x12')]){throw new db[(_0x422a('0x24'))][(_0x422a('0x25'))](_0x422a('0x2c'));}_0x5a0c59=_0x30a1e3;logger['info'](util[_0x422a('0xe')](_0x422a('0x2d'),_0x489953['id']));return sentiment(_0x5a0c59[_0x422a('0x11')],_0x5a0c59[_0x422a('0x12')],_0xebe70d['sentimentRegion'],_0x26bce9[_0x422a('0x1f')],_0xebe70d['language']);})[_0x422a('0x14')](function(_0x100e45){logger['info'](util[_0x422a('0xe')](_0x422a('0x2e'),_0x489953['id']));return _0x489953['update']({'sentiment':_0x100e45[_0x422a('0x2f')],'sPositive':_0x100e45[_0x422a('0x30')][_0x422a('0x31')],'sNegative':_0x100e45[_0x422a('0x30')][_0x422a('0x32')],'sMixed':_0x100e45[_0x422a('0x30')][_0x422a('0x33')],'sNeutral':_0x100e45[_0x422a('0x30')]['Neutral']});})['then'](function(){logger[_0x422a('0xd')](util[_0x422a('0xe')](_0x422a('0x22'),_0x489953['id']));_0x5a1200();});case'FAILED':return _0x489953[_0x422a('0x21')]({'transcribeStatus':_0x422a('0x34'),'failureReason':_0x30b6e9['TranscriptionJob']['FailureReason']})[_0x422a('0x14')](function(){logger['info'](util[_0x422a('0xe')](_0x422a('0x22'),_0x489953['id']));_0x5a1200();});default:logger[_0x422a('0xd')](util['format'](_0x422a('0x35'),_0x489953['id']));break;}}else{return BPromise[_0x422a('0x36')]()[_0x422a('0x14')](function(){if(!_0x489953['tempSentiment']){throw new db['Sequelize'][(_0x422a('0x25'))](_0x422a('0x37'));}if(!_0xebe70d[_0x422a('0x26')]||!_0xebe70d['sentimentAccountId']||!_0xebe70d[_0x422a('0x38')]||!_0xebe70d[_0x422a('0x39')]||!_0xebe70d[_0x422a('0x28')]){throw new db[(_0x422a('0x24'))][(_0x422a('0x25'))]('Sentiment\x20Analysis\x20not\x20configured');}if(!_0x489953[_0x422a('0x1f')]){throw new Error(_0x422a('0x3a'));}if(_0xebe70d[_0x422a('0x27')]===_0x4dbed1['id']){return _0x4dbed1;}else{return db[_0x422a('0x29')][_0x422a('0x2a')]({'where':{'id':_settings['sentimentAccountId']}});}})['then'](function(_0x2b700d){if(!_0x2b700d){throw new db['Sequelize'][(_0x422a('0x25'))](_0x422a('0x2b'));}if(!_0x2b700d[_0x422a('0x11')]||!_0x2b700d[_0x422a('0x12')]){throw new db[(_0x422a('0x24'))][(_0x422a('0x25'))](_0x422a('0x2c'));}_0x5a0c59=_0x2b700d;logger['info'](util[_0x422a('0xe')](_0x422a('0x2d'),_0x489953['id']));return sentiment(_0x5a0c59[_0x422a('0x11')],_0x5a0c59[_0x422a('0x12')],_0xebe70d[_0x422a('0x38')],_0x489953[_0x422a('0x1f')],_0xebe70d[_0x422a('0x39')]);})['then'](function(_0x4127a5){logger[_0x422a('0xd')](util['format'](_0x422a('0x2e'),_0x489953['id']));return _0x489953[_0x422a('0x21')]({'sentiment':_0x4127a5[_0x422a('0x2f')],'sPositive':_0x4127a5[_0x422a('0x30')][_0x422a('0x31')],'sNegative':_0x4127a5[_0x422a('0x30')][_0x422a('0x32')],'sMixed':_0x4127a5['SentimentScore'][_0x422a('0x33')],'sNeutral':_0x4127a5[_0x422a('0x30')][_0x422a('0x3b')],'transcribeStatus':_0x422a('0x3c')});})[_0x422a('0x14')](function(){logger[_0x422a('0xd')](util[_0x422a('0xe')](_0x422a('0x22'),_0x489953['id']));_0x5a1200();});}})[_0x422a('0x3d')](function(_0x1a187b){if(_0x1a187b){if(!_0x1a187b['name']||_0x1a187b['name']!==_0x422a('0x3e')){logger[_0x422a('0x3f')](util['format'](_0x422a('0x40'),_0x489953['id']),util[_0x422a('0x41')](_0x1a187b,{'showHidden':![],'depth':null}));}}_0x5a1200();});});}function checkTranscribe(){var _0x41dd0a;var _0xf4ddc5;var _0x706497;return db[_0x422a('0x42')][_0x422a('0x43')]({'where':{'transcribeStatus':_0x422a('0x44')}})[_0x422a('0x14')](function(_0x3846bd){if(!_0x3846bd||!_0x3846bd['length']){throw new db['Sequelize'][(_0x422a('0x25'))](_0x422a('0x45'));}_0x41dd0a=_0x3846bd;return db['Setting'][_0x422a('0x2a')]({'where':{'id':0x1}});})[_0x422a('0x14')](function(_0x20b4d3){if(!_0x20b4d3){throw new db[(_0x422a('0x24'))][(_0x422a('0x25'))](_0x422a('0x46'));}_0xf4ddc5=_0x20b4d3;if(!_0xf4ddc5[_0x422a('0x7')]||!_0xf4ddc5['transcribeAccountId']||!_0xf4ddc5[_0x422a('0x13')]||!_0xf4ddc5[_0x422a('0x39')]||!_0xf4ddc5[_0x422a('0x28')]){throw new db[(_0x422a('0x24'))][(_0x422a('0x25'))](_0x422a('0x47'));}return db['CloudProvider']['findOne']({'where':{'id':_0xf4ddc5[_0x422a('0x48')]}});})[_0x422a('0x14')](function(_0x1ff3ad){if(!_0x1ff3ad){throw new db[(_0x422a('0x24'))][(_0x422a('0x25'))](_0x422a('0x2b'));}if(!_0x1ff3ad[_0x422a('0x11')]||!_0x1ff3ad[_0x422a('0x12')]){throw new db[(_0x422a('0x24'))]['ValidationError']('Cloud\x20Provider\x20not\x20configured');}_0x706497=_0x1ff3ad;var _0x58780a=[];_0x41dd0a[_0x422a('0x49')](function(_0x23372f){_0x58780a[_0x422a('0x4a')](checkTranscribeJob(_0x23372f,_0xf4ddc5,_0x706497));});return BPromise[_0x422a('0x4b')](_0x58780a);})['catch'](function(_0x3c04e6){if(_0x3c04e6){if(!_0x3c04e6[_0x422a('0x4c')]||_0x3c04e6[_0x422a('0x4c')]!==_0x422a('0x3e')){logger[_0x422a('0x3f')](util[_0x422a('0x41')](_0x3c04e6,{'showHidden':![],'depth':null}));}}});}exports['start']=function(){setInterval(function(){return checkTranscribe();},0x3c*0x3e8);return checkTranscribe();};