fix: team and org invite e2e tests (#12566)
This commit is contained in:
1 parent
d3ddbf418f
commit
c765ea9b31
9 files changed
+66
-84
No files matched your search
@@ -6,7 +6,7 @@ import { Controller, useForm } from "react-hook-form";
|
||||
|
||||
import TeamInviteFromOrg from "@calcom/ee/organizations/components/TeamInviteFromOrg";
|
||||
import { classNames } from "@calcom/lib";
|
||||
import { IS_TEAM_BILLING_ENABLED, WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { IS_TEAM_BILLING_ENABLED } from "@calcom/lib/constants";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
import type { RouterOutputs } from "@calcom/trpc";
|
||||
@@ -25,7 +25,6 @@ import {
|
||||
TextAreaField,
|
||||
} from "@calcom/ui";
|
||||
import { Link } from "@calcom/ui/components/icon";
|
||||
import type { Window as WindowWithClipboardValue } from "@calcom/web/playwright/fixtures/clipboard";
|
||||
|
||||
import type { PendingMember } from "../lib/types";
|
||||
import { GoogleWorkspaceInviteButton } from "./GoogleWorkspaceInviteButton";
|
||||
@@ -76,8 +75,8 @@ export default function MemberInvitationModal(props: MemberInvitationModalProps)
|
||||
);
|
||||
|
||||
const createInviteMutation = trpc.viewer.teams.createInvite.useMutation({
|
||||
onSuccess(token) {
|
||||
copyInviteLinkToClipboard(token);
|
||||
async onSuccess({ inviteLink }) {
|
||||
await copyInviteLinkToClipboard(inviteLink);
|
||||
trpcContext.viewer.teams.get.invalidate();
|
||||
trpcContext.viewer.teams.list.invalidate();
|
||||
},
|
||||
@@ -86,20 +85,11 @@ export default function MemberInvitationModal(props: MemberInvitationModalProps)
|
||||
},
|
||||
});
|
||||
|
||||
const copyInviteLinkToClipboard = async (token: string) => {
|
||||
const isOrgInvite = isOrg;
|
||||
const teamInviteLink = `${WEBAPP_URL}/teams?token=${token}`;
|
||||
const orgInviteLink = `${WEBAPP_URL}/signup?token=${token}&callbackUrl=/getting-started`;
|
||||
|
||||
const inviteLink =
|
||||
isOrgInvite || (props?.orgMembers && props.orgMembers?.length > 0) ? orgInviteLink : teamInviteLink;
|
||||
const copyInviteLinkToClipboard = async (inviteLink: string) => {
|
||||
try {
|
||||
await navigator.clipboard.writeText(inviteLink);
|
||||
showToast(t("invite_link_copied"), "success");
|
||||
} catch (e) {
|
||||
if (process.env.NEXT_PUBLIC_IS_E2E) {
|
||||
(window as WindowWithClipboardValue).E2E_CLIPBOARD_VALUE = inviteLink;
|
||||
}
|
||||
console.error(e);
|
||||
}
|
||||
};
|
||||
@@ -373,11 +363,9 @@ export default function MemberInvitationModal(props: MemberInvitationModalProps)
|
||||
type="button"
|
||||
color="minimal"
|
||||
variant="icon"
|
||||
onClick={() =>
|
||||
props.token
|
||||
? copyInviteLinkToClipboard(props.token)
|
||||
: createInviteMutation.mutate({ teamId: props.teamId })
|
||||
}
|
||||
onClick={() => {
|
||||
createInviteMutation.mutate({ teamId: props.teamId, token: props.token });
|
||||
}}
|
||||
className={classNames("gap-2", props.token && "opacity-50")}
|
||||
data-testid="copy-invite-link-button">
|
||||
<Link className="text-default h-4 w-4" aria-hidden="true" />
|
||||
|
||||
@@ -214,16 +214,17 @@ export async function getTeamWithMembers(args: {
|
||||
|
||||
// also returns team
|
||||
export async function isTeamAdmin(userId: number, teamId: number) {
|
||||
return (
|
||||
(await prisma.membership.findFirst({
|
||||
where: {
|
||||
userId,
|
||||
teamId,
|
||||
accepted: true,
|
||||
OR: [{ role: "ADMIN" }, { role: "OWNER" }],
|
||||
},
|
||||
})) || false
|
||||
);
|
||||
const team = await prisma.membership.findFirst({
|
||||
where: {
|
||||
userId,
|
||||
teamId,
|
||||
accepted: true,
|
||||
OR: [{ role: "ADMIN" }, { role: "OWNER" }],
|
||||
},
|
||||
include: { team: true },
|
||||
});
|
||||
if (!team) return false;
|
||||
return team;
|
||||
}
|
||||
|
||||
export async function isTeamOwner(userId: number, teamId: number) {
|
||||
|
||||
@@ -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>;
|
||||
Reference in new issue
Block a user