bd64202193183e384d20d944eabfa3b82d74e572
[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 _0x3f9b=['../../mysqldb','TranscribeService','getTranscriptionJob','Comprehend','detectSentiment','substring','info','format','[RECORDING:%d]\x20Get\x20status\x20for\x20transcript\x20job\x20(%s)','data1','transcribeName','then','[RECORDING:%d]\x20Status\x20of\x20transcript\x20job\x20(%s):\x20%s','TranscriptionJob','fileText','COMPLETED','TranscriptionJobStatus','Transcript','TranscriptFileUri','[RECORDING:%d]\x20Transcript\x20downloaded','results','transcripts','length','update','[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated','Sequelize','ValidationError','Sentiment\x20Analysis\x20not\x20requested','sentiment','sentimentAccountId','sentimentRegion','language','bucket','Sentiment\x20Analysis\x20not\x20configured','Transcript\x20not\x20available','Cloud\x20Provider\x20not\x20available','data2','[RECORDING:%d]\x20Sentiment\x20Analysis\x20available','Sentiment','SentimentScore','Negative','FAILED','FailureReason','[RECORDING:%d]\x20Transcript\x20Job\x20status\x20not\x20managed','resolve','tempSentiment','CloudProvider','findOne','[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis','Positive','Mixed','Neutral','catch','name','SequelizeValidationError','error','[RECORDING:%d]\x20Error\x20running\x20Transcribe\x20&\x20Sentiment\x20Analysis','inspect','findAll','IN_PROGRESS','No\x20transcribe\x20job\x20to\x20check','Setting','Settings\x20not\x20available','transcribe','transcribeRegion','Transcribe\x20not\x20configured','transcribeAccountId','forEach','all','start','request-promise','bluebird'];(function(_0x1fea21,_0x322c1d){var _0x21feab=function(_0x503750){while(--_0x503750){_0x1fea21['push'](_0x1fea21['shift']());}};_0x21feab(++_0x322c1d);}(_0x3f9b,0x11e));var _0xb3f9=function(_0x1426ad,_0x170021){_0x1426ad=_0x1426ad-0x0;var _0x476c33=_0x3f9b[_0x1426ad];return _0x476c33;};var _=require('lodash');var AWS=require('aws-sdk');var rp=require(_0xb3f9('0x0'));var util=require('util');var BPromise=require(_0xb3f9('0x1'));var db=require(_0xb3f9('0x2'))['db'];var logger=require('../../config/logger')('transcribe');function getTranscriptionJob(_0x4970d5,_0x45e0d6,_0x477deb,_0x2bcb25){return new Promise(function(_0x57c82f,_0x26b29c){var _0x34d23e=new AWS[(_0xb3f9('0x3'))]({'accessKeyId':_0x45e0d6,'secretAccessKey':_0x4970d5,'region':_0x477deb});_0x34d23e[_0xb3f9('0x4')]({'TranscriptionJobName':_0x2bcb25},function(_0x16e819,_0x542b09){if(_0x16e819){return _0x26b29c(_0x16e819);}else{return _0x57c82f(_0x542b09);}});});}function sentiment(_0x5e0d4e,_0x5ee80d,_0xf96649,_0x52452b,_0x2fba79){return new Promise(function(_0x4b7690,_0x488a7b){var _0x877c5e=new AWS[(_0xb3f9('0x5'))]({'accessKeyId':_0x5ee80d,'secretAccessKey':_0x5e0d4e,'region':_0xf96649});_0x877c5e[_0xb3f9('0x6')]({'Text':_0x52452b,'LanguageCode':_0x2fba79[_0xb3f9('0x7')](0x0,0x2)},function(_0x21de5a,_0x21f87f){if(_0x21de5a){return _0x488a7b(_0x21de5a);}else{return _0x4b7690(_0x21f87f);}});});}function checkTranscribeJob(_0x535f8b,_0x1cdce2,_0xbbc1cb){var _0x395561;return new BPromise(function(_0x30b11e,_0x1af6c0){logger[_0xb3f9('0x8')](util[_0xb3f9('0x9')](_0xb3f9('0xa'),_0x535f8b['id'],_0x535f8b['transcribeName']));return getTranscriptionJob(_0xbbc1cb[_0xb3f9('0xb')],_0xbbc1cb['data2'],_0x1cdce2['transcribeRegion'],_0x535f8b[_0xb3f9('0xc')])[_0xb3f9('0xd')](function(_0x171378){logger[_0xb3f9('0x8')](util[_0xb3f9('0x9')](_0xb3f9('0xe'),_0x535f8b['id'],_0x535f8b[_0xb3f9('0xc')],_0x171378[_0xb3f9('0xf')]['TranscriptionJobStatus']));if(!_0x535f8b[_0xb3f9('0x10')]){switch(_0x171378['TranscriptionJob']['TranscriptionJobStatus']){case _0xb3f9('0x11'):var _0x4c629f={'transcribeStatus':_0x171378[_0xb3f9('0xf')][_0xb3f9('0x12')],'fileUri':_0x171378[_0xb3f9('0xf')][_0xb3f9('0x13')][_0xb3f9('0x14')]};logger[_0xb3f9('0x8')](util['format']('[RECORDING:%d]\x20Get\x20transcript',_0x535f8b['id']));return rp({'uri':_0x171378['TranscriptionJob'][_0xb3f9('0x13')][_0xb3f9('0x14')],'json':!![]})[_0xb3f9('0xd')](function(_0x4bffc7){logger['info'](util[_0xb3f9('0x9')](_0xb3f9('0x15'),_0x535f8b['id']));if(_0x4bffc7&&_0x4bffc7[_0xb3f9('0x16')]&&_0x4bffc7['results'][_0xb3f9('0x17')]&&_0x4bffc7['results'][_0xb3f9('0x17')][_0xb3f9('0x18')]){_0x4c629f[_0xb3f9('0x10')]='';for(var _0x539f7a=0x0;_0x539f7a<_0x4bffc7[_0xb3f9('0x16')][_0xb3f9('0x17')][_0xb3f9('0x18')];_0x539f7a++){_0x4c629f['fileText']+=_0x4bffc7[_0xb3f9('0x16')][_0xb3f9('0x17')][_0x539f7a]['transcript']+'';}}return _0x535f8b[_0xb3f9('0x19')](_0x4c629f);})[_0xb3f9('0xd')](function(){logger[_0xb3f9('0x8')](util[_0xb3f9('0x9')](_0xb3f9('0x1a'),_0x535f8b['id']));if(!_0x535f8b['tempSentiment']){throw new db[(_0xb3f9('0x1b'))][(_0xb3f9('0x1c'))](_0xb3f9('0x1d'));}if(!_0x1cdce2[_0xb3f9('0x1e')]||!_0x1cdce2[_0xb3f9('0x1f')]||!_0x1cdce2[_0xb3f9('0x20')]||!_0x1cdce2[_0xb3f9('0x21')]||!_0x1cdce2[_0xb3f9('0x22')]){throw new db[(_0xb3f9('0x1b'))][(_0xb3f9('0x1c'))](_0xb3f9('0x23'));}if(!_0x4c629f[_0xb3f9('0x10')]){throw new Error(_0xb3f9('0x24'));}if(_0x1cdce2['sentimentAccountId']===_0xbbc1cb['id']){return _0xbbc1cb;}else{return db['CloudProvider']['findOne']({'where':{'id':_settings[_0xb3f9('0x1f')]}});}})['then'](function(_0x47b88a){if(!_0x47b88a){throw new db['Sequelize'][(_0xb3f9('0x1c'))](_0xb3f9('0x25'));}if(!_0x47b88a['data1']||!_0x47b88a[_0xb3f9('0x26')]){throw new db[(_0xb3f9('0x1b'))][(_0xb3f9('0x1c'))]('Cloud\x20Provider\x20not\x20configured');}_0x395561=_0x47b88a;logger['info'](util[_0xb3f9('0x9')]('[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis',_0x535f8b['id']));return sentiment(_0x395561[_0xb3f9('0xb')],_0x395561[_0xb3f9('0x26')],_0x1cdce2[_0xb3f9('0x20')],_0x4c629f['fileText'],_0x1cdce2[_0xb3f9('0x21')]);})[_0xb3f9('0xd')](function(_0x293d29){logger['info'](util[_0xb3f9('0x9')](_0xb3f9('0x27'),_0x535f8b['id']));return _0x535f8b['update']({'sentiment':_0x293d29[_0xb3f9('0x28')],'sPositive':_0x293d29['SentimentScore']['Positive'],'sNegative':_0x293d29[_0xb3f9('0x29')][_0xb3f9('0x2a')],'sMixed':_0x293d29['SentimentScore']['Mixed'],'sNeutral':_0x293d29[_0xb3f9('0x29')]['Neutral']});})[_0xb3f9('0xd')](function(){logger[_0xb3f9('0x8')](util[_0xb3f9('0x9')](_0xb3f9('0x1a'),_0x535f8b['id']));_0x30b11e();});case'FAILED':return _0x535f8b['update']({'transcribeStatus':_0xb3f9('0x2b'),'failureReason':_0x171378['TranscriptionJob'][_0xb3f9('0x2c')]})['then'](function(){logger[_0xb3f9('0x8')](util[_0xb3f9('0x9')](_0xb3f9('0x1a'),_0x535f8b['id']));_0x30b11e();});default:logger[_0xb3f9('0x8')](util[_0xb3f9('0x9')](_0xb3f9('0x2d'),_0x535f8b['id']));break;}}else{return BPromise[_0xb3f9('0x2e')]()[_0xb3f9('0xd')](function(){if(!_0x535f8b[_0xb3f9('0x2f')]){throw new db['Sequelize'][(_0xb3f9('0x1c'))](_0xb3f9('0x1d'));}if(!_0x1cdce2[_0xb3f9('0x1e')]||!_0x1cdce2[_0xb3f9('0x1f')]||!_0x1cdce2[_0xb3f9('0x20')]||!_0x1cdce2[_0xb3f9('0x21')]||!_0x1cdce2[_0xb3f9('0x22')]){throw new db['Sequelize'][(_0xb3f9('0x1c'))](_0xb3f9('0x23'));}if(!_0x535f8b[_0xb3f9('0x10')]){throw new Error(_0xb3f9('0x24'));}if(_0x1cdce2[_0xb3f9('0x1f')]===_0xbbc1cb['id']){return _0xbbc1cb;}else{return db[_0xb3f9('0x30')][_0xb3f9('0x31')]({'where':{'id':_settings[_0xb3f9('0x1f')]}});}})['then'](function(_0x2f59b3){if(!_0x2f59b3){throw new db[(_0xb3f9('0x1b'))][(_0xb3f9('0x1c'))]('Cloud\x20Provider\x20not\x20available');}if(!_0x2f59b3[_0xb3f9('0xb')]||!_0x2f59b3[_0xb3f9('0x26')]){throw new db[(_0xb3f9('0x1b'))][(_0xb3f9('0x1c'))]('Cloud\x20Provider\x20not\x20configured');}_0x395561=_0x2f59b3;logger[_0xb3f9('0x8')](util['format'](_0xb3f9('0x32'),_0x535f8b['id']));return sentiment(_0x395561[_0xb3f9('0xb')],_0x395561[_0xb3f9('0x26')],_0x1cdce2[_0xb3f9('0x20')],_0x535f8b[_0xb3f9('0x10')],_0x1cdce2[_0xb3f9('0x21')]);})[_0xb3f9('0xd')](function(_0x13b27b){logger[_0xb3f9('0x8')](util['format']('[RECORDING:%d]\x20Sentiment\x20Analysis\x20available',_0x535f8b['id']));return _0x535f8b[_0xb3f9('0x19')]({'sentiment':_0x13b27b[_0xb3f9('0x28')],'sPositive':_0x13b27b['SentimentScore'][_0xb3f9('0x33')],'sNegative':_0x13b27b[_0xb3f9('0x29')][_0xb3f9('0x2a')],'sMixed':_0x13b27b['SentimentScore'][_0xb3f9('0x34')],'sNeutral':_0x13b27b['SentimentScore'][_0xb3f9('0x35')],'transcribeStatus':'COMPLETED'});})[_0xb3f9('0xd')](function(){logger[_0xb3f9('0x8')](util[_0xb3f9('0x9')](_0xb3f9('0x1a'),_0x535f8b['id']));_0x30b11e();});}})[_0xb3f9('0x36')](function(_0x3e121f){if(_0x3e121f){if(!_0x3e121f['name']||_0x3e121f[_0xb3f9('0x37')]!==_0xb3f9('0x38')){logger[_0xb3f9('0x39')](util[_0xb3f9('0x9')](_0xb3f9('0x3a'),_0x535f8b['id']),util[_0xb3f9('0x3b')](_0x3e121f,{'showHidden':![],'depth':null}));}}_0x30b11e();});});}function checkTranscribe(){var _0x183b48;var _0x16709b;var _0x245ddd;return db['VoiceRecording'][_0xb3f9('0x3c')]({'where':{'transcribeStatus':_0xb3f9('0x3d')}})[_0xb3f9('0xd')](function(_0x4bbe4d){if(!_0x4bbe4d||!_0x4bbe4d[_0xb3f9('0x18')]){throw new db['Sequelize'][(_0xb3f9('0x1c'))](_0xb3f9('0x3e'));}_0x183b48=_0x4bbe4d;return db[_0xb3f9('0x3f')][_0xb3f9('0x31')]({'where':{'id':0x1}});})['then'](function(_0x184f7b){if(!_0x184f7b){throw new db[(_0xb3f9('0x1b'))][(_0xb3f9('0x1c'))](_0xb3f9('0x40'));}_0x16709b=_0x184f7b;if(!_0x16709b[_0xb3f9('0x41')]||!_0x16709b['transcribeAccountId']||!_0x16709b[_0xb3f9('0x42')]||!_0x16709b[_0xb3f9('0x21')]||!_0x16709b[_0xb3f9('0x22')]){throw new db[(_0xb3f9('0x1b'))][(_0xb3f9('0x1c'))](_0xb3f9('0x43'));}return db[_0xb3f9('0x30')]['findOne']({'where':{'id':_0x16709b[_0xb3f9('0x44')]}});})[_0xb3f9('0xd')](function(_0x136362){if(!_0x136362){throw new db[(_0xb3f9('0x1b'))][(_0xb3f9('0x1c'))]('Cloud\x20Provider\x20not\x20available');}if(!_0x136362[_0xb3f9('0xb')]||!_0x136362[_0xb3f9('0x26')]){throw new db[(_0xb3f9('0x1b'))][(_0xb3f9('0x1c'))]('Cloud\x20Provider\x20not\x20configured');}_0x245ddd=_0x136362;var _0x3fa7b0=[];_0x183b48[_0xb3f9('0x45')](function(_0xbab718){_0x3fa7b0['push'](checkTranscribeJob(_0xbab718,_0x16709b,_0x245ddd));});return BPromise[_0xb3f9('0x46')](_0x3fa7b0);})[_0xb3f9('0x36')](function(_0x966388){if(_0x966388){if(!_0x966388[_0xb3f9('0x37')]||_0x966388[_0xb3f9('0x37')]!==_0xb3f9('0x38')){logger[_0xb3f9('0x39')](util[_0xb3f9('0x3b')](_0x966388,{'showHidden':![],'depth':null}));}}});}exports[_0xb3f9('0x47')]=function(){setInterval(function(){return checkTranscribe();},0x3c*0x3e8);return checkTranscribe();};