Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(roles): handle empty response in deleteRole and ensure revalidation #6977

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions ui/actions/invitations/invitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";

import { auth } from "@/auth.config";
import { getErrorMessage, parseStringify, wait } from "@/lib";
import { getErrorMessage, parseStringify } from "@/lib";

export const getInvitations = async ({
page = 1,
Expand Down Expand Up @@ -189,23 +189,43 @@ export const getInvitationInfoById = async (invitationId: string) => {
export const revokeInvite = async (formData: FormData) => {
const session = await auth();
const keyServer = process.env.API_BASE_URL;

const invitationId = formData.get("invitationId");

if (!invitationId) {
return { error: "Invitation ID is required" };
}

const url = new URL(`${keyServer}/tenants/invitations/${invitationId}`);

try {
const response = await fetch(url.toString(), {
method: "DELETE",
headers: {
Authorization: `Bearer ${session?.accessToken}`,
},
});
const data = await response.json();
await wait(1000);

if (!response.ok) {
try {
const errorData = await response.json();
throw new Error(
errorData?.message || "Failed to revoke the invitation",
);
} catch {
throw new Error("Failed to revoke the invitation");
}
}

let data = null;
if (response.status !== 204) {
data = await response.json();
}

revalidatePath("/invitations");
return parseStringify(data);
return data || { success: true };
} catch (error) {
return {
error: getErrorMessage(error),
};
// eslint-disable-next-line no-console
console.error("Error revoking invitation:", error);
return { error: getErrorMessage(error) };
}
};
35 changes: 27 additions & 8 deletions ui/actions/manage-groups/manage-groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";

import { auth } from "@/auth.config";
import { getErrorMessage, parseStringify, wait } from "@/lib";
import { getErrorMessage, parseStringify } from "@/lib";
import { ManageGroupPayload, ProviderGroupsResponse } from "@/types/components";

export const getProviderGroups = async ({
Expand Down Expand Up @@ -210,8 +210,12 @@ export const updateProviderGroup = async (
export const deleteProviderGroup = async (formData: FormData) => {
const session = await auth();
const keyServer = process.env.API_BASE_URL;

const providerGroupId = formData.get("id");

if (!providerGroupId) {
return { error: "Provider Group ID is required" };
}

const url = new URL(`${keyServer}/provider-groups/${providerGroupId}`);

try {
Expand All @@ -221,13 +225,28 @@ export const deleteProviderGroup = async (formData: FormData) => {
Authorization: `Bearer ${session?.accessToken}`,
},
});
const data = await response.json();
await wait(2000);

if (!response.ok) {
try {
const errorData = await response.json();
throw new Error(
errorData?.message || "Failed to delete the provider group",
);
} catch {
throw new Error("Failed to delete the provider group");
}
}

let data = null;
if (response.status !== 204) {
data = await response.json();
}

revalidatePath("/manage-groups");
return parseStringify(data);
return data || { success: true };
} catch (error) {
return {
error: getErrorMessage(error),
};
// eslint-disable-next-line no-console
console.error("Error deleting provider group:", error);
return { error: getErrorMessage(error) };
}
};
62 changes: 50 additions & 12 deletions ui/actions/providers/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,11 @@ export const checkConnectionProvider = async (formData: FormData) => {
export const deleteCredentials = async (secretId: string) => {
const session = await auth();
const keyServer = process.env.API_BASE_URL;

if (!secretId) {
return { error: "Secret ID is required" };
}

const url = new URL(`${keyServer}/providers/secrets/${secretId}`);

try {
Expand All @@ -389,21 +394,41 @@ export const deleteCredentials = async (secretId: string) => {
Authorization: `Bearer ${session?.accessToken}`,
},
});
const data = await response.json();

if (!response.ok) {
try {
const errorData = await response.json();
throw new Error(
errorData?.message || "Failed to delete the credentials",
);
} catch {
throw new Error("Failed to delete the credentials");
}
}

let data = null;
if (response.status !== 204) {
data = await response.json();
}

revalidatePath("/providers");
return parseStringify(data);
return data || { success: true };
} catch (error) {
return {
error: getErrorMessage(error),
};
// eslint-disable-next-line no-console
console.error("Error deleting credentials:", error);
return { error: getErrorMessage(error) };
}
};

export const deleteProvider = async (formData: FormData) => {
const session = await auth();
const keyServer = process.env.API_BASE_URL;

const providerId = formData.get("id");

if (!providerId) {
return { error: "Provider ID is required" };
}

const url = new URL(`${keyServer}/providers/${providerId}`);

try {
Expand All @@ -413,13 +438,26 @@ export const deleteProvider = async (formData: FormData) => {
Authorization: `Bearer ${session?.accessToken}`,
},
});
const data = await response.json();
await wait(1000);

if (!response.ok) {
try {
const errorData = await response.json();
throw new Error(errorData?.message || "Failed to delete the provider");
} catch {
throw new Error("Failed to delete the provider");
}
}

let data = null;
if (response.status !== 204) {
data = await response.json();
}

revalidatePath("/providers");
return parseStringify(data);
return data || { success: true };
} catch (error) {
return {
error: getErrorMessage(error),
};
// eslint-disable-next-line no-console
console.error("Error deleting provider:", error);
return { error: getErrorMessage(error) };
}
};
22 changes: 15 additions & 7 deletions ui/actions/roles/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,24 @@ export const deleteRole = async (roleId: string) => {
});

if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData?.message || "Failed to delete the role");
try {
const errorData = await response.json();
throw new Error(errorData?.message || "Failed to delete the role");
} catch {
throw new Error("Failed to delete the role");
}
}

let data = null;
if (response.status !== 204) {
data = await response.json();
}

const data = await response.json();
revalidatePath("/roles");
return data;
return data || { success: true };
} catch (error) {
return {
error: getErrorMessage(error),
};
// eslint-disable-next-line no-console
console.error("Error deleting role:", error);
return { error: getErrorMessage(error) };
}
};
32 changes: 25 additions & 7 deletions ui/actions/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,24 +157,42 @@ export const updateUserRole = async (formData: FormData) => {
export const deleteUser = async (formData: FormData) => {
const session = await auth();
const keyServer = process.env.API_BASE_URL;

const userId = formData.get("userId");

if (!userId) {
return { error: "User ID is required" };
}

const url = new URL(`${keyServer}/users/${userId}`);

try {
const response = await fetch(url.toString(), {
method: "DELETE",
headers: {
Authorization: `Bearer ${session?.accessToken}`,
},
});
const data = await response.json();
await wait(1000);

if (!response.ok) {
try {
const errorData = await response.json();
throw new Error(errorData?.message || "Failed to delete the user");
} catch {
throw new Error("Failed to delete the user");
}
}

let data = null;
if (response.status !== 204) {
data = await response.json();
}

revalidatePath("/users");
return parseStringify(data);
return data || { success: true };
} catch (error) {
return {
error: getErrorMessage(error),
};
// eslint-disable-next-line no-console
console.error("Error deleting user:", error);
return { error: getErrorMessage(error) };
}
};

Expand Down
Loading