import { redirect } from "next/navigation" import { eq, desc } from "drizzle-orm" import { db } from "@/lib/db" import { rent_payments } from "@/lib/db/schema" import { getSessionUser } from "@/lib/session" import { getEffectiveOwnerId } from "@/lib/account" import Link from "next/link" import { CreditCard, Plus, Upload } from "lucide-react" import { EmptyState } from "@/components/shared/empty-state" import { RentTable } from "./rent-table" import { CsvExportButton } from "@/components/forms/csv-export-button" export const metadata = { title: "Rent Tracker" } export default async function RentPage() { const user = await getSessionUser() if (!user) redirect("/login") const ownerId = await getEffectiveOwnerId(user.id) const payments = await db.query.rent_payments.findMany({ where: eq(rent_payments.user_id, ownerId), with: { tenant: { columns: { first_name: true, last_name: true } }, property: { columns: { name: true } }, unit: { columns: { unit_number: true } }, }, orderBy: desc(rent_payments.due_date), }) return (
{payments?.length ?? 0} total records