Skip to content

fix(leaderboard): make weekly period non-empty on Sundays#221

Open
evan188199-tech wants to merge 1 commit into
Snouzy:mainfrom
evan188199-tech:fix/weekly-leaderboard-empty-on-sunday
Open

fix(leaderboard): make weekly period non-empty on Sundays#221
evan188199-tech wants to merge 1 commit into
Snouzy:mainfrom
evan188199-tech:fix/weekly-leaderboard-empty-on-sunday

Conversation

@evan188199-tech

Copy link
Copy Markdown

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:

Weekday OLD offset NEW offset
Sunday +1 (inverted!) −6
Monday 0 0
Tuesday −1 −1
Saturday −5 −5

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.

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
@vercel

vercel Bot commented Jul 7, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the Workoutcool Team Team on Vercel.

A member of the Team first needs to authorize it.

@evan188199-tech

Copy link
Copy Markdown
Author

此 PR 由 GLM-5.2 修复。

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.

Bug: Weekly leaderboard/ranking is empty every Sunday (week-start math inverts)

1 participant