fix(summaries-slice): exclude soft-deleted measurements

The four `$queryRaw` blocks in `computeFromRollups` and
`computeFromLiveAggregate` (narrow aggregate + DISTINCT ON latest,
both fast and cold paths) folded every row regardless of
`deleted_at`. The DAY-bucket rollup aggregate is unaffected because
its source table is already filtered by the writer.

Add `AND m."deleted_at" IS NULL` to every `FROM measurements m`
WHERE clause so the slim slice's avg7/avg30/slope/r2 plus the
per-type latest never reflect a row the user soft-deleted.
This commit is contained in:
Marc-André Bombeck committed 2026-05-21 11:41:05 +02:00
1 parent f0a0551a80
commit 1f49041025
1 file changed
+4
+4
View File
@@ -301,6 +301,7 @@ async function computeFromRollups(userId: string): Promise<SummariesSlice> {
)::double precision AS r2_90
FROM measurements m
WHERE m."user_id" = ${userId}
AND m."deleted_at" IS NULL
GROUP BY m."type"
`,
prisma.$queryRaw<LatestRow[]>`
@@ -310,6 +311,7 @@ async function computeFromRollups(userId: string): Promise<SummariesSlice> {
m."measured_at" AS measured_at
FROM measurements m
WHERE m."user_id" = ${userId}
AND m."deleted_at" IS NULL
ORDER BY m."type", m."measured_at" DESC
`,
// v1.4.37.2 hotfix — the v1.4.35 implementation read EVERY DAY
@@ -487,6 +489,7 @@ async function computeFromLiveAggregate(
)::double precision AS r2_90
FROM measurements m
WHERE m."user_id" = ${userId}
AND m."deleted_at" IS NULL
GROUP BY m."type"
`,
prisma.$queryRaw<LatestRow[]>`
@@ -496,6 +499,7 @@ async function computeFromLiveAggregate(
m."measured_at" AS measured_at
FROM measurements m
WHERE m."user_id" = ${userId}
AND m."deleted_at" IS NULL
ORDER BY m."type", m."measured_at" DESC
`,
]);