From 1243b73a040a2b202d0c4e4e1843902091469cc5 Mon Sep 17 00:00:00 2001 From: jtricerolph Date: Tue, 28 Jul 2026 15:00:12 +0000 Subject: [PATCH] Exclude categories with no meters from internal cost/trend responses Water and Oil are seeded default categories with no meters installed yet, so they were showing up as all-zero rows/flat-line charts in the reports app's new per-meter table and trend charts. Skip any category with zero active meters in both the shared cost-breakdown loop and the trend endpoint's category list. Co-Authored-By: Claude Sonnet 5 --- backend/src/routes/internal.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backend/src/routes/internal.js b/backend/src/routes/internal.js index add8e6f..f296d18 100644 --- a/backend/src/routes/internal.js +++ b/backend/src/routes/internal.js @@ -39,6 +39,8 @@ async function categoryCostBreakdown(start, end) { for (const cat of categories) { const { rows: meters } = await pool.query('SELECT id, name FROM meters WHERE category_id = $1 AND active = TRUE', [cat.id]) + if (meters.length === 0) continue // no meters yet (e.g. Water/Oil seeded but unused) — nothing to report + const catTotals = { usage_cost_pence: 0, standing_cost_pence: 0, ccl_cost_pence: 0, rab_levy_cost_pence: 0, metering_cost_pence: 0, other_charges_cost_pence: 0, vat_pence: 0, total_pence: 0, consumption: 0, @@ -127,7 +129,9 @@ export async function internalRoutes(app) { app.get('/api/internal/trend', async (req) => { const monthsCount = Math.min(parseInt(req.query.months) || 12, 24) const { rows: categories } = await pool.query( - 'SELECT key, name, unit_label FROM meter_categories WHERE active = TRUE ORDER BY sort_order' + `SELECT DISTINCT c.key, c.name, c.unit_label, c.sort_order FROM meter_categories c + JOIN meters m ON m.category_id = c.id AND m.active = TRUE + WHERE c.active = TRUE ORDER BY c.sort_order` ) const now = new Date()