fix(sync): answer with what a run wrote, not that it completed

Six provider syncs returned a bare count, which narrowed their failures away:
a Nightscout window in which every SGV row was refused by a constraint
returned the same 0 as a quiet night, and an Oura or Polar run whose
collections all failed returned 0 too. The routes then answered 200 with a
count and the settings card drew a green tick over it.

Every sync now reports what it imported AND whether any part of it did not
land, and the route resolves the outcome server-side so the web card and the
native client cannot disagree. Google Health keeps its 502 for a run that
failed and wrote nothing, but a run that failed after writing some resources
is now a partial rather than a discarded result. The labs OCR commit carries
the same resolved outcome.
This commit is contained in:
Marc-André Bombeck committed 2026-07-27 22:53:43 +02:00
1 parent 52288b7f77
commit 530f78dcf5
29 files changed
+310 -112

No files matched your search

+6 -2
View File
@@ -3,6 +3,7 @@ import { apiSuccess, apiError } from "@/lib/api-response";
import { checkRateLimit } from "@/lib/rate-limit";
import { annotate } from "@/lib/logging/context";
import { syncUserFitbit } from "@/lib/fitbit/sync";
import { resolveSyncOutcome } from "@/lib/outcome/written-outcome";
import { NextRequest } from "next/server";
/**
@@ -58,6 +59,9 @@ export const POST = apiHandler(async (request: NextRequest) => {
}
}
const { imported } = await syncUserFitbit(user.id, { fullSync });
return apiSuccess({ imported, fullSync });
// The verdict is resolved here, not in the card: a run that imported nothing
// because every resource failed is not the same answer as a quiet hour, and
// both used to render as a green tick with a count.
const result = await syncUserFitbit(user.id, { fullSync });
return apiSuccess({ ...resolveSyncOutcome(result), fullSync });
});
@@ -56,6 +56,28 @@ describe("POST /api/google-health/sync", () => {
expect(envelope.error).toBe("Google Health sync failed");
});
it("reports a run that failed after writing some resources as a partial", async () => {
// A 502 threw the honest half away: rows DID land, and the card showed the
// generic error instead of "imported N, some data is behind".
vi.mocked(syncUserGoogleHealth).mockResolvedValue({
imported: 7,
failed: true,
});
const response = await POST(request());
const envelope = (await response.json()) as {
data: { imported: number; failed: boolean; outcome: string };
error: string | null;
};
expect(response.status).toBe(200);
expect(envelope.data).toMatchObject({
imported: 7,
failed: true,
outcome: "partial",
});
});
it("keeps the successful imported-count response unchanged", async () => {
vi.mocked(syncUserGoogleHealth).mockResolvedValue({
imported: 7,
@@ -70,7 +92,7 @@ describe("POST /api/google-health/sync", () => {
expect(response.status).toBe(200);
expect(envelope).toEqual({
data: { imported: 7, fullSync: false },
data: { imported: 7, failed: false, outcome: "success", fullSync: false },
error: null,
});
expect(syncUserGoogleHealth).toHaveBeenCalledWith("u1", {
+7 -5
View File
@@ -3,6 +3,7 @@ import { apiSuccess, apiError } from "@/lib/api-response";
import { checkRateLimit } from "@/lib/rate-limit";
import { annotate } from "@/lib/logging/context";
import { syncUserGoogleHealth } from "@/lib/google-health/sync";
import { resolveSyncOutcome } from "@/lib/outcome/written-outcome";
import { NextRequest } from "next/server";
/**
@@ -55,11 +56,12 @@ export const POST = apiHandler(async (request: NextRequest) => {
}
}
const { imported, failed } = await syncUserGoogleHealth(user.id, {
fullSync,
});
if (failed) {
const result = await syncUserGoogleHealth(user.id, { fullSync });
// A run that failed AND wrote nothing is an error, as it always was. A run
// that failed after writing some of its resources is a partial, and answering
// 502 for it threw away the honest half of the result.
if (result.failed && result.imported === 0) {
return apiError("Google Health sync failed", 502);
}
return apiSuccess({ imported, fullSync });
return apiSuccess({ ...resolveSyncOutcome(result), fullSync });
});
+14 -1
View File
@@ -9,6 +9,11 @@
*
* `userId` is always narrowed from the session — never a body field. The write
* `data` object is built field-by-field (no mass assignment).
*
* The response carries the resolved `outcome` alongside the two lists. The
* dialog used to raise a green toast on any 200, so a re-scan in which every
* confirmed row turned out to be a duplicate reported "saved 0 readings" under
* a tick. The verdict is the server's to compute; the dialog only renders it.
*/
import { NextRequest } from "next/server";
import { fireAndForget } from "@/lib/logging/fire-and-forget";
@@ -33,6 +38,7 @@ import {
type InsertedLabForLink,
} from "@/lib/labs/vault-link";
import { annotate } from "@/lib/logging/context";
import { classifyWrittenOutcome } from "@/lib/outcome/written-outcome";
import {
ocrCommitSchema,
type OcrCommitRow,
@@ -235,5 +241,12 @@ async function commitOcrRows(request: NextRequest) {
}
}
return apiSuccess({ inserted, skipped });
return apiSuccess({
inserted,
skipped,
outcome: classifyWrittenOutcome({
written: inserted.length,
skipped: skipped.length,
}),
});
}
@@ -57,18 +57,65 @@ beforeEach(() => {
describe("POST /api/nightscout/sync", () => {
it("syncs the session user and answers with the freshly inserted count", async () => {
vi.mocked(syncUserNightscout).mockResolvedValue(36);
vi.mocked(syncUserNightscout).mockResolvedValue({
imported: 36,
failed: false,
});
const response = await post(request());
expect(response.status).toBe(200);
expect(await envelope(response)).toEqual({
data: { imported: 36 },
data: { imported: 36, failed: false, outcome: "success" },
error: null,
});
expect(syncUserNightscout).toHaveBeenCalledWith("u1");
});
it("does not call a window whose every row was refused a success", async () => {
// The defect: 200 + `imported: 0` rendered as a green tick with a count,
// when in fact not one SGV row reached the database.
vi.mocked(syncUserNightscout).mockResolvedValue({
imported: 0,
failed: true,
});
const response = await post(request());
expect(response.status).toBe(200);
expect((await envelope(response)).data).toEqual({
imported: 0,
failed: true,
outcome: "failed",
});
});
it("calls a window that wrote some rows and lost others a partial", async () => {
vi.mocked(syncUserNightscout).mockResolvedValue({
imported: 30,
failed: true,
});
const response = await post(request());
expect((await envelope(response)).data).toMatchObject({
outcome: "partial",
});
});
it("calls a window that found nothing new empty, not a success", async () => {
vi.mocked(syncUserNightscout).mockResolvedValue({
imported: 0,
failed: false,
});
const response = await post(request());
expect((await envelope(response)).data).toMatchObject({
outcome: "empty",
});
});
it("refuses an unauthenticated caller before syncing anything", async () => {
vi.mocked(requireAuth).mockRejectedValue(
new HttpError(401, "Not authenticated"),
+6 -3
View File
@@ -3,12 +3,15 @@ import { apiSuccess, apiError } from "@/lib/api-response";
import { checkRateLimit } from "@/lib/rate-limit";
import { annotate } from "@/lib/logging/context";
import { syncUserNightscout } from "@/lib/nightscout/sync";
import { resolveSyncOutcome } from "@/lib/outcome/written-outcome";
/**
* Manually trigger a Nightscout sync for the current user (v1.32.28).
*
* Pulls the recent SGV window from the configured instance; `imported` counts
* the readings that were genuinely new this run, not the ones re-confirmed.
* the readings that were genuinely new this run, not the ones re-confirmed,
* and `outcome` says how that count reads — a window in which every row was
* refused by a constraint imports 0 and must not answer like a quiet night.
*
* No body and no `fullSync` flag: there is no full-history arm to call.
*
@@ -34,8 +37,8 @@ export const POST = apiHandler(async () => {
}
try {
const imported = await syncUserNightscout(user.id);
return apiSuccess({ imported });
const result = await syncUserNightscout(user.id);
return apiSuccess(resolveSyncOutcome(result));
} catch {
return apiError("Nightscout sync failed", 502);
}
+13 -2
View File
@@ -53,19 +53,30 @@ beforeEach(() => {
describe("POST /api/oura/sync", () => {
it("syncs the session user and answers with the imported count", async () => {
vi.mocked(syncUserOura).mockResolvedValue(12);
vi.mocked(syncUserOura).mockResolvedValue({ imported: 12, failed: false });
const response = await post(request());
expect(response.status).toBe(200);
expect(await envelope(response)).toEqual({
data: { imported: 12 },
data: { imported: 12, failed: false, outcome: "success" },
error: null,
});
// The id comes from the session, never from the request.
expect(syncUserOura).toHaveBeenCalledWith("u1");
});
it("does not call a run whose every collection failed a success", async () => {
vi.mocked(syncUserOura).mockResolvedValue({ imported: 0, failed: true });
const response = await post(request());
expect(response.status).toBe(200);
expect((await envelope(response)).data).toMatchObject({
outcome: "failed",
});
});
it("refuses an unauthenticated caller before syncing anything", async () => {
vi.mocked(requireAuth).mockRejectedValue(
new HttpError(401, "Not authenticated"),
+3 -2
View File
@@ -2,6 +2,7 @@ import { apiHandler, requireAuth } from "@/lib/api-handler";
import { apiSuccess, apiError } from "@/lib/api-response";
import { checkRateLimit } from "@/lib/rate-limit";
import { annotate } from "@/lib/logging/context";
import { resolveSyncOutcome } from "@/lib/outcome/written-outcome";
import { syncUserOura } from "@/lib/oura/sync";
/**
@@ -31,8 +32,8 @@ export const POST = apiHandler(async () => {
}
try {
const imported = await syncUserOura(user.id);
return apiSuccess({ imported });
const result = await syncUserOura(user.id);
return apiSuccess(resolveSyncOutcome(result));
} catch {
// The failure is already on the `oura` ledger, classified by the provider
// that owns the error shape. Answer generically — an upstream message can
+17 -3
View File
@@ -67,26 +67,40 @@ beforeEach(() => {
vi.clearAllMocks();
vi.mocked(checkRateLimit).mockResolvedValue({ allowed: true } as never);
vi.mocked(requireAuth).mockResolvedValue({ user: { id: "u1" } } as never);
syncUserPolarMock.mockResolvedValue(0);
syncUserPolarMock.mockResolvedValue({ imported: 0, failed: false });
syncUserPolarWorkoutsMock.mockResolvedValue(0);
});
describe("POST /api/polar/sync", () => {
it("runs both Polar legs and totals what they imported", async () => {
syncUserPolarMock.mockResolvedValue(5);
syncUserPolarMock.mockResolvedValue({ imported: 5, failed: false });
syncUserPolarWorkoutsMock.mockResolvedValue(2);
const response = await post(request());
expect(response.status).toBe(200);
expect(await envelope(response)).toEqual({
data: { imported: 7 },
data: { imported: 7, failed: false, outcome: "success" },
error: null,
});
expect(syncUserPolarMock).toHaveBeenCalledWith("u1");
expect(syncUserPolarWorkoutsMock).toHaveBeenCalledWith("u1");
});
it("does not call a run whose vitals collections all failed a success", async () => {
syncUserPolarMock.mockResolvedValue({ imported: 0, failed: true });
syncUserPolarWorkoutsMock.mockResolvedValue(0);
const response = await post(request());
expect(response.status).toBe(200);
expect((await envelope(response)).data).toEqual({
imported: 0,
failed: true,
outcome: "failed",
});
});
it("goes through the shared legs entry point, not the provider internals", () => {
const source = readFileSync(join(__dirname, "..", "route.ts"), "utf8");
expect(source).toContain("syncUserPolarLegs");
+3 -2
View File
@@ -3,6 +3,7 @@ import { apiSuccess, apiError } from "@/lib/api-response";
import { checkRateLimit } from "@/lib/rate-limit";
import { annotate } from "@/lib/logging/context";
import { syncUserPolarLegs } from "@/lib/jobs/reminder/polar-sync";
import { resolveSyncOutcome } from "@/lib/outcome/written-outcome";
/**
* Manually trigger a Polar sync for the current user (v1.32.28).
@@ -31,8 +32,8 @@ export const POST = apiHandler(async () => {
}
try {
const imported = await syncUserPolarLegs(user.id);
return apiSuccess({ imported });
const result = await syncUserPolarLegs(user.id);
return apiSuccess(resolveSyncOutcome(result));
} catch {
// Both legs record their own classified failure on the `polar` ledger.
// Answer generically so no upstream detail rides the response body.
@@ -60,7 +60,7 @@ describe("POST /api/strava/sync", () => {
expect(response.status).toBe(200);
expect(await envelope(response)).toEqual({
data: { imported: 4 },
data: { imported: 4, failed: false, outcome: "success" },
error: null,
});
expect(syncUserStrava).toHaveBeenCalledWith("u1");
+6 -1
View File
@@ -2,6 +2,7 @@ import { apiHandler, requireAuth } from "@/lib/api-handler";
import { apiSuccess, apiError } from "@/lib/api-response";
import { checkRateLimit } from "@/lib/rate-limit";
import { annotate } from "@/lib/logging/context";
import { resolveSyncOutcome } from "@/lib/outcome/written-outcome";
import { syncUserStrava } from "@/lib/strava/sync";
/**
@@ -28,7 +29,11 @@ export const POST = apiHandler(async () => {
try {
const imported = await syncUserStrava(user.id);
return apiSuccess({ imported });
// `failed: false` is a statement, not a placeholder: the Strava walk has no
// settled-but-partial arm. `upsertStravaWorkouts` rethrows the first row
// error after attempting the batch and the page walk rethrows its own, so
// reaching this line means every fetched activity was written.
return apiSuccess(resolveSyncOutcome({ imported, failed: false }));
} catch {
// Already recorded on the `strava` ledger with its classification. Answer
// generically so no upstream detail rides the response body.
+5 -2
View File
@@ -1,6 +1,7 @@
import { apiHandler, requireAuth } from "@/lib/api-handler";
import { apiSuccess, apiError } from "@/lib/api-response";
import { annotate } from "@/lib/logging/context";
import { resolveSyncOutcome } from "@/lib/outcome/written-outcome";
import { syncUserWhoop } from "@/lib/whoop/sync";
import { NextRequest } from "next/server";
@@ -27,6 +28,8 @@ export const POST = apiHandler(async (request: NextRequest) => {
// no body provided -> default incremental sync
}
const { imported } = await syncUserWhoop(user.id, { fullSync });
return apiSuccess({ imported, fullSync });
// Resolved server-side so the card and the native client cannot disagree on
// how the same run reads.
const result = await syncUserWhoop(user.id, { fullSync });
return apiSuccess({ ...resolveSyncOutcome(result), fullSync });
});
+3 -4
View File
@@ -1,6 +1,7 @@
import { apiHandler, requireAuth } from "@/lib/api-handler";
import { apiSuccess, apiError } from "@/lib/api-response";
import { annotate } from "@/lib/logging/context";
import { resolveSyncOutcome } from "@/lib/outcome/written-outcome";
import { syncUserMeasurements } from "@/lib/withings/sync";
import { NextRequest } from "next/server";
@@ -24,8 +25,6 @@ export const POST = apiHandler(async (request: NextRequest) => {
// no body provided -> default incremental sync
}
const imported = await syncUserMeasurements(user.id, {
fullSync,
});
return apiSuccess({ imported, fullSync });
const result = await syncUserMeasurements(user.id, { fullSync });
return apiSuccess({ ...resolveSyncOutcome(result), fullSync });
});