fix(doctor-report): compare course dates as days in the report zone

A medication's start and end dates are calendar dates, but the report
compared them against the window's instants. West of UTC the window
begins hours after UTC midnight, so a course that ended on the first day
was left out, and one that started the day after the window was listed.
Both now compare against the window's first and last day in the report
zone.
This commit is contained in:
Marc-André Bombeck committed 2026-09-26 22:11:27 +02:00
1 parent 6769f90f4c
commit 04eb8dd7f8
2 files changed
+51 -3

No files matched your search

+9 -3
View File
@@ -61,7 +61,7 @@ import { loadDenseMeasurementBuckets } from "./dense-buckets";
import { summariseDenseBuckets } from "./measurement-series";
import { emptyGlucoseClinical } from "./glucose-panel";
import { buildAdministrationLedger, buildGlp1Block } from "./medications";
import { validTimezoneOr, DEFAULT_TIMEZONE } from "@/lib/tz/format";
import { userDayKey, validTimezoneOr, DEFAULT_TIMEZONE } from "@/lib/tz/format";
import {
loadAllergies,
loadAnamnesis,
@@ -148,6 +148,12 @@ export async function collectDoctorReportData(
},
});
const reportTz = validTimezoneOr(userProfile?.timezone, DEFAULT_TIMEZONE);
// `startsOn` / `endsOn` are calendar dates (stored as UTC midnight), so
// they compare against the window's first and last day in the report zone,
// not its instants: west of UTC the window starts hours after UTC midnight,
// and a course that ended on the first day read as over before it began.
const firstDay = new Date(`${userDayKey(start, reportTz)}T00:00:00.000Z`);
const lastDay = new Date(`${userDayKey(end, reportTz)}T00:00:00.000Z`);
const aggregateDenseTypes = days > DENSE_REPORT_RAW_WINDOW_DAYS;
const densePulse = aggregateDenseTypes && !excluded.includes("PULSE");
@@ -202,7 +208,7 @@ export async function collectDoctorReportData(
AND: [
{
OR: [
{ startsOn: { lte: end } },
{ startsOn: { lte: lastDay } },
{ startsOn: null, createdAt: { lte: end } },
{
intakeEvents: {
@@ -214,7 +220,7 @@ export async function collectDoctorReportData(
},
],
},
{ OR: [{ endsOn: null }, { endsOn: { gte: start } }] },
{ OR: [{ endsOn: null }, { endsOn: { gte: firstDay } }] },
],
},
include: {