mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-10-26 05:14:27 +00:00
refactor(ui): extract launchOrganizationScans server action
This commit is contained in:
2 files changed
+72
-15
No files matched your search
@@ -165,6 +165,72 @@ export const scheduleDaily = async (formData: FormData) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const launchOrganizationScans = async (
|
||||
providerIds: string[],
|
||||
scheduleOption: "daily" | "single",
|
||||
) => {
|
||||
const validProviderIds = providerIds.filter(Boolean);
|
||||
if (validProviderIds.length === 0) {
|
||||
return {
|
||||
successCount: 0,
|
||||
failureCount: 0,
|
||||
totalCount: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const launchPromises = validProviderIds.map(async (providerId) => {
|
||||
const formData = new FormData();
|
||||
formData.set("providerId", providerId);
|
||||
|
||||
const result =
|
||||
scheduleOption === "daily"
|
||||
? await scheduleDaily(formData)
|
||||
: await scanOnDemand(formData);
|
||||
|
||||
return {
|
||||
providerId,
|
||||
ok: !result?.error,
|
||||
error: result?.error ? String(result.error) : null,
|
||||
};
|
||||
});
|
||||
|
||||
const settled = await Promise.allSettled(launchPromises);
|
||||
const summary = settled.reduce(
|
||||
(acc, item) => {
|
||||
if (item.status === "fulfilled") {
|
||||
if (item.value.ok) {
|
||||
acc.successCount += 1;
|
||||
} else {
|
||||
acc.failureCount += 1;
|
||||
acc.errors.push({
|
||||
providerId: item.value.providerId,
|
||||
error: item.value.error || "Failed to launch scan.",
|
||||
});
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
|
||||
acc.failureCount += 1;
|
||||
acc.errors.push({
|
||||
providerId: "",
|
||||
error:
|
||||
item.reason instanceof Error
|
||||
? item.reason.message
|
||||
: String(item.reason),
|
||||
});
|
||||
return acc;
|
||||
},
|
||||
{
|
||||
successCount: 0,
|
||||
failureCount: 0,
|
||||
totalCount: validProviderIds.length,
|
||||
errors: [] as Array<{ providerId: string; error: string }>,
|
||||
},
|
||||
);
|
||||
|
||||
return summary;
|
||||
};
|
||||
|
||||
export const updateScan = async (formData: FormData) => {
|
||||
const headers = await getAuthHeaders({ contentType: true });
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
|
||||
import { scanOnDemand, scheduleDaily } from "@/actions/scans/scans";
|
||||
import { launchOrganizationScans } from "@/actions/scans/scans";
|
||||
import { AWSProviderBadge } from "@/components/icons/providers-badge";
|
||||
import {
|
||||
WIZARD_FOOTER_ACTION_TYPE,
|
||||
@@ -54,20 +54,11 @@ export function OrgLaunchScan({
|
||||
const handleLaunchScan = async () => {
|
||||
setIsLaunching(true);
|
||||
|
||||
let successCount = 0;
|
||||
|
||||
for (const providerId of createdProviderIds) {
|
||||
const formData = new FormData();
|
||||
formData.set("providerId", providerId);
|
||||
|
||||
const result =
|
||||
scheduleOption === SCAN_SCHEDULE.DAILY
|
||||
? await scheduleDaily(formData)
|
||||
: await scanOnDemand(formData);
|
||||
if (!result?.error) {
|
||||
successCount++;
|
||||
}
|
||||
}
|
||||
const result = await launchOrganizationScans(
|
||||
createdProviderIds,
|
||||
scheduleOption,
|
||||
);
|
||||
const successCount = result.successCount;
|
||||
|
||||
setIsLaunching(false);
|
||||
reset();
|
||||
|
||||
Reference in new issue
Block a user