feat: Add copy invite link (#8355)
* feat: Add shared invite link * refactor: Rename Invite to Team Invite model * feat: add admin check for team invite link procedures * Replace TeamInvite with VerificationToken * Add team invite null checks * Migrates tRPC procedures to new format * Type fixes * Update common.json --------- Co-authored-by: zomars <[email protected]> Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: Joe Au-Yeung <[email protected]> Co-authored-by: alannnc <[email protected]> Co-authored-by: Efraín Rochín <[email protected]> Co-authored-by: Keith Williams <[email protected]>
This commit is contained in:
22 files changed
+655
-49
No files matched your search
@@ -3,14 +3,18 @@ import { router } from "../../../trpc";
|
||||
import { ZAcceptOrLeaveInputSchema } from "./acceptOrLeave.schema";
|
||||
import { ZChangeMemberRoleInputSchema } from "./changeMemberRole.schema";
|
||||
import { ZCreateInputSchema } from "./create.schema";
|
||||
import { ZCreateInviteInputSchema } from "./createInvite.schema";
|
||||
import { ZDeleteInputSchema } from "./delete.schema";
|
||||
import { ZDeleteInviteInputSchema } from "./deleteInvite.schema";
|
||||
import { ZGetInputSchema } from "./get.schema";
|
||||
import { ZGetMemberAvailabilityInputSchema } from "./getMemberAvailability.schema";
|
||||
import { ZGetMembershipbyUserInputSchema } from "./getMembershipbyUser.schema";
|
||||
import { ZInviteMemberInputSchema } from "./inviteMember.schema";
|
||||
import { ZInviteMemberByTokenSchemaInputSchema } from "./inviteMemberByToken.schema";
|
||||
import { ZListMembersInputSchema } from "./listMembers.schema";
|
||||
import { ZPublishInputSchema } from "./publish.schema";
|
||||
import { ZRemoveMemberInputSchema } from "./removeMember.schema";
|
||||
import { ZSetInviteExpirationInputSchema } from "./setInviteExpiration.schema";
|
||||
import { ZUpdateInputSchema } from "./update.schema";
|
||||
import { ZUpdateMembershipInputSchema } from "./updateMembership.schema";
|
||||
|
||||
@@ -32,6 +36,10 @@ type TeamsRouterHandlerCache = {
|
||||
listMembers?: typeof import("./listMembers.handler").listMembersHandler;
|
||||
hasTeamPlan?: typeof import("./hasTeamPlan.handler").hasTeamPlanHandler;
|
||||
listInvites?: typeof import("./listInvites.handler").listInvitesHandler;
|
||||
createInvite?: typeof import("./createInvite.handler").createInviteHandler;
|
||||
setInviteExpiration?: typeof import("./setInviteExpiration.handler").setInviteExpirationHandler;
|
||||
deleteInvite?: typeof import("./deleteInvite.handler").deleteInviteHandler;
|
||||
inviteMemberByToken?: typeof import("./inviteMemberByToken.handler").inviteMemberByTokenHandler;
|
||||
};
|
||||
|
||||
const UNSTABLE_HANDLER_CACHE: TeamsRouterHandlerCache = {};
|
||||
@@ -334,4 +342,76 @@ export const viewerTeamsRouter = router({
|
||||
ctx,
|
||||
});
|
||||
}),
|
||||
createInvite: authedProcedure.input(ZCreateInviteInputSchema).mutation(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.createInvite) {
|
||||
UNSTABLE_HANDLER_CACHE.createInvite = await import("./createInvite.handler").then(
|
||||
(mod) => mod.createInviteHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.createInvite) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.createInvite({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
}),
|
||||
setInviteExpiration: authedProcedure
|
||||
.input(ZSetInviteExpirationInputSchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.setInviteExpiration) {
|
||||
UNSTABLE_HANDLER_CACHE.setInviteExpiration = await import("./setInviteExpiration.handler").then(
|
||||
(mod) => mod.setInviteExpirationHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.setInviteExpiration) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.setInviteExpiration({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
}),
|
||||
deleteInvite: authedProcedure.input(ZDeleteInviteInputSchema).mutation(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.deleteInvite) {
|
||||
UNSTABLE_HANDLER_CACHE.deleteInvite = await import("./deleteInvite.handler").then(
|
||||
(mod) => mod.deleteInviteHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.deleteInvite) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.deleteInvite({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
}),
|
||||
inviteMemberByToken: authedProcedure
|
||||
.input(ZInviteMemberByTokenSchemaInputSchema)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
if (!UNSTABLE_HANDLER_CACHE.inviteMemberByToken) {
|
||||
UNSTABLE_HANDLER_CACHE.inviteMemberByToken = await import("./inviteMemberByToken.handler").then(
|
||||
(mod) => mod.inviteMemberByTokenHandler
|
||||
);
|
||||
}
|
||||
|
||||
// Unreachable code but required for type safety
|
||||
if (!UNSTABLE_HANDLER_CACHE.inviteMemberByToken) {
|
||||
throw new Error("Failed to load handler");
|
||||
}
|
||||
|
||||
return UNSTABLE_HANDLER_CACHE.inviteMemberByToken({
|
||||
ctx,
|
||||
input,
|
||||
});
|
||||
}),
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
import { randomBytes } from "crypto";
|
||||
|
||||
import { isTeamAdmin } from "@calcom/lib/server/queries/teams";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { TRPCError } from "@calcom/trpc/server";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
||||
|
||||
import type { TCreateInviteInputSchema } from "./createInvite.schema";
|
||||
|
||||
type CreateInviteOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TCreateInviteInputSchema;
|
||||
};
|
||||
|
||||
export const createInviteHandler = async ({ ctx, input }: CreateInviteOptions) => {
|
||||
const { teamId } = input;
|
||||
|
||||
if (!(await isTeamAdmin(ctx.user.id, teamId))) throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
|
||||
const token = randomBytes(32).toString("hex");
|
||||
await prisma.verificationToken.create({
|
||||
data: {
|
||||
identifier: "",
|
||||
token,
|
||||
expires: new Date(),
|
||||
teamId,
|
||||
},
|
||||
});
|
||||
return token;
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZCreateInviteInputSchema = z.object({
|
||||
teamId: z.number(),
|
||||
});
|
||||
|
||||
export type TCreateInviteInputSchema = z.infer<typeof ZCreateInviteInputSchema>;
|
||||
@@ -0,0 +1,33 @@
|
||||
import { isTeamAdmin } from "@calcom/lib/server/queries/teams";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { TRPCError } from "@calcom/trpc/server";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
||||
|
||||
import type { TDeleteInviteInputSchema } from "./deleteInvite.schema";
|
||||
|
||||
type DeleteInviteOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TDeleteInviteInputSchema;
|
||||
};
|
||||
|
||||
export const deleteInviteHandler = async ({ ctx, input }: DeleteInviteOptions) => {
|
||||
const { token } = input;
|
||||
|
||||
const verificationToken = await prisma.verificationToken.findFirst({
|
||||
where: {
|
||||
token: token,
|
||||
},
|
||||
select: {
|
||||
teamId: true,
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!verificationToken) throw new TRPCError({ code: "NOT_FOUND" });
|
||||
if (!verificationToken.teamId || !(await isTeamAdmin(ctx.user.id, verificationToken.teamId)))
|
||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
|
||||
await prisma.verificationToken.delete({ where: { id: verificationToken.id } });
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZDeleteInviteInputSchema = z.object({
|
||||
token: z.string(),
|
||||
});
|
||||
|
||||
export type TDeleteInviteInputSchema = z.infer<typeof ZDeleteInviteInputSchema>;
|
||||
@@ -19,8 +19,7 @@ export const getUpgradeableHandler = async ({ ctx }: GetUpgradeableOptions) => {
|
||||
/** We only need to return teams that don't have a `subscriptionId` on their metadata */
|
||||
teams = teams.filter((m) => {
|
||||
const metadata = teamMetadataSchema.safeParse(m.team.metadata);
|
||||
if (metadata.success && metadata.data?.subscriptionId) return false;
|
||||
return true;
|
||||
return !(metadata.success && metadata.data?.subscriptionId);
|
||||
});
|
||||
return teams;
|
||||
};
|
||||
@@ -0,0 +1,66 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
import { updateQuantitySubscriptionFromStripe } from "@calcom/ee/teams/lib/payments";
|
||||
import { IS_TEAM_BILLING_ENABLED } from "@calcom/lib/constants";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
import { TRPCError } from "@calcom/trpc/server";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
||||
|
||||
import type { TInviteMemberByTokenSchemaInputSchema } from "./inviteMemberByToken.schema";
|
||||
|
||||
type InviteMemberByTokenOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TInviteMemberByTokenSchemaInputSchema;
|
||||
};
|
||||
|
||||
export const inviteMemberByTokenHandler = async ({ ctx, input }: InviteMemberByTokenOptions) => {
|
||||
const { token } = input;
|
||||
|
||||
const verificationToken = await prisma.verificationToken.findFirst({
|
||||
where: {
|
||||
token,
|
||||
OR: [{ expiresInDays: null }, { expires: { gte: new Date() } }],
|
||||
},
|
||||
include: {
|
||||
team: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!verificationToken) throw new TRPCError({ code: "NOT_FOUND", message: "Invite not found" });
|
||||
if (!verificationToken.teamId || !verificationToken.team)
|
||||
throw new TRPCError({
|
||||
code: "NOT_FOUND",
|
||||
message: "Invite token is not associated with any team",
|
||||
});
|
||||
|
||||
try {
|
||||
await prisma.membership.create({
|
||||
data: {
|
||||
teamId: verificationToken.teamId,
|
||||
userId: ctx.user.id,
|
||||
role: MembershipRole.MEMBER,
|
||||
accepted: false,
|
||||
},
|
||||
});
|
||||
} catch (e) {
|
||||
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
if (e.code === "P2002") {
|
||||
throw new TRPCError({
|
||||
code: "FORBIDDEN",
|
||||
message: "This user is a member of this team / has a pending invitation.",
|
||||
});
|
||||
}
|
||||
} else throw e;
|
||||
}
|
||||
|
||||
if (IS_TEAM_BILLING_ENABLED) await updateQuantitySubscriptionFromStripe(verificationToken.teamId);
|
||||
|
||||
return verificationToken.team.name;
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZInviteMemberByTokenSchemaInputSchema = z.object({
|
||||
token: z.string(),
|
||||
});
|
||||
|
||||
export type TInviteMemberByTokenSchemaInputSchema = z.infer<typeof ZInviteMemberByTokenSchemaInputSchema>;
|
||||
@@ -14,7 +14,11 @@ export const listHandler = async ({ ctx }: ListOptions) => {
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
include: {
|
||||
team: true,
|
||||
team: {
|
||||
include: {
|
||||
inviteToken: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: { role: "desc" },
|
||||
});
|
||||
|
||||
@@ -43,7 +43,7 @@ export const listMembersHandler = async ({ ctx, input }: ListMembersOptions) =>
|
||||
});
|
||||
|
||||
type UserMap = Record<number, (typeof teams)[number]["members"][number]["user"]>;
|
||||
// flattern users to be unique by id
|
||||
// flatten users to be unique by id
|
||||
const users = teams
|
||||
.flatMap((t) => t.members)
|
||||
.reduce((acc, m) => (m.user.id in acc ? acc : { ...acc, [m.user.id]: m.user }), {} as UserMap);
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { isTeamAdmin } from "@calcom/lib/server/queries/teams";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import { TRPCError } from "@calcom/trpc/server";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
||||
|
||||
import type { TSetInviteExpirationInputSchema } from "./setInviteExpiration.schema";
|
||||
|
||||
type SetInviteExpirationOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TSetInviteExpirationInputSchema;
|
||||
};
|
||||
|
||||
export const setInviteExpirationHandler = async ({ ctx, input }: SetInviteExpirationOptions) => {
|
||||
const { token, expiresInDays } = input;
|
||||
|
||||
const verificationToken = await prisma.verificationToken.findFirst({
|
||||
where: {
|
||||
token: token,
|
||||
},
|
||||
select: {
|
||||
teamId: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!verificationToken) throw new TRPCError({ code: "NOT_FOUND" });
|
||||
if (!verificationToken.teamId || !(await isTeamAdmin(ctx.user.id, verificationToken.teamId)))
|
||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
|
||||
const oneDay = 24 * 60 * 60 * 1000;
|
||||
const expires = expiresInDays ? new Date(Date.now() + expiresInDays * oneDay) : new Date();
|
||||
|
||||
await prisma.verificationToken.update({
|
||||
where: { token },
|
||||
data: {
|
||||
expires,
|
||||
expiresInDays,
|
||||
},
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,8 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZSetInviteExpirationInputSchema = z.object({
|
||||
token: z.string(),
|
||||
expiresInDays: z.number().optional(),
|
||||
});
|
||||
|
||||
export type TSetInviteExpirationInputSchema = z.infer<typeof ZSetInviteExpirationInputSchema>;
|
||||
Reference in new issue
Block a user