diff --git a/actions/omada-actions.ts b/actions/omada-actions.ts index 4638677..af689bd 100644 --- a/actions/omada-actions.ts +++ b/actions/omada-actions.ts @@ -123,11 +123,7 @@ export async function blockDevice({ if (!macAddress) { throw new Error("macAddress is a required parameter"); } - // const device = await prisma.device.findFirst({ - // where: { - // mac: macAddress, - // }, - // }); + try { const baseUrl: string = process.env.OMADA_BASE_URL || ""; const url: string = `${baseUrl}/api/v2/sites/${process.env.OMADA_SITE_ID}/cmd/clients/${formatMacAddress(macAddress)}/${type}`; diff --git a/app/(dashboard)/devices/[deviceId]/page.tsx b/app/(dashboard)/devices/[deviceId]/page.tsx index 37739da..bd6dfb8 100644 --- a/app/(dashboard)/devices/[deviceId]/page.tsx +++ b/app/(dashboard)/devices/[deviceId]/page.tsx @@ -1,3 +1,8 @@ +import BlockDeviceDialog from "@/components/block-device-dialog"; +import Search from "@/components/search"; +import { Badge } from "@/components/ui/badge"; +import { getDevice } from "@/queries/devices"; +import { tryCatch } from "@/utils/tryCatch"; import React from "react"; export default async function DeviceDetails({ @@ -6,20 +11,46 @@ export default async function DeviceDetails({ params: Promise<{ deviceId: string }>; }) { const deviceId = (await params)?.deviceId; + const [error, device] = await tryCatch(getDevice({ deviceId: deviceId })); + if (error) return
+ Device active until{" "} + {new Date(device?.expiry_date || "").toLocaleDateString("en-US", { + month: "short", + day: "2-digit", + year: "numeric", + })} +
++ ACTIVE +
+ )} +{JSON.stringify(device.blocked, null, 2)}+
Inactive
+Device Inactive
)} {device.blocked_by === "ADMIN" && device.blocked && ( diff --git a/queries/devices.ts b/queries/devices.ts index 782e535..f03484d 100644 --- a/queries/devices.ts +++ b/queries/devices.ts @@ -2,6 +2,7 @@ import { authOptions } from "@/app/auth"; import type { Api400Error, ApiResponse, Device } from "@/lib/backend-types"; +import { checkSession } from "@/utils/session"; import { getServerSession } from "next-auth"; import { revalidatePath } from "next/cache"; @@ -12,7 +13,7 @@ type GetDevicesProps = { status?: string; }; export async function getDevices({ query }: GetDevicesProps) { - const session = await getServerSession(authOptions); + const session = await checkSession(); const respose = await fetch( `${process.env.SARLINK_API_BASE_URL}/api/devices/?name=${query}`, { @@ -27,6 +28,22 @@ export async function getDevices({ query }: GetDevicesProps) { return data; } +export async function getDevice({ deviceId }: { deviceId: string }) { + const session = await checkSession(); + const respose = await fetch( + `${process.env.SARLINK_API_BASE_URL}/api/devices/${deviceId}/`, + { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: `Token ${session?.apiToken}`, + }, + }, + ); + const device = (await respose.json()) as Device; + return device; +} + export async function addDevice({ name, mac, diff --git a/utils/session.ts b/utils/session.ts new file mode 100644 index 0000000..7783cee --- /dev/null +++ b/utils/session.ts @@ -0,0 +1,11 @@ +"use server"; + +import { authOptions } from "@/app/auth"; +import { getServerSession } from "next-auth"; +import { redirect } from "next/navigation"; + +export async function checkSession() { + const session = await getServerSession(authOptions); + if (!session) return redirect("/auth/signin"); + return session; +}