72 lines
2.4 KiB
TypeScript
72 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" });
|
||
|
|
}
|