added team deletion

This commit is contained in:
Zai Shi 2024-05-08 16:39:54 +02:00
parent a975987356
commit 90ca97339d
4 changed files with 54 additions and 3 deletions

View File

@ -152,14 +152,14 @@ function Actions(props: { params: any }) {
open={isDeleteModalOpen}
onClose={() => setIsDeleteModalOpen(false)}
okButton={{
label: "Delete user",
label: "Delete Team",
onClick: async () => {
await props.params.row.delete();
}
}}
cancelButton={true}
>
Are you sure you want to delete the user '{props.params.row.displayName}' with ID {props.params.row.id}? This action cannot be undone.
Are you sure you want to delete the team '{props.params.row.displayName}' with ID {props.params.row.id}? This action cannot be undone. All team members will be removed from the team.
</Dialog>
</>
);

View File

@ -5,7 +5,7 @@ import { deprecatedSmartRouteHandler } from "@/route-handlers/smart-route-handle
import { deprecatedParseRequest } from "@/route-handlers/smart-request";
import { checkApiKeySet, secretServerKeyHeaderSchema } from "@/lib/api-keys";
import { isProjectAdmin } from "@/lib/projects";
import { updateServerTeam } from "@/lib/teams";
import { deleteServerTeam, updateServerTeam } from "@/lib/teams";
const putSchema = yup.object({
query: yup.object({
@ -47,3 +47,41 @@ export const PUT = deprecatedSmartRouteHandler(async (req: NextRequest, options:
return NextResponse.json(null);
});
const deleteSchema = yup.object({
query: yup.object({
server: yup.string().oneOf(["true"]).required(),
}).required(),
headers: yup.object({
"x-stack-secret-server-key": secretServerKeyHeaderSchema.default(""),
"x-stack-admin-access-token": yup.string().default(""),
"x-stack-project-id": yup.string().required(),
}).required(),
});
export const DELETE = deprecatedSmartRouteHandler(async (req: NextRequest, options: { params: { teamId: string } }) => {
const {
query: {
server,
},
headers: {
"x-stack-project-id": projectId,
"x-stack-secret-server-key": secretServerKey,
"x-stack-admin-access-token": adminAccessToken,
},
} = await deprecatedParseRequest(req, deleteSchema);
const skValid = await checkApiKeySet(projectId, { secretServerKey });
const asValid = await isProjectAdmin(projectId, adminAccessToken);
if (server === "true") {
if (!skValid && !asValid) {
throw new StatusError(StatusError.Forbidden);
}
await deleteServerTeam(projectId, options.params.teamId);
}
return NextResponse.json(null);
});

View File

@ -216,6 +216,14 @@ export class StackServerInterface extends StackClientInterface {
);
}
async deleteTeam(teamId: string): Promise<void> {
await this.sendServerRequest(
`/teams/${teamId}?server=true`,
{ method: "DELETE" },
null,
);
}
async addUserToTeam(options: {
userId: string,
teamId: string,

View File

@ -1152,6 +1152,10 @@ class _StackServerAppImpl<HasTokenStore extends boolean, ProjectId extends strin
await app._interface.updateTeam(json.id, update);
await app._serverTeamsCache.refresh([]);
},
async delete() {
await app._interface.deleteTeam(json.id);
await app._serverTeamsCache.refresh([]);
},
useMembers() {
const result = useCache(app._serverTeamMembersCache, [json.id], "team.useUsers()");
return useMemo(() => result.map((u) => app._serverTeamMemberFromJson(u)), [result]);
@ -1576,6 +1580,7 @@ export type ServerTeam = Team & {
listMembers(): Promise<ServerTeamMember[]>,
useMembers(): ServerTeamMember[],
update(update: Partial<ServerTeamCustomizableJson>): Promise<void>,
delete(): Promise<void>,
addUser(userId: string): Promise<void>,
removeUser(userId: string): Promise<void>,
};