Examining my database structure, I am aiming to create a Firebase trigger that will update the RoundScore for a specific PlayerID whenever any section of the '/SCORES' node is modified.
"SCORES" : {
"2017" : {
"Round_1" : {
"3" : {
"Emoji" : "",
"PlayerName" : "Person A",
"RoundScore" : 100
},
},
},
},
"SELECTIONS" : {
"2015" : {
"Round_1" : {
"TEAM A" : {
"18" : {
"emoji" : " ",
"playerName" : "Person A",
"position" : "POS"
},
"19" : {
"emoji" : " ",
"playerName" : "Person B",
"position" : "POS"
}
},
// more team data...
}
}
}
The desired outcome after triggering this change in the database is as follows:
"SCORES" : {
"2017" : {
"Round_1" : {
"3" : {
"Emoji" : "", <------------ DUPLICATE FROM HERE
"PlayerName" : "Person A",
"RoundScore" : 100 <------------ AND HERE
},
},
},
},
"SELECTIONS" : {
"2015" : {
"Round_1" : {
"TEAM A" : {
"3" : {
"emoji" : "", <------------ INSERT HERE
"playerName" : "Person A",
"position" : "POS"
"RoundScore" : 100 <------------ AND HERE
},
},
},
}
Currently, my implementation only functions with a hardcoded teamID (TEAM A in the provided example).
exports.whenScoresUpdate = functions.database
.ref('/SCORES/{yearId}/{roundId}/{playerId}')
.onCreate((snap, context) => {
const newScoreData = snap.val();
const yearId = context.params.yearId;
const roundId = context.params.roundId;
const playerId = context.params.playerId;
const scoreObj = {
"RoundScore" : newScoreData.RoundScore,
"Emoji" : newScoreData.Emoji,
};
return admin.database().ref('/SELECTIONS/' + yearId + '/' + roundId + '/{teamId}/' + playerId).update(scoreObj);