mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-02-22 17:42:00 +00:00
- Added a new `bun.lockb` file for dependency management. - Updated `next.config.ts` to set output to "standalone" for better deployment options. - Removed `package-lock.json` to streamline package management. - Modified `package.json` to update dependencies, including `@prisma/client` and `sonner`, and adjusted build scripts for improved functionality. - Enhanced Tailwind CSS configuration to include new animations and color schemes. - Refactored various dashboard components to improve UI consistency, including adding a new `My Wallet` page and updating existing pages to use a unified styling approach. - Introduced a new `BlockDeviceDialog` component for managing device blocking with user-defined reasons. - Improved logging and error handling in payment verification and device management functions. These changes enhance the overall functionality, maintainability, and user experience of the application.
158 lines
4.3 KiB
TypeScript
158 lines
4.3 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,
|
|
reason
|
|
}: { macAddress: string; type: "block" | "unblock", reason?: string }) {
|
|
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",
|
|
},
|
|
});
|
|
revalidatePath("/parental-control");
|
|
} catch (error) {
|
|
console.error("Error blocking device:", error);
|
|
throw error instanceof Error ? error : new Error("Unknown error occurred");
|
|
}
|
|
}
|