Files
HealthLog/src/app/api/polar/sync/route.ts
T
Marc-André Bombeck 530f78dcf5 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.
2026-07-27 22:53:43 +02:00

43 lines
1.7 KiB
TypeScript

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 { 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).
*
* Calls `syncUserPolarLegs` — the same entry point the hourly poll uses — so
* both Polar legs run with the attribution rules that were built for them
* (vitals then workouts, independent failure recording, one success stamp).
* Reaching past it into `@/lib/polar/*` would run half the provider and get the
* ledger wrong. The module graph behind this import bottoms out in the shared
* Prisma client and constants, so it is safe on the request path.
*
* No body and no `fullSync` flag: there is no full-history arm to call.
*
* Rate-limited 5/60s per user, matching the hardened Fitbit / Google Health
* routes.
*/
export const POST = apiHandler(async () => {
const { user } = await requireAuth();
annotate({ action: { name: "polar.sync" } });
const rl = await checkRateLimit(`polar-sync:${user.id}`, 5, 60_000);
if (!rl.allowed) {
return apiError("Too many sync requests", 429, {
errorCode: "rate_limited_self",
});
}
try {
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.
return apiError("Polar sync failed", 502);
}
});