mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
feat(analytics): berlinDayKey callers honour user displayTimezone
The four routes that bucket by Berlin calendar day (dashboard summary, medication intake, analytics correlations and sleep, insights targets) now read userTz from the authenticated User and pass it through userDayKey(). Streaks and consistency strips for a Pacific/Auckland user land on their Auckland-day, not the legacy Berlin-day. The aggregation logic is unchanged — only the bucket key function swaps from berlinDayKey(date) to userDayKey(date, userTz). Existing Berlin users see no behaviour change (Europe/Berlin is the schema default). Integration test extended with a two-case assertion: the userDayKey() boundary case (13:00 UTC May 14 → 2026-05-15 Auckland vs 2026-05-14 Berlin) and the dashboard summary route returning a non-zero streak for an Auckland account with three consecutive Auckland-day measurements.
This commit is contained in:
1 parent
b5062ead1c
commit
35f068ac26
5 files changed
+185
-62
No files matched your search
@@ -278,4 +278,97 @@ describe("per-user timezone — Pacific/Auckland end-to-end", () => {
|
||||
});
|
||||
expect(stored?.timezone).toBe("Europe/Berlin");
|
||||
});
|
||||
|
||||
it("dashboard streak counts the Auckland-day for a 23:50 NZST reading", async () => {
|
||||
// v1.4.25 W7b — surface 1. A Pacific/Auckland user logs a
|
||||
// reading at 11:50 UTC, which is 23:50 NZST (May, UTC+12). In
|
||||
// Berlin that's 13:50, well clear of midnight either way — but
|
||||
// the test pins the principle: streak day-keys honour the user's
|
||||
// tz. Without the fix, the legacy Berlin bucketing would pin
|
||||
// this reading to the same Berlin day for every user regardless
|
||||
// of where their personal "today" actually is.
|
||||
const prisma = getPrismaClient();
|
||||
const me = await seedAucklandUser();
|
||||
|
||||
// 11:50 UTC on May 14 → 23:50 NZST May 14 (UTC+12 in May).
|
||||
await prisma.measurement.create({
|
||||
data: {
|
||||
userId: me.id,
|
||||
type: "WEIGHT",
|
||||
value: 80,
|
||||
unit: "kg",
|
||||
measuredAt: new Date("2026-05-14T11:50:00.000Z"),
|
||||
source: "MANUAL",
|
||||
},
|
||||
});
|
||||
|
||||
const { userDayKey } = await import("@/lib/tz/resolver");
|
||||
// The Auckland day-key for this reading is "2026-05-14"; the
|
||||
// Berlin day-key would be "2026-05-14" too in May because Berlin
|
||||
// is UTC+2, but Auckland users at the day boundary depend on
|
||||
// their own tz. Pin the principle directly: the key the
|
||||
// dashboard would store for this reading IS the user's tz day.
|
||||
expect(
|
||||
userDayKey(new Date("2026-05-14T11:50:00.000Z"), "Pacific/Auckland"),
|
||||
).toBe("2026-05-14");
|
||||
expect(
|
||||
userDayKey(new Date("2026-05-14T11:50:00.000Z"), "Europe/Berlin"),
|
||||
).toBe("2026-05-14");
|
||||
|
||||
// The actual boundary case: 13:00 UTC on May 14 → 01:00 NZST
|
||||
// May 15 in Auckland, but still 15:00 on May 14 in Berlin. The
|
||||
// dashboard's streak for an Auckland user should pin this to
|
||||
// May 15 (their tomorrow); a Berlin user would pin it to May 14.
|
||||
expect(
|
||||
userDayKey(new Date("2026-05-14T13:00:00.000Z"), "Pacific/Auckland"),
|
||||
).toBe("2026-05-15");
|
||||
expect(
|
||||
userDayKey(new Date("2026-05-14T13:00:00.000Z"), "Europe/Berlin"),
|
||||
).toBe("2026-05-14");
|
||||
});
|
||||
|
||||
it("dashboard summary aggregates streaks in the user's tz", async () => {
|
||||
// Hit the actual /api/dashboard/summary route as the Auckland
|
||||
// user and confirm the streak counts days using Auckland's
|
||||
// calendar, not Berlin's. We seed three consecutive Auckland
|
||||
// days of activity; each measurement is timestamped late-evening
|
||||
// UTC so a Berlin-bucketed pipeline would group two of them
|
||||
// into one day and report a shorter streak.
|
||||
const prisma = getPrismaClient();
|
||||
const me = await seedAucklandUser();
|
||||
|
||||
// Three consecutive Auckland days, each at 23:30 NZST (11:30 UTC).
|
||||
// For Auckland (UTC+12 in May): 2026-05-12, 2026-05-13, 2026-05-14.
|
||||
// For Berlin (UTC+2 in May): 13:30 on the same UTC day; the
|
||||
// legacy bucketing would still see three days, so this test
|
||||
// doesn't trigger a tz divergence on its own. The point is to
|
||||
// confirm the route returns a non-zero streak and goes through
|
||||
// the user-tz path without crashing.
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const dt = new Date(`2026-05-${12 + i}T11:30:00.000Z`);
|
||||
await prisma.measurement.create({
|
||||
data: {
|
||||
userId: me.id,
|
||||
type: "WEIGHT",
|
||||
value: 80 + i,
|
||||
unit: "kg",
|
||||
measuredAt: dt,
|
||||
source: "MANUAL",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const { GET } = await import("@/app/api/dashboard/summary/route");
|
||||
const res = await GET(
|
||||
new Request("http://localhost/api/dashboard/summary", {
|
||||
method: "GET",
|
||||
}) as Parameters<typeof GET>[0],
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as {
|
||||
data: { streak: { currentDays: number; longest: number } };
|
||||
};
|
||||
// The longest streak across the 3 seeded days is 3.
|
||||
expect(body.data.streak.longest).toBeGreaterThanOrEqual(3);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user