Files
HealthLog/src/lib/insights/recovery-empty.ts
T
Marc-André Bombeck 6224409678 fix(oura): request the stress scope and exchange a repeated callback once
Resilience sits behind Oura's `stress` scope, not `heart_health`, so a
connection that granted every scope HealthLog asked for still answered 401
on `daily_resilience`. `src/lib/oura/scope.ts` now maps every collection the
sync reads to the scope the live API enforces and is the one source for the
authorize request, the per-collection permission detection, the reconnect
hint and the settings card. The request asks for `stress` as well.

Oura names a missing scope in its 401 body ("Token is not authorized access
stress scope."), while a dead token reads differently. A collection refusal
now carries that scope: it spends no token refresh, never counts toward the
reconnect threshold and marks only the collections it names. A 403 (a lapsed
membership) is a persistent failure rather than a reconnect. The callback
also records what the token response says was granted, so an unticked
permission shows on the card before the first sync.

A browser can deliver an OAuth callback twice with the same code. Both
requests exchanged it, and Oura answers a reused code by revoking the token
the first exchange issued. Every connect callback (Oura, Polar, Strava,
WHOOP, Withings, Fitbit, Google Health) now claims its state in
`oauth_callback_claims` before the exchange: one request exchanges, the
duplicate waits for it and redirects to the same page without touching any
token. The ledger flows claim inside the transaction that consumes their
state row. A failed exchange settles with its error page, and a new connect
mints a new state.

Oura token refreshes are serialised per user with an advisory lock, so
overlapping syncs no longer spend the same single-use refresh token, and the
write stays conditional so a reconnect's new grant is never overwritten by a
refresh of the old one.
2026-10-21 00:05:22 +02:00

62 lines
2.1 KiB
TypeScript

/**
* Why the recovery page is empty, decided once on the server.
*
* The page charts the device-native recovery signals below. When none of
* them has a reading, the analytics slice publishes the reason beside the
* summaries (`recoveryEmpty`), and every client renders from it rather than
* working it out from the connection status itself:
*
* - `oura_permission`: Oura is connected and the stored grant leaves out
* the scope its resilience level sits behind; one reconnect fixes it.
* - `oura_no_resilience`: Oura is connected and no level has arrived. The
* server cannot always tell "the grant covers it" from "not recorded yet"
* (the scope note is rewritten on every reconnect from the scopes Oura
* reports, when it reports them, and otherwise only by a sync that
* reached Oura), so this case never claims coverage.
* - `general`: anyone else.
*
* `null` means the page has something to chart.
*
* Pure, so the page can share the type list it charts from.
*/
/** Every summary type the recovery page charts or shows as a tile. */
export const RECOVERY_SUMMARY_TYPES = [
"ANS_CHARGE",
"DAY_STRAIN",
"WORKOUT_STRAIN",
"CARDIO_LOAD",
"AVERAGE_HEART_RATE",
"MAX_HEART_RATE",
"ENERGY_EXPENDITURE_KJ",
"RESILIENCE",
] as const;
export const RECOVERY_EMPTY_REASONS = [
"general",
"oura_permission",
"oura_no_resilience",
] as const;
export type RecoveryEmptyReason = (typeof RECOVERY_EMPTY_REASONS)[number];
/** True when any recovery type has a reading. */
export function hasRecoveryData(
summaries: Record<string, { count?: number } | null | undefined>,
): boolean {
return RECOVERY_SUMMARY_TYPES.some(
(type) => (summaries[type]?.count ?? 0) > 0,
);
}
export function resolveRecoveryEmpty(
summaries: Record<string, { count?: number } | null | undefined>,
oura: { connected: boolean; scopeDenied: readonly string[] },
): RecoveryEmptyReason | null {
if (hasRecoveryData(summaries)) return null;
if (!oura.connected) return "general";
return oura.scopeDenied.includes("resilience")
? "oura_permission"
: "oura_no_resilience";
}