fix(medications): end a course on the person's day, as a date

"End medication" sent new Date().toISOString(), a full instant, against
an endsOn that only accepts YYYY-MM-DD, so every press answered 422 and
the toast said the medication could not be ended. The instant's date was
also the UTC one, a day off around midnight for anyone not on UTC.

The button now sends the person's calendar day in their timezone
(userDayKey), the same day key the medication card uses.
This commit is contained in:
haritowa committed 2026-10-24 16:57:20 +00:00
1 parent cf61bd1c85
commit 19ae91f80a
2 files changed
+59 -3

No files matched your search

@@ -0,0 +1,40 @@
import { describe, expect, it } from "vitest";
import { updateMedicationSchema } from "@/lib/validations/medication/create-update";
import { endCourseBody } from "../destructive-zone-section";
/**
* "End medication" sent `new Date().toISOString()`, a full instant, against an
* `endsOn` that only takes `YYYY-MM-DD`, so every press answered 422 and the
* toast said the medication could not be ended. The body now carries the
* person's own calendar day.
*/
describe("endCourseBody", () => {
it("is a date the update schema accepts", () => {
const body = endCourseBody(
new Date("2026-10-21T09:30:00.000Z"),
"Europe/Berlin",
);
expect(body).toEqual({ endsOn: "2026-10-21" });
expect(updateMedicationSchema.safeParse(body).success).toBe(true);
});
it("is the person's day, not the UTC one", () => {
// 23:30 UTC is already the next day in Warsaw and still the same day in
// New York.
const lateUtc = new Date("2026-10-20T23:30:00.000Z");
expect(endCourseBody(lateUtc, "Europe/Warsaw").endsOn).toBe("2026-10-21");
expect(endCourseBody(lateUtc, "America/New_York").endsOn).toBe(
"2026-10-20",
);
});
it("is what the update schema refused before: a full instant is not a date", () => {
expect(
updateMedicationSchema.safeParse({
endsOn: new Date("2026-10-21T09:30:00.000Z").toISOString(),
}).success,
).toBe(false);
});
});
@@ -13,7 +13,7 @@
* - Tier 1 Pausieren: PUT `{ active: false | true }`. Server derives
* `pausedAt`. Switch wraps in `<label>` so the entire row is the
* AT hit target.
* - Tier 2 Beenden: PUT `{ endsOn: <today-iso> }`. AlertDialog
* - Tier 2 Beenden: PUT `{ endsOn: <today YYYY-MM-DD> }`. AlertDialog
* confirm; Cancel autofocus stays default.
* - Tier 3a Verlauf löschen: DELETE `/intake/purge`. Disabled when
* `intakeCount === 0`. AlertDialog confirm.
@@ -46,9 +46,21 @@ import {
import { Button } from "@/components/ui/button";
import { Separator } from "@/components/ui/separator";
import { Switch } from "@/components/ui/switch";
import { useAuth } from "@/hooks/use-auth";
import { useTranslations } from "@/lib/i18n/context";
import { invalidateMedicationReads, queryKeys } from "@/lib/query-keys";
import { apiDelete, apiPut } from "@/lib/api/api-fetch";
import { DEFAULT_TIMEZONE, userDayKey } from "@/lib/tz/format";
/**
* The PUT body that ends a course today: the person's own calendar day as
* `YYYY-MM-DD`, the only shape `endsOn` accepts. A full ISO instant was
* refused with 422 on every press, and its date was the UTC one, a day off
* around midnight for anyone not on UTC.
*/
export function endCourseBody(now: Date, timeZone: string): { endsOn: string } {
return { endsOn: userDayKey(now, timeZone) };
}
const PAUSE_SWITCH_ID = "medication-detail-pause-switch";
const PAUSE_TITLE_ID = "medication-detail-pause-title";
@@ -71,6 +83,8 @@ export function LifecycleManageBody({
}) {
const { t } = useTranslations();
const queryClient = useQueryClient();
const { user } = useAuth();
const userTz = user?.timezone || DEFAULT_TIMEZONE;
const [paused, setPaused] = useState(!active);
const [tier1Busy, setTier1Busy] = useState(false);
@@ -107,8 +121,10 @@ export function LifecycleManageBody({
if (tier2Busy) return;
setTier2Busy(true);
try {
const today = new Date().toISOString();
await apiPut(`/api/medications/${medicationId}`, { endsOn: today });
await apiPut(
`/api/medications/${medicationId}`,
endCourseBody(new Date(), userTz),
);
await invalidateMedicationReads(queryClient);
toast.success(t("medications.detail.zone.end.toast"));
setEndDialogOpen(false);