import "server-only"; import { serverAuth } from "./pb"; import { requireUser } from "./auth"; import type { Asset, MonthlyIncome, MonthlyRunningCost, Overhead, Invoice, InvoiceLine, Site } from "./data"; export * from "./data"; async function dataPB() { await requireUser(); return serverAuth(); } export async function getAssets(): Promise { const pb = await dataPB(); return pb.collection("assets").getFullList({ sort: "category,name" }); } export async function getAssetsByCategory(category: string): Promise { const all = await getAssets(); return all.filter((a) => a.category === category); } export async function getMonthlyIncome(year = 2026): Promise { const pb = await dataPB(); return pb.collection("monthly_income").getFullList({ filter: `year = ${year}`, sort: "month", }); } // Part 1.2: YTD aggregations operate on actuals only by default. // `is_forecast` is a boolean field on monthly_income; rows where it's true are excluded. export async function getActualMonthlyIncome(year = 2026): Promise { const all = await getMonthlyIncome(year); return all.filter((r) => !(r as any).is_forecast); } export async function getRunningCosts(year = 2026): Promise { const pb = await dataPB(); return pb.collection("monthly_running_costs").getFullList({ filter: `year = ${year}`, sort: "month", }); } export async function getOverheads(): Promise { const pb = await dataPB(); return pb.collection("overheads").getFullList({ sort: "section,description" }); } export async function getOverheadsBySection(section: string): Promise { const all = await getOverheads(); return all.filter((o) => o.section === section); } export async function getInvoices(): Promise { const pb = await dataPB(); return pb.collection("invoices").getFullList({ sort: "-invoice_date,-invoice_no" }); } export async function getInvoiceLines(invoiceId?: string): Promise { const pb = await dataPB(); return pb.collection("invoice_lines").getFullList({ filter: invoiceId ? `invoice = "${invoiceId}"` : "", sort: "line_no", }); } export async function getSites(): Promise { const pb = await dataPB(); return pb.collection("sites").getFullList({ sort: "name" }); }