Files
coder/site/src/api/queries/organizations.ts
T
Jaayden Halko 2e05329111 feat: add custom roles (#14069)
* feat: initial commit custom roles

* feat: add page to create and edit custom roles

* feat: add assign org role permission

* feat: wip

* feat: cleanup

* fix: role name is disabled when editing the role

* fix: assign role context menu falls back to name when no display_name

* feat: add helper text to let users know that role name is immutable

* fix: format

* feat: - hide custom roles tab if experiment is not enabled

* fix: use custom TableLoader

* fix: fix custom roles text

* fix: use PatchRoleRequest

* fix: use addIcon to create roles

* feat: add cancel and save buttons to top of page

* fix: use nameValidator for name

* chore: cleanup

* feat: add show all permissions checkbox

* fix: update sidebar for roles

* fix: fix format

* fix: custom roles is not needed outside orgs

* fix: fix sidebar stories

* feat: add custom roles page stories

* fix: use organization permissions

* feat: add stories for CreateEditRolePageView

* fix: design improvements for the create edit role form

* feat: add show all resources checkbox to bottom of table

* feat: improve spacing
2024-08-08 21:05:20 -04:00

191 lines
4.8 KiB
TypeScript

import type { QueryClient } from "react-query";
import { API } from "api/api";
import type {
CreateOrganizationRequest,
UpdateOrganizationRequest,
} from "api/typesGenerated";
import { meKey } from "./users";
export const createOrganization = (queryClient: QueryClient) => {
return {
mutationFn: (params: CreateOrganizationRequest) =>
API.createOrganization(params),
onSuccess: async () => {
await queryClient.invalidateQueries(meKey);
await queryClient.invalidateQueries(organizationsKey);
},
};
};
interface UpdateOrganizationVariables {
organizationId: string;
req: UpdateOrganizationRequest;
}
export const updateOrganization = (queryClient: QueryClient) => {
return {
mutationFn: (variables: UpdateOrganizationVariables) =>
API.updateOrganization(variables.organizationId, variables.req),
onSuccess: async () => {
await queryClient.invalidateQueries(organizationsKey);
},
};
};
export const deleteOrganization = (queryClient: QueryClient) => {
return {
mutationFn: (organizationId: string) =>
API.deleteOrganization(organizationId),
onSuccess: async () => {
await queryClient.invalidateQueries(meKey);
await queryClient.invalidateQueries(organizationsKey);
},
};
};
export const organizationMembers = (id: string) => {
return {
queryFn: () => API.getOrganizationMembers(id),
queryKey: ["organization", id, "members"],
};
};
export const addOrganizationMember = (queryClient: QueryClient, id: string) => {
return {
mutationFn: (userId: string) => {
return API.addOrganizationMember(id, userId);
},
onSuccess: async () => {
await queryClient.invalidateQueries(["organization", id, "members"]);
},
};
};
export const removeOrganizationMember = (
queryClient: QueryClient,
id: string,
) => {
return {
mutationFn: (userId: string) => {
return API.removeOrganizationMember(id, userId);
},
onSuccess: async () => {
await queryClient.invalidateQueries(["organization", id, "members"]);
},
};
};
export const updateOrganizationMemberRoles = (
queryClient: QueryClient,
organizationId: string,
) => {
return {
mutationFn: ({ userId, roles }: { userId: string; roles: string[] }) => {
return API.updateOrganizationMemberRoles(organizationId, userId, roles);
},
onSuccess: async () => {
await queryClient.invalidateQueries([
"organization",
organizationId,
"members",
]);
},
};
};
export const organizationsKey = ["organizations"] as const;
export const organizations = () => {
return {
queryKey: organizationsKey,
queryFn: () => API.getOrganizations(),
};
};
export const getProvisionerDaemonsKey = (organization: string) => [
"organization",
organization,
"provisionerDaemons",
];
export const provisionerDaemons = (organization: string) => {
return {
queryKey: getProvisionerDaemonsKey(organization),
queryFn: () => API.getProvisionerDaemonsByOrganization(organization),
};
};
/**
* Fetch permissions for a single organization.
*
* If the ID is undefined, return a disabled query.
*/
export const organizationPermissions = (organizationId: string | undefined) => {
if (!organizationId) {
return { enabled: false };
}
return {
queryKey: ["organization", organizationId, "permissions"],
queryFn: () =>
API.checkAuthorization({
checks: {
viewMembers: {
object: {
resource_type: "organization_member",
organization_id: organizationId,
},
action: "read",
},
editMembers: {
object: {
resource_type: "organization_member",
organization_id: organizationId,
},
action: "update",
},
createGroup: {
object: {
resource_type: "group",
organization_id: organizationId,
},
action: "create",
},
viewGroups: {
object: {
resource_type: "group",
organization_id: organizationId,
},
action: "read",
},
editOrganization: {
object: {
resource_type: "organization",
organization_id: organizationId,
},
action: "update",
},
auditOrganization: {
object: {
resource_type: "audit_log",
organization_id: organizationId,
},
action: "read",
},
assignOrgRole: {
object: {
resource_type: "assign_org_role",
organization_id: organizationId,
},
action: "create",
},
},
}),
};
};