1f6e405c852933518f29349e0112147882efe4d6
[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 _0x2f68=['No\x20transcribe\x20job\x20to\x20check','Setting','Settings\x20not\x20available','transcribe','transcribeRegion','Transcribe\x20not\x20configured','forEach','push','SequelizeValidationError','start','aws-sdk','request-promise','util','bluebird','../../config/logger','TranscribeService','Comprehend','detectSentiment','substring','info','format','[RECORDING:%d]\x20Get\x20status\x20for\x20transcript\x20job\x20(%s)','transcribeName','data1','data2','[RECORDING:%d]\x20Status\x20of\x20transcript\x20job\x20(%s):\x20%s','TranscriptionJob','TranscriptionJobStatus','fileText','COMPLETED','TranscriptFileUri','[RECORDING:%d]\x20Get\x20transcript','Transcript','[RECORDING:%d]\x20Transcript\x20downloaded','transcripts','results','length','update','then','[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated','tempSentiment','Sequelize','Sentiment\x20Analysis\x20not\x20requested','sentiment','sentimentAccountId','sentimentRegion','language','bucket','ValidationError','Sentiment\x20Analysis\x20not\x20configured','Transcript\x20not\x20available','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','Neutral','FAILED','FailureReason','[RECORDING:%d]\x20Transcript\x20Job\x20status\x20not\x20managed','name','error','[RECORDING:%d]\x20Error\x20running\x20Transcribe\x20&\x20Sentiment\x20Analysis','inspect','VoiceRecording','findAll'];(function(_0x56d826,_0x1b6540){var _0x282f3d=function(_0x1fe820){while(--_0x1fe820){_0x56d826['push'](_0x56d826['shift']());}};_0x282f3d(++_0x1b6540);}(_0x2f68,0x9a));var _0x82f6=function(_0x3c0b53,_0x36a14b){_0x3c0b53=_0x3c0b53-0x0;var _0x4ca2f2=_0x2f68[_0x3c0b53];return _0x4ca2f2;};var _=require('lodash');var AWS=require(_0x82f6('0x0'));var rp=require(_0x82f6('0x1'));var util=require(_0x82f6('0x2'));var BPromise=require(_0x82f6('0x3'));var db=require('../../mysqldb')['db'];var logger=require(_0x82f6('0x4'))('transcribe');function getTranscriptionJob(_0x2a06dc,_0x2ab73d,_0x5dad09,_0x3ad120){return new Promise(function(_0x2c4302,_0x542737){var _0x5a8089=new AWS[(_0x82f6('0x5'))]({'accessKeyId':_0x2ab73d,'secretAccessKey':_0x2a06dc,'region':_0x5dad09});_0x5a8089['getTranscriptionJob']({'TranscriptionJobName':_0x3ad120},function(_0x5daa3c,_0x864400){if(_0x5daa3c){return _0x542737(_0x5daa3c);}else{return _0x2c4302(_0x864400);}});});}function sentiment(_0xbd30d5,_0x250d71,_0x5ee0e5,_0x27db58,_0x432289){return new Promise(function(_0x4e7c12,_0x1cf88e){var _0x1eec2f=new AWS[(_0x82f6('0x6'))]({'accessKeyId':_0x250d71,'secretAccessKey':_0xbd30d5,'region':_0x5ee0e5});_0x1eec2f[_0x82f6('0x7')]({'Text':_0x27db58,'LanguageCode':_0x432289[_0x82f6('0x8')](0x0,0x2)},function(_0x34bd7c,_0x4c870e){if(_0x34bd7c){return _0x1cf88e(_0x34bd7c);}else{return _0x4e7c12(_0x4c870e);}});});}function checkTranscribeJob(_0x12c5bc,_0x442262,_0x4cc967){var _0x194da7;return new BPromise(function(_0x1f8bf5,_0x397527){logger[_0x82f6('0x9')](util[_0x82f6('0xa')](_0x82f6('0xb'),_0x12c5bc['id'],_0x12c5bc[_0x82f6('0xc')]));return getTranscriptionJob(_0x4cc967[_0x82f6('0xd')],_0x4cc967[_0x82f6('0xe')],_0x442262['transcribeRegion'],_0x12c5bc[_0x82f6('0xc')])['then'](function(_0x249222){logger[_0x82f6('0x9')](util[_0x82f6('0xa')](_0x82f6('0xf'),_0x12c5bc['id'],_0x12c5bc[_0x82f6('0xc')],_0x249222[_0x82f6('0x10')][_0x82f6('0x11')]));if(!_0x12c5bc[_0x82f6('0x12')]){switch(_0x249222[_0x82f6('0x10')][_0x82f6('0x11')]){case _0x82f6('0x13'):var _0x1d9091={'transcribeStatus':_0x249222[_0x82f6('0x10')][_0x82f6('0x11')],'fileUri':_0x249222[_0x82f6('0x10')]['Transcript'][_0x82f6('0x14')]};logger[_0x82f6('0x9')](util['format'](_0x82f6('0x15'),_0x12c5bc['id']));return rp({'uri':_0x249222[_0x82f6('0x10')][_0x82f6('0x16')][_0x82f6('0x14')],'json':!![]})['then'](function(_0xdc4dea){logger['info'](util[_0x82f6('0xa')](_0x82f6('0x17'),_0x12c5bc['id']));if(_0xdc4dea&&_0xdc4dea['results']&&_0xdc4dea['results'][_0x82f6('0x18')]&&_0xdc4dea[_0x82f6('0x19')]['transcripts']['length']){_0x1d9091[_0x82f6('0x12')]='';for(var _0x31face=0x0;_0x31face<_0xdc4dea['results'][_0x82f6('0x18')][_0x82f6('0x1a')];_0x31face++){_0x1d9091['fileText']+=_0xdc4dea['results'][_0x82f6('0x18')][_0x31face]['transcript']+'';}}return _0x12c5bc[_0x82f6('0x1b')](_0x1d9091);})[_0x82f6('0x1c')](function(){logger['info'](util[_0x82f6('0xa')](_0x82f6('0x1d'),_0x12c5bc['id']));if(!_0x12c5bc[_0x82f6('0x1e')]){throw new db[(_0x82f6('0x1f'))]['ValidationError'](_0x82f6('0x20'));}if(!_0x442262[_0x82f6('0x21')]||!_0x442262[_0x82f6('0x22')]||!_0x442262[_0x82f6('0x23')]||!_0x442262[_0x82f6('0x24')]||!_0x442262[_0x82f6('0x25')]){throw new db[(_0x82f6('0x1f'))][(_0x82f6('0x26'))](_0x82f6('0x27'));}if(!_0x1d9091[_0x82f6('0x12')]){throw new Error(_0x82f6('0x28'));}if(_0x442262[_0x82f6('0x22')]===_0x4cc967['id']){return _0x4cc967;}else{return db[_0x82f6('0x29')][_0x82f6('0x2a')]({'where':{'id':_settings['sentimentAccountId']}});}})[_0x82f6('0x1c')](function(_0x2c764f){if(!_0x2c764f){throw new db[(_0x82f6('0x1f'))]['ValidationError'](_0x82f6('0x2b'));}if(!_0x2c764f[_0x82f6('0xd')]||!_0x2c764f[_0x82f6('0xe')]){throw new db[(_0x82f6('0x1f'))][(_0x82f6('0x26'))](_0x82f6('0x2c'));}_0x194da7=_0x2c764f;logger[_0x82f6('0x9')](util[_0x82f6('0xa')](_0x82f6('0x2d'),_0x12c5bc['id']));return sentiment(_0x194da7[_0x82f6('0xd')],_0x194da7[_0x82f6('0xe')],_0x442262[_0x82f6('0x23')],_0x1d9091[_0x82f6('0x12')],_0x442262[_0x82f6('0x24')]);})['then'](function(_0x13a409){logger[_0x82f6('0x9')](util[_0x82f6('0xa')](_0x82f6('0x2e'),_0x12c5bc['id']));return _0x12c5bc[_0x82f6('0x1b')]({'sentiment':_0x13a409[_0x82f6('0x2f')],'sPositive':_0x13a409[_0x82f6('0x30')][_0x82f6('0x31')],'sNegative':_0x13a409[_0x82f6('0x30')][_0x82f6('0x32')],'sMixed':_0x13a409[_0x82f6('0x30')][_0x82f6('0x33')],'sNeutral':_0x13a409[_0x82f6('0x30')][_0x82f6('0x34')]});})['then'](function(){logger[_0x82f6('0x9')](util['format']('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x12c5bc['id']));_0x1f8bf5();});case _0x82f6('0x35'):return _0x12c5bc[_0x82f6('0x1b')]({'transcribeStatus':'FAILED','failureReason':_0x249222[_0x82f6('0x10')][_0x82f6('0x36')]})[_0x82f6('0x1c')](function(){logger['info'](util[_0x82f6('0xa')]('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x12c5bc['id']));_0x1f8bf5();});default:logger[_0x82f6('0x9')](util['format'](_0x82f6('0x37'),_0x12c5bc['id']));break;}}else{return BPromise['resolve']()[_0x82f6('0x1c')](function(){if(!_0x12c5bc[_0x82f6('0x1e')]){throw new db['Sequelize'][(_0x82f6('0x26'))](_0x82f6('0x20'));}if(!_0x442262[_0x82f6('0x21')]||!_0x442262[_0x82f6('0x22')]||!_0x442262['sentimentRegion']||!_0x442262[_0x82f6('0x24')]||!_0x442262[_0x82f6('0x25')]){throw new db[(_0x82f6('0x1f'))][(_0x82f6('0x26'))]('Sentiment\x20Analysis\x20not\x20configured');}if(!_0x12c5bc[_0x82f6('0x12')]){throw new Error(_0x82f6('0x28'));}if(_0x442262['sentimentAccountId']===_0x4cc967['id']){return _0x4cc967;}else{return db[_0x82f6('0x29')][_0x82f6('0x2a')]({'where':{'id':_settings[_0x82f6('0x22')]}});}})[_0x82f6('0x1c')](function(_0xc3e250){if(!_0xc3e250){throw new db['Sequelize'][(_0x82f6('0x26'))]('Cloud\x20Provider\x20not\x20available');}if(!_0xc3e250[_0x82f6('0xd')]||!_0xc3e250[_0x82f6('0xe')]){throw new db[(_0x82f6('0x1f'))][(_0x82f6('0x26'))](_0x82f6('0x2c'));}_0x194da7=_0xc3e250;logger[_0x82f6('0x9')](util[_0x82f6('0xa')]('[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis',_0x12c5bc['id']));return sentiment(_0x194da7[_0x82f6('0xd')],_0x194da7[_0x82f6('0xe')],_0x442262[_0x82f6('0x23')],_0x12c5bc[_0x82f6('0x12')],_0x442262['language']);})[_0x82f6('0x1c')](function(_0x4797af){logger[_0x82f6('0x9')](util[_0x82f6('0xa')](_0x82f6('0x2e'),_0x12c5bc['id']));return _0x12c5bc[_0x82f6('0x1b')]({'sentiment':_0x4797af[_0x82f6('0x2f')],'sPositive':_0x4797af[_0x82f6('0x30')]['Positive'],'sNegative':_0x4797af[_0x82f6('0x30')][_0x82f6('0x32')],'sMixed':_0x4797af[_0x82f6('0x30')][_0x82f6('0x33')],'sNeutral':_0x4797af[_0x82f6('0x30')][_0x82f6('0x34')],'transcribeStatus':_0x82f6('0x13')});})[_0x82f6('0x1c')](function(){logger['info'](util[_0x82f6('0xa')](_0x82f6('0x1d'),_0x12c5bc['id']));_0x1f8bf5();});}})['catch'](function(_0xfedf51){if(_0xfedf51){if(!_0xfedf51['name']||_0xfedf51[_0x82f6('0x38')]!=='SequelizeValidationError'){logger[_0x82f6('0x39')](util[_0x82f6('0xa')](_0x82f6('0x3a'),_0x12c5bc['id']),util[_0x82f6('0x3b')](_0xfedf51,{'showHidden':![],'depth':null}));}}_0x1f8bf5();});});}function checkTranscribe(){var _0x17a5ae;var _0x2fdcc6;var _0x53f7d4;return db[_0x82f6('0x3c')][_0x82f6('0x3d')]({'where':{'transcribeStatus':'IN_PROGRESS'}})['then'](function(_0x3868cc){if(!_0x3868cc||!_0x3868cc[_0x82f6('0x1a')]){throw new db[(_0x82f6('0x1f'))]['ValidationError'](_0x82f6('0x3e'));}_0x17a5ae=_0x3868cc;return db[_0x82f6('0x3f')][_0x82f6('0x2a')]({'where':{'id':0x1}});})[_0x82f6('0x1c')](function(_0x4e9e13){if(!_0x4e9e13){throw new db['Sequelize'][(_0x82f6('0x26'))](_0x82f6('0x40'));}_0x2fdcc6=_0x4e9e13;if(!_0x2fdcc6[_0x82f6('0x41')]||!_0x2fdcc6['transcribeAccountId']||!_0x2fdcc6[_0x82f6('0x42')]||!_0x2fdcc6[_0x82f6('0x24')]||!_0x2fdcc6['bucket']){throw new db['Sequelize']['ValidationError'](_0x82f6('0x43'));}return db['CloudProvider'][_0x82f6('0x2a')]({'where':{'id':_0x2fdcc6['transcribeAccountId']}});})[_0x82f6('0x1c')](function(_0x4f69de){if(!_0x4f69de){throw new db[(_0x82f6('0x1f'))]['ValidationError'](_0x82f6('0x2b'));}if(!_0x4f69de[_0x82f6('0xd')]||!_0x4f69de[_0x82f6('0xe')]){throw new db[(_0x82f6('0x1f'))][(_0x82f6('0x26'))](_0x82f6('0x2c'));}_0x53f7d4=_0x4f69de;var _0x597844=[];_0x17a5ae[_0x82f6('0x44')](function(_0x1c8304){_0x597844[_0x82f6('0x45')](checkTranscribeJob(_0x1c8304,_0x2fdcc6,_0x53f7d4));});return BPromise['all'](_0x597844);})['catch'](function(_0x13406f){if(_0x13406f){if(!_0x13406f[_0x82f6('0x38')]||_0x13406f[_0x82f6('0x38')]!==_0x82f6('0x46')){logger['error'](util[_0x82f6('0x3b')](_0x13406f,{'showHidden':![],'depth':null}));}}});}exports[_0x82f6('0x47')]=function(){setInterval(function(){return checkTranscribe();},0x3c*0x3e8);return checkTranscribe();};