fix(cycle): stamp a wearable period end only on a cycle it opened

Garmin's period end was written on any cycle starting on Garmin's start
date, a cycle the person opened herself included, even when her own days
ran past it. The stamp now requires the cycle's first day to be the
wearable's own live row.
This commit is contained in:
Marc-André Bombeck committed 2026-11-07 01:04:12 +01:00
1 parent 2aa98bb6c2
commit 6c5146942b
4 files changed
+115 -5

No files matched your search

+9 -2
View File
@@ -91,11 +91,15 @@ export async function createSubordinatePeriodDays(
/**
* Stamp the last day of a finished period on the cycle that opens at
* `start`, when that cycle has no period end yet. A person's own end, or one
* an earlier pass stamped, stays. Returns whether a stamp was written.
* `start`, when that cycle has no period end yet and the wearable opened it:
* its first day is the wearable's own live row. A cycle the person opened
* herself on that date carries her account of the period (her days, her end
* or none yet), and a wearable never writes over it. An end already set, by
* her or an earlier pass, stays. Returns whether a stamp was written.
*/
export async function stampSubordinatePeriodEnd(
userId: string,
source: SubordinateCycleSource,
start: string,
lastDay: string,
): Promise<boolean> {
@@ -106,6 +110,9 @@ export async function stampSubordinatePeriodEnd(
startDate: start,
deletedAt: null,
periodEndDate: null,
dayLogs: {
some: { userId, date: start, source, deletedAt: null },
},
},
data: { periodEndDate: lastDay, syncVersion: { increment: 1 } },
});
+7 -2
View File
@@ -7,7 +7,7 @@
* The write goes through `createSubordinatePeriodDays`, which attributes each
* created day to its cycle and opens the cycle a first bleeding day starts,
* as every other cycle writer does. A finished period also stamps its last
* day on that cycle, when the cycle has no period end yet.
* day on that cycle, when Garmin opened it and it has no period end yet.
*
* `GET /periodichealth-service/menstrualcycle/summary/{date}` describes the
* cycle a date falls in: its start and its period length. A cycle Garmin
@@ -98,7 +98,12 @@ export async function writeGarminPeriod(
period.days,
);
if (period.ended) {
await stampSubordinatePeriodEnd(userId, period.start, period.days.at(-1)!);
await stampSubordinatePeriodEnd(
userId,
"GARMIN",
period.start,
period.days.at(-1)!,
);
}
return created;
}
+4 -1
View File
@@ -182,7 +182,10 @@ async function repairAccount(
// An open cycle may still be bleeding; the hourly sync stamps its end.
if (!cycle?.endDate) continue;
const end = await garminRunEnd(prisma, userId, start);
if (end && (await stampSubordinatePeriodEnd(userId, start, end))) {
if (
end &&
(await stampSubordinatePeriodEnd(userId, "GARMIN", start, end))
) {
result.periodEnds += 1;
}
}