- Simplified fetchOmadaGroupProfiles function by removing unnecessary parameters and using environment variables for base URL and API key. - Improved error handling in fetchOmadaGroupProfiles to check for error codes in the response. - Updated addDevicesToGroup function to remove redundant parameters and streamline device addition logic. - Integrated formatMacAddress utility to ensure consistent MAC address formatting during payment verification. - Enhanced verifyPayment function to include device addition upon successful payment verification, with improved logging. - Refactored DevicesToPay component to clean up payment verification logic and improve UI responsiveness. These changes enhance the clarity and efficiency of device management and payment processing functionalities.
141 lines
3.4 KiB
TypeScript
141 lines
3.4 KiB
TypeScript
interface IpAddress {
|
|
ip: string;
|
|
mask: number;
|
|
}
|
|
|
|
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[];
|
|
};
|
|
}
|
|
|
|
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, type MacAddress };
|
|
|
|
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);
|
|
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");
|
|
}
|
|
}
|