import Link from "next/link"; import { redirect } from "next/navigation"; import { getPayments } from "@/actions/payment"; import Pagination from "@/components/pagination"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { tryCatch } from "@/utils/tryCatch"; import ClientErrorMessage from "../client-error-message"; export async function UsersPaymentsTable({ searchParams, }: { searchParams: Promise<{ [key: string]: unknown; }>; }) { const resolvedParams = await searchParams; const page = Number.parseInt(resolvedParams.page as string) || 1; const limit = 10; const offset = (page - 1) * limit; // Build params object for getDevices const apiParams: Record = {}; for (const [key, value] of Object.entries(resolvedParams)) { if (value !== undefined && value !== "") { apiParams[key] = typeof value === "number" ? value : String(value); } } apiParams.limit = limit; apiParams.offset = offset; const [error, payments] = await tryCatch(getPayments(apiParams, true)); if (error) { if (error.message === "UNAUTHORIZED") { redirect("/auth/signin"); } else { return ; } } const { meta, data } = payments; // return
{JSON.stringify(payments, null, 2)}
; return (
{data.length === 0 ? (

No user payments yet.

) : ( <> Table of all users. Devices paid User Amount Duration Payment Status Payment Method MIB Reference Paid At Action {data.map((payment) => (
    {payment.devices.map((device) => (
  1. {device.name}
  2. ))}
{/* {payment.user.id_card} */}
{payment?.user?.name} {payment?.user?.id_card}
{" "}
{payment.amount} MVR {payment.number_of_months} Months {payment.status === "PENDING" ? ( {payment.status} ) : payment.status === "PAID" ? ( {payment.status} ) : ( {payment.status} )} {payment.method} {payment.mib_reference} {new Date(payment.paid_at ?? "").toLocaleDateString( "en-US", { month: "short", day: "2-digit", year: "numeric", minute: "2-digit", hour: "2-digit", }, )}
))}
{meta?.total === 1 ? (

Total {meta?.total} payment.

) : (

Total {meta?.total} payments.

)}{" "}
)}
); }