fix(moderation): windowValue must not fall back to the 30d total on a null column #97
Reference in New Issue
Block a user
No description provided.
Delete Branch "fix/window-value-null-column"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
windowValue(moderation dashboard tiles) mapped only the24h/7dkeys and used?? row.d30as the fallback:Because
??triggers onnull/undefined, a nulld1/d7— which the function's comment says it tolerates — returned the 30-day total instead of0, inflating the 24h/7d counts.This is currently unreachable in production: the source rows come from
SUM(created_at >= ?), which nullsd1/d7/d30only in unison (empty set), so wheneverd30is non-null the same rows maked1/d7a numeric0. But the contract is wrong, and the existing test used an all-null row ({d1:null, d7:null, d30:null}) that masked it.Introduced by the code-smell cleanup in
12d50fd; the siblingdiscordIdregex bug from the same commit is #96.Fix
Map all three window keys explicitly so each reads its own column, and let a null coerce to
0viaNumber(col) || 0(no??fallback):This stays Sonar-clean (no nested ternary, no cross-column fallthrough).
Test
Adds a regression test that would have caught the original — a null narrow-window column with a non-null
d30:Full moderation suite passes (14/14).
AI-assisted: this change was authored with Claude Code.
🤖 Generated with Claude Code
windowValue mapped only the 24h/7d keys and used `?? row.d30` as the fallback: const col = { '24h': row.d1, '7d': row.d7 }[key] ?? row.d30 so a null d1/d7 (which the function is documented to tolerate) returned the 30-day count instead of 0, inflating the 24h/7d moderation tiles. It happens to be masked today because `SUM(created_at >= ?)` nulls d1/d7/d30 only in unison, but the contract is wrong and the existing test used an all-null row that hid it. Map all three window keys explicitly so each reads its own column and a null coerces to 0 via `Number(col) || 0`. Add a regression test with a null narrow column and a non-null d30. Co-Authored-By: Claude <noreply@anthropic.com>