"use server"; import prisma from "@/lib/db"; import type { GroupProfile, MacAddress, OmadaResponse } from "@/lib/types"; import { formatMacAddress } from "@/lib/utils"; import { revalidatePath } from "next/cache"; async function fetchOmadaGroupProfiles(siteId: string): Promise { if (!siteId) { throw new Error("siteId is a required parameter"); } const baseUrl: string = process.env.OMADA_BASE_URL || ""; const url: string = `${baseUrl}/api/v2/sites/${siteId}/setting/profiles/groups`; const headers: HeadersInit = { "X-API-key": process.env.OMADA_PROXY_API_KEY || "", }; try { const response: Response = await fetch(url, { method: "GET", headers: headers, }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data: OmadaResponse = await response.json(); if (data.errorCode !== 0) { throw new Error(`Error fetching group profiles: ${data.msg}`); } console.log({ data }); return data; } catch (error) { console.error("Error fetching Omada group profiles:", error); throw error instanceof Error ? error : new Error("Unknown error occurred"); } } export { fetchOmadaGroupProfiles }; export async function addDevicesToGroup({ siteId, groupId, newDevices, }: { siteId?: string; groupId?: string; newDevices: MacAddress[]; }) { if (!siteId || !groupId) { throw new Error("omadacId, siteId, and groupId are required parameters"); } try { // Fetch the existing group profiles const groupProfiles: OmadaResponse = await fetchOmadaGroupProfiles(siteId); // console.log(groupProfiles); // Find the group profile with the specified groupId const groupProfile: GroupProfile | undefined = groupProfiles.result.data.find((profile) => profile.groupId === groupId); if (!groupProfile) { throw new Error(`Group with ID ${groupId} not found`); } // Create a new array with the existing and new devices const updatedMacAddressList: MacAddress[] = [ ...(groupProfile.macAddressList || []), ...newDevices, ]; // console.log({ updatedMacAddressList }); // Prepare the request payload const requestBody = { name: groupProfile.name, type: groupProfile.type, resource: groupProfile.resource, ipList: groupProfile.ipList, ipv6List: groupProfile.ipv6List, macAddressList: updatedMacAddressList, portList: null, countryList: null, portType: null, portMaskList: null, domainNamePort: null, }; console.log(requestBody); const baseUrl = process.env.OMADA_BASE_URL || ""; const url: string = `${baseUrl}/api/v2/sites/${siteId}/setting/profiles/groups/2/${groupId}`; const headers: HeadersInit = { "X-API-key": process.env.OMADA_PROXY_API_KEY || "", }; const response = await fetch(url, { method: "PATCH", headers: headers, body: JSON.stringify(requestBody), }); console.log(response.status); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } } catch (error) { console.error("Error adding devices to group:", error); throw error instanceof Error ? error : new Error("Unknown error occurred"); } } export async function blockDevice({ macAddress, type, reason, blockedBy = "PARENT", }: { macAddress: string; type: "block" | "unblock"; reason?: string; blockedBy?: "ADMIN" | "PARENT"; }) { console.log("hello world asdasd"); 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}`; console.log(url); const headers: HeadersInit = { "X-API-key": process.env.OMADA_PROXY_API_KEY || "", }; const response = await fetch(url, { method: "POST", headers: headers, }); console.log("blocking..."); console.log(response); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } await prisma.device.update({ where: { id: device?.id, }, data: { reasonForBlocking: type === "block" ? reason : "", blocked: type === "block", blockedBy: blockedBy, }, }); revalidatePath("/parental-control"); } catch (error) { console.error("Error blocking device:", error); throw error instanceof Error ? error : new Error("Unknown error occurred"); } }