mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 07:13:29 +00:00
Harden consent receipts and document iOS series + ai-provider contract
Document GET /api/measurements/series and GET /api/user/ai-provider in the OpenAPI registry — both are consumed by the iOS client (charts and Coach visibility gating) but were absent from the spec. Break migration 0159's active-receipt dedup on (created_at, id) so an exact created_at tie still leaves one survivor, keeping the unique index build from aborting 23505 on affected tenants. Catch P2002 in createReceipt: two concurrent first grants both reach create, the loser re-reads the winning active receipt and returns it instead of a 500.
This commit is contained in:
1 parent
555240cd28
commit
32d275b4aa
6 files changed
+519
-17
No files matched your search
@@ -216,7 +216,112 @@ const sleepNightResponse = z
|
||||
"One reconstructed sleep night: the main session's hypnogram segments + breakdown, plus same-wake-day naps surfaced separately.",
|
||||
});
|
||||
|
||||
// ── Time-series adapter (iOS chart source) ───────────────────────────
|
||||
//
|
||||
// v1.16.16 — documents the long-shipped `GET /api/measurements/series`
|
||||
// the iOS charts read off. The response is described AS-IS: `points`
|
||||
// carry `id`/`at`/`value`/`secondary` (the route's wire field names),
|
||||
// not the canonical `measuredAt`. `secondary` pairs the diastolic value
|
||||
// for `bloodPressure`; `sleepStages` is per-night stage hours for `sleep`
|
||||
// and null otherwise. `unit` is the resolved per-kind token (glucose +
|
||||
// sleep resolve at request time to the user's display unit / hours).
|
||||
const seriesKindEnum = z.enum([
|
||||
"weight",
|
||||
"bloodPressure",
|
||||
"pulse",
|
||||
"bodyFat",
|
||||
"glucose",
|
||||
"sleep",
|
||||
"steps",
|
||||
"totalBodyWater",
|
||||
"boneMass",
|
||||
"oxygenSaturation",
|
||||
"restingHeartRate",
|
||||
"heartRateVariability",
|
||||
"vo2Max",
|
||||
]);
|
||||
|
||||
const seriesQuerySchema = z.object({
|
||||
kind: seriesKindEnum,
|
||||
days: z.coerce
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.max(3650)
|
||||
.optional()
|
||||
.default(30)
|
||||
.describe(
|
||||
"Look-back window in days (1..3650, default 30). `sleep` is internally capped to 365 regardless of the requested range.",
|
||||
),
|
||||
});
|
||||
|
||||
const seriesPointSchema = z.object({
|
||||
id: z.string().describe("Measurement row id, or `sleep:<wake-day>` for a sleep night."),
|
||||
at: z.iso.datetime({ offset: true }).describe("Point timestamp (ISO-8601)."),
|
||||
value: z.number().describe("Primary value in the top-level `unit`."),
|
||||
secondary: z
|
||||
.number()
|
||||
.nullable()
|
||||
.describe(
|
||||
"Diastolic value for `kind=bloodPressure` (paired within ±5 min); null for every other kind.",
|
||||
),
|
||||
sleepStages: z
|
||||
.record(z.string(), z.number())
|
||||
.nullable()
|
||||
.optional()
|
||||
.describe(
|
||||
"Per-stage hours for a `kind=sleep` night (CORE/DEEP/REM/…); null/absent for non-sleep kinds.",
|
||||
),
|
||||
});
|
||||
|
||||
const seriesResponse = z
|
||||
.object({
|
||||
kind: seriesKindEnum,
|
||||
unit: z
|
||||
.string()
|
||||
.describe(
|
||||
"Resolved unit token for `value`. `glucose` follows the user's mg/dL|mmol/L preference; `sleep` is `h` (per-night time-asleep in hours).",
|
||||
),
|
||||
points: z.array(seriesPointSchema),
|
||||
stats: z
|
||||
.object({
|
||||
mean: z.number(),
|
||||
min: z.number(),
|
||||
max: z.number(),
|
||||
stdDev: z.number(),
|
||||
count: z.number().int().nonnegative(),
|
||||
})
|
||||
.describe("Summary over the returned points; all-zero when empty."),
|
||||
})
|
||||
.meta({
|
||||
id: "MeasurementsSeriesResponse",
|
||||
description:
|
||||
"iOS-friendly per-kind time series: one point per reading (or per reconstructed night for `sleep`), an explicit `unit` token, and a summary `stats` block.",
|
||||
});
|
||||
|
||||
export const measurementPaths: NonNullable<ZodOpenApiObject["paths"]> = {
|
||||
"/api/measurements/series": {
|
||||
get: {
|
||||
tags: ["Measurements"],
|
||||
summary: "Per-kind time series (iOS chart source)",
|
||||
description:
|
||||
"Maps a camelCase `kind` to the canonical MeasurementType(s) and returns an ordered point series with an explicit `unit` token and a summary `stats` block. `bloodPressure` pairs systolic + diastolic (`secondary`) within ±5 min so one fetch renders the dual-line chart; `sleep` collapses per-stage rows into one per-night point carrying time-asleep in hours (`sleepStages` holds the per-stage breakdown) and is internally capped to 365 days. `glucose` values + `unit` resolve to the user's mg/dL|mmol/L preference. Auth via cookie or Bearer.",
|
||||
requestParams: {
|
||||
query: seriesQuerySchema,
|
||||
},
|
||||
responses: {
|
||||
"200": {
|
||||
description: "Resolved series.",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: dataEnvelope(seriesResponse, "GetMeasurementsSeriesResponse"),
|
||||
},
|
||||
},
|
||||
},
|
||||
...stdResponses,
|
||||
},
|
||||
},
|
||||
},
|
||||
"/api/measurements": {
|
||||
get: {
|
||||
tags: ["Measurements"],
|
||||
|
||||
@@ -215,7 +215,73 @@ const profileUpdateResponse = z
|
||||
"Profile echo returned by the PATCH/PUT update path. Reports KVNR presence as a boolean rather than re-emitting the decrypted value.",
|
||||
});
|
||||
|
||||
// v1.16.16 — AI-provider read surface. iOS gates Coach visibility off
|
||||
// `aiAvailable` + `managedBy` so a server-managed (operator-key) provider
|
||||
// is no longer invisible to the client. The response reports key PRESENCE
|
||||
// + a 4-char masked preview only; no plaintext key or admin endpoint is
|
||||
// ever surfaced. `managedBy` reports the resolved provider origin: `user`
|
||||
// (BYOK), `local` (self-hosted Ollama/LM Studio), `server` (operator's
|
||||
// shared key), or null when no provider can serve the caller.
|
||||
const aiProviderResponse = z
|
||||
.object({
|
||||
provider: z
|
||||
.enum(["OPENAI", "ANTHROPIC", "LOCAL", "CHATGPT_OAUTH"])
|
||||
.nullable()
|
||||
.describe("The user's selected provider, or null when none is set."),
|
||||
model: z.string().nullable(),
|
||||
baseUrl: z
|
||||
.string()
|
||||
.nullable()
|
||||
.describe("Custom base URL (LOCAL provider only); null otherwise."),
|
||||
aiAvailable: z
|
||||
.boolean()
|
||||
.describe(
|
||||
"True when ANY provider can serve this user — including the operator's server-managed key when the user set none. iOS keys Coach visibility off this.",
|
||||
),
|
||||
managedBy: z
|
||||
.enum(["user", "local", "server"])
|
||||
.nullable()
|
||||
.describe(
|
||||
"Resolved provider origin: `user` (BYOK), `local` (self-hosted), `server` (operator key), or null when no provider is available.",
|
||||
),
|
||||
hasAnthropicKey: z.boolean(),
|
||||
anthropicKeyPreview: z
|
||||
.string()
|
||||
.nullable()
|
||||
.describe("`...` + last 4 chars of the stored Anthropic key, or null."),
|
||||
hasLocalKey: z.boolean(),
|
||||
hasOpenaiKey: z.boolean(),
|
||||
openaiKeyPreview: z
|
||||
.string()
|
||||
.nullable()
|
||||
.describe("`...` + last 4 chars of the stored OpenAI key, or null."),
|
||||
})
|
||||
.meta({
|
||||
id: "AiProviderResponse",
|
||||
description:
|
||||
"The calling user's AI-provider configuration plus the effective availability (`aiAvailable` + `managedBy`). Reports key presence + a masked preview only — never a plaintext key.",
|
||||
});
|
||||
|
||||
export const profilePaths: NonNullable<ZodOpenApiObject["paths"]> = {
|
||||
"/api/user/ai-provider": {
|
||||
get: {
|
||||
tags: ["Auth"],
|
||||
summary: "Read the calling user's AI-provider configuration",
|
||||
description:
|
||||
"Returns the user's selected provider/model/baseUrl plus the effective availability: `aiAvailable` is true when any provider can serve the user (BYOK, self-hosted, or the operator's server-managed key), and `managedBy` reports which. iOS gates Coach visibility off these two fields. Key material is reported as presence + a 4-char masked preview only.",
|
||||
responses: {
|
||||
"200": {
|
||||
description: "Resolved AI-provider configuration.",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: dataEnvelope(aiProviderResponse, "GetAiProviderResponse"),
|
||||
},
|
||||
},
|
||||
},
|
||||
...stdResponses,
|
||||
},
|
||||
},
|
||||
},
|
||||
"/api/auth/me/coach-prefs": {
|
||||
get: {
|
||||
tags: ["Auth"],
|
||||
|
||||
Reference in new issue
Block a user