feat(healthkit): record Apple Health dose arrivals and the last background arrival per type

Migration 0401 turns healthkit_type_syncs.type into text so the ledger can
hold MEDICATION_DOSE, and adds last_background_at (seeded from rows whose
latest arrival was a background one). POST /api/medications/intake/bulk
takes syncTrigger and records APPLE_HEALTH dose arrivals; the Apple Health
status read serves a MEDICATION_DOSE freshness entry and lastBackgroundAt
on every entry with arrival facts.
This commit is contained in:
Marc-André Bombeck committed 2026-10-15 15:33:30 +02:00
1 parent 6313c0174a
commit 03b24461ad
22 files changed
+352 -54

No files matched your search

@@ -0,0 +1,23 @@
-- Per-type HealthKit arrival ledger: room for non-measurement types, and the
-- last background arrival per type.
--
-- `type` moves from the `measurement_type` enum to text so the ledger can
-- also record Apple Health medication doses (`MEDICATION_DOSE`), which are
-- not a measurement type. Every stored enum value converts to its own label
-- through the enum's text output, so no row changes meaning. The table holds
-- at most one row per (user, delivered type), a few dozen per account; the
-- rewrite and the primary-key rebuild it implies are cheap at any size this
-- table reaches, and the key keeps its name and columns.
ALTER TABLE "healthkit_type_syncs"
ALTER COLUMN "type" SET DATA TYPE TEXT USING "type"::text;
-- When a batch with `syncTrigger = background` last carried the type. A
-- later foreground or manual arrival leaves it standing.
ALTER TABLE "healthkit_type_syncs" ADD COLUMN "last_background_at" TIMESTAMP(3);
-- A row whose latest arrival was already a background one says exactly that,
-- so it starts with that date rather than null. Rows whose latest arrival
-- came another way cannot know an earlier background date and stay null.
UPDATE "healthkit_type_syncs"
SET "last_background_at" = "last_received_at"
WHERE "last_trigger" = 'background';
+14 -5
View File
@@ -4005,16 +4005,25 @@ model ManagedProfileHandover {
/// batch route; read by the Apple Health card's per-type freshness list.
/// Diagnostic state: rebuilt by the next sync, so it is not backed up.
model HealthKitTypeSync {
userId String @map("user_id")
type MeasurementType
userId String @map("user_id")
/// A `MeasurementType` name for rows the measurement batch writes, or
/// `MEDICATION_DOSE` for Apple Health doses the intake bulk route
/// receives. Text, not the enum, since v1.42.4 (migration 0401) because a
/// dose is not a measurement type.
type String
/// Last batch that carried this type.
lastReceivedAt DateTime @map("last_received_at")
lastReceivedAt DateTime @map("last_received_at")
/// `syncTrigger` of that batch
/// (`foreground` / `background` / `push` / `manual`), NULL when absent.
lastTrigger String? @map("last_trigger")
lastTrigger String? @map("last_trigger")
/// Last batch that inserted or updated a sample of
/// this type (as opposed to sending only duplicates).
lastNewSampleAt DateTime? @map("last_new_sample_at")
lastNewSampleAt DateTime? @map("last_new_sample_at")
/// Last batch with `syncTrigger = background` that carried this type. A
/// later foreground, push or manual arrival leaves it standing; NULL when
/// none has (or only before v1.42.4, except a row whose latest arrival was
/// a background one, which migration 0401 seeded).
lastBackgroundAt DateTime? @map("last_background_at")
user User @relation(fields: [userId], references: [id], onDelete: Cascade)