fix(seed): guarantee every displayed month of Monthly Revenue is in-band#38
Conversation
The dashboard Monthly Revenue chart aggregates paid invoices by paid_at month over the trailing 6 months. The random invoice distribution alone could leave the oldest displayed month at $0 on some reseeds (its paid_at spilling just outside the window), so the chart sometimes read as a ramp-up from zero rather than an established business. Deterministically plant a paid-invoice baseline in each of the 6 displayed months on top of the random invoices. Each month's baseline paid_at is pinned to the 15th at noon UTC (clamped into the past) so it always lands in that month's date_trunc bucket and inside the dashboard window. Documented band: $40,000 floor, $200,000 ceiling per month; observed reseed totals land ~$54k–$126k with the current month never a spike. Add a Postgres-backed regression test that runs the seed and asserts every month in the 6-month window is within the band. Verified it fails on the pre-baseline seed and passes after.
There was a problem hiding this comment.
Code Review
This pull request introduces a deterministic revenue baseline within the demoSeed function to ensure the dashboard's monthly revenue chart consistently displays data for the trailing six months. It also includes a new integration test to verify that the seeded revenue remains within the expected $40,000 to $200,000 range. Feedback was provided regarding an inefficient UPDATE query in the seeding logic that could lead to NULL values for total_amount if tax_amount is not set, potentially defeating the purpose of the baseline fix. A more robust and optimized query was suggested.
| `UPDATE invoices SET | ||
| subtotal = (SELECT COALESCE(SUM(amount),0) FROM invoice_line_items WHERE invoice_id = $1), | ||
| total_amount = (SELECT COALESCE(SUM(amount),0) FROM invoice_line_items WHERE invoice_id = $1) + tax_amount | ||
| WHERE id = $1`, | ||
| [invId], | ||
| ); | ||
| const history: Array<[string, string, string]> = [ |
There was a problem hiding this comment.
The UPDATE query is inefficient as it executes the same subquery twice for every baseline invoice. More importantly, if tax_amount is NULL in the database (which is likely since it is not specified in the preceding INSERT statement), the total_amount calculation (subtotal + tax_amount) will result in NULL. This would cause the dashboard revenue aggregation to return 0 for that month, defeating the purpose of this baseline fix. Since we know the subtotal for these specific baseline invoices is exactly perInvoice, we can optimize the query and use COALESCE to ensure the total is correctly calculated.
await pool.query(
`UPDATE invoices SET
subtotal = $2,
total_amount = $2 + COALESCE(tax_amount, 0)
WHERE id = $1`,
[invId, perInvoice],
);|
Live verification complete. Backed up the production DB to |
Problem
The dashboard Monthly Revenue chart aggregates paid invoices by
paid_atmonth over the trailing 6 months (
DashboardRepository.getMonthlyRevenue). Theseed's random invoice distribution is realistic for project-based billing but,
on some reseeds, left the oldest displayed month at $0 — its
paid_atspilling just outside the dashboard window. The chart then read as a ramp-up
from zero rather than an established business, and varied per reseed.
Live "before" (current production seed) — Dec starts at ~$0, May trails to ~$0:
Fix
Deterministically plant a paid-invoice baseline in each of the 6 displayed
months, layered on top of the existing random invoices. Each month's baseline
paid_atis pinned to the 15th at noon UTC (clamped into the past) so italways lands in that month's
date_trunc('month')bucket and inside thedashboard window. The baseline amounts are fixed (no RNG), so the floor is
guaranteed on every reseed.
Determinism approach (one sentence): the seed plants two fixed-amount paid
invoices per displayed month, dated to mid-month in the past, before the random
invoices are layered on — so every month's total is deterministically above the
floor regardless of the random draw.
Chosen band (documented in
seed.ts):$50–64k) plus random top-up stays well$54k–$126k**, current month never aunder this; observed reseed totals land **
spike (consistent with fix(seed): revenue chart should not spike on the current month #37).
After (4 consecutive reseeds, all 6 months in-band):
Test
New Postgres-backed regression test
dashboard-revenue.int-spec.tsruns thedemo seed against a fresh database, calls the exact dashboard aggregation, and
asserts every month in the 6-month window is
> 0and within[40k, 200k].Verified it fails on the pre-baseline seed (3/3 runs, a month at
~$12k–$31k) and passes after (4/4 runs).
The chart component is unchanged; no months are hidden — data is guaranteed,
not its absence masked.
Scope
Touches only the invoice seed for the displayed revenue window and the new
test. No other settled data (compliance, audit, W-9, currency, names,
classification, risk weights) is modified.