feat: Handle deleting vercel domain when org is deleted (#9661)

feat/organizations-no-middleware-rewrite-1
sean-brydon 2023-06-20 16:57:21 +01:00 committed by GitHub
parent f69c254fbc
commit a3eeeab259
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 46 additions and 1 deletions

View File

@ -1,8 +1,10 @@
import { subdomainSuffix } from "@calcom/ee/organizations/lib/orgDomains";
import { cancelTeamSubscriptionFromStripe } from "@calcom/features/ee/teams/lib/payments";
import { IS_TEAM_BILLING_ENABLED } from "@calcom/lib/constants";
import { IS_PRODUCTION, IS_TEAM_BILLING_ENABLED } from "@calcom/lib/constants";
import { isTeamOwner } from "@calcom/lib/server/queries/teams";
import { closeComDeleteTeam } from "@calcom/lib/sync/SyncServiceManager";
import { prisma } from "@calcom/prisma";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
import { TRPCError } from "@trpc/server";
@ -16,6 +18,41 @@ type DeleteOptions = {
input: TDeleteInputSchema;
};
const deleteVercelDomain = async ({
slug,
isOrganization,
}: {
slug?: string | null;
isOrganization?: boolean | null;
}) => {
if (!isOrganization || !slug) {
return false;
}
const fullDomain = `${slug}.${subdomainSuffix()}`;
const response = await fetch(
`https://api.vercel.com/v9/projects/${process.env.PROJECT_ID_VERCEL}/domains/${fullDomain}?teamId=${process.env.TEAM_ID_VERCEL}`,
{
headers: {
Authorization: `Bearer ${process.env.AUTH_BEARER_TOKEN_VERCEL}`,
},
method: "DELETE",
}
);
const data = await response.json();
// Domain is already owned by another team but you can request delegation to access it
if (data.error?.code === "forbidden")
throw new TRPCError({ code: "CONFLICT", message: "domain_taken_team" });
// Domain is already being used by a different project
if (data.error?.code === "domain_taken")
throw new TRPCError({ code: "CONFLICT", message: "domain_taken_project" });
return true;
};
export const deleteHandler = async ({ ctx, input }: DeleteOptions) => {
if (!(await isTeamOwner(ctx.user?.id, input.teamId))) throw new TRPCError({ code: "UNAUTHORIZED" });
@ -34,6 +71,14 @@ export const deleteHandler = async ({ ctx, input }: DeleteOptions) => {
},
});
const deletedTeamMetadata = teamMetadataSchema.parse(deletedTeam.metadata);
if (IS_PRODUCTION)
deleteVercelDomain({
slug: deletedTeam.slug,
isOrganization: deletedTeamMetadata?.isOrganization,
});
// Sync Services: Close.cm
closeComDeleteTeam(deletedTeam);
};