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()