mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-04-20 03:50:20 +00:00
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 1m37s
103 lines
2.8 KiB
TypeScript
103 lines
2.8 KiB
TypeScript
"use server";
|
|
|
|
import { authOptions } from "@/app/auth";
|
|
import type { ApiError, ApiResponse, Device } from "@/lib/backend-types";
|
|
import { checkSession } from "@/utils/session";
|
|
import { getServerSession } from "next-auth";
|
|
import { revalidatePath } from "next/cache";
|
|
|
|
type GetDevicesProps = {
|
|
query?: string;
|
|
offset?: number;
|
|
limit?: number;
|
|
page?: number;
|
|
sortBy?: string;
|
|
status?: string;
|
|
};
|
|
export async function getDevices({ query, offset, limit }: GetDevicesProps) {
|
|
const session = await checkSession();
|
|
const response = await fetch(
|
|
`${process.env.SARLINK_API_BASE_URL}/api/devices/?name=${query}&offset=${offset}&limit=${limit}`,
|
|
{
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Token ${session?.apiToken}`,
|
|
},
|
|
},
|
|
);
|
|
|
|
if (!response.ok) {
|
|
const errorData = (await response.json()) as ApiError;
|
|
const errorMessage =
|
|
errorData.message || errorData.detail || "An error occurred.";
|
|
const error = new Error(errorMessage);
|
|
(error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object
|
|
throw error;
|
|
}
|
|
|
|
const data = (await response.json()) as ApiResponse<Device>;
|
|
return data;
|
|
}
|
|
|
|
export async function getDevice({ deviceId }: { deviceId: string }) {
|
|
const session = await checkSession();
|
|
const response = await fetch(
|
|
`${process.env.SARLINK_API_BASE_URL}/api/devices/${deviceId}/`,
|
|
{
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Token ${session?.apiToken}`,
|
|
},
|
|
},
|
|
);
|
|
if (!response.ok) {
|
|
const errorData = (await response.json()) as ApiError;
|
|
const errorMessage =
|
|
errorData.message || errorData.detail || "An error occurred.";
|
|
const error = new Error(errorMessage);
|
|
(error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object
|
|
throw error;
|
|
}
|
|
const device = (await response.json()) as Device;
|
|
return device;
|
|
}
|
|
|
|
export async function addDevice({
|
|
name,
|
|
mac,
|
|
}: {
|
|
name: string;
|
|
mac: string;
|
|
}) {
|
|
type SingleDevice = Pick<Device, "name" | "mac">;
|
|
const session = await getServerSession(authOptions);
|
|
const response = await fetch(
|
|
`${process.env.SARLINK_API_BASE_URL}/api/devices/`,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Token ${session?.apiToken}`,
|
|
},
|
|
body: JSON.stringify({
|
|
name: name,
|
|
mac: mac,
|
|
registered: true,
|
|
}),
|
|
},
|
|
);
|
|
if (!response.ok) {
|
|
const errorData = (await response.json()) as ApiError;
|
|
const errorMessage =
|
|
errorData.message || errorData.detail || "An error occurred.";
|
|
const error = new Error(errorMessage);
|
|
(error as ApiError & { details?: ApiError }).details = errorData; // Attach the errorData to the error object
|
|
throw error;
|
|
}
|
|
const data = (await response.json()) as SingleDevice;
|
|
revalidatePath("/devices");
|
|
return data;
|
|
}
|