mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-26 07:30:22 +00:00
feat(admin): add admin payment tables + filters✨
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Has been cancelled
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Has been cancelled
This commit is contained in:
@ -29,6 +29,7 @@ This is a web portal for SAR Link customers.
|
|||||||
## Admin Controls
|
## Admin Controls
|
||||||
### Users
|
### Users
|
||||||
- [x] Show users table
|
- [x] Show users table
|
||||||
|
- [ ] handle verify api no response case
|
||||||
- [ ] Add all relavant filters for users table
|
- [ ] Add all relavant filters for users table
|
||||||
- [x] Verify or reject users with a custom message
|
- [x] Verify or reject users with a custom message
|
||||||
- [ ] Add functionality to send custom sms to users in user:id page
|
- [ ] Add functionality to send custom sms to users in user:id page
|
||||||
@ -40,6 +41,6 @@ This is a web portal for SAR Link customers.
|
|||||||
- [x] Add all relevant filters for user devices table
|
- [x] Add all relevant filters for user devices table
|
||||||
|
|
||||||
### User Payments
|
### User Payments
|
||||||
- [ ] Show user payments table
|
- [x] Show user payments table
|
||||||
- [ ] Add relevant filters for user payments table
|
- [x] Add relevant filters for user payments table
|
||||||
- [ ] Add computation of total value for filtered results
|
- [ ] Add computation of total value for filtered results
|
@ -93,7 +93,7 @@ type GetPaymentProps = {
|
|||||||
[key: string]: string | number | undefined; // Allow additional properties for flexibility
|
[key: string]: string | number | undefined; // Allow additional properties for flexibility
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function getPayments(params: GetPaymentProps) {
|
export async function getPayments(params: GetPaymentProps, allPayments = false) {
|
||||||
// Build query string from all defined params
|
// Build query string from all defined params
|
||||||
const query = Object.entries(params)
|
const query = Object.entries(params)
|
||||||
.filter(([_, value]) => value !== undefined && value !== "")
|
.filter(([_, value]) => value !== undefined && value !== "")
|
||||||
@ -101,7 +101,7 @@ export async function getPayments(params: GetPaymentProps) {
|
|||||||
.join("&");
|
.join("&");
|
||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${process.env.SARLINK_API_BASE_URL}/api/billing/payment/?${query}`,
|
`${process.env.SARLINK_API_BASE_URL}/api/billing/payment/?${query}&all_payments=${allPayments}`,
|
||||||
{
|
{
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { Suspense } from "react";
|
import { Suspense } from "react";
|
||||||
import { UsersPaymentsTable } from "@/components/admin/user-payments-table";
|
import { UsersPaymentsTable } from "@/components/admin/user-payments-table";
|
||||||
|
import DynamicFilter from "@/components/generic-filter";
|
||||||
|
|
||||||
export default async function UserPayments({
|
export default async function UserPayments({
|
||||||
searchParams,
|
searchParams,
|
||||||
@ -18,6 +19,62 @@ export default async function UserPayments({
|
|||||||
<div className="flex justify-between items-center border rounded-md border-dashed font-bold title-bg py-4 px-2 mb-4">
|
<div className="flex justify-between items-center border rounded-md border-dashed font-bold title-bg py-4 px-2 mb-4">
|
||||||
<h3 className="text-sarLinkOrange text-2xl">User Payments</h3>
|
<h3 className="text-sarLinkOrange text-2xl">User Payments</h3>
|
||||||
</div>
|
</div>
|
||||||
|
<DynamicFilter
|
||||||
|
description="Filter user payments by name, MAC address, or vendor."
|
||||||
|
title="User Payments Filter"
|
||||||
|
inputs={[
|
||||||
|
{
|
||||||
|
name: "user",
|
||||||
|
label: "User",
|
||||||
|
type: "string",
|
||||||
|
placeholder: "Enter user name",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "mib_reference",
|
||||||
|
label: "MIB Reference",
|
||||||
|
type: "string",
|
||||||
|
placeholder: "Enter MIB Reference",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "dual-range-slider",
|
||||||
|
label: "Amount Range",
|
||||||
|
name: "amount",
|
||||||
|
max: 1200,
|
||||||
|
min: 0,
|
||||||
|
step: 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "dual-range-slider",
|
||||||
|
label: "Duration Range",
|
||||||
|
name: "number_of_months",
|
||||||
|
max: 12,
|
||||||
|
min: 1,
|
||||||
|
step: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "radio-group",
|
||||||
|
label: "Payment Status",
|
||||||
|
name: "status",
|
||||||
|
options: [
|
||||||
|
{ label: "All", value: "" },
|
||||||
|
{ label: "Pending", value: "PENDING" },
|
||||||
|
{ label: "Paid", value: "PAID" },
|
||||||
|
{ label: "Cancelled", value: "CANCELLED" },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "radio-group",
|
||||||
|
label: "Payment Method",
|
||||||
|
name: "method",
|
||||||
|
options: [
|
||||||
|
{ label: "All", value: "" },
|
||||||
|
{ label: "Wallet", value: "WALLET" },
|
||||||
|
{ label: "Transfer", value: "TRANSFER" },
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
]}
|
||||||
|
/>
|
||||||
<Suspense key={query} fallback={"loading...."}>
|
<Suspense key={query} fallback={"loading...."}>
|
||||||
<UsersPaymentsTable searchParams={searchParams} />
|
<UsersPaymentsTable searchParams={searchParams} />
|
||||||
</Suspense>
|
</Suspense>
|
||||||
|
@ -1,236 +1,176 @@
|
|||||||
// import Pagination from "@/components/pagination";
|
import Link from "next/link";
|
||||||
// import { Badge } from "@/components/ui/badge";
|
import { redirect } from "next/navigation";
|
||||||
// import { Button } from "@/components/ui/button";
|
import { getServerSession } from "next-auth";
|
||||||
// import {
|
import { getPayments } from "@/actions/payment";
|
||||||
// Table,
|
import { authOptions } from "@/app/auth";
|
||||||
// TableBody,
|
import Pagination from "@/components/pagination";
|
||||||
// TableCaption,
|
import { Badge } from "@/components/ui/badge";
|
||||||
// TableCell,
|
import { Button } from "@/components/ui/button";
|
||||||
// TableFooter,
|
import {
|
||||||
// TableHead,
|
Table,
|
||||||
// TableHeader,
|
TableBody,
|
||||||
// TableRow,
|
TableCaption,
|
||||||
// } from "@/components/ui/table";
|
TableCell,
|
||||||
// import Link from "next/link";
|
TableFooter,
|
||||||
|
TableHead,
|
||||||
|
TableHeader,
|
||||||
|
TableRow,
|
||||||
|
} from "@/components/ui/table";
|
||||||
|
import { tryCatch } from "@/utils/tryCatch";
|
||||||
|
import ClientErrorMessage from "../client-error-message";
|
||||||
|
|
||||||
export async function UsersPaymentsTable({
|
export async function UsersPaymentsTable({
|
||||||
searchParams,
|
searchParams,
|
||||||
}: {
|
}: {
|
||||||
searchParams: Promise<{
|
searchParams: Promise<{
|
||||||
query: string;
|
[key: string]: unknown;
|
||||||
page: number;
|
|
||||||
sortBy: string;
|
|
||||||
status: string;
|
|
||||||
}>;
|
}>;
|
||||||
}) {
|
}) {
|
||||||
const query = (await searchParams)?.query || "";
|
const resolvedParams = await searchParams;
|
||||||
console.log(query);
|
|
||||||
// const page = (await searchParams)?.page;
|
|
||||||
// const sortBy = (await searchParams)?.sortBy || "asc";
|
|
||||||
// const totalPayments = await prisma.payment.count({
|
|
||||||
// where: {
|
|
||||||
// OR: [
|
|
||||||
// {
|
|
||||||
// user: {
|
|
||||||
// name: {
|
|
||||||
// contains: query || "",
|
|
||||||
// mode: "insensitive",
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// user: {
|
|
||||||
// phoneNumber: {
|
|
||||||
// contains: query || "",
|
|
||||||
// mode: "insensitive",
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// user: {
|
|
||||||
// address: {
|
|
||||||
// contains: query || "",
|
|
||||||
// mode: "insensitive",
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// user: {
|
|
||||||
// id_card: {
|
|
||||||
// contains: query || "",
|
|
||||||
// mode: "insensitive",
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// ],
|
|
||||||
// },
|
|
||||||
// });
|
|
||||||
|
|
||||||
// const totalPages = Math.ceil(totalPayments / 10);
|
const page = Number.parseInt(resolvedParams.page as string) || 1;
|
||||||
// const limit = 10;
|
const limit = 10;
|
||||||
// const offset = (Number(page) - 1) * limit || 0;
|
const offset = (page - 1) * limit;
|
||||||
|
|
||||||
// const payments = await prisma.payment.findMany({
|
// Build params object for getDevices
|
||||||
// where: {
|
const apiParams: Record<string, string | number | undefined> = {};
|
||||||
// OR: [
|
for (const [key, value] of Object.entries(resolvedParams)) {
|
||||||
// {
|
if (value !== undefined && value !== "") {
|
||||||
// user: {
|
apiParams[key] = typeof value === "number" ? value : String(value);
|
||||||
// name: {
|
}
|
||||||
// contains: query || "",
|
}
|
||||||
// mode: "insensitive",
|
apiParams.limit = limit;
|
||||||
// }
|
apiParams.offset = offset;
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// user: {
|
|
||||||
// phoneNumber: {
|
|
||||||
// contains: query || "",
|
|
||||||
// mode: "insensitive",
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// user: {
|
|
||||||
// address: {
|
|
||||||
// contains: query || "",
|
|
||||||
// mode: "insensitive",
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// user: {
|
|
||||||
// id_card: {
|
|
||||||
// contains: query || "",
|
|
||||||
// mode: "insensitive",
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// ],
|
|
||||||
// },
|
|
||||||
// include: {
|
|
||||||
// user: true,
|
|
||||||
// devices: true,
|
|
||||||
// },
|
|
||||||
// skip: offset,
|
|
||||||
// take: limit,
|
|
||||||
// orderBy: {
|
|
||||||
// id: `${sortBy}` as "asc" | "desc",
|
|
||||||
// },
|
|
||||||
// });
|
|
||||||
|
|
||||||
// const users = await prisma.user.findMany({
|
const [error, payments] = await tryCatch(getPayments(apiParams, true));
|
||||||
// where: {
|
if (error) {
|
||||||
// role: "USER",
|
if (error.message === "UNAUTHORIZED") {
|
||||||
// },
|
redirect("/auth/signin");
|
||||||
// include: {
|
} else {
|
||||||
// atoll: true,
|
return <ClientErrorMessage message={error.message} />;
|
||||||
// island: true,
|
}
|
||||||
// },
|
}
|
||||||
// });
|
const { meta, data } = payments;
|
||||||
return null;
|
|
||||||
// return (
|
|
||||||
// <div>
|
|
||||||
// {payments.length === 0 ? (
|
|
||||||
// <div className="h-[calc(100svh-400px)] flex flex-col items-center justify-center my-4">
|
|
||||||
// <h3>No user payments yet.</h3>
|
|
||||||
// </div>
|
|
||||||
// ) : (
|
|
||||||
// <>
|
|
||||||
// <Table className="overflow-scroll">
|
|
||||||
// <TableCaption>Table of all users.</TableCaption>
|
|
||||||
// <TableHeader>
|
|
||||||
// <TableRow>
|
|
||||||
// <TableHead>Devices paid</TableHead>
|
|
||||||
// <TableHead>User</TableHead>
|
|
||||||
// <TableHead>Amount</TableHead>
|
|
||||||
// <TableHead>Duration</TableHead>
|
|
||||||
// <TableHead>Payment Status</TableHead>
|
|
||||||
// <TableHead>Payment Method</TableHead>
|
|
||||||
// <TableHead>Paid At</TableHead>
|
|
||||||
// <TableHead>Action</TableHead>
|
|
||||||
// </TableRow>
|
|
||||||
// </TableHeader>
|
|
||||||
// <TableBody className="overflow-scroll">
|
|
||||||
// {payments.map((payment) => (
|
|
||||||
// <TableRow
|
|
||||||
// className={`${payment.paid && "title-bg dark:bg-black"}`}
|
|
||||||
// key={payment.id}
|
|
||||||
// >
|
|
||||||
// <TableCell className="font-medium">
|
|
||||||
// <ol className="list-disc list-inside text-sm">
|
|
||||||
// {payment.devices.map((device) => (
|
|
||||||
// <li
|
|
||||||
// key={device.id}
|
|
||||||
// className="text-sm text-muted-foreground"
|
|
||||||
// >
|
|
||||||
// {device.name}
|
|
||||||
// </li>
|
|
||||||
// ))}
|
|
||||||
// </ol>
|
|
||||||
// </TableCell>
|
|
||||||
// <TableCell className="font-medium">
|
|
||||||
// {payment.user.id_card}
|
|
||||||
// </TableCell>
|
|
||||||
// <TableCell>{payment.user?.name}</TableCell>
|
|
||||||
// <TableCell>{payment.user?.name}</TableCell>
|
|
||||||
// <TableCell>{payment.id}</TableCell>
|
|
||||||
|
|
||||||
// <TableCell>
|
// return <pre>{JSON.stringify(payments, null, 2)}</pre>;
|
||||||
// {payment.paid ? (
|
return (
|
||||||
// <Badge
|
<div>
|
||||||
// variant="outline"
|
{data.length === 0 ? (
|
||||||
// className="bg-lime-100 text-black"
|
<div className="h-[calc(100svh-400px)] flex flex-col items-center justify-center my-4">
|
||||||
// >
|
<h3>No user payments yet.</h3>
|
||||||
// Verified
|
</div>
|
||||||
// </Badge>
|
) : (
|
||||||
// ) : (
|
<>
|
||||||
// <Badge
|
<Table className="overflow-scroll">
|
||||||
// variant="outline"
|
<TableCaption>Table of all users.</TableCaption>
|
||||||
// className="bg-yellow-100 text-black"
|
<TableHeader>
|
||||||
// >
|
<TableRow>
|
||||||
// Unverified
|
<TableHead>Devices paid</TableHead>
|
||||||
// </Badge>
|
<TableHead>User</TableHead>
|
||||||
// )}
|
<TableHead>Amount</TableHead>
|
||||||
// </TableCell>
|
<TableHead>Duration</TableHead>
|
||||||
// <TableCell>
|
<TableHead>Payment Status</TableHead>
|
||||||
// {new Date(payment.paidAt ?? "").toLocaleDateString(
|
<TableHead>Payment Method</TableHead>
|
||||||
// "en-US",
|
<TableHead>MIB Reference</TableHead>
|
||||||
// {
|
<TableHead>Paid At</TableHead>
|
||||||
// month: "short",
|
<TableHead>Action</TableHead>
|
||||||
// day: "2-digit",
|
</TableRow>
|
||||||
// year: "numeric",
|
</TableHeader>
|
||||||
// },
|
<TableBody className="overflow-scroll">
|
||||||
// )}
|
{data.map((payment) => (
|
||||||
// </TableCell>
|
<TableRow
|
||||||
|
className={`${payment.paid && "title-bg dark:bg-black"}`}
|
||||||
|
key={payment.id}
|
||||||
|
>
|
||||||
|
<TableCell className="font-medium">
|
||||||
|
<ol className="list-disc list-inside text-sm">
|
||||||
|
{payment.devices.map((device) => (
|
||||||
|
<li
|
||||||
|
key={device.id}
|
||||||
|
className="text-sm text-muted-foreground"
|
||||||
|
>
|
||||||
|
{device.name}
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ol>
|
||||||
|
</TableCell>
|
||||||
|
<TableCell className="font-medium">
|
||||||
|
{/* {payment.user.id_card} */}
|
||||||
|
<div className="flex flex-col items-start">
|
||||||
|
{payment.devices[0]?.user?.name}
|
||||||
|
<span className="text-muted-foreground">
|
||||||
|
{payment.devices[0]?.user?.id_card}
|
||||||
|
</span>
|
||||||
|
</div>{" "}
|
||||||
|
</TableCell>
|
||||||
|
<TableCell>{payment.amount}</TableCell>
|
||||||
|
<TableCell>{payment.number_of_months} Months</TableCell>
|
||||||
|
|
||||||
// <TableCell>{payment.id}</TableCell>
|
<TableCell>
|
||||||
// <TableCell>
|
{payment.status === "PENDING" ? (
|
||||||
// <Link href={`/payments/${payment.id}/verify`}>
|
<Badge
|
||||||
// <Button>Details</Button>
|
variant="outline"
|
||||||
// </Link>
|
className="bg-yellow-100 text-black"
|
||||||
// </TableCell>
|
>
|
||||||
// </TableRow>
|
{payment.status}
|
||||||
// ))}
|
</Badge>
|
||||||
// </TableBody>
|
) : payment.status === "PAID" ? (
|
||||||
// <TableFooter>
|
<Badge
|
||||||
// <TableRow>
|
variant="outline"
|
||||||
// <TableCell colSpan={8}>
|
className="bg-lime-100 text-black"
|
||||||
// {query.length > 0 && (
|
>
|
||||||
// <p className="text-sm text-muted-foreground">
|
{payment.status}
|
||||||
// Showing {payments.length} locations for "{query}
|
</Badge>
|
||||||
// "
|
) : (
|
||||||
// </p>
|
<Badge
|
||||||
// )}
|
variant="outline"
|
||||||
// </TableCell>
|
className="bg-red-100 text-black"
|
||||||
// <TableCell className="text-muted-foreground">
|
>
|
||||||
// {totalPayments} payments
|
{payment.status}
|
||||||
// </TableCell>
|
</Badge>
|
||||||
// </TableRow>
|
)}
|
||||||
// </TableFooter>
|
</TableCell>
|
||||||
// </Table>
|
<TableCell>{payment.method}</TableCell>
|
||||||
// <Pagination totalPages={totalPages} currentPage={page} />
|
<TableCell>{payment.mib_reference}</TableCell>
|
||||||
// </>
|
<TableCell>
|
||||||
// )}
|
{new Date(payment.paid_at ?? "").toLocaleDateString(
|
||||||
// </div>
|
"en-US",
|
||||||
// );
|
{
|
||||||
|
month: "short",
|
||||||
|
day: "2-digit",
|
||||||
|
year: "numeric",
|
||||||
|
minute: "2-digit",
|
||||||
|
hour: "2-digit",
|
||||||
|
},
|
||||||
|
)}
|
||||||
|
</TableCell>
|
||||||
|
|
||||||
|
<TableCell>
|
||||||
|
<Link href={`/payments/${payment.id}/verify`}>
|
||||||
|
<Button>Details</Button>
|
||||||
|
</Link>
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
))}
|
||||||
|
</TableBody>
|
||||||
|
<TableFooter>
|
||||||
|
<TableRow>
|
||||||
|
<TableCell colSpan={10} className="text-muted-foreground">
|
||||||
|
{meta?.total === 1 ? (
|
||||||
|
<p className="text-center">Total {meta?.total} payment.</p>
|
||||||
|
) : (
|
||||||
|
<p className="text-center">Total {meta?.total} payments.</p>
|
||||||
|
)}{" "}
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
</TableFooter>
|
||||||
|
</Table>
|
||||||
|
<Pagination
|
||||||
|
totalPages={meta?.last_page}
|
||||||
|
currentPage={meta?.current_page}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user