Skip to content

Commit

Permalink
fix: Handle removing user from org (calcom#11227)
Browse files Browse the repository at this point in the history
* Handle removing user from org

* Address no password or username being set

---------

Co-authored-by: Leo Giovanetti <hello@leog.me>
  • Loading branch information
sean-brydon and leog authored Sep 11, 2023
1 parent b8794ba commit 0a0d212
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/trpc/server/routers/viewer/teams/removeMember.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IS_TEAM_BILLING_ENABLED } from "@calcom/lib/constants";
import { isTeamAdmin, isTeamOwner } from "@calcom/lib/server/queries/teams";
import { closeComDeleteTeamMembership } from "@calcom/lib/sync/SyncServiceManager";
import type { PrismaClient } from "@calcom/prisma";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";

import { TRPCError } from "@trpc/server";
Expand Down Expand Up @@ -54,6 +55,47 @@ export const removeMemberHandler = async ({ ctx, input }: RemoveMemberOptions) =

if (input.isOrg) {
// Deleting membership from all child teams
const foundUser = await ctx.prisma.user.findUnique({
where: { id: input.memberId },
select: {
email: true,
password: true,
username: true,
completedOnboarding: true,
},
});

const orgInfo = await ctx.prisma.team.findUnique({
where: { id: input.teamId },
select: {
metadata: true,
},
});

if (!foundUser || !orgInfo) throw new TRPCError({ code: "NOT_FOUND" });

const parsedMetadata = teamMetadataSchema.parse(orgInfo.metadata);

if (
parsedMetadata?.isOrganization &&
parsedMetadata.isOrganizationVerified &&
parsedMetadata.orgAutoAcceptEmail
) {
if (foundUser.email.endsWith(parsedMetadata.orgAutoAcceptEmail)) {
await ctx.prisma.user.delete({
where: { id: input.memberId },
});
// This should cascade delete all memberships and hosts etc
return;
}
} else if ((!foundUser.username || !foundUser.password) && !foundUser.completedOnboarding) {
await ctx.prisma.user.delete({
where: { id: input.memberId },
});
// This should cascade delete all memberships and hosts etc
return;
}

await ctx.prisma.membership.deleteMany({
where: {
team: {
Expand Down

0 comments on commit 0a0d212

Please sign in to comment.