fix: A user joining from invite link of a team doesn't automatically become member of the org (#12774)

* fix: Add org membership when invite link for a team in an org is generated

* Run tests sequentially till we fix emails fixture
This commit is contained in:
Hariom Balhara authored and GitHub committed 2023-12-18 17:48:35 +05:30
1 parent 7d2500a32f
commit a28e8ff39b
16 files changed
+549 -240

No files matched your search

@@ -7,7 +7,6 @@ import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { TRPCError } from "@calcom/trpc/server";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
import { getMembersHandler } from "../organizations/getMembers.handler";
import type { TCreateInviteInputSchema } from "./createInvite.schema";
type CreateInviteOptions = {
@@ -23,11 +22,7 @@ export const createInviteHandler = async ({ ctx, input }: CreateInviteOptions) =
if (!membership || !membership?.team) throw new TRPCError({ code: "UNAUTHORIZED" });
const teamMetadata = teamMetadataSchema.parse(membership.team.metadata);
const isOrg = !!(membership.team?.parentId === null && teamMetadata?.isOrganization);
const orgMembers = await getMembersHandler({
ctx,
input: { teamIdToExclude: teamId, distinctUser: true },
});
const isOrganizationOrATeamInOrganization = !!(membership.team?.parentId || teamMetadata?.isOrganization);
if (input.token) {
const existingToken = await prisma.verificationToken.findFirst({
@@ -36,7 +31,7 @@ export const createInviteHandler = async ({ ctx, input }: CreateInviteOptions) =
if (!existingToken) throw new TRPCError({ code: "NOT_FOUND" });
return {
token: existingToken.token,
inviteLink: await getInviteLink(existingToken.token, isOrg, orgMembers?.length),
inviteLink: await getInviteLink(existingToken.token, isOrganizationOrATeamInOrganization),
};
}
@@ -50,13 +45,13 @@ export const createInviteHandler = async ({ ctx, input }: CreateInviteOptions) =
},
});
return { token, inviteLink: await getInviteLink(token, isOrg, orgMembers?.length) };
return { token, inviteLink: await getInviteLink(token, isOrganizationOrATeamInOrganization) };
};
async function getInviteLink(token = "", isOrg = false, orgMembers = 0) {
async function getInviteLink(token = "", isOrgContext = false) {
const teamInviteLink = `${WEBAPP_URL}/teams?token=${token}`;
const orgInviteLink = `${WEBAPP_URL}/signup?token=${token}&callbackUrl=/getting-started`;
if (isOrg || orgMembers > 0) return orgInviteLink;
if (isOrgContext) return orgInviteLink;
return teamInviteLink;
}