import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import prisma from "@/lib/db"; import Link from "next/link"; import { auth } from "@/lib/auth"; import { cn } from "@/lib/utils"; import type { Prisma } from "@prisma/client"; import { Calendar } from "lucide-react"; import { headers } from "next/headers"; import Pagination from "./pagination"; import { Badge } from "./ui/badge"; import { Button } from "./ui/button"; import { Separator } from "./ui/separator"; type PaymentWithDevices = Prisma.PaymentGetPayload<{ include: { devices: true; }; }> export async function PaymentsTable({ searchParams, }: { searchParams: Promise<{ query: string; page: number; sortBy: string; }>; }) { const session = await auth.api.getSession({ headers: await headers() }) const query = (await searchParams)?.query || ""; const page = (await searchParams)?.page; const totalPayments = await prisma.payment.count({ where: { userId: session?.session.userId, OR: [ { devices: { every: { name: { contains: query || "", mode: "insensitive", }, }, }, }, ], }, }); const totalPages = Math.ceil(totalPayments / 10); const limit = 10; const offset = (Number(page) - 1) * limit || 0; const payments = await prisma.payment.findMany({ where: { userId: session?.session.userId, OR: [ { devices: { every: { name: { contains: query || "", mode: "insensitive", }, }, }, }, ], }, include: { devices: true }, skip: offset, take: limit, orderBy: { createdAt: "desc", }, }); return (
{payments.length === 0 ? (

No Payments yet.

) : ( <>
Table of all devices. Details Duration Amount {payments.map((payment) => (
{new Date(payment.createdAt).toLocaleDateString("en-US", { month: "short", day: "2-digit", year: "numeric", })}
{payment.paid ? "Paid" : "Unpaid"}

Devices

    {payment.devices.map((device) => (
  1. {device.name}
  2. ))}
{payment.numberOfMonths} Months {payment.amount.toFixed(2)} MVR
))}
{query.length > 0 && (

Showing {payments.length} locations for "{query} "

)}
{totalPayments} payments
{payments.map((payment) => ( ))}
)}
); } function MobilePaymentDetails({ payment }: { payment: PaymentWithDevices }) { return (
{new Date(payment.createdAt).toLocaleDateString("en-US", { month: "short", day: "2-digit", year: "numeric", })}
{payment.paid ? "Paid" : "Unpaid"}

Devices

    {payment.devices.map((device) => (
  1. {device.name}
  2. ))}

Duration

{payment.numberOfMonths} Months

Amount

{payment.amount.toFixed(2)} MVR
) }