fix: team and org invite e2e tests (#12566)

This commit is contained in:
Omar López authored and GitHub committed 2023-11-28 10:49:06 +00:00
1 parent d3ddbf418f
commit c765ea9b31
9 files changed
+66 -84

No files matched your search

@@ -1,10 +1,13 @@
import { randomBytes } from "crypto";
import { WEBAPP_URL } from "@calcom/lib/constants";
import { isTeamAdmin } from "@calcom/lib/server/queries/teams";
import { prisma } from "@calcom/prisma";
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 = {
@@ -16,8 +19,26 @@ type CreateInviteOptions = {
export const createInviteHandler = async ({ ctx, input }: CreateInviteOptions) => {
const { teamId } = input;
const membership = await isTeamAdmin(ctx.user.id, teamId);
if (!(await isTeamAdmin(ctx.user.id, teamId))) throw new TRPCError({ code: "UNAUTHORIZED" });
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 },
});
if (input.token) {
const existingToken = await prisma.verificationToken.findFirst({
where: { token: input.token, identifier: `invite-link-for-teamId-${teamId}`, teamId },
});
if (!existingToken) throw new TRPCError({ code: "NOT_FOUND" });
return {
token: existingToken.token,
inviteLink: await getInviteLink(existingToken.token, isOrg, orgMembers?.length),
};
}
const token = randomBytes(32).toString("hex");
await prisma.verificationToken.create({
@@ -28,5 +49,13 @@ export const createInviteHandler = async ({ ctx, input }: CreateInviteOptions) =
teamId,
},
});
return token;
return { token, inviteLink: await getInviteLink(token, isOrg, orgMembers?.length) };
};
async function getInviteLink(token = "", isOrg = false, orgMembers = 0) {
const teamInviteLink = `${WEBAPP_URL}/teams?token=${token}`;
const orgInviteLink = `${WEBAPP_URL}/signup?token=${token}&callbackUrl=/getting-started`;
if (isOrg || orgMembers > 0) return orgInviteLink;
return teamInviteLink;
}
@@ -2,6 +2,7 @@ import { z } from "zod";
export const ZCreateInviteInputSchema = z.object({
teamId: z.number(),
token: z.string().optional(),
});
export type TCreateInviteInputSchema = z.infer<typeof ZCreateInviteInputSchema>;