fix: prevent duplicate prestige bot announcements on concurrent requests

Adds an optimistic lock on the prestige route so that a second concurrent
request for the same state is rejected with 409 rather than firing the
Discord announcement twice. Also adds missing branch-coverage tests for
debug.ts to satisfy the 100% threshold.

Closes #162
This commit is contained in:
2026-03-31 12:12:42 -07:00
committed by Naomi Carrigan
parent 3735cff23f
commit b3d257048f
3 changed files with 123 additions and 8 deletions
+13 -2
View File
@@ -102,12 +102,23 @@ prestigeRouter.post("/", async(context) => {
}).length;
const now = Date.now();
await prisma.gameState.update({
const { updatedAt } = record;
/*
* Use the record's current updatedAt as an optimistic lock — if another
* concurrent prestige request already committed, this update will match
* 0 rows and we can safely reject the duplicate without a double webhook.
*/
const updateResult = await prisma.gameState.updateMany({
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Prisma requires object */
data: { state: finalState as object, updatedAt: now },
where: { discordId },
where: { discordId, updatedAt },
});
if (updateResult.count === 0) {
return context.json({ error: "Prestige already in progress" }, 409);
}
await prisma.player.update({
data: {
characterName: state.player.characterName,