@@ -1017,40 +1017,48 @@ async function sendStatements(statements, lrs) {
10171017 } ) ;
10181018}
10191019
1020- /**
1021- * Checks if LRSes are scoped.
1022- */
1023- function isScoped ( ) {
1024- if ( 'scoped' in config . LRS . clients ) {
1025- return true ;
1026- }
1027- return false ;
1028- }
1020+ let LRS_CLIENTS ;
10291021
10301022/**
1031- * Returns LRS settings found by scope .
1023+ * Returns LRS clients .
10321024 */
1033- function getLRS ( scope ) {
1034- let lrses = { } ;
1035- if ( 'default' in config . LRS . clients ) {
1036- lrses [ 'default' ] = {
1037- 'auth' :{
1038- 'username' :config . LRS . clients . default . user ,
1039- 'password' :config . LRS . clients . default . pass
1040- } ,
1041- } ;
1042- }
1043- if ( 'scoped' in config . LRS . clients ) {
1044- config . LRS . clients . scoped . forEach ( client => {
1045- lrses [ client . scope ] = {
1046- 'auth' :{
1047- 'username' :client . user ,
1048- 'password' :client . pass
1049- } ,
1050- } ;
1051- } ) ;
1025+ async function getLRSClients ( ) {
1026+ if ( ! ( config . LRS . client . key && config . LRS . client . secret ) ) {
1027+ process . exitCode = 1 ;
1028+ throw new Error ( 'Specify LRS client in config/app.js' ) ;
10521029 }
1053- return lrses [ scope ] ;
1030+ const clientResponse = await axios . get ( `${ config . url } :3000/api/v2/client` , {
1031+ 'auth' : {
1032+ 'username' :config . LRS . client . key ,
1033+ 'password' :config . LRS . client . secret
1034+ }
1035+ } ) . catch ( err => {
1036+ process . exitCode = 1 ;
1037+ throw new Error ( `Failed to get LRS clients - ${ err } ` ) ;
1038+ } ) ;
1039+ const lrsResponse = await axios . get ( `${ config . url } :3000/api/v2/lrs` , {
1040+ 'auth' : {
1041+ 'username' :config . LRS . client . key ,
1042+ 'password' :config . LRS . client . secret
1043+ }
1044+ } ) . catch ( err => {
1045+ process . exitCode = 1 ;
1046+ throw new Error ( `Failed to get LRSes - ${ err } ` ) ;
1047+ } ) ;
1048+ let clients = { }
1049+ lrsResponse . data . forEach ( ( lrs ) => {
1050+ const client = clientResponse . data . find ( c => c . lrs_id === lrs . _id )
1051+ const key = client . api . basic_key
1052+ const secret = client . api . basic_secret
1053+ const scope = ( key === config . LRS . client . key && secret === config . LRS . client . secret ) ? 'default' : lrs . title
1054+ clients [ scope ] = {
1055+ 'auth' : {
1056+ 'username' : key ,
1057+ 'password' : secret
1058+ }
1059+ } ;
1060+ } ) ;
1061+ return clients ;
10541062}
10551063
10561064/**
@@ -1067,7 +1075,7 @@ async function routeStatements(objecttable, xapis){
10671075 const statements = xapis [ scope ] . map ( ( value , index , array ) => {
10681076 return value . statement ;
10691077 } ) ;
1070- const lrs = getLRS ( scope ) ;
1078+ const lrs = LRS_CLIENTS [ scope ] ;
10711079 const seq = objectids [ 0 ] ;
10721080 logger . info (
10731081 `[SEQ:${ seq } ][SCOPE:${ scope } ] Sending ${ statements . length } statements...`
@@ -1109,15 +1117,15 @@ function getScopeFromEppn(username) {
11091117 */
11101118function getLRSScope ( userAttrs , userid ) {
11111119 let scope = 'default' ;
1112- if ( isScoped ( ) ) {
1120+ if ( config . LRS . ePPNScoped ) {
11131121 scope = userid in userAttrs ? userAttrs [ userid ] . scope : null ;
11141122 if ( ! scope ) {
11151123 logger . trace (
11161124 `The user(id:${ userid } ) doesn't have ` +
11171125 'an ePPN scope, sending the statement to the default LRS.'
11181126 ) ;
11191127 scope = 'default' ;
1120- } else if ( ! getLRS ( scope ) ) {
1128+ } else if ( ! LRS_CLIENTS [ scope ] ) {
11211129 logger . trace (
11221130 `LRS for ${ scope } not found, ` +
11231131 'sending the statement to the default LRS.'
@@ -3417,6 +3425,7 @@ async function translateStandardLogs(logs, userAttrs, courseNames){ // eslint-di
34173425 ) ;
34183426 continue ;
34193427 }
3428+
34203429 const scope = getLRSScope ( userAttrs , log . userid ) ;
34213430 if ( ! ( scope in xapis ) ) {
34223431 xapis [ scope ] = [ ] ;
@@ -3683,10 +3692,7 @@ const waitForInterval = () => {
36833692 * Processes logs until no more data exists.
36843693 */
36853694module . exports = async function main ( ) { // eslint-disable-line max-statements
3686- if ( ! ( 'default' in config . LRS . clients ) && ! ( 'scoped' in config . LRS . clients ) ) {
3687- process . exitCode = 1 ;
3688- throw new Error ( 'Specify LRS clients in config/app.js' ) ;
3689- }
3695+ LRS_CLIENTS = await getLRSClients ( ) ;
36903696
36913697 // Retrieve all users
36923698 const users = await USER . findAll ( {
0 commit comments