2026-06-23 20:36:07 -04:00
|
|
|
import { redirect } from "next/navigation"
|
|
|
|
|
import { asc, desc, eq } from "drizzle-orm"
|
|
|
|
|
import { db } from "@/lib/db"
|
|
|
|
|
import { expenses, properties } from "@/lib/db/schema"
|
|
|
|
|
import { getSessionUser } from "@/lib/session"
|
2026-07-02 13:42:34 -04:00
|
|
|
import { getEffectiveOwnerId } from "@/lib/account"
|
2026-06-23 20:36:07 -04:00
|
|
|
import { ExpensesClient } from "./expenses-client"
|
|
|
|
|
|
|
|
|
|
export const metadata = { title: "Expenses" }
|
|
|
|
|
|
|
|
|
|
export default async function ExpensesPage() {
|
|
|
|
|
const user = await getSessionUser()
|
|
|
|
|
if (!user) redirect("/login")
|
|
|
|
|
|
2026-07-02 13:42:34 -04:00
|
|
|
const ownerId = await getEffectiveOwnerId(user.id)
|
|
|
|
|
|
2026-06-23 20:36:07 -04:00
|
|
|
const [expenseList, propertyList] = await Promise.all([
|
|
|
|
|
db.query.expenses.findMany({
|
2026-07-02 13:42:34 -04:00
|
|
|
where: eq(expenses.user_id, ownerId),
|
2026-06-23 20:36:07 -04:00
|
|
|
with: {
|
|
|
|
|
property: { columns: { name: true } },
|
|
|
|
|
unit: { columns: { unit_number: true } },
|
|
|
|
|
},
|
|
|
|
|
orderBy: desc(expenses.expense_date),
|
|
|
|
|
}),
|
|
|
|
|
db
|
|
|
|
|
.select({ id: properties.id, name: properties.name })
|
|
|
|
|
.from(properties)
|
2026-07-02 13:42:34 -04:00
|
|
|
.where(eq(properties.user_id, ownerId))
|
2026-06-23 20:36:07 -04:00
|
|
|
.orderBy(asc(properties.name)),
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ExpensesClient
|
|
|
|
|
expenses={expenseList ?? []}
|
|
|
|
|
properties={propertyList ?? []}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|