mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
feat(import): add CSV measurement import with per-row status and dry-run
A self-hoster migrating from a spreadsheet or a meter export overwhelmingly has CSV, which neither the JSON nor the Apple Health import covers. Add `POST /api/import/csv` reusing the existing write loop and rollup re-fold. The documented header is `type,value,unit,measuredAt[,glucoseContext,notes, externalId]`, order-independent. measuredAt must carry an explicit ISO-8601 offset — a row without one is skipped rather than guessed — and is bounded by the shared entry-instant validator. Glucose accepts mmol/L (converted to canonical mg/dL) and weight accepts lb (converted to kg); any other non-canonical unit is skipped, never mis-stored. An optional externalId column makes a re-upload idempotent; without it a re-upload duplicates. The response carries a per-row status envelope so a 2000-row file with three bad rows imports 1997 and names the three. `?dryRun=1` validates and previews the outcome without writing. The parse, validation, and unit conversion live in a pure module so they are unit-tested without a database. Settings -> Export & Import gains a third CSV card mirroring the JSON and Apple Health cards, with upload, paste, preview, and a downloadable example. The new route is documented in the OpenAPI contract.
This commit is contained in:
1 parent
82e2d7dfb1
commit
fbdcb31202
15 files changed
+1666
-2
No files matched your search
@@ -27,7 +27,13 @@ vi.mock("next/navigation", () => ({
|
||||
}));
|
||||
|
||||
import { I18nProvider } from "@/lib/i18n/context";
|
||||
import { ImportPanel, EXAMPLE_IMPORT, parseImportJson } from "../import-panel";
|
||||
import {
|
||||
ImportPanel,
|
||||
EXAMPLE_IMPORT,
|
||||
EXAMPLE_CSV,
|
||||
parseImportJson,
|
||||
} from "../import-panel";
|
||||
import { parseCsvMeasurements } from "@/lib/import/csv-measurements";
|
||||
|
||||
function render(node: React.ReactElement, locale: "en" | "de" = "en") {
|
||||
const queryClient = new QueryClient({
|
||||
@@ -46,6 +52,7 @@ describe("<ImportPanel> — SSR smoke", () => {
|
||||
expect(html).toContain('id="settings-section-import-title"');
|
||||
expect(html).toContain('data-testid="import-card-apple-health"');
|
||||
expect(html).toContain('data-testid="import-card-json"');
|
||||
expect(html).toContain('data-testid="import-card-csv"');
|
||||
// Raw i18n keys never leak past the provider.
|
||||
expect(html).not.toContain("settings.sections.export.import.");
|
||||
});
|
||||
@@ -58,6 +65,11 @@ describe("<ImportPanel> — SSR smoke", () => {
|
||||
"import-json-choose-file",
|
||||
"import-json-download-example",
|
||||
"import-action-json",
|
||||
"import-csv-textarea",
|
||||
"import-csv-choose-file",
|
||||
"import-csv-download-example",
|
||||
"import-csv-preview",
|
||||
"import-action-csv",
|
||||
]) {
|
||||
expect(html).toContain(`data-testid="${id}"`);
|
||||
}
|
||||
@@ -112,6 +124,19 @@ describe("EXAMPLE_IMPORT payload", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("EXAMPLE_CSV", () => {
|
||||
it("parses entirely to ok rows (example must not drift from the schema)", () => {
|
||||
// Pin the clock well past the fixture timestamps so the entry-instant
|
||||
// bound never rejects the example as future-dated.
|
||||
const out = parseCsvMeasurements(EXAMPLE_CSV, {
|
||||
now: new Date("2026-06-01T00:00:00Z").getTime(),
|
||||
});
|
||||
expect(out.fatal).toBeUndefined();
|
||||
expect(out.rows.length).toBeGreaterThan(0);
|
||||
expect(out.rows.every((r) => r.status === "ok")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseImportJson guard", () => {
|
||||
it("rejects an unparseable string before any network call", () => {
|
||||
const result = parseImportJson("{ not json");
|
||||
|
||||
Reference in new issue
Block a user