feat(whoop): pg-boss sync workers + self-converging backfill

Build the WHOOP sync layer on the W2 client, mirroring the Withings
queues. A per-resource sync (recovery/sleep/cycle/workout) pulls since
the last cursor with a 24h overlap on recovery/sleep to absorb WHOOP's
after-the-fact re-scoring, maps via the W2 mappers, and upserts
Measurements with source=WHOOP keyed on (userId,type,source,externalId)
so a re-post overwrites in place. Workouts upsert into Workout with
per-workout strain + HR zones in metadata and kJ->kcal energy.

Token refresh persists BOTH rotated tokens (WHOOP invalidates the prior
access and refresh token on every refresh). A self-converging boot
backfill enqueues one full-history sync per un-backfilled connection and
stamps backfillCompletedAt; the discovery query drops completed accounts,
idempotent across reboots.

Every new queue is registered in the worker's allQueues + cron schedule
(recovery/sleep/workout poll catch-all, cycle poll-only, backfill,
oauth-state sweep). IntegrationStatus gains the whoop key + label.

Tests: rotating-refresh persistence, idempotent measurement upsert,
incremental-window overlap, backfill self-convergence, and a
queue-registration inventory guard.
This commit is contained in:
Marc-André Bombeck committed 2026-06-04 01:21:42 +02:00
1 parent 6aa3c75d27
commit 337f024a87
12 files changed
+1485 -2

No files matched your search

+6 -2
View File
@@ -39,7 +39,7 @@ import { auditLog } from "@/lib/auth/audit";
import { getEvent } from "@/lib/logging/context";
import { dispatchNotification } from "@/lib/notifications/dispatcher";
export type IntegrationKey = "withings" | "moodlog";
export type IntegrationKey = "withings" | "whoop" | "moodlog";
/**
* Failure kinds carried into `recordSyncFailure`.
@@ -820,7 +820,11 @@ export function formatAdminAlertPayload(input: AlertInput): {
metadata: Record<string, unknown>;
} {
const integrationLabel =
input.integration === "withings" ? "Withings" : "moodLog";
input.integration === "withings"
? "Withings"
: input.integration === "whoop"
? "WHOOP"
: "moodLog";
const subjectLabel = input.subjectLabel ?? input.userId;
const { reason: reasonLabel, action: actionLabel } =
FAILURE_KIND_COPY[input.kind];