Files
calendar/packages/trpc/server/routers/viewer/teams/createInvite.handler.ts
T
0970c77009 fix: Revert -> "Revert "chore: Chore team metadata table + isOrganization migration (#13700)
* Revert "Revert "chore: Chore team metadata table + isOrganization migration from metadata (#12828)""

This reverts commit 2408338ed4.

* Remove constraint slug,isOrganization and reset migration

* fix: conflicts

* change schema to bust cache

* Fix issues reported by TS

* change schema to bust cache

* Review fixes

* Colaesce for orgAutoAcceptEmail as well

* Fix missing negation

---------

Co-authored-by: zomars <zomars@me.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: Keith Williams <keithwillcode@gmail.com>
Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
2024-03-01 15:04:28 +00:00

58 lines
2.0 KiB
TypeScript

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 { 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;
const membership = await isTeamAdmin(ctx.user.id, teamId);
if (!membership || !membership?.team) throw new TRPCError({ code: "UNAUTHORIZED" });
const isOrganizationOrATeamInOrganization = !!(membership.team?.parentId || membership.team.isOrganization);
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, isOrganizationOrATeamInOrganization),
};
}
const token = randomBytes(32).toString("hex");
await prisma.verificationToken.create({
data: {
identifier: `invite-link-for-teamId-${teamId}`,
token,
expires: new Date(new Date().setHours(168)), // +1 week,
expiresInDays: 7,
teamId,
},
});
return { token, inviteLink: await getInviteLink(token, isOrganizationOrATeamInOrganization) };
};
async function getInviteLink(token = "", isOrgContext = false) {
const teamInviteLink = `${WEBAPP_URL}/teams?token=${token}`;
const orgInviteLink = `${WEBAPP_URL}/signup?token=${token}&callbackUrl=/getting-started`;
if (isOrgContext) return orgInviteLink;
return teamInviteLink;
}
export default createInviteHandler;