mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
feat(import): show lab results from health records under an Apple Health import
A finished import now lists the lab results it found next to the ECG line, and an archive whose only new data is lab results no longer says nothing was imported. The status endpoint bounds the lab counts the same way it bounds the ECG counts.
This commit is contained in:
1 parent
736e6e9c86
commit
14c3c6696a
15 files changed
+166
-30
No files matched your search
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"$comment": "Production functions over 200 lines, keyed \"file :: name\", and their length. Maintained by src/__tests__/source-size-ratchet.test.ts.",
|
||||
"count": 335,
|
||||
"total": 109021,
|
||||
"total": 109016,
|
||||
"entries": {
|
||||
"app/achievements/page.tsx :: AchievementsPage": 206,
|
||||
"app/api/admin/backups/upload/route.ts :: apiHandler(arg)": 275,
|
||||
@@ -192,7 +192,7 @@
|
||||
"components/settings/coach-nudge-card.tsx :: CoachNudgeCard": 251,
|
||||
"components/settings/coach-prefs-section.tsx :: CoachPrefsSection": 350,
|
||||
"components/settings/environment-section.tsx :: EnvironmentSection": 666,
|
||||
"components/settings/import-panel/apple-health-import-card.tsx :: AppleHealthImportCard": 298,
|
||||
"components/settings/import-panel/apple-health-import-card.tsx :: AppleHealthImportCard": 293,
|
||||
"components/settings/import-panel/csv-import-card.tsx :: CsvImportCard": 212,
|
||||
"components/settings/import-panel/dose-history-import-card.tsx :: DoseHistoryImportCard": 221,
|
||||
"components/settings/import-panel/health-connect-import-card.tsx :: HealthConnectImportCard": 293,
|
||||
|
||||
@@ -47,6 +47,10 @@ const ADMIN_SESSION = {
|
||||
|
||||
const NOW = new Date("2026-05-15T10:00:00.000Z");
|
||||
|
||||
function ownerSession(): Awaited<ReturnType<typeof getSession>> {
|
||||
return OWNER_SESSION as unknown as Awaited<ReturnType<typeof getSession>>;
|
||||
}
|
||||
|
||||
function makeRow(overrides: Partial<Record<string, unknown>> = {}) {
|
||||
return {
|
||||
id: "ij-1",
|
||||
@@ -94,7 +98,7 @@ describe("GET /api/import/apple-health-export/[jobId]/status", () => {
|
||||
});
|
||||
|
||||
it("bounds the lab counts and drops anything else in the block", async () => {
|
||||
vi.mocked(getSession).mockResolvedValue(OWNER_SESSION as never);
|
||||
vi.mocked(getSession).mockResolvedValue(ownerSession());
|
||||
mocks.importJobFindUnique.mockResolvedValue(
|
||||
makeRow({
|
||||
status: "done",
|
||||
@@ -127,7 +131,7 @@ describe("GET /api/import/apple-health-export/[jobId]/status", () => {
|
||||
});
|
||||
|
||||
it("reports zeroed lab counts for a job from before labs were read", async () => {
|
||||
vi.mocked(getSession).mockResolvedValue(OWNER_SESSION as never);
|
||||
vi.mocked(getSession).mockResolvedValue(ownerSession());
|
||||
mocks.importJobFindUnique.mockResolvedValue(
|
||||
makeRow({ status: "done", result: { totals: { recordsRead: 1 } } }),
|
||||
);
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* The ECG and lab-result lines under a finished Apple Health import.
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
|
||||
import { I18nProvider } from "@/lib/i18n/context";
|
||||
import type { MessageBundle } from "@/lib/i18n/load-locale";
|
||||
import en from "../../../../../messages/en.json";
|
||||
import de from "../../../../../messages/de.json";
|
||||
|
||||
import { AppleHealthAuxiliaryLines } from "../apple-health-auxiliary-lines";
|
||||
|
||||
function render(
|
||||
props: Parameters<typeof AppleHealthAuxiliaryLines>[0],
|
||||
locale: "en" | "de" = "en",
|
||||
): string {
|
||||
return renderToStaticMarkup(
|
||||
<I18nProvider
|
||||
initialLocale={locale}
|
||||
initialMessages={(locale === "en" ? en : de) as unknown as MessageBundle}
|
||||
>
|
||||
<AppleHealthAuxiliaryLines {...props} />
|
||||
</I18nProvider>,
|
||||
);
|
||||
}
|
||||
|
||||
describe("<AppleHealthAuxiliaryLines>", () => {
|
||||
it("renders nothing for an archive with neither", () => {
|
||||
expect(render({})).toBe("");
|
||||
expect(render({ ecg: { discovered: 0 }, labs: { discovered: 0 } })).toBe(
|
||||
"",
|
||||
);
|
||||
});
|
||||
|
||||
it("renders the lab line with every count", () => {
|
||||
const html = render({
|
||||
labs: { discovered: 7, imported: 4, duplicate: 1, skipped: 1, failed: 1 },
|
||||
});
|
||||
expect(html).toContain('data-testid="import-apple-health-labs-result"');
|
||||
expect(html).toContain(
|
||||
"Lab results from health records: 7 found, 4 imported, 1 already in your record, 1 skipped, 1 failed.",
|
||||
);
|
||||
expect(html).not.toContain("import-apple-health-ecg-result");
|
||||
});
|
||||
|
||||
it("renders the ECG line beside it", () => {
|
||||
const html = render({
|
||||
ecg: { discovered: 2, imported: 2 },
|
||||
labs: { discovered: 1, imported: 1 },
|
||||
});
|
||||
expect(html).toContain("import-apple-health-ecg-result");
|
||||
expect(html).toContain("import-apple-health-labs-result");
|
||||
});
|
||||
|
||||
it("speaks the active locale", () => {
|
||||
expect(render({ labs: { discovered: 2, imported: 2 } }, "de")).toContain(
|
||||
"Laborwerte aus Gesundheitsakten: 2 gefunden, 2 importiert",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,66 @@
|
||||
"use client";
|
||||
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
|
||||
export interface AppleHealthEcgCounts {
|
||||
discovered?: number;
|
||||
imported?: number;
|
||||
updated?: number;
|
||||
skipped?: number;
|
||||
failed?: number;
|
||||
}
|
||||
|
||||
export interface AppleHealthLabCounts {
|
||||
discovered?: number;
|
||||
imported?: number;
|
||||
duplicate?: number;
|
||||
skipped?: number;
|
||||
failed?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* The outcome lines of the passes that run after export.xml: the ECG
|
||||
* recordings and the lab results from health records. A line shows only when
|
||||
* the archive held something of its kind.
|
||||
*/
|
||||
export function AppleHealthAuxiliaryLines({
|
||||
ecg,
|
||||
labs,
|
||||
}: {
|
||||
ecg?: AppleHealthEcgCounts;
|
||||
labs?: AppleHealthLabCounts;
|
||||
}) {
|
||||
const { t } = useTranslations();
|
||||
return (
|
||||
<>
|
||||
{(ecg?.discovered ?? 0) > 0 && (
|
||||
<p
|
||||
data-testid="import-apple-health-ecg-result"
|
||||
className="text-muted-foreground text-xs"
|
||||
>
|
||||
{t("settings.sections.export.import.appleHealth.ecgResult", {
|
||||
discovered: ecg?.discovered ?? 0,
|
||||
imported: ecg?.imported ?? 0,
|
||||
updated: ecg?.updated ?? 0,
|
||||
skipped: ecg?.skipped ?? 0,
|
||||
failed: ecg?.failed ?? 0,
|
||||
})}
|
||||
</p>
|
||||
)}
|
||||
{(labs?.discovered ?? 0) > 0 && (
|
||||
<p
|
||||
data-testid="import-apple-health-labs-result"
|
||||
className="text-muted-foreground text-xs"
|
||||
>
|
||||
{t("settings.sections.export.import.appleHealth.labsResult", {
|
||||
discovered: labs?.discovered ?? 0,
|
||||
imported: labs?.imported ?? 0,
|
||||
duplicate: labs?.duplicate ?? 0,
|
||||
skipped: labs?.skipped ?? 0,
|
||||
failed: labs?.failed ?? 0,
|
||||
})}
|
||||
</p>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -11,6 +11,11 @@ import { queryKeys } from "@/lib/query-keys";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { apiFetchRaw, apiGet } from "@/lib/api/api-fetch";
|
||||
import {
|
||||
AppleHealthAuxiliaryLines,
|
||||
type AppleHealthEcgCounts,
|
||||
type AppleHealthLabCounts,
|
||||
} from "./apple-health-auxiliary-lines";
|
||||
import { ImportCardShell } from "./import-card-shell";
|
||||
import { WrittenOutcomeLine } from "@/components/outcome/written-outcome-line";
|
||||
import { classifyWrittenOutcome } from "@/lib/outcome/written-outcome";
|
||||
@@ -49,13 +54,8 @@ interface JobStatus {
|
||||
daysFailed?: number;
|
||||
firstFailureReason?: string | null;
|
||||
};
|
||||
ecg?: {
|
||||
discovered?: number;
|
||||
imported?: number;
|
||||
updated?: number;
|
||||
skipped?: number;
|
||||
failed?: number;
|
||||
};
|
||||
ecg?: AppleHealthEcgCounts;
|
||||
labs?: AppleHealthLabCounts;
|
||||
} | null;
|
||||
failureReason: string | null;
|
||||
}
|
||||
@@ -389,7 +389,10 @@ export function AppleHealthImportCard() {
|
||||
}
|
||||
})();
|
||||
const ecg = status?.result?.ecg;
|
||||
const ecgWritten = (ecg?.imported ?? 0) + (ecg?.updated ?? 0);
|
||||
const labs = status?.result?.labs;
|
||||
const rowsWritten = status?.result?.totals?.rowsUpserted ?? 0;
|
||||
const auxWritten =
|
||||
(ecg?.imported ?? 0) + (ecg?.updated ?? 0) + (labs?.imported ?? 0);
|
||||
const skips = summarizeAppleHealthSkips(status?.result ?? null);
|
||||
const cycleStats = status?.result?.cycle;
|
||||
|
||||
@@ -482,18 +485,23 @@ export function AppleHealthImportCard() {
|
||||
{isDone && (
|
||||
<WrittenOutcomeLine
|
||||
outcome={classifyWrittenOutcome({
|
||||
written: (status?.result?.totals?.rowsUpserted ?? 0) + ecgWritten,
|
||||
written: rowsWritten + auxWritten,
|
||||
// Deliberate exclusions (clinical records, unsupported /
|
||||
// deferred types, a disabled cycle module) are information
|
||||
// and must not tip the archive into a warning. Genuine
|
||||
// refusals — samples that SHOULD have landed (unparseable,
|
||||
// out of range, upsert failure) plus failed cycle days — do.
|
||||
skipped: skips.refused + (cycleStats?.daysFailed ?? 0),
|
||||
skipped:
|
||||
skips.refused +
|
||||
(cycleStats?.daysFailed ?? 0) +
|
||||
(labs?.failed ?? 0),
|
||||
})}
|
||||
message={
|
||||
(status?.result?.totals?.rowsUpserted ?? 0) > 0
|
||||
// An archive whose only new data is lab results or ECGs still
|
||||
// imported something.
|
||||
rowsWritten + auxWritten > 0
|
||||
? t("settings.sections.export.import.appleHealth.doneSummary", {
|
||||
imported: status?.result?.totals?.rowsUpserted ?? 0,
|
||||
imported: rowsWritten,
|
||||
read: status?.result?.totals?.recordsRead ?? 0,
|
||||
skipped: status?.result?.clinical?.skipped ?? 0,
|
||||
})
|
||||
@@ -505,20 +513,7 @@ export function AppleHealthImportCard() {
|
||||
/>
|
||||
)}
|
||||
{isDone && <AppleHealthSkipLines skips={skips} cycle={cycleStats} />}
|
||||
{isDone && (ecg?.discovered ?? 0) > 0 && (
|
||||
<p
|
||||
data-testid="import-apple-health-ecg-result"
|
||||
className="text-muted-foreground text-xs"
|
||||
>
|
||||
{t("settings.sections.export.import.appleHealth.ecgResult", {
|
||||
discovered: ecg?.discovered ?? 0,
|
||||
imported: ecg?.imported ?? 0,
|
||||
updated: ecg?.updated ?? 0,
|
||||
skipped: ecg?.skipped ?? 0,
|
||||
failed: ecg?.failed ?? 0,
|
||||
})}
|
||||
</p>
|
||||
)}
|
||||
{isDone && <AppleHealthAuxiliaryLines ecg={ecg} labs={labs} />}
|
||||
{isDone && ownRecordsSkipped > 0 && (
|
||||
<AppleHealthOwnRecordsNote count={ownRecordsSkipped} />
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user