fix(withings): keep the sync inside the account's API plan

Withings split its public API into plans on 2026-10-12. A request naming
one meastype outside the free plan is refused whole with status 277, so
every measure sync stopped importing anything, and the 200..299 reauth
range parked the connection at error_reauth.

The measure, sleep-summary and ECG legs now drop the documented advanced
types on a 277, split the rest until the refused keys are isolated
(bounded), remember the refusal per connection and re-probe weekly. 277
is classified persistent and never enters the ledger; connections parked
over it are lifted by the fallback tick, and the held watermark lets the
next run read the whole gap.
This commit is contained in:
Marc-André Bombeck committed 2026-10-14 18:53:58 +02:00
1 parent bc7f3bf209
commit 39b0cabb43
12 files changed
+722 -13

No files matched your search

@@ -0,0 +1,18 @@
-- v1.42.3 — remember what an integration's upstream refuses by contract.
--
-- Withings split its public API into plans on 2026-10-12. A self-hoster's own
-- developer app sits on the free plan, and a request that names a meastype
-- outside that plan is refused whole with status 277. The sync now drops the
-- gated types and keeps the rest; this column remembers which ones per
-- connection so later runs skip them without a refused round trip, plus when
-- that was last checked so a plan upgrade is noticed within a week.
--
-- Oura's current authorization server enforces scopes per collection. A grant
-- issued before HealthLog asked for `spo2` and `heart_health` answers 401 on
-- the collections behind them while every other collection succeeds. This
-- column holds those collection names so the Oura card can ask for a single
-- reconnect instead of painting the connection as broken.
--
-- Additive only: two nullable columns, no default, no rewrite, no data change.
ALTER TABLE "withings_connections" ADD COLUMN IF NOT EXISTS "api_plan_state" JSONB;
ALTER TABLE "users" ADD COLUMN IF NOT EXISTS "oura_scope_denied" JSONB;
+14
View File
@@ -264,6 +264,13 @@ model User {
// `src/lib/crypto.ts`, matching every other `*Encrypted` column.
ouraClientIdEncrypted String? @map("oura_client_id_encrypted")
ouraClientSecretEncrypted String? @map("oura_client_secret_encrypted")
/// v1.42.3 — Oura collections the current grant does not cover, as a JSON
/// string array of collection names (`spo2`, `vo2max`, `cardio_age`,
/// `resilience`). Written by the sync when a collection answers 401/403 while
/// its siblings succeed with the same token, which is a scope the grant never
/// carried, not a dead token. The Oura card reads it to ask for one
/// reconnect. Cleared on connect, disconnect and credential removal.
ouraScopeDenied Json? @map("oura_scope_denied")
// v1.28.x — Strava OAuth credentials (per-user BYO-app, encrypted at app
// level). Strava caps every newly-created API app at single-player mode
@@ -4149,6 +4156,13 @@ model WithingsConnection {
/// earliest transient/pending category retry for indexed hourly discovery.
webhookSubscriptionState Json? @map("webhook_subscription_state")
webhookSubscriptionRetryAt DateTime? @map("webhook_subscription_retry_at")
/// v1.42.3 — what this connection's Withings API plan refuses (status 277),
/// per request family: `{ measures, sleepSummary, ecg }`, each
/// `{ unavailable, checkedAt }`. Lets the sync skip gated meastypes without
/// paying a refused round trip each time, re-probe them weekly, and lets the
/// Withings card name what the plan withholds. Versioned and validated on
/// read (`src/lib/withings/api-plan.ts`); reset on reconnect.
apiPlanState Json? @map("api_plan_state")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")