fix(nutrients): never let the empty-state explanation break the read it explains

The last-attempt lookup runs on the zero-rows path, which is exactly the path a
fresh account takes. When it threw, the whole nutrients read returned 500 — so an
account with nothing to show got an error instead of an empty page, and the
feature that exists to explain an empty surface was the thing preventing it from
rendering.

Wrapped so a failed ledger read degrades to the plain empty state and is recorded
for the operator rather than shown to the person. The route's shape assertion now
carries `lastAttempt: null` explicitly, so a field cannot join this response
without someone deciding it should.
This commit is contained in:
Marc-André Bombeck committed 2026-07-31 05:09:36 +02:00
1 parent 4c35c16d64
commit f7d846458a
2 files changed
+37 -15

No files matched your search

@@ -96,7 +96,15 @@ describe("GET /api/nutrients", () => {
const res = await GET(getReq());
expect(res.status).toBe(200);
const body = (await res.json()) as OverviewResponse;
expect(body.data).toEqual({ windowDays: 14, nutrients: [] });
// `lastAttempt` is null, not absent: a fresh account has no ingest in the
// ledger, and the empty page says "nothing yet" rather than naming a failure
// that never happened. Kept as an exact shape check so a field cannot appear
// on this response without someone deciding it should.
expect(body.data).toEqual({
windowDays: 14,
nutrients: [],
lastAttempt: null,
});
});
it("folds rows into catalog-ordered summaries with latest day/amount and day counts", async () => {
+28 -14
View File
@@ -189,21 +189,35 @@ export const GET = apiHandler(async (request: NextRequest) => {
// user-facing message depends on it.
let lastAttempt: { at: string; topReason: string } | null = null;
if (nutrients.length === 0) {
const lastIngest = await prisma.auditLog.findFirst({
where: { userId: user.id, action: "nutrient.batch.ingest" },
orderBy: { createdAt: "desc" },
select: { createdAt: true, details: true },
});
const details = parseLastIngestDetails(lastIngest?.details ?? null);
// Only surface a reason when the last attempt landed NOTHING: a
// succeeded attempt whose day just falls outside this window is a
// different, unrelated situation — showing a failure notice over it
// would be the very false claim this feature exists to prevent.
if (details && details.inserted === 0 && details.updated === 0) {
const topReason = pickTopSkipReason(details.skippedByReason);
if (topReason) {
lastAttempt = { at: lastIngest!.createdAt.toISOString(), topReason };
// Fail soft, deliberately. This lookup exists only to explain an empty
// page; it is not the page. If it throws — a ledger read that errors, a
// shape nobody expected — the nutrients read must still answer, with the
// plain empty state rather than a 500. An explanation that can take down
// the thing it explains is worse than no explanation.
try {
const lastIngest = await prisma.auditLog.findFirst({
where: { userId: user.id, action: "nutrient.batch.ingest" },
orderBy: { createdAt: "desc" },
select: { createdAt: true, details: true },
});
const details = parseLastIngestDetails(lastIngest?.details ?? null);
// Only surface a reason when the last attempt landed NOTHING: a
// succeeded attempt whose day just falls outside this window is a
// different, unrelated situation — showing a failure notice over it
// would be the very false claim this feature exists to prevent.
if (details && details.inserted === 0 && details.updated === 0) {
const topReason = pickTopSkipReason(details.skippedByReason);
if (topReason) {
lastAttempt = { at: lastIngest!.createdAt.toISOString(), topReason };
}
}
} catch (err) {
// Recorded, never rendered: the operator sees it, the person sees the
// ordinary empty state.
annotate({
action: { name: "nutrient.intake.last_attempt_lookup_failed" },
meta: { reason: err instanceof Error ? err.name : "unknown" },
});
}
}