mega-plant/lib/server-data.ts
Dave 2594dead84 feat: add user access and improve plant dashboard
Add PocketBase login, first-sign-in password changes, account settings,
and user activation, reactivation and deletion with self-removal guards.
Protect application pages and APIs and keep private pages out of PWA
caches.

Resolve plant locations from site assignments and improve map controls,
profit chart views, navigation and mobile table layouts.

Validation: production build and TypeScript passed; 28 unit tests passed.
Backend access integration tests and mobile verification are documented.
2026-10-06 04:02:08 +02:00

71 lines
2.4 KiB
TypeScript

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<Asset[]> {
const pb = await dataPB();
return pb.collection("assets").getFullList<Asset>({ sort: "category,name" });
}
export async function getAssetsByCategory(category: string): Promise<Asset[]> {
const all = await getAssets();
return all.filter((a) => a.category === category);
}
export async function getMonthlyIncome(year = 2026): Promise<MonthlyIncome[]> {
const pb = await dataPB();
return pb.collection("monthly_income").getFullList<MonthlyIncome>({
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<MonthlyIncome[]> {
const all = await getMonthlyIncome(year);
return all.filter((r) => !(r as any).is_forecast);
}
export async function getRunningCosts(year = 2026): Promise<MonthlyRunningCost[]> {
const pb = await dataPB();
return pb.collection("monthly_running_costs").getFullList<MonthlyRunningCost>({
filter: `year = ${year}`,
sort: "month",
});
}
export async function getOverheads(): Promise<Overhead[]> {
const pb = await dataPB();
return pb.collection("overheads").getFullList<Overhead>({ sort: "section,description" });
}
export async function getOverheadsBySection(section: string): Promise<Overhead[]> {
const all = await getOverheads();
return all.filter((o) => o.section === section);
}
export async function getInvoices(): Promise<Invoice[]> {
const pb = await dataPB();
return pb.collection("invoices").getFullList<Invoice>({ sort: "-invoice_date,-invoice_no" });
}
export async function getInvoiceLines(invoiceId?: string): Promise<InvoiceLine[]> {
const pb = await dataPB();
return pb.collection("invoice_lines").getFullList<InvoiceLine>({
filter: invoiceId ? `invoice = "${invoiceId}"` : "",
sort: "line_no",
});
}
export async function getSites(): Promise<Site[]> {
const pb = await dataPB();
return pb.collection("sites").getFullList<Site>({ sort: "name" });
}