mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 07:42:23 +00:00
refactor(medications): consolidate the hhmm-to-minutes helpers into one shared util
Seven near-identical copies of the HH:mm to minutes-since-midnight parser had accumulated across the scheduling surfaces in two flavours: a strict one that trusts an already-validated literal and a null-returning one for unvalidated input. Both now live in src/lib/medications/scheduling/hhmm.ts as hhmmToMinutes and hhmmToMinutesOrNull, and every call site imports from there. Behaviour is identical: the per-medication route keeps its malformed-to-zero fallback as a thin adapter over the shared core, and dose-window re-exports the strict variant so its consumers and unit suite keep their import site.
This commit is contained in:
1 parent
61a1592665
commit
9e6453c51d
8 files changed
+59
-52
No files matched your search
@@ -32,6 +32,7 @@ import {
|
||||
recomputeMedicationComplianceForDay,
|
||||
} from "@/lib/rollups/medication-compliance-rollups";
|
||||
import { assertMedicationOwnership } from "@/lib/medications/route-guards";
|
||||
import { hhmmToMinutesOrNull } from "@/lib/medications/scheduling/hhmm";
|
||||
import { getUserTodayBounds } from "@/lib/tz/local-day";
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
@@ -39,9 +40,7 @@ type RouteParams = { params: Promise<{ id: string }> };
|
||||
|
||||
/** Parse "HH:mm" into minutes-of-day; NaN-safe (malformed → 0). */
|
||||
function hhmmToMinutes(value: string): number {
|
||||
const [h, m] = value.split(":").map(Number);
|
||||
if (!Number.isFinite(h) || !Number.isFinite(m)) return 0;
|
||||
return h * 60 + m;
|
||||
return hhmmToMinutesOrNull(value) ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user