generated from nhcarrigan/template
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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user