mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 23:12:04 +00:00
fix(reminders): read a first-due date sent as a date as that day in the user's zone
A reminder's first-due date could only be sent as a date-time, and the web form sent the browser's local midnight, so a browser set to a zone other than the profile's could move the day. The API now also takes a bare calendar date and stores it as that day's midnight in the user's zone, and the web form sends the date itself. A date-time still means the instant it names.
This commit is contained in:
1 parent
b6b51fcb5e
commit
9311337997
8 files changed
+101
-20
No files matched your search
@@ -9,7 +9,7 @@ import { describe, expect, it } from "vitest";
|
||||
|
||||
import { wallClockInTz, zonedWallClockToUtc } from "@/lib/tz/wall-clock";
|
||||
|
||||
import { computeReminderNextDueAt } from "../scheduling";
|
||||
import { computeReminderNextDueAt, parseReminderAnchor } from "../scheduling";
|
||||
|
||||
const ZONES = [
|
||||
"America/New_York",
|
||||
@@ -75,3 +75,29 @@ describe.each(ZONES)("reminder anchors in %s", (tz) => {
|
||||
expect(localLabel(next, tz)).toBe("2026-09-20 19:00 w0");
|
||||
});
|
||||
});
|
||||
|
||||
describe.each(ZONES)("a first-due date sent as a calendar date in %s", (tz) => {
|
||||
it("is that day in the user's zone", () => {
|
||||
const anchor = parseReminderAnchor("2026-10-01", tz);
|
||||
expect(anchor).toEqual(localMidnight(tz, 2026, 10, 1));
|
||||
const next = computeReminderNextDueAt(
|
||||
{
|
||||
intervalDays: 30,
|
||||
rrule: null,
|
||||
anchorDate: anchor,
|
||||
notifyHour: 9,
|
||||
lastSatisfiedAt: null,
|
||||
createdAt: new Date("2026-09-01T12:00:00Z"),
|
||||
},
|
||||
tz,
|
||||
new Date("2026-09-26T12:00:00Z"),
|
||||
);
|
||||
expect(localLabel(next, tz)?.slice(0, 16)).toBe("2026-10-01 09:00");
|
||||
});
|
||||
|
||||
it("keeps a date-time as the instant it names", () => {
|
||||
expect(parseReminderAnchor("2026-10-01T00:00:00.000Z", tz)).toEqual(
|
||||
new Date("2026-10-01T00:00:00.000Z"),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -31,6 +31,7 @@ import {
|
||||
} from "@/lib/medications/scheduling/recurrence";
|
||||
import { wallClockInTz } from "@/lib/tz/wall-clock";
|
||||
import { DEFAULT_TIMEZONE } from "@/lib/tz/format";
|
||||
import { startOfLocalDayKey } from "@/lib/tz/local-day";
|
||||
|
||||
/**
|
||||
* The reminder fields this module reads. A subset of the Prisma row so
|
||||
@@ -52,6 +53,18 @@ export interface ReminderScheduleInput {
|
||||
endsOn?: Date | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A first-due date from a request: a calendar date (`YYYY-MM-DD`) is that
|
||||
* day in the user's zone, stored as its local midnight; a date-time is the
|
||||
* instant it names. A date sent as UTC midnight would read as the evening
|
||||
* before west of UTC, which is why a bare date is accepted at all.
|
||||
*/
|
||||
export function parseReminderAnchor(value: string, timeZone: string): Date {
|
||||
return /^\d{4}-\d{2}-\d{2}$/.test(value)
|
||||
? startOfLocalDayKey(value, timeZone || DEFAULT_TIMEZONE)
|
||||
: new Date(value);
|
||||
}
|
||||
|
||||
/** The calendar day `instant` falls on in `tz`, as UTC midnight of that date. */
|
||||
function calendarDayInZone(instant: Date, tz: string): Date {
|
||||
const p = wallClockInTz(instant, tz);
|
||||
|
||||
@@ -118,3 +118,19 @@ describe("measurementReminderTypeEnum (V3 — completeness)", () => {
|
||||
expect(parsed.success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("createMeasurementReminderSchema — first-due date", () => {
|
||||
const base = { label: "Blood test", intervalDays: 365 };
|
||||
it.each([
|
||||
["2026-10-01", true],
|
||||
["2026-10-01T00:00:00.000Z", true],
|
||||
["2026-10-01T00:00:00-07:00", true],
|
||||
["2026-10-01T00:00:00", false],
|
||||
["01.10.2026", false],
|
||||
])("anchorDate %s is accepted: %s", (anchorDate, ok) => {
|
||||
expect(
|
||||
createMeasurementReminderSchema.safeParse({ ...base, anchorDate })
|
||||
.success,
|
||||
).toBe(ok);
|
||||
});
|
||||
});
|
||||
@@ -143,13 +143,22 @@ const cadenceRefinement = (
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* The first-due date: a calendar date (`YYYY-MM-DD`, read as that day in the
|
||||
* user's zone) or a date-time (the instant it names).
|
||||
*/
|
||||
const anchorDateField = z.union([
|
||||
z.iso.date(),
|
||||
z.iso.datetime({ offset: true }),
|
||||
]);
|
||||
|
||||
export const createMeasurementReminderSchema = z
|
||||
.object({
|
||||
label: z.string().trim().min(1).max(120),
|
||||
measurementType: measurementReminderTypeEnum.nullish(),
|
||||
intervalDays: z.number().int().min(1).max(3650).nullish(),
|
||||
rrule: rruleField.nullish(),
|
||||
anchorDate: z.iso.datetime({ offset: true }).nullish(),
|
||||
anchorDate: anchorDateField.nullish(),
|
||||
notifyHour: z.number().int().min(0).max(23).default(9),
|
||||
location: z.string().trim().max(200).nullish(),
|
||||
enabled: z.boolean().default(true),
|
||||
@@ -177,7 +186,7 @@ export const updateMeasurementReminderSchema = z
|
||||
measurementType: measurementReminderTypeEnum.nullish(),
|
||||
intervalDays: z.number().int().min(1).max(3650).nullish(),
|
||||
rrule: rruleField.nullish(),
|
||||
anchorDate: z.iso.datetime({ offset: true }).nullish(),
|
||||
anchorDate: anchorDateField.nullish(),
|
||||
notifyHour: z.number().int().min(0).max(23).optional(),
|
||||
location: z.string().trim().max(200).nullish(),
|
||||
enabled: z.boolean().optional(),
|
||||
|
||||
Reference in new issue
Block a user