Files
HealthLog/src/lib/validations/consent.ts
T
Marc-André Bombeck 1114cf7e63 chore(release): v1.4.40 — comprehensive Critical+High architecture closure plus iOS PB30 enablement
Eleven touch-disjoint waves landed on develop plus a six-axis QA
reconcile round before tagging. See CHANGELOG.md for the user-facing
prose; this commit message documents the wave + report inventory for
release-management traceability.

Waves shipped (each with a phase report under .planning/):

- phase-W-POOL-v1440-report.md — p-limit(4) wrapper around the
  analytics 15-way fan-out + pg.Pool max raised 10 -> 20. Closes the
  v1.4.39 empirical-trace finding #1 (Prisma pool starvation).
- phase-W-DELETED-v1440-report.md — 11 reader-tier helpers + 3
  integration contracts close audit Critical Finding #3 (soft-delete
  invisibility) across dashboard, analytics, summaries, rollup tiers,
  and the Coach snapshot path.
- phase-W-INSIGHTS-v1440-report.md — mood-rollup swap on the three
  /api/insights/{features,targets,cards} routes plus the
  compliance-rollup hook gap closure for bulk-projection mints; the
  post-QA reconcile patch closes the six remaining measurement.findMany
  sites in the cluster that still leaked tombstoned rows.
- phase-W-INFRA-v1440-report.md — rollup helper umbrella consolidation
  under src/lib/rollups/* with zero orphan re-exports; lint, typecheck,
  knip, and 93/93 rollup tests green post-restructure.
- phase-W-RSC-v1440-report.md — per-tile Suspense boundaries plus the
  queryKey factory-bypass walker test (extended to comparison/ in the
  reconcile pass).
- phase-W-PRIVACY-v1440-report.md — bilingual /privacy page with 17
  paired-section tests pinning the 9 SB-3 disclosure requirements.
- phase-W-AASA-v1440-report.md — /.well-known/apple-app-site-association
  endpoint pinned to S8WDX4W5KX.dev.healthlog.app with app-ID parity
  assertions guarding against split rotation.
- phase-W-APNS-NOTIFY-v1440-report.md — MEDICATION_REMINDER
  time-sensitive APNs payload (priority 10 + interruption-level)
  scoped via a parameterised test that pins all 6 other event-types
  do not bypass Focus. AP-2 .p8 install gates real delivery.
- phase-W-CONSENT-v1440-report.md — AI consent receipts CRUD with
  append-only invariant pinned in test; revoke-then-regrant leaves
  both rows; idempotent DELETE returns 200 + receipt: null.
- phase-W-WMY-WIRE-v1440-report.md — three previously-dead WMY readers
  (week/month/year) wired through summaries-slice and
  health-score-fast-path with a linear-composability parity test
  pinning the cross-consumer routing contract.
- phase-W-GHOSTS-v1440-report.md — ~1 177 lines of dead code removed
  across 9 atomic commits; startOfUtcDay + wallClockInTz consolidation
  eliminates a sibling-drift risk; knip whitelist trimmed to three
  files pending follow-up.

QA reconcile patches applied per the v1.4.40 QA round (six reports
under .planning/round-v1440-QA-*-findings.md):

- senior C1 — dashboard-suspense-boundaries regex aligned with the
  post-W-INFRA Thread 2 userTimezone-local lift.
- senior H1 — soft-delete filter added to the six remaining
  insights measurement.findMany sites.
- specialist H1 — Promise.allSettled around bulk-projection
  compliance recompute on both /api/medications/intake and
  /api/dashboard/summary.
- ux H1 — compare-toggle migrated to queryKeys.dashboardWidgets
  factory; walker test extended to src/components/comparison/.
- security M1 — consent artefact 64 KB cap enforced via
  Buffer.byteLength (UTF-8 bytes, not UTF-16 code units).

Quality gates green at tag: pnpm typecheck, pnpm lint, pnpm knip
--include files,dependencies,binaries,unlisted, pnpm test --run
(4726 passing / 1 skipped). Tests 4525 -> 4726 unit + 1 skipped over
the v1.4.39 -> v1.4.40 span.

User-facing prose: see CHANGELOG.md.
2026-05-21 12:48:02 +02:00

71 lines
2.5 KiB
TypeScript

import { z } from "zod/v4";
/**
* v1.4.40 SB-10 — AI consent receipts (App-Store Guideline 5.1.2(i) +
* GDPR Art. 7 audit trail).
*
* The discriminator covers the three consent surfaces the iOS client
* collects independently:
* - `ai_full` full assistant + on-device personalisation
* - `ai_insights_only` read-only Insights generation (no Coach)
* - `ai_coach` chat-style Coach access
*
* Keeping each surface as its own row means the legal team can prove
* which exact scope the user agreed to without parsing the signed
* artefact server-side. The server stays opaque to the artefact's
* format — we accept either a base64-encoded PDF or a signed JWT, the
* audit trail *is* the storage plus the `signedAt` timestamp.
*/
export const consentKindEnum = z.enum([
"ai_full",
"ai_insights_only",
"ai_coach",
]);
export type ConsentKind = z.infer<typeof consentKindEnum>;
/**
* 64 KB upper bound on the artefact. A signed PDF receipt sits well
* under 32 KB; a signed JWT is a few hundred bytes. The cap exists so
* a misbehaving client (or a malicious one) can't fill the audit table
* with multi-megabyte rows.
*
* v1.4.40 security M1 — the cap is enforced via `Buffer.byteLength(...,
* "utf8")` because `z.string().max()` counts JavaScript string units
* (UTF-16 code units), not bytes. A UTF-8 artefact full of multi-byte
* code points (CJK, emoji) would otherwise be allowed past the 64 KB
* row budget — the database stores bytes, not code units, and the
* audit-table guarantee is byte-bounded.
*/
const ARTEFACT_MAX_BYTES = 64 * 1024;
export const consentPostBody = z.object({
kind: consentKindEnum,
artefact: z
.string()
.min(1, "artefact must not be empty")
.refine(
(value) => Buffer.byteLength(value, "utf8") <= ARTEFACT_MAX_BYTES,
{ message: "artefact exceeds 64 KB cap (UTF-8 byte length)" },
),
signedAt: z.iso
.datetime({ offset: true })
.transform((s) => new Date(s)),
});
export type ConsentPostBody = z.infer<typeof consentPostBody>;
/**
* Query schema for both `/api/consent/ai/latest` GET and DELETE.
*
* Omitting `kind` on GET returns the latest active receipt per kind
* (a `Record<ConsentKind, …>` shape). Omitting it on DELETE revokes
* the latest receipt across all three kinds — handy for the iOS
* "deaktivieren alle KI-Funktionen" master toggle.
*/
export const consentLatestQuery = z.object({
kind: consentKindEnum.optional(),
});
export type ConsentLatestQuery = z.infer<typeof consentLatestQuery>;