| 1 |
import { get } from "../../api"; |
| 2 |
import type { AccountBudget } from "../../api/validators"; |
| 3 |
import type { AccountTreeNode } from "../../charts/hierarchy"; |
| 4 |
import { getUrlPath } from "../../helpers"; |
| 5 |
import { getURLFilters } from "../../stores/filters"; |
| 6 |
import { Route } from "../route"; |
| 7 |
import AccountReport from "./AccountReport.svelte"; |
| 8 |
|
| 9 |
export type AccountReportType = "journal" | "balances" | "changes"; |
| 10 |
|
| 11 |
const to_report_type = (s: string | null): AccountReportType => |
| 12 |
s === "balances" || s === "changes" ? s : "journal"; |
| 13 |
|
| 14 |
export interface AccountReportProps { |
| 15 |
account: string; |
| 16 |
report_type: AccountReportType; |
| 17 |
charts: unknown; |
| 18 |
journal: string | null; |
| 19 |
interval_balances: AccountTreeNode[] | null; |
| 20 |
dates: { begin: Date; end: Date }[] | null; |
| 21 |
budgets: Record<string, AccountBudget[]> | null; |
| 22 |
} |
| 23 |
|
| 24 |
export const account_report = new Route<AccountReportProps>( |
| 25 |
"account", |
| 26 |
AccountReport, |
| 27 |
async (url) => { |
| 28 |
const [, account = ""] = getUrlPath(url)?.split("/") ?? []; |
| 29 |
const report_type = to_report_type(url.searchParams.get("r")); |
| 30 |
const res = await get("account_report", { |
| 31 |
...getURLFilters(url), |
| 32 |
a: account, |
| 33 |
r: report_type, |
| 34 |
}); |
| 35 |
return { ...res, account, report_type }; |
| 36 |
}, |
| 37 |
(route) => { |
| 38 |
if (route.url) { |
| 39 |
const [, account] = getUrlPath(route.url)?.split("/") ?? []; |
| 40 |
return `account:${account ?? "ERROR"}`; |
| 41 |
} |
| 42 |
throw new Error("Internal error: Expected route to have URL."); |
| 43 |
}, |
| 44 |
); |