From 1c1625d3fc475755ca0f4c23a832192b8b25afaa Mon Sep 17 00:00:00 2001 From: alannnc Date: Tue, 12 Sep 2023 09:19:17 -0700 Subject: [PATCH] fix: org admin delete other teams (#11247) * fix org-admin delete other teams * Fix delete from teams to delete from orgs endpoint --- .../pages/components/OtherTeamList.tsx | 5 ++- .../pages/components/OtherTeamListItem.tsx | 31 ++++++++++++++++++- .../settings/other-team-profile-view.tsx | 4 +-- .../routers/viewer/organizations/_router.tsx | 5 +++ .../organizations/deleteTeam.handler.ts | 24 ++++++++++++++ .../viewer/organizations/deleteTeam.schema.ts | 7 +++++ 6 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 packages/trpc/server/routers/viewer/organizations/deleteTeam.handler.ts create mode 100644 packages/trpc/server/routers/viewer/organizations/deleteTeam.schema.ts diff --git a/packages/features/ee/organizations/pages/components/OtherTeamList.tsx b/packages/features/ee/organizations/pages/components/OtherTeamList.tsx index 4c4dc48423..d82c9b5a14 100644 --- a/packages/features/ee/organizations/pages/components/OtherTeamList.tsx +++ b/packages/features/ee/organizations/pages/components/OtherTeamList.tsx @@ -24,10 +24,9 @@ export default function OtherTeamList(props: Props) { } } - const deleteTeamMutation = trpc.viewer.teams.delete.useMutation({ + const deleteTeamMutation = trpc.viewer.organizations.deleteTeam.useMutation({ async onSuccess() { - await utils.viewer.teams.list.invalidate(); - await utils.viewer.teams.hasTeamPlan.invalidate(); + await utils.viewer.organizations.listOtherTeams.invalidate(); }, async onError(err) { showToast(err.message, "error"); diff --git a/packages/features/ee/organizations/pages/components/OtherTeamListItem.tsx b/packages/features/ee/organizations/pages/components/OtherTeamListItem.tsx index db3a7f3311..1ed749d5c8 100644 --- a/packages/features/ee/organizations/pages/components/OtherTeamListItem.tsx +++ b/packages/features/ee/organizations/pages/components/OtherTeamListItem.tsx @@ -5,6 +5,9 @@ import { Avatar, Button, ButtonGroup, + ConfirmationDialogContent, + Dialog, + DialogTrigger, Dropdown, DropdownItem, DropdownMenuContent, @@ -13,7 +16,7 @@ import { showToast, Tooltip, } from "@calcom/ui"; -import { Edit2, ExternalLink, Link as LinkIcon, MoreHorizontal } from "@calcom/ui/components/icon"; +import { Edit2, ExternalLink, Link as LinkIcon, MoreHorizontal, Trash } from "@calcom/ui/components/icon"; import { useOrgBranding } from "../../../organizations/context/provider"; @@ -119,6 +122,32 @@ export default function OtherTeamListItem(props: Props) { )} + + + + + { + e.stopPropagation(); + }}> + {t("disband_team")} + + + { + props.onActionSelect("disband"); + }}> + {t("disband_team_confirmation_message")} + + + diff --git a/packages/features/ee/organizations/pages/settings/other-team-profile-view.tsx b/packages/features/ee/organizations/pages/settings/other-team-profile-view.tsx index aabbfaa187..1d97111bbc 100644 --- a/packages/features/ee/organizations/pages/settings/other-team-profile-view.tsx +++ b/packages/features/ee/organizations/pages/settings/other-team-profile-view.tsx @@ -106,9 +106,9 @@ const OtherTeamProfileView = () => { const isBioEmpty = !team || !team.bio || !team.bio.replace("


", "").length; - const deleteTeamMutation = trpc.viewer.teams.delete.useMutation({ + const deleteTeamMutation = trpc.viewer.organizations.deleteTeam.useMutation({ async onSuccess() { - await utils.viewer.teams.list.invalidate(); + await utils.viewer.organizations.listOtherTeams.invalidate(); showToast(t("your_team_disbanded_successfully"), "success"); router.push(`${WEBAPP_URL}/teams`); }, diff --git a/packages/trpc/server/routers/viewer/organizations/_router.tsx b/packages/trpc/server/routers/viewer/organizations/_router.tsx index c8ddaebb5b..6022cf06ab 100644 --- a/packages/trpc/server/routers/viewer/organizations/_router.tsx +++ b/packages/trpc/server/routers/viewer/organizations/_router.tsx @@ -10,6 +10,7 @@ import { ZAdminVerifyInput } from "./adminVerify.schema"; import { ZBulkUsersDelete } from "./bulkDeleteUsers.schema."; import { ZCreateInputSchema } from "./create.schema"; import { ZCreateTeamsSchema } from "./createTeams.schema"; +import { ZDeleteTeamInputSchema } from "./deleteTeam.schema"; import { ZGetMembersInput } from "./getMembers.schema"; import { ZGetOtherTeamInputSchema } from "./getOtherTeam.handler"; import { ZGetUserInput } from "./getUser.schema"; @@ -120,4 +121,8 @@ export const viewerOrganizationsRouter = router({ ); return handler(opts); }), + deleteTeam: authedOrgAdminProcedure.input(ZDeleteTeamInputSchema).mutation(async (opts) => { + const handler = await importHandler(namespaced("deleteTeam"), () => import("./deleteTeam.handler")); + return handler(opts); + }), }); diff --git a/packages/trpc/server/routers/viewer/organizations/deleteTeam.handler.ts b/packages/trpc/server/routers/viewer/organizations/deleteTeam.handler.ts new file mode 100644 index 0000000000..2db525619d --- /dev/null +++ b/packages/trpc/server/routers/viewer/organizations/deleteTeam.handler.ts @@ -0,0 +1,24 @@ +import { prisma } from "@calcom/prisma"; + +import type { TDeleteTeamInputSchema } from "./deleteTeam.schema"; + +type DeleteOptions = { + input: TDeleteTeamInputSchema; +}; + +export const deleteTeamHandler = async ({ input }: DeleteOptions) => { + // delete all memberships + await prisma.membership.deleteMany({ + where: { + teamId: input.teamId, + }, + }); + + await prisma.team.delete({ + where: { + id: input.teamId, + }, + }); +}; + +export default deleteTeamHandler; diff --git a/packages/trpc/server/routers/viewer/organizations/deleteTeam.schema.ts b/packages/trpc/server/routers/viewer/organizations/deleteTeam.schema.ts new file mode 100644 index 0000000000..21ed78621f --- /dev/null +++ b/packages/trpc/server/routers/viewer/organizations/deleteTeam.schema.ts @@ -0,0 +1,7 @@ +import { z } from "zod"; + +export const ZDeleteTeamInputSchema = z.object({ + teamId: z.number(), +}); + +export type TDeleteTeamInputSchema = z.infer;