diff --git a/functions/src/index.ts b/functions/src/index.ts index 01f94ae..53ca904 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -49,13 +49,6 @@ export const finishGame = functions.https.onCall(async (data, context) => { .ref(`gameData/${gameId}`) .once("value"); const gameSnap = await admin.database().ref(`games/${gameId}`).once("value"); - if (!gameSnap.exists()) { - throw new functions.https.HttpsError( - "not-found", - `The game with gameId ${gameId} was not found in the database.` - ); - } - const gameMode = (gameSnap.child("mode").val() as GameMode) || "normal"; const { lastSet, deck, finalTime, scores } = replayEvents(gameData, gameMode); @@ -73,6 +66,14 @@ export const finishGame = functions.https.onCall(async (data, context) => { .database() .ref(`games/${gameId}`) .transaction((game) => { + // Transaction handler should always handle null, because firebase often + // calls it like that: https://stackoverflow.com/a/65415636/5190601 + if (game === null) { + throw new functions.https.HttpsError( + "not-found", + `The game with gameId ${gameId} was not found in the database.` + ); + } if (game.status !== "ingame") { // Someone beat us to the atomic update, so we cancel the transaction. return;