mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-02-22 17:42:00 +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.
156 lines
4.2 KiB
TypeScript
156 lines
4.2 KiB
TypeScript
"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<OmadaResponse> {
|
|
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,
|
|
}: { macAddress: string; type: "block" | "unblock" }) {
|
|
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: {
|
|
blocked: type === "block",
|
|
},
|
|
});
|
|
revalidatePath("/parental-control");
|
|
} catch (error) {
|
|
console.error("Error blocking device:", error);
|
|
throw error instanceof Error ? error : new Error("Unknown error occurred");
|
|
}
|
|
}
|