feat(nutrients): tell the account holder when a sync arrived but nothing landed

GET /api/nutrients now looks up the most recent nutrient.batch.ingest
audit row when the window is genuinely empty, and surfaces it as
lastAttempt only when that call landed nothing (inserted === 0 &&
updated === 0). A real success elsewhere is left alone, so an empty
window for an unrelated reason never gets reframed as a failure.

The lookup runs on the zero-rows path only, never on the happy path,
and stays counts-and-reason-codes only - no user-typed content, no
per-entry health values. The reason code never reaches the page as
text: each of the four closed skip reasons maps to its own human
sentence, and the two that have no useful action for the account
holder (unit_mismatch, upsert_failed) say plainly that the entries
couldn't be stored rather than inventing advice. day_invalid is the
one case worth naming a real cause for - a clock or time zone mismatch
on the sending side - without claiming it was a phone, since any
token holder can call this route.

This depends on AuditLog retention: cleanupOldAuditLogs prunes rows
past 365 days by default, so a dormant account's last failed attempt
can age out and the explanation silently reverts to the plain empty
state. Documented at the lookup for whoever next touches that job.
This commit is contained in:
Marc-André Bombeck committed 2026-07-30 23:30:28 +02:00
1 parent 3ec3a8c6aa
commit 12d3239de0
12 files changed
+374 -17

No files matched your search

+59 -9
View File
@@ -12,13 +12,36 @@ import { HydrationCard } from "@/components/insights/nutrients/hydration-card";
import { CaffeineCard } from "@/components/insights/nutrients/caffeine-card";
import { MicronutrientsCard } from "@/components/insights/nutrients/micronutrients-card";
import { useAuth } from "@/hooks/use-auth";
import { useTranslations } from "@/lib/i18n/context";
import { useTranslations, useFormatters } from "@/lib/i18n/context";
import { apiGet, apiPatch } from "@/lib/api/api-fetch";
import { queryKeys } from "@/lib/query-keys";
import type { NutrientIntakeOverview } from "@/components/insights/nutrients/types";
const WINDOW_DAYS = 30;
/**
* `nutrient.batch.ingest`'s closed skip-reason codes
* (`src/app/api/nutrients/batch/route.ts`) mapped to a human sentence —
* never render the raw code. A reason with no useful action for the
* account holder says plainly that the entries couldn't be stored rather
* than inventing advice; `dayInvalid` is the one case with something
* genuinely worth checking (a clock/time-zone mismatch on the sending
* side), so it says so.
*/
const LAST_ATTEMPT_REASON_KEYS: Record<string, string> = {
unit_mismatch: "nutrients.page.lastAttemptReason.unitMismatch",
value_out_of_range: "nutrients.page.lastAttemptReason.valueOutOfRange",
day_invalid: "nutrients.page.lastAttemptReason.dayInvalid",
upsert_failed: "nutrients.page.lastAttemptReason.upsertFailed",
};
function lastAttemptReasonKey(reason: string): string {
return (
LAST_ATTEMPT_REASON_KEYS[reason] ??
"nutrients.page.lastAttemptReason.unknown"
);
}
/**
* `/insights/nutrients` — hydration + micronutrients (v1.29).
*
@@ -44,13 +67,22 @@ const WINDOW_DAYS = 30;
* `queryKeys.nutrientIntake(WINDOW_DAYS)` query and already renders
* its own error card, but this page's old empty-state branch
* short-circuited before that card ever mounted.
* - module on, zero rows anywhere in the window → one EmptyState
* explaining where data comes from (phone health-app sync or
* manual water entry) instead of three near-empty cards.
* - module on, zero rows anywhere in the window, AND the server found a
* recent sync attempt that landed nothing (`overview.data.lastAttempt`,
* from `GET /api/nutrients` — see that route for the AuditLog lookup
* and its retention coupling) → an EmptyState naming the attempt and
* the reason, not the generic copy. A batch CAN arrive and have every
* entry rejected (wrong unit, an implausible amount, a bad date, a
* write failure); without this branch that read the same as nothing
* ever syncing.
* - module on, zero rows, no recorded attempt → the generic EmptyState
* explaining where data comes from (a health-app sync or a manual
* water entry) instead of three near-empty cards.
* - otherwise → the three-card spine.
*/
export default function InsightsNutrientsPage() {
const { t } = useTranslations();
const fmt = useFormatters();
const { user } = useAuth();
const queryClient = useQueryClient();
@@ -110,16 +142,34 @@ export default function InsightsNutrientsPage() {
const hasAnyData = (overview.data?.nutrients.length ?? 0) > 0;
if (!overview.isLoading && !hasAnyData) {
const lastAttempt = overview.data?.lastAttempt ?? null;
return (
<SubPageShell
title={t("nutrients.page.title")}
description={t("nutrients.page.description")}
>
<EmptyState
icon={<Leaf className="size-6" />}
title={t("nutrients.page.emptyTitle")}
description={t("nutrients.page.emptyDescription")}
/>
{lastAttempt ? (
<EmptyState
icon={<Leaf className="size-6" />}
title={t("nutrients.page.lastAttemptTitle")}
description={
<>
{t(lastAttemptReasonKey(lastAttempt.topReason))}
<span className="mt-1 block text-xs">
{t("nutrients.page.lastAttemptAt", {
date: fmt.dateTime(lastAttempt.at),
})}
</span>
</>
}
/>
) : (
<EmptyState
icon={<Leaf className="size-6" />}
title={t("nutrients.page.emptyTitle")}
description={t("nutrients.page.emptyDescription")}
/>
)}
</SubPageShell>
);
}