fix(leaderboard): make weekly period non-empty on Sundays#221
Open
evan188199-tech wants to merge 1 commit into
Open
fix(leaderboard): make weekly period non-empty on Sundays#221evan188199-tech wants to merge 1 commit into
evan188199-tech wants to merge 1 commit into
Conversation
getDateRangeForPeriod("weekly") derived Monday from startOf("week").add(1, day).
dayjs' default week starts on Sunday, so on a Sunday startOf("week") is
today and the +1 day lands on the *following* Monday — after `now`. The
returned range (startDate > endDate) then fed an inverted Prisma filter
`startedAt: { gte, lte }` in both leaderboard actions, which matches
zero sessions, so the weekly top-users list and weekly user position were
empty every Sunday.
Compute Monday directly from the day-of-week instead:
daysSinceMonday = (now.day() + 6) % 7 // Sun→6 … Mon→0
which always points backwards (or to today on Monday).
Fixes Snouzy#220
|
Someone is attempting to deploy a commit to the Workoutcool Team Team on Vercel. A member of the Team first needs to authorize it. |
Author
|
此 PR 由 GLM-5.2 修复。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The weekly leaderboard (top users + your position) returns no results every Sunday. This is a bug I found while working on the leaderboard — it's not covered by #217 (which only filters by `endedAt`).
Root cause
`getDateRangeForPeriod("weekly")` in `src/features/leaderboard/lib/utils.ts` computes Monday as:
```ts
const startOfWeek = now.startOf("week").add(1, "day"); // dayjs week starts on Sunday, add 1 for Monday
```
dayjs' default week starts on Sunday. On a Sunday, `startOf("week")` is today (Sunday 00:00), so `add(1, "day")` lands on the following Monday — in the future. The range becomes `startDate > endDate`, and both leaderboard actions feed it into an inverted Prisma filter `startedAt: { gte, lte }` that matches zero sessions.
Quick table of the Monday offset (in days from today) for each weekday:
Only Sunday is wrong, but it breaks the entire weekly view one day a week.
Fix
Derive Monday from the day-of-week so it always points backwards:
```ts
const daysSinceMonday = (now.day() + 6) % 7; // Sun→6, Mon→0, …, Sat→5
const startOfWeek = now.subtract(daysSinceMonday, "day").startOf("day");
```
`monthly` and `all-time` are unaffected.
Fixes #220.