feat(measurements): resolve each reading's Apple Health write-back

A reading sent with a measurements:write token keeps its EXTERNAL source
and now points at the token that carried it (measurements.ingest_token_id).
Every stored row answers a server-resolved appleHealthMirror: true for
WITHINGS, IMPORT and MANUAL, the token's choice for EXTERNAL, false
otherwise. The plain list accepts appleHealthMirror=true so a client can
page exactly those rows.

Migration 0476 adds the columns and links existing EXTERNAL readings only
where one measurement token provably sent them; 0477 builds the index
concurrently. Backups carry the link and a restore keeps it only for a
token the account still holds. A guard holds every EXTERNAL writer to
setting the link.
This commit is contained in:
Marc-André Bombeck committed 2026-10-23 19:27:52 +02:00
1 parent a0dce10c1a
commit 8d10e5ab94
40 files changed
+1014 -142

No files matched your search

+21
View File
@@ -394,6 +394,10 @@ export const listMeasurementsSchema = z
// threaded into the plain-list `where`. Backed by the `(userId, source,
// measuredAt)` index (migration 0136).
sourceEq: measurementSourceEnum.optional(),
// Only the rows the HealthLog app writes into Apple Health
// (`appleHealthMirror`, #1272): the app's historical backfill pages this
// instead of one fetch per source. A plain-list filter.
appleHealthMirror: z.enum(["true"]).optional(),
// v1.18.5 — value-range FILTER (backlog G). Narrows the plain list to
// readings whose `value` falls within an optional `[valueMin, valueMax]`
// band. Either bound may be omitted for an open-ended range. Coerced
@@ -478,6 +482,23 @@ export const listMeasurementsSchema = z
message: "valueMin must be <= valueMax",
path: ["valueMin"],
},
)
// `appleHealthMirror` narrows the plain row list. The other modes answer
// something else (night rows, day buckets, a drill-down, a series), where
// a row filter would either do nothing or change what the mode means.
.refine(
({ appleHealthMirror, type, dayKey, groupBy, aggregate, source }) =>
appleHealthMirror === undefined ||
(type !== "SLEEP_DURATION" &&
dayKey === undefined &&
groupBy === undefined &&
source === undefined &&
(aggregate === undefined || aggregate === "raw")),
{
message:
"appleHealthMirror filters the plain row list only; combine it with type, from, to, sourceEq and paging",
path: ["appleHealthMirror"],
},
);
export const createBatchMeasurementSchema = z.object({