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 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-07-28 15:00:12 +00:00
parent 811e3e9c31
commit 1243b73a04

View file

@ -39,6 +39,8 @@ async function categoryCostBreakdown(start, end) {
for (const cat of categories) { 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]) 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 = { const catTotals = {
usage_cost_pence: 0, standing_cost_pence: 0, ccl_cost_pence: 0, rab_levy_cost_pence: 0, 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, 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) => { app.get('/api/internal/trend', async (req) => {
const monthsCount = Math.min(parseInt(req.query.months) || 12, 24) const monthsCount = Math.min(parseInt(req.query.months) || 12, 24)
const { rows: categories } = await pool.query( 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() const now = new Date()