fix(doctor-report): keep a report with a past end date inside its dates

A report whose end date lies in the past listed medications that started
after it, the dose history up to today and the latest injection even when it
came later. It now lists a medication only if its course had begun by the
end date (or it had a dose scheduled inside the window) and had not ended
before the start, and dose history and the last injection stop at the end
date.
This commit is contained in:
Marc-André Bombeck committed 2026-09-26 16:25:45 +02:00
1 parent 9d2fd1db14
commit ff4aa32f6d
2 files changed
+174 -3

No files matched your search

+33 -3
View File
@@ -189,7 +189,34 @@ export async function collectDoctorReportData(
}),
wantsMedications
? prisma.medication.findMany({
where: { userId, active: true },
// The report describes its window, and a link can pin that window
// in the past: a medication whose course had not begun by the
// window's end, or had ended before it began, is not part of it.
// One with a dose scheduled inside the window was taken then,
// whenever it was entered. Dose history and the last intake stop
// at the window's end too, so a report for March does not show a
// dose set in June.
where: {
userId,
active: true,
AND: [
{
OR: [
{ startsOn: { lte: end } },
{ startsOn: null, createdAt: { lte: end } },
{
intakeEvents: {
some: {
deletedAt: null,
scheduledFor: { gte: start, lte: end },
},
},
},
],
},
{ OR: [{ endsOn: null }, { endsOn: { gte: start } }] },
],
},
include: {
schedules: {
select: { ...SCHEDULE_COMPLIANCE_SELECT, label: true },
@@ -205,9 +232,12 @@ export async function collectDoctorReportData(
},
},
pauseEras: { select: { pausedAt: true, resumedAt: true } },
doseChanges: { orderBy: { effectiveFrom: "asc" } },
doseChanges: {
where: { effectiveFrom: { lte: end } },
orderBy: { effectiveFrom: "asc" },
},
intakeEvents: {
where: { takenAt: { not: null } },
where: { takenAt: { not: null, lte: end } },
orderBy: { takenAt: "desc" },
take: 1,
select: { takenAt: true, injectionSite: true },