feat: Scan on demand can be executed now from the UI

This commit is contained in:
Pablo Lara
2024-10-11 15:33:03 +02:00
parent 69a1468c18
commit f29e87f45b
11 changed files with 383 additions and 100 deletions
+1 -16
View File
@@ -4,7 +4,7 @@ import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { auth } from "@/auth.config";
import { parseStringify } from "@/lib";
import { getErrorMessage, parseStringify } from "@/lib";
export const getProviders = async ({
page = 1,
@@ -198,18 +198,3 @@ export const deleteProvider = async (formData: FormData) => {
};
}
};
export const getErrorMessage = async (error: unknown): Promise<string> => {
let message: string;
if (error instanceof Error) {
message = error.message;
} else if (error && typeof error === "object" && "message" in error) {
message = String(error.message);
} else if (typeof error === "string") {
message = error;
} else {
message = "Oops! Something went wrong.";
}
return message;
};
+49 -1
View File
@@ -4,7 +4,7 @@ import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { auth } from "@/auth.config";
import { parseStringify } from "@/lib";
import { getErrorMessage, parseStringify } from "@/lib";
export const getScans = async ({
page = 1,
@@ -46,3 +46,51 @@ export const getScans = async ({
return undefined;
}
};
export const scanOnDemand = async (formData: FormData) => {
const session = await auth();
const keyServer = process.env.API_BASE_URL;
const providerId = formData.get("providerId");
const scanName = formData.get("scanName");
const url = new URL(`${keyServer}/scans`);
try {
const response = await fetch(url.toString(), {
method: "POST",
headers: {
"Content-Type": "application/vnd.api+json",
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
body: JSON.stringify({
data: {
type: "Scan",
attributes: {
name: scanName,
scanner_args: {
checks_to_execute: ["accessanalyzer_enabled"],
},
},
relationships: {
provider: {
data: {
type: "Provider",
id: providerId,
},
},
},
},
}),
});
const data = await response.json();
revalidatePath("/scans");
return parseStringify(data);
} catch (error) {
console.error(error);
return {
error: getErrorMessage(error),
};
}
};