mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-03 06:48:21 +00:00
Implement parental control features and enhance device management
- 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.
This commit is contained in:
@ -1,39 +1,9 @@
|
||||
interface IpAddress {
|
||||
ip: string;
|
||||
mask: number;
|
||||
}
|
||||
"use server";
|
||||
|
||||
interface Ipv6Address {
|
||||
ip: string;
|
||||
prefix: number;
|
||||
}
|
||||
|
||||
interface MacAddress {
|
||||
ruleId?: number;
|
||||
name: string;
|
||||
macAddress: string;
|
||||
}
|
||||
|
||||
interface GroupProfile {
|
||||
groupId: string;
|
||||
site?: string;
|
||||
name: string;
|
||||
buildIn?: boolean;
|
||||
ipList?: IpAddress[];
|
||||
ipv6List?: Ipv6Address[];
|
||||
macAddressList?: MacAddress[];
|
||||
count: number;
|
||||
type: number;
|
||||
resource: number;
|
||||
}
|
||||
|
||||
interface OmadaResponse {
|
||||
errorCode: number;
|
||||
msg: string;
|
||||
result: {
|
||||
data: GroupProfile[];
|
||||
};
|
||||
}
|
||||
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) {
|
||||
@ -68,7 +38,7 @@ async function fetchOmadaGroupProfiles(siteId: string): Promise<OmadaResponse> {
|
||||
}
|
||||
}
|
||||
|
||||
export { fetchOmadaGroupProfiles, type MacAddress };
|
||||
export { fetchOmadaGroupProfiles };
|
||||
|
||||
export async function addDevicesToGroup({
|
||||
siteId,
|
||||
@ -129,7 +99,7 @@ export async function addDevicesToGroup({
|
||||
headers: headers,
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
console.log(response);
|
||||
console.log(response.status);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
@ -138,3 +108,48 @@ export async function addDevicesToGroup({
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user