fix(labs): keep a reading's conversion record unless its value actually changes

The single-reading edit cleared sourceValue and sourceUnit whenever the body
carried a value, and the web edit sheet always sends one, so a date or note
edit erased the "converted from" hint. It is now cleared only when the value
differs from the stored one. The sheet also opened a converted reading at its
stored precision (100.90191999999999); it now starts from the value as the lab
surfaces show it and leaves an untouched value out of the request, so the
stored full-precision number is not replaced by its rounded display.
This commit is contained in:
Marc-André Bombeck committed 2026-10-14 19:15:33 +02:00
1 parent 47a91d6922
commit cd1449e63e
6 files changed
+118 -9

No files matched your search

@@ -0,0 +1,48 @@
import { describe, expect, it } from "vitest";
import { resolveValueForUpdate, valueEditSeed } from "../value-update";
describe("valueEditSeed", () => {
it("opens a converted reading at display precision, not its stored tail", () => {
// 5.6 mmol/L of glucose, stored in mg/dL.
expect(valueEditSeed(100.90191999999999)).toBe("100.9");
});
it("keeps whole numbers bare and at most two decimals", () => {
expect(valueEditSeed(110)).toBe("110");
expect(valueEditSeed(4.567)).toBe("4.57");
});
});
describe("resolveValueForUpdate", () => {
it("leaves an untouched value out of the update", () => {
expect(
resolveValueForUpdate({
seed: "100.9",
editorValue: "100.9",
parsed: 100.9,
}),
).toBeUndefined();
});
it("sends a changed value", () => {
expect(
resolveValueForUpdate({
seed: "100.9",
editorValue: "101,5",
parsed: 101.5,
}),
).toBe(101.5);
});
it("sends a value typed back to the same number in another spelling", () => {
// "100,9" is a deliberate re-entry; the stored tail goes, as for any edit.
expect(
resolveValueForUpdate({
seed: "100.9",
editorValue: "100,9",
parsed: 100.9,
}),
).toBe(100.9);
});
});
+29
View File
@@ -0,0 +1,29 @@
import { formatLabValue } from "./format-value";
/**
* The numeric value of a lab reading in the edit sheet.
*
* A converted reading is stored at full precision (5.6 mmol/L of glucose is
* 100.90191999999999 mg/dL), and the sheet must not open with that tail. The
* field starts from the value as every lab surface shows it: whole numbers
* bare, at most two decimals. Written with a dot, which the sheet's parser
* reads, rather than in the reader's locale, which an input cannot round-trip.
*/
export function valueEditSeed(value: number): string {
return formatLabValue(value, String);
}
/**
* What the edit PUT sends for the value. A field the person did not touch is
* omitted (`undefined`), so the stored full-precision number stays as it is and
* a converted reading keeps its "converted from" record: sending the rounded
* start value back would both lose precision and read to the server as a new
* number. A changed field sends the parsed number.
*/
export function resolveValueForUpdate(args: {
seed: string;
editorValue: string;
parsed: number;
}): number | undefined {
return args.editorValue.trim() === args.seed.trim() ? undefined : args.parsed;
}