mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
feat(integrations): resolve one health state per connection
Fold the ledger state, liveness verdict, plan and scope gaps and the provider reauth flag into ok / limited / degraded / action_required with a reason code, the one action, the unavailable types and redacted diagnostics. Keep the last ten sync attempts per connection for those diagnostics, publish the state on the integrations envelope, every per-provider status route and the Apple Health config read, and page the operator once per failure streak instead of once a day.
This commit is contained in:
1 parent
cdd1e9c87f
commit
d10179f9a3
17 files changed
+1351
-27
No files matched your search
@@ -2,6 +2,7 @@ import { prisma } from "@/lib/db";
|
||||
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { apiSuccess } from "@/lib/api-response";
|
||||
import { loadOneConnectionHealth } from "@/lib/integrations/connection-health-load";
|
||||
|
||||
/**
|
||||
* Get Fitbit / Google Health connection status for the current user (v1.12.0).
|
||||
@@ -53,5 +54,9 @@ export const GET = apiHandler(async () => {
|
||||
tokenExpiresAt: connection.tokenExpiresAt,
|
||||
backfillCompleted: !!connection.backfillCompletedAt,
|
||||
scope: connection.scope,
|
||||
// The one server-resolved state (ok / limited / degraded /
|
||||
// action_required) every client renders. The fields above stay for
|
||||
// clients that still read them; see `ConnectionHealth` in the API docs.
|
||||
health: await loadOneConnectionHealth(user.id, "fitbit"),
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import { prisma } from "@/lib/db";
|
||||
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { apiSuccess } from "@/lib/api-response";
|
||||
import { loadOneConnectionHealth } from "@/lib/integrations/connection-health-load";
|
||||
|
||||
/**
|
||||
* Get Google Health connection status for the current user (v1.26.0).
|
||||
@@ -58,5 +59,9 @@ export const GET = apiHandler(async () => {
|
||||
backfillCompleted: !!connection.backfillCompletedAt,
|
||||
needsReauth: connection.needsReauth,
|
||||
scope: connection.scope,
|
||||
// The one server-resolved state (ok / limited / degraded /
|
||||
// action_required) every client renders. The fields above stay for
|
||||
// clients that still read them; see `ConnectionHealth` in the API docs.
|
||||
health: await loadOneConnectionHealth(user.id, "google-health"),
|
||||
});
|
||||
});
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
withHealthKitArrivals,
|
||||
} from "@/lib/integrations/healthkit-type-sync";
|
||||
import { getSourceFreshness } from "@/lib/integrations/metric-freshness";
|
||||
import { loadOneConnectionHealth } from "@/lib/integrations/connection-health-load";
|
||||
import {
|
||||
classifyMetricFreshness,
|
||||
PUSH_CADENCE,
|
||||
@@ -173,10 +174,14 @@ export const GET = apiHandler(async () => {
|
||||
// progress; the phone's queue and throttle state stay on the phone and are
|
||||
// not guessed at. Fail-soft for the same reason the freshness read is —
|
||||
// a progress line is never worth 500-ing the config the iOS client needs.
|
||||
const [samples, arrivals, syncProgress] = await Promise.all([
|
||||
// `health` is the unified connection state; fail-soft to null for the same
|
||||
// reason as the reads beside it, so the config the phone needs never 500s
|
||||
// over a diagnostic.
|
||||
const [samples, arrivals, syncProgress, health] = await Promise.all([
|
||||
getSourceFreshness(user.id, "APPLE_HEALTH").catch(() => []),
|
||||
getHealthKitTypeArrivals(user.id).catch(() => null),
|
||||
getAppleHealthSyncProgress(user.id).catch(() => null),
|
||||
loadOneConnectionHealth(user.id, "apple-health").catch(() => null),
|
||||
]);
|
||||
// No `connected` hint: Apple Health has nothing to connect or disconnect, so
|
||||
// "never delivered" resolves to `pending_first_sync`, not `disconnected`.
|
||||
@@ -202,6 +207,7 @@ export const GET = apiHandler(async () => {
|
||||
arrivals,
|
||||
),
|
||||
syncProgress,
|
||||
health,
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -51,6 +51,8 @@ import {
|
||||
import { getPolarClientCredentials } from "@/lib/polar/credentials";
|
||||
import { getStravaClientCredentials } from "@/lib/strava/credentials";
|
||||
import { hasActivityScope } from "@/lib/withings/client";
|
||||
import { loadConnectionHealth } from "@/lib/integrations/connection-health-load";
|
||||
import type { ConnectionHealth } from "@/lib/integrations/connection-health";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
@@ -159,6 +161,20 @@ export const GET = apiHandler(async () => {
|
||||
getStravaClientCredentials(user.id).then((c) => !!c),
|
||||
]);
|
||||
|
||||
// The unified per-connection state. Resolved by the same loader the
|
||||
// per-provider routes and the integration watchdog use, so the envelope and
|
||||
// every other surface say the same thing about a connection.
|
||||
const health = await loadConnectionHealth(user.id, [
|
||||
"withings",
|
||||
"whoop",
|
||||
"fitbit",
|
||||
"google-health",
|
||||
"polar",
|
||||
"oura",
|
||||
"strava",
|
||||
"nightscout",
|
||||
]);
|
||||
|
||||
// F-SYNC-1 — per-metric-type last-value timestamps so the card can show that a
|
||||
// single metric type has gone silent even while the integration reads green
|
||||
// ("connected · 5 min ago"). Fail-soft: this is an honesty signal, never worth
|
||||
@@ -301,6 +317,7 @@ export const GET = apiHandler(async () => {
|
||||
parseWithingsApiPlanState(withingsConn.apiPlanState),
|
||||
)
|
||||
: [],
|
||||
health: health["withings"],
|
||||
...resolve("withings", withingsStatus, {
|
||||
connected: !!withingsConn,
|
||||
configured: withingsConfigured,
|
||||
@@ -324,6 +341,7 @@ export const GET = apiHandler(async () => {
|
||||
backfillCompleted: whoopConnected
|
||||
? !!whoopConn?.backfillCompletedAt
|
||||
: null,
|
||||
health: health["whoop"],
|
||||
...resolve("whoop", whoopStatus, {
|
||||
connected: whoopConnected,
|
||||
configured: whoopConfigured,
|
||||
@@ -341,6 +359,7 @@ export const GET = apiHandler(async () => {
|
||||
? fitbitConn.tokenExpiresAt.getTime() <= now
|
||||
: null,
|
||||
backfillCompleted: fitbitConn ? !!fitbitConn.backfillCompletedAt : null,
|
||||
health: health["fitbit"],
|
||||
...resolve("fitbit", fitbitStatus, {
|
||||
connected: fitbitConnected,
|
||||
configured: fitbitConfigured,
|
||||
@@ -364,6 +383,7 @@ export const GET = apiHandler(async () => {
|
||||
// user-revoked grant) surfaced from the connection row; the card reads it
|
||||
// to raise a distinct "Reconnect" CTA separate from parked/disconnected.
|
||||
needsReauth: googleHealthConn ? googleHealthConn.needsReauth : false,
|
||||
health: health["google-health"],
|
||||
...resolve("google-health", googleHealthStatus, {
|
||||
connected: googleHealthConnected,
|
||||
configured: googleHealthConfigured,
|
||||
@@ -382,6 +402,7 @@ export const GET = apiHandler(async () => {
|
||||
hasOwnCredentials:
|
||||
!!dbUser?.polarClientIdEncrypted &&
|
||||
!!dbUser?.polarClientSecretEncrypted,
|
||||
health: health["polar"],
|
||||
...resolve("polar", polarStatus, {
|
||||
connected: polarConnected,
|
||||
configured: polarConnected,
|
||||
@@ -400,6 +421,7 @@ export const GET = apiHandler(async () => {
|
||||
scopeReconnect: ouraConnected
|
||||
? ouraScopeReconnectList(readOuraScopeDenied(dbUser?.ouraScopeDenied))
|
||||
: [],
|
||||
health: health["oura"],
|
||||
...resolve("oura", ouraStatus, {
|
||||
connected: ouraConnected,
|
||||
configured: ouraConnected,
|
||||
@@ -416,6 +438,7 @@ export const GET = apiHandler(async () => {
|
||||
!!dbUser?.stravaClientSecretEncrypted,
|
||||
// Strava writes Workout rows, not Measurements — its freshness list
|
||||
// carries the `WORKOUTS` pseudo-entry alone.
|
||||
health: health["strava"],
|
||||
...resolve("strava", stravaStatus, {
|
||||
connected: stravaConnected,
|
||||
configured: stravaConnected,
|
||||
@@ -431,6 +454,7 @@ export const GET = apiHandler(async () => {
|
||||
configured: nightscoutConfigured,
|
||||
hasToken: !!dbUser?.nightscoutTokenEncrypted,
|
||||
allowPrivateHost: dbUser?.nightscoutAllowPrivateHost ?? false,
|
||||
health: health["nightscout"],
|
||||
...resolve("nightscout", nightscoutStatus, {
|
||||
connected: nightscoutConfigured,
|
||||
configured: nightscoutConfigured,
|
||||
@@ -442,6 +466,16 @@ export const GET = apiHandler(async () => {
|
||||
|
||||
interface IntegrationViewModel {
|
||||
integration: IntegrationKey;
|
||||
/**
|
||||
* The one server-resolved state per connection (ok / limited / degraded /
|
||||
* action_required), null when the provider is not connected. Every other
|
||||
* health-shaped field on this entry (`state`, `lastError`,
|
||||
* `consecutiveFailuresByKind`, `syncHealth`, `tokenExpired`, `needsReauth`,
|
||||
* `hasActivityScope`, `planUnavailable`, `scopeReconnect`) is folded into
|
||||
* it and goes once the settings cards render `health`; no other client
|
||||
* reads this envelope.
|
||||
*/
|
||||
health: ConnectionHealth | null;
|
||||
state: string;
|
||||
lastSuccessAt: string | null;
|
||||
lastAttemptAt: string | null;
|
||||
|
||||
@@ -2,6 +2,7 @@ import { prisma } from "@/lib/db";
|
||||
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { apiSuccess } from "@/lib/api-response";
|
||||
import { loadOneConnectionHealth } from "@/lib/integrations/connection-health-load";
|
||||
import { getIntegrationStatus } from "@/lib/integrations/status";
|
||||
import {
|
||||
INTEGRATION_CADENCE,
|
||||
@@ -62,5 +63,9 @@ export const GET = apiHandler(async () => {
|
||||
failingSinceAt: status.failingSince,
|
||||
cadence: INTEGRATION_CADENCE["nightscout"],
|
||||
}),
|
||||
// The one server-resolved state (ok / limited / degraded /
|
||||
// action_required) every client renders. The fields above stay for
|
||||
// clients that still read them; see `ConnectionHealth` in the API docs.
|
||||
health: await loadOneConnectionHealth(user.id, "nightscout"),
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import { prisma } from "@/lib/db";
|
||||
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { apiSuccess } from "@/lib/api-response";
|
||||
import { loadOneConnectionHealth } from "@/lib/integrations/connection-health-load";
|
||||
import { getOuraClientCredentials } from "@/lib/oura/credentials";
|
||||
import { getIntegrationStatus } from "@/lib/integrations/status";
|
||||
import {
|
||||
@@ -70,5 +71,9 @@ export const GET = apiHandler(async () => {
|
||||
failingSinceAt: status.failingSince,
|
||||
cadence: INTEGRATION_CADENCE["oura"],
|
||||
}),
|
||||
// The one server-resolved state (ok / limited / degraded /
|
||||
// action_required) every client renders. The fields above stay for
|
||||
// clients that still read them; see `ConnectionHealth` in the API docs.
|
||||
health: await loadOneConnectionHealth(user.id, "oura"),
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import { prisma } from "@/lib/db";
|
||||
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { apiSuccess } from "@/lib/api-response";
|
||||
import { loadOneConnectionHealth } from "@/lib/integrations/connection-health-load";
|
||||
import { getPolarClientCredentials } from "@/lib/polar/credentials";
|
||||
import { getIntegrationStatus } from "@/lib/integrations/status";
|
||||
import {
|
||||
@@ -71,5 +72,9 @@ export const GET = apiHandler(async () => {
|
||||
failingSinceAt: status.failingSince,
|
||||
cadence: INTEGRATION_CADENCE["polar"],
|
||||
}),
|
||||
// The one server-resolved state (ok / limited / degraded /
|
||||
// action_required) every client renders. The fields above stay for
|
||||
// clients that still read them; see `ConnectionHealth` in the API docs.
|
||||
health: await loadOneConnectionHealth(user.id, "polar"),
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import { prisma } from "@/lib/db";
|
||||
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { apiSuccess } from "@/lib/api-response";
|
||||
import { loadOneConnectionHealth } from "@/lib/integrations/connection-health-load";
|
||||
import { getStravaClientCredentials } from "@/lib/strava/credentials";
|
||||
import { getIntegrationStatus } from "@/lib/integrations/status";
|
||||
import {
|
||||
@@ -70,5 +71,9 @@ export const GET = apiHandler(async () => {
|
||||
failingSinceAt: status.failingSince,
|
||||
cadence: INTEGRATION_CADENCE["strava"],
|
||||
}),
|
||||
// The one server-resolved state (ok / limited / degraded /
|
||||
// action_required) every client renders. The fields above stay for
|
||||
// clients that still read them; see `ConnectionHealth` in the API docs.
|
||||
health: await loadOneConnectionHealth(user.id, "strava"),
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import { prisma } from "@/lib/db";
|
||||
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { apiSuccess } from "@/lib/api-response";
|
||||
import { loadOneConnectionHealth } from "@/lib/integrations/connection-health-load";
|
||||
|
||||
/**
|
||||
* Get WHOOP connection status for the current user (v1.11.0).
|
||||
@@ -54,5 +55,9 @@ export const GET = apiHandler(async () => {
|
||||
tokenExpiresAt: connection.tokenExpiresAt,
|
||||
backfillCompleted: !!connection.backfillCompletedAt,
|
||||
scope: connection.scope,
|
||||
// The one server-resolved state (ok / limited / degraded /
|
||||
// action_required) every client renders. The fields above stay for
|
||||
// clients that still read them; see `ConnectionHealth` in the API docs.
|
||||
health: await loadOneConnectionHealth(user.id, "whoop"),
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import { prisma } from "@/lib/db";
|
||||
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
||||
import { annotate, getEvent } from "@/lib/logging/context";
|
||||
import { apiSuccess } from "@/lib/api-response";
|
||||
import { loadOneConnectionHealth } from "@/lib/integrations/connection-health-load";
|
||||
import { decrypt, encrypt } from "@/lib/crypto";
|
||||
import { getUserWithingsCredentials } from "@/lib/withings/credentials";
|
||||
import { hasActivityScope, refreshAccessToken } from "@/lib/withings/client";
|
||||
@@ -118,5 +119,9 @@ export const GET = apiHandler(async () => {
|
||||
// never re-authed since v1.4.25 — it counts as "missing activity".
|
||||
scope: connection.scope,
|
||||
hasActivityScope: hasActivityScope(connection.scope),
|
||||
// The one server-resolved state (ok / limited / degraded /
|
||||
// action_required) every client renders. The fields above stay for
|
||||
// clients that still read them; see `ConnectionHealth` in the API docs.
|
||||
health: await loadOneConnectionHealth(user.id, "withings"),
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user