Skip to content

Keep one reminder rule per team when first saves race - #150

Open
wayx112 wants to merge 2 commits into
Team6083:mainfrom
wayx112:fix/reminder-one-rule
Open

wayx112 wants to merge 2 commits into
Team6083:mainfrom
wayx112:fix/reminder-one-rule

Conversation

@wayx112

@wayx112 wayx112 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Summary

saveTeamReminder and setTeamReminderWebhook found the team's rule and created one when there was none, as two steps. A double submit, or two admins saving for the first time, could create two rules for one team: both send, and the admin page only edits the oldest, so the second can't be changed or turned off (#147).

saveTeamRule(team)
  transaction:
    team exists?                       → otherwise "Team not found."
    upsert ReminderRuleLock(team)      ← two first saves for the same team conflict here (P2034, or P2002)
    find the team's rule → update it, or create it
  conflict → wait 20–60 ms × attempt at random, retry (5 attempts)
  • Why a lock, not a unique index: a unique index on (teamId, kind, channel) would also stop a team from ever having a second rule of the same kind, which ReminderRule was designed to allow. So this takes the second option from the issue, the same pattern as ClockInLock.
  • Lock: a new ReminderRuleLock model, one row per team (teamId unique, onDelete: Cascade), so deleting a team deletes it.
  • Every save lands: without the wait, retries of many simultaneous saves collided again and some gave up. With a short random wait, all of them land.

Closes #147.

Evidence

Simultaneous first saves for a new team against a local MongoDB replica set: 4 saveTeamReminder and 2 setTeamReminderWebhook at once, 3 runs.

before the wait   1 rule; 5 of 6 saves failed after 3 conflicts (the webhook was lost)
after             6 × ok, 1 rule with the settings and the webhook, in each of the 3 runs
delete the team   lock gone
Test Files  25 passed (25)
     Tests  335 passed (335)

New tests: a first save that loses to another (P2034 and P2002) updates the winner's rule and leaves one, and a missing team is refused.

tsc --noEmit and ESLint on the changed files pass.

Merge danger

Door: two-way. Two functions and one new collection.

Blast radius: saving a team's reminder settings or webhook.

  • Run npx prisma db push when deploying so the ReminderRuleLock collection and its unique index exist.

🤖 Generated with Claude Code

saveTeamReminder and setTeamReminderWebhook found the team's rule and created one when there was none, as two
steps, so a double submit or two admins saving for the first time could create two rules. Both sent, and the page
only edits the oldest. A unique index on the team would stop a team from ever having more rules, so each save now
upserts a ReminderRuleLock for the team first, in the same transaction as the find and the create or update.
Concurrent first saves conflict, and the retries, spaced by a short random wait, update the rule the winner created.
The lock goes with the team. A team that doesn't exist is refused.

Closes Team6083#147

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@kennhung kennhung left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. The lock plus transaction matches the ClockInLock pattern, and the reasons for not using a unique index make sense. One nit inline. This does not clean up duplicate rules that already exist; that seems fine for this PR.

This PR, #151 and #152 all touch team-reminder-dto.ts and its test, so whichever merges later will need a conflict resolution. Suggested order: #150, #152, #151.

Comment thread src/lib/data/team-reminder-dto.test.ts Outdated
});

it("ends with one rule when another first save for the team wins, updating that rule", async () => {
db.rules = [];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this db.rules = []; is redundant, the loop resets it on the next line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wayx112

wayx112 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, removed the redundant reset in 549069e. Agreed on the merge order (#150, #152, #151); I'll resolve the conflicts on the later ones as they come.

@wayx112
wayx112 requested a review from kennhung November 15, 2026 23:51

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Two admins saving a team's reminder at once can create two rules

2 participants