Wire real utility cost data into Weekly Actuals report

Replaces the Utilities placeholder with a cost-breakdown table by
category (usage/standing/CCL/RAB levy/fixed extras/VAT/total) sourced
from the utilities app's internal API, plus a projected-total note for
the current month. Mirrors the existing forecasting integration
pattern (best-effort fetch, doesn't break the rest of the report on
failure). Also adds the missing UTILITIES_URL/UTILITIES_API_KEY env
vars to docker-compose.yml, needed by both this and the pre-existing
directors-forecast integration.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
jtricerolph 2026-07-28 14:32:48 +00:00
parent 932302ebe2
commit dfd3a506d1
4 changed files with 142 additions and 6 deletions

View file

@ -1,5 +1,6 @@
import { requireAuth } from '../auth.js'
import { getSetting } from '../db.js'
import { getUtilityCosts, getUtilityEstimate } from '../lib/utilities-client.js'
async function fcFetch(path) {
const apiKey = await getSetting('forecasting_api_key') || process.env.FORECASTING_API_KEY
@ -289,6 +290,25 @@ export async function weeklyActualRoutes(fastify) {
})
}
// ── Utilities (cost breakdown/estimate from the `utilities` app) ──────────
// Fetched best-effort for the report's calendar month — a failure here
// (e.g. utilities app not deployed, or key not configured) must not break
// the rest of the report.
let utilities = { costs: null, estimate: null, error: null }
try {
const period = `${year}-${String(month).padStart(2, '0')}`
const now = new Date()
const isCurrentPeriod = year === now.getFullYear() && month === now.getMonth() + 1
const [costs, estimate] = await Promise.all([
getUtilityCosts(period),
isCurrentPeriod ? getUtilityEstimate() : Promise.resolve(null),
])
utilities = { costs, estimate, error: null }
} catch (err) {
utilities = { costs: null, estimate: null, error: err.message }
}
return {
week_ending: toDateStr(weekEndDate),
week_start: toDateStr(weekStartDate),
@ -300,6 +320,7 @@ export async function weeklyActualRoutes(fastify) {
month_daily,
monthly_trend,
monthly_split,
utilities,
}
})
}