Skip to content

fix: use insert and delete instead of upsert for custom roles #14252

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: use insert and update instead of upsert
  • Loading branch information
jaaydenh committed Aug 12, 2024
commit 704211933bd60fddde90621d55a91a77a299b8b0
19 changes: 17 additions & 2 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,11 +603,26 @@ class ApiMethods {
/**
* @param organization Can be the organization's ID or name
*/
patchOrganizationRole = async (
createOrganizationRole = async (
organization: string,
role: TypesGen.Role,
): Promise<TypesGen.Role> => {
const response = await this.axios.patch<TypesGen.Role>(
const response = await this.axios.post<TypesGen.Role>(
`/api/v2/organizations/${organization}/members/roles`,
role,
);

return response.data;
};

/**
* @param organization Can be the organization's ID or name
*/
updateOrganizationRole = async (
organization: string,
role: TypesGen.Role,
): Promise<TypesGen.Role> => {
const response = await this.axios.put<TypesGen.Role>(
`/api/v2/organizations/${organization}/members/roles`,
role,
);
Expand Down
18 changes: 16 additions & 2 deletions site/src/api/queries/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,27 @@ export const organizationRoles = (organization: string) => {
};
};

export const patchOrganizationRole = (
export const createOrganizationRole = (
queryClient: QueryClient,
organization: string,
) => {
return {
mutationFn: (request: Role) =>
API.patchOrganizationRole(organization, request),
API.createOrganizationRole(organization, request),
onSuccess: async (updatedRole: Role) =>
await queryClient.invalidateQueries(
getRoleQueryKey(organization, updatedRole.name),
),
};
};

export const updateOrganizationRole = (
queryClient: QueryClient,
organization: string,
) => {
return {
mutationFn: (request: Role) =>
API.updateOrganizationRole(organization, request),
onSuccess: async (updatedRole: Role) =>
await queryClient.invalidateQueries(
getRoleQueryKey(organization, updatedRole.name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import { useMutation, useQuery, useQueryClient } from "react-query";
import { useNavigate, useParams } from "react-router-dom";
import { getErrorMessage } from "api/errors";
import { organizationPermissions } from "api/queries/organizations";
import { patchOrganizationRole, organizationRoles } from "api/queries/roles";
import type { PatchRoleRequest } from "api/typesGenerated";
import {
organizationRoles,
createOrganizationRole,
updateOrganizationRole,
} from "api/queries/roles";
import type { CustomRoleRequest } from "api/typesGenerated";
import { displayError } from "components/GlobalSnackbar/utils";
import { Loader } from "components/Loader/Loader";
import { pageTitle } from "utils/page";
Expand All @@ -22,8 +26,11 @@ export const CreateEditRolePage: FC = () => {
const { organizations } = useOrganizationSettings();
const organization = organizations?.find((o) => o.name === organizationName);
const permissionsQuery = useQuery(organizationPermissions(organization?.id));
const patchOrganizationRoleMutation = useMutation(
patchOrganizationRole(queryClient, organizationName),
const createOrganizationRoleMutation = useMutation(
createOrganizationRole(queryClient, organizationName),
);
const updateOrganizationRoleMutation = useMutation(
updateOrganizationRole(queryClient, organizationName),
);
const { data: roleData, isLoading } = useQuery(
organizationRoles(organizationName),
Expand All @@ -47,18 +54,31 @@ export const CreateEditRolePage: FC = () => {

<CreateEditRolePageView
role={role}
onSubmit={async (data: PatchRoleRequest) => {
onSubmit={async (data: CustomRoleRequest) => {
try {
await patchOrganizationRoleMutation.mutateAsync(data);
navigate(`/organizations/${organizationName}/roles`);
if (role) {
await updateOrganizationRoleMutation.mutateAsync(data);
navigate(`/organizations/${organizationName}/roles`);
} else {
await createOrganizationRoleMutation.mutateAsync(data);
navigate(`/organizations/${organizationName}/roles`);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (role) {
await updateOrganizationRoleMutation.mutateAsync(data);
navigate(`/organizations/${organizationName}/roles`);
} else {
await createOrganizationRoleMutation.mutateAsync(data);
navigate(`/organizations/${organizationName}/roles`);
}
if (role) {
await updateOrganizationRoleMutation.mutateAsync(data);
} else {
await createOrganizationRoleMutation.mutateAsync(data);
}
navigate(`/organizations/${organizationName}/roles`);

} catch (error) {
displayError(
getErrorMessage(error, "Failed to update custom role"),
);
}
}}
error={patchOrganizationRoleMutation.error}
isLoading={patchOrganizationRoleMutation.isLoading}
error={
role
? updateOrganizationRoleMutation.error
: createOrganizationRoleMutation.error
}
isLoading={
role
? updateOrganizationRoleMutation.isLoading
: createOrganizationRoleMutation.isLoading
}
organizationName={organizationName}
canAssignOrgRole={permissions.assignOrgRole}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { isApiValidationError } from "api/errors";
import { RBACResourceActions } from "api/rbacresources_gen";
import type {
Role,
PatchRoleRequest,
CustomRoleRequest,
Permission,
AssignableRoles,
RBACResource,
Expand All @@ -38,7 +38,7 @@ const validationSchema = Yup.object({

export type CreateEditRolePageViewProps = {
role: AssignableRoles | undefined;
onSubmit: (data: PatchRoleRequest) => void;
onSubmit: (data: CustomRoleRequest) => void;
error?: unknown;
isLoading: boolean;
organizationName: string;
Expand All @@ -58,7 +58,7 @@ export const CreateEditRolePageView: FC<CreateEditRolePageViewProps> = ({
const navigate = useNavigate();
const onCancel = () => navigate(-1);

const form = useFormik<PatchRoleRequest>({
const form = useFormik<CustomRoleRequest>({
initialValues: {
name: role?.name || "",
display_name: role?.display_name || "",
Expand Down
Loading