mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-02-23 00:01:59 +00:00
- Added a new Parental Control page for managing device access and notifications. - Introduced blockDevice function to handle blocking and unblocking devices based on payment status. - Enhanced omada-actions.ts to include device blocking logic and improved error handling. - Updated DevicesTable component to integrate BlockDeviceButton for managing device states. - Implemented API route for checking device statuses and sending notifications for expiring devices. - Refactored payment processing to update device statuses upon successful payment verification. - Added new utility functions for API key validation and SMS notifications. These changes improve user control over device management and enhance the overall functionality of the application.
169 lines
3.8 KiB
TypeScript
169 lines
3.8 KiB
TypeScript
import {
|
|
Table,
|
|
TableBody,
|
|
TableCaption,
|
|
TableCell,
|
|
TableFooter,
|
|
TableHead,
|
|
TableHeader,
|
|
TableRow,
|
|
} from "@/components/ui/table";
|
|
import { auth } from "@/lib/auth";
|
|
import prisma from "@/lib/db";
|
|
import { headers } from "next/headers";
|
|
import Link from "next/link";
|
|
import AddDevicesToCartButton from "./add-devices-to-cart-button";
|
|
import BlockDeviceButton from "./block-device-button";
|
|
import Pagination from "./pagination";
|
|
|
|
export async function DevicesTable({
|
|
searchParams,
|
|
parentalControl
|
|
}: {
|
|
searchParams: Promise<{
|
|
query: string;
|
|
page: number;
|
|
sortBy: string;
|
|
}>;
|
|
parentalControl?: boolean;
|
|
}) {
|
|
const session = await auth.api.getSession({
|
|
headers: await headers()
|
|
})
|
|
const query = (await searchParams)?.query || "";
|
|
const page = (await searchParams)?.page;
|
|
const sortBy = (await searchParams)?.sortBy || "asc";
|
|
const totalDevices = await prisma.device.count({
|
|
where: {
|
|
userId: session?.session.userId,
|
|
OR: [
|
|
{
|
|
name: {
|
|
contains: query || "",
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
{
|
|
mac: {
|
|
contains: query || "",
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
],
|
|
NOT: {
|
|
payment: {
|
|
paid: false
|
|
}
|
|
},
|
|
isActive: parentalControl ? parentalControl : undefined,
|
|
},
|
|
});
|
|
|
|
const totalPages = Math.ceil(totalDevices / 10);
|
|
const limit = 10;
|
|
const offset = (Number(page) - 1) * limit || 0;
|
|
|
|
const devices = await prisma.device.findMany({
|
|
where: {
|
|
userId: session?.session.userId,
|
|
OR: [
|
|
{
|
|
name: {
|
|
contains: query || "",
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
{
|
|
mac: {
|
|
contains: query || "",
|
|
mode: "insensitive",
|
|
},
|
|
},
|
|
],
|
|
NOT: {
|
|
payment: {
|
|
paid: false
|
|
}
|
|
},
|
|
isActive: parentalControl,
|
|
},
|
|
|
|
skip: offset,
|
|
take: limit,
|
|
orderBy: {
|
|
name: `${sortBy}` as "asc" | "desc",
|
|
},
|
|
});
|
|
|
|
return (
|
|
<div>
|
|
{devices.length === 0 ? (
|
|
<div className="h-[calc(100svh-400px)] flex flex-col items-center justify-center my-4">
|
|
<h3>No devices yet.</h3>
|
|
</div>
|
|
) : (
|
|
<>
|
|
<Table className="overflow-scroll">
|
|
<TableCaption>Table of all devices.</TableCaption>
|
|
<TableHeader>
|
|
<TableRow>
|
|
<TableHead>Device Name</TableHead>
|
|
<TableHead>MAC Address</TableHead>
|
|
<TableHead>Actions</TableHead>
|
|
</TableRow>
|
|
</TableHeader>
|
|
<TableBody className="overflow-scroll">
|
|
{devices.map((device) => (
|
|
<TableRow key={device.id}>
|
|
<TableCell>
|
|
<div className="flex flex-col items-start">
|
|
<Link
|
|
className="font-medium hover:underline"
|
|
href={`/devices/${device.id}`}
|
|
>
|
|
{device.name}
|
|
</Link>
|
|
<span className="text-muted-foreground">
|
|
Active until{" "}
|
|
{new Date().toLocaleDateString("en-US", {
|
|
month: "short",
|
|
day: "2-digit",
|
|
year: "numeric",
|
|
})}
|
|
</span>
|
|
</div>
|
|
</TableCell>
|
|
<TableCell className="font-medium">{device.mac}</TableCell>
|
|
<TableCell>
|
|
{!parentalControl ? (
|
|
<AddDevicesToCartButton device={device} />
|
|
) : (
|
|
<BlockDeviceButton device={device} />
|
|
)}
|
|
</TableCell>
|
|
</TableRow>
|
|
))}
|
|
</TableBody>
|
|
<TableFooter>
|
|
<TableRow>
|
|
<TableCell colSpan={2}>
|
|
{query.length > 0 && (
|
|
<p className="text-sm text-muted-foreground">
|
|
Showing {devices.length} locations for "{query}
|
|
"
|
|
</p>
|
|
)}
|
|
</TableCell>
|
|
<TableCell className="text-muted-foreground">
|
|
{totalDevices} devices
|
|
</TableCell>
|
|
</TableRow>
|
|
</TableFooter>
|
|
</Table>
|
|
<Pagination totalPages={totalPages} currentPage={page} />
|
|
</>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|