mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-09 21:10:59 +00:00
Audit Critical Finding #2 + High finding 3 (round-v1439-arch-qa-infra-db). The mood block at the bottom of the route used to run an unbounded `prisma.moodEntry.findMany({ where: { userId } })` on every mount — pulling every mood the user had ever logged into Node only to bucket- average them in JS. The `findMany({distinct:['type']})` for 'absolute latest measurement per type' carried no `measuredAt >=` floor either, so on a 347 k-row tenant Postgres still had to sort the full set before the driver applied DISTINCT ON (Prisma's `distinct` doesn't compile to PG's `DISTINCT ON`). This commit: - swaps the mood findMany for `readMoodDayRollups(userId, since)` plus a single `findFirst` for the latest score; coverage-fallback runs a bounded 30-day raw walk + fires `ensureUserMoodRollupsFresh` so the next request lands on the rollup tier (same posture as /api/mood/analytics post-v1.4.39) - adds a 365-day floor to the latest-ever-per-type read so the driver-side distinct walk stays bounded - pins all three behaviours in `__tests__/route.test.ts` (4 new tests: rollup fast path, coverage-fallback bounded walk, no-mood branch, distinct floor present)