mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 07:13:29 +00:00
fix(inventory): serialise unknown units as null, not 0 (iOS shows unknown, no phantom negatives)
This commit is contained in:
commit
fcd498d252
12 files changed
+146
-30
No files matched your search
@@ -15,6 +15,7 @@ import { prisma } from "@/lib/db";
|
||||
import {
|
||||
buildCreateInventoryInput,
|
||||
expireStaleInUseItems,
|
||||
serializeInventoryItem,
|
||||
} from "../service";
|
||||
|
||||
const NOW = new Date("2026-05-14T12:00:00Z");
|
||||
@@ -104,3 +105,55 @@ describe("buildCreateInventoryInput", () => {
|
||||
expect(input.expiresAt).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("serializeInventoryItem (iOS#31)", () => {
|
||||
it("serialises a Decimal-as-string unit count to a JSON number", () => {
|
||||
const out = serializeInventoryItem({
|
||||
id: "i1",
|
||||
unitsTotal: "30",
|
||||
unitsRemaining: "29.5",
|
||||
});
|
||||
expect(out.unitsTotal).toBe(30);
|
||||
expect(out.unitsRemaining).toBe(29.5);
|
||||
});
|
||||
|
||||
it("keeps a genuine tracked zero as 0, not null", () => {
|
||||
const out = serializeInventoryItem({
|
||||
id: "i2",
|
||||
unitsTotal: 4,
|
||||
unitsRemaining: 0,
|
||||
});
|
||||
expect(out.unitsRemaining).toBe(0);
|
||||
expect(out.unitsTotal).toBe(4);
|
||||
});
|
||||
|
||||
it("serialises an unknown (null) unit count as null, not a fabricated 0", () => {
|
||||
const out = serializeInventoryItem({
|
||||
id: "i3",
|
||||
unitsTotal: null,
|
||||
unitsRemaining: null,
|
||||
});
|
||||
expect(out.unitsTotal).toBeNull();
|
||||
expect(out.unitsRemaining).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects non-finite corrupt values (NaN / Infinity) to null", () => {
|
||||
const out = serializeInventoryItem({
|
||||
id: "i4",
|
||||
unitsTotal: "not-a-number",
|
||||
unitsRemaining: Infinity,
|
||||
});
|
||||
expect(out.unitsTotal).toBeNull();
|
||||
expect(out.unitsRemaining).toBeNull();
|
||||
});
|
||||
|
||||
it("preserves every other field unchanged", () => {
|
||||
const out = serializeInventoryItem({
|
||||
id: "i5",
|
||||
state: "ACTIVE",
|
||||
unitsTotal: "10",
|
||||
unitsRemaining: "10",
|
||||
});
|
||||
expect(out).toMatchObject({ id: "i5", state: "ACTIVE" });
|
||||
});
|
||||
});
|
||||
@@ -108,7 +108,10 @@ describe("supply surfaces — both detail-page readouts ride the shared helper (
|
||||
"src/components/medications/sections/inventory-section.tsx",
|
||||
);
|
||||
expect(src).toContain('from "@/lib/medications/inventory/summary"');
|
||||
expect(src).toMatch(/summariseSupply\(items, perDose\)/);
|
||||
// v1.18.3 (iOS#31) — the items are mapped through a nullable→0 coalesce
|
||||
// (an unknown-units row contributes nothing to the available headline)
|
||||
// before the shared helper, but the summary still rides summariseSupply.
|
||||
expect(src).toMatch(/summariseSupply\(\s*items\.map\(/);
|
||||
expect(src).not.toContain('state !== "USED_UP"');
|
||||
expect(src).toContain("medications.detail.bestand.expiredSuffix");
|
||||
});
|
||||
|
||||
@@ -56,19 +56,36 @@ export async function expireStaleInUseItems(input: {
|
||||
* `unitsTotal` / `unitsRemaining` as numbers. Convert at every response
|
||||
* boundary that returns an item, so a fractional remainder (29.5 after a
|
||||
* half-tablet dose) lands as a JSON number, never `"29.5"`.
|
||||
*
|
||||
* v1.18.3 (iOS#31) — serialise a genuinely-unknown unit count as `null`,
|
||||
* not `0`. A well-formed row always carries both Decimals, but a corrupt
|
||||
* or legacy row could hold null / NaN / Infinity; `Number(null) === 0`
|
||||
* (and `Number("x") === NaN`) would otherwise fabricate a misleading `0`
|
||||
* that the client decrements into negatives. `null` lets the client
|
||||
* render "unbekannt" instead. A real tracked `0` stays `0`.
|
||||
*/
|
||||
function toFiniteUnit(value: unknown): number | null {
|
||||
// `Number(null) === 0` and `Number("") === 0`, so reject the empty /
|
||||
// nullish cases up front — a genuinely-absent count must read as null,
|
||||
// not a fabricated 0. Anything else goes through the finiteness gate
|
||||
// (NaN / Infinity from a corrupt string also fall to null).
|
||||
if (value === null || value === undefined || value === "") return null;
|
||||
const n = Number(value);
|
||||
return Number.isFinite(n) ? n : null;
|
||||
}
|
||||
|
||||
export function serializeInventoryItem<
|
||||
T extends { unitsTotal: unknown; unitsRemaining: unknown },
|
||||
>(
|
||||
item: T,
|
||||
): Omit<T, "unitsTotal" | "unitsRemaining"> & {
|
||||
unitsTotal: number;
|
||||
unitsRemaining: number;
|
||||
unitsTotal: number | null;
|
||||
unitsRemaining: number | null;
|
||||
} {
|
||||
return {
|
||||
...item,
|
||||
unitsTotal: Number(item.unitsTotal),
|
||||
unitsRemaining: Number(item.unitsRemaining),
|
||||
unitsTotal: toFiniteUnit(item.unitsTotal),
|
||||
unitsRemaining: toFiniteUnit(item.unitsRemaining),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -296,13 +296,15 @@ const medicationInventoryItemResource = z
|
||||
),
|
||||
unitsTotal: z
|
||||
.number()
|
||||
.nullable()
|
||||
.describe(
|
||||
"Units the container shipped with (tablets / ampoules / puffs; 1–1000). v1.16.12 — fractional, so a split-pill remainder reads cleanly. Dose-derived readouts divide by the medication's `unitsPerDose`.",
|
||||
"Units the container shipped with (tablets / ampoules / puffs; 1–1000). v1.16.12 — fractional, so a split-pill remainder reads cleanly. Dose-derived readouts divide by the medication's `unitsPerDose`. v1.18.3 (iOS#31) — NULL when the unit count is unknown (a corrupt or legacy row); the client renders \"unknown\", never a fabricated 0.",
|
||||
),
|
||||
unitsRemaining: z
|
||||
.number()
|
||||
.nullable()
|
||||
.describe(
|
||||
"Units left in the container. v1.16.12 — fractional (a ½-tablet dose leaves 29.5 of 30). Decremented by the intake consumption hook (FEFO with spillover across containers); refunded when a taken dose is skipped, edited away, or deleted.",
|
||||
"Units left in the container. v1.16.12 — fractional (a ½-tablet dose leaves 29.5 of 30). Decremented by the intake consumption hook (FEFO with spillover across containers); refunded when a taken dose is skipped, edited away, or deleted. v1.18.3 (iOS#31) — NULL when the count is unknown (a corrupt or legacy row); the client renders \"unknown\", never a fabricated 0 it could decrement into negatives.",
|
||||
),
|
||||
firstUseAt: z.iso
|
||||
.datetime({ offset: true })
|
||||
|
||||
Reference in new issue
Block a user