import config from "@payload-config"; import { getPayload } from "payload"; import { headers as nextHeaders } from "next/headers"; import { notFound } from "next/navigation"; import type { BankAccount, LedgerEntry } from "@/payload-types"; import { AccountDetail } from "@/components/frontend/banking/AccountDetail"; import { hasPermission } from "@/utils/access-control/hasPermission"; import { hasLogisticsQualification } from "@/utils/access-control/hasLogisticsQualification"; import { getCurrencyConfig } from "@/lib/banking"; export const metadata = { title: "Account — Polaris Task Force", }; interface AccountPageProps { params: Promise<{ id: string }>; } export default async function AccountPage({ params }: AccountPageProps) { const { id } = await params; const payload = await getPayload({ config }); const { user } = await payload.auth({ headers: await nextHeaders(), canSetHeaders: false, }); const account = await payload .findByID({ collection: "bank-accounts", id: Number(id), depth: 1, }) .catch(() => null); if (!account) { notFound(); } const typedAccount = account as unknown as BankAccount; const isManager = user ? (await hasPermission(payload, user, "banking:manage")) || (await hasLogisticsQualification(payload, user).catch(() => false)) : false; const ownerId = typeof typedAccount.ownerUser === "object" ? typedAccount.ownerUser?.id : typedAccount.ownerUser; const isOwner = typedAccount.accountType === "personal" && user !== null && ownerId === user.id; if (!isManager && !isOwner) { notFound(); } const ledgerRes = await payload.find({ collection: "ledger-entries", where: { account: { equals: Number(id) } }, sort: "-timestamp", limit: 200, depth: 0, }); const ledger = ledgerRes.docs as unknown as LedgerEntry[]; const allAccountsRes = await payload.find({ collection: "bank-accounts", limit: 500, depth: 0, }); const otherAccounts = (allAccountsRes.docs as unknown as BankAccount[]) .filter((a) => a.id !== typedAccount.id) .map((a) => ({ id: a.id, name: a.name })); const currencyConfig = await getCurrencyConfig(payload); return (