feat(withings): OAuth scope upgrade + reconnect banner

v1.4.25 W5d. The Withings Activity / Sleep endpoints sit behind the
`user.activity` OAuth scope which v1.4.24-and-earlier connections
never requested. This commit ships the scope plumbing + reconnect
surface so existing users can opt in to step / active energy /
distance / floors-climbed ingest with a single click.

  - Schema: `WithingsConnection.scope` nullable column. NULL =
    legacy connection that's never re-authed since v1.4.25.
  - Migration 0050 — additive column add.
  - Client: `WITHINGS_OAUTH_SCOPE = "user.metrics,user.activity"`
    + `parseWithingsScope` / `hasActivityScope` helpers.
  - Callback: persists `scope: WITHINGS_OAUTH_SCOPE` on every
    successful exchange, so reconnecting upgrades the row.
  - Status route: exposes `scope` + `hasActivityScope` so the UI
    can render the reconnect banner conditionally.
  - Settings → Integrations: paint a reconnect banner when the
    user has an active connection AND `hasActivityScope === false`.
    Click → /api/withings/connect → standard OAuth flow.
  - i18n: EN + DE strings for the banner +
    `measurements.types.{fatFreeMass,fatMass,muscleMass,
    skinTemperature,pulseWaveVelocity,vascularAge,visceralFat}`.
  - 5 new unit tests covering the scope helpers.

Note: the Activity sync routine itself
(`POST /v2/measure?action=getactivity`) lands in v1.4.26. Shipping the
scope upgrade now means users only reconnect once instead of twice.

Cross-source dedup protection is already in place via the W5e
foundation (`pickCanonicalSourceRows`): once Activity sync lands AND
a user also has Apple Health passthrough (v1.5), the per-day picker
prevents double-counting cumulative metrics from two sources.
This commit is contained in:
Marc-André Bombeck committed 2026-05-14 14:13:58 +02:00
1 parent 6f04123982
commit 5a9612822e
9 files changed
+199 -5

No files matched your search

+35 -1
View File
@@ -1,5 +1,11 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { MEASURE_TYPE_MAP, fetchMeasurements } from "../client";
import {
MEASURE_TYPE_MAP,
WITHINGS_OAUTH_SCOPE,
fetchMeasurements,
hasActivityScope,
parseWithingsScope,
} from "../client";
import { WITHINGS_NOTIFY_APPLIS } from "../sync";
/**
@@ -103,6 +109,34 @@ describe("MEASURE_TYPE_MAP", () => {
});
});
describe("WITHINGS_OAUTH_SCOPE + scope helpers", () => {
it("requests user.metrics + user.activity (v1.4.25 default)", () => {
expect(WITHINGS_OAUTH_SCOPE).toBe("user.metrics,user.activity");
});
it("parses a comma-separated scope string into a Set", () => {
expect(parseWithingsScope("user.metrics,user.activity")).toEqual(
new Set(["user.metrics", "user.activity"]),
);
});
it("treats null/empty scope as empty set (legacy connection)", () => {
expect(parseWithingsScope(null).size).toBe(0);
expect(parseWithingsScope("").size).toBe(0);
});
it("hasActivityScope returns false for legacy v1.4.24 connections", () => {
expect(hasActivityScope(null)).toBe(false);
expect(hasActivityScope("user.metrics")).toBe(false);
});
it("hasActivityScope returns true once user.activity is granted", () => {
expect(hasActivityScope("user.metrics,user.activity")).toBe(true);
expect(hasActivityScope("user.activity")).toBe(true);
expect(hasActivityScope(" user.activity , user.metrics ")).toBe(true);
});
});
describe("WITHINGS_NOTIFY_APPLIS", () => {
it("subscribes to weight + temperature + pressure categories", () => {
expect(WITHINGS_NOTIFY_APPLIS).toEqual([1, 2, 4]);
+43 -1
View File
@@ -22,6 +22,48 @@ function getRedirectUri(): string {
);
}
/**
* v1.4.25 W5d — Withings OAuth scope set HealthLog requests.
*
* - `user.metrics` : every meastype the Measure / Heart-list /
* Sleep endpoints expose (weight, BP, pulse,
* SpO2, body comp, temperature, VO2 max, …)
* - `user.activity` : steps / active energy / distance / floors —
* served by `POST /v2/measure?action=getactivity`.
* The sync routine itself lands in v1.4.26; the
* scope is requested now so existing users
* reconnect once instead of twice.
*
* v1.4.24-and-earlier connections requested `user.metrics` only — the
* Settings → Integrations card surfaces a reconnect banner for those
* users (see `WithingsConnection.scope IS NULL` or scope without
* `user.activity`).
*/
export const WITHINGS_OAUTH_SCOPE = "user.metrics,user.activity" as const;
/**
* Parse the persisted scope string into a Set for membership checks.
* Legacy NULL → empty set (treated as "no scopes yet") so the
* reconnect-banner conditional reads as truthy.
*/
export function parseWithingsScope(scope: string | null): Set<string> {
if (!scope) return new Set();
return new Set(
scope
.split(",")
.map((s) => s.trim())
.filter(Boolean),
);
}
/**
* Returns true when the connection holds `user.activity` — the
* minimum scope required by the Activity / Sleep endpoints.
*/
export function hasActivityScope(scope: string | null): boolean {
return parseWithingsScope(scope).has("user.activity");
}
/**
* Generate Withings OAuth authorization URL.
*/
@@ -33,7 +75,7 @@ export function getAuthorizationUrl(
response_type: "code",
client_id: creds.clientId,
redirect_uri: getRedirectUri(),
scope: "user.metrics",
scope: WITHINGS_OAUTH_SCOPE,
state,
});
return `${WITHINGS_OAUTH_URL}?${params}`;