chore: migrate api for team upgrade to App Router (#19100)
* remove old pages router api * add new app router api * fix * fix redirect * refactor
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
import { cookies, headers } from "next/headers";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import type Stripe from "stripe";
|
||||
import { z } from "zod";
|
||||
|
||||
import { getRequestedSlugError } from "@calcom/app-store/stripepayment/lib/team-billing";
|
||||
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
|
||||
import stripe from "@calcom/features/ee/payments/server/stripe";
|
||||
import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
import { buildLegacyRequest } from "@lib/buildLegacyCtx";
|
||||
|
||||
const querySchema = z.object({
|
||||
team: z.string().transform((val) => parseInt(val)),
|
||||
session_id: z.string().min(1),
|
||||
});
|
||||
|
||||
export async function GET(req: NextRequest, { params }: { params: { team: string } }) {
|
||||
try {
|
||||
const searchParams = req.nextUrl.searchParams;
|
||||
const { team: id, session_id } = querySchema.parse({
|
||||
team: params.team,
|
||||
session_id: searchParams.get("session_id"),
|
||||
});
|
||||
|
||||
const checkoutSession = await stripe.checkout.sessions.retrieve(session_id, {
|
||||
expand: ["subscription"],
|
||||
});
|
||||
if (!checkoutSession) {
|
||||
throw new HttpError({ statusCode: 404, message: "Checkout session not found" });
|
||||
}
|
||||
|
||||
const subscription = checkoutSession.subscription as Stripe.Subscription;
|
||||
if (checkoutSession.payment_status !== "paid") {
|
||||
throw new HttpError({ statusCode: 402, message: "Payment required" });
|
||||
}
|
||||
|
||||
let team = await prisma.team.findFirst({
|
||||
where: { metadata: { path: ["paymentId"], equals: checkoutSession.id } },
|
||||
});
|
||||
|
||||
let metadata;
|
||||
|
||||
if (!team) {
|
||||
const prevTeam = await prisma.team.findFirstOrThrow({ where: { id } });
|
||||
|
||||
metadata = teamMetadataSchema.safeParse(prevTeam.metadata);
|
||||
if (!metadata.success) {
|
||||
throw new HttpError({ statusCode: 400, message: "Invalid team metadata" });
|
||||
}
|
||||
|
||||
const { requestedSlug, ...newMetadata } = metadata.data || {};
|
||||
team = await prisma.team.update({
|
||||
where: { id },
|
||||
data: {
|
||||
metadata: {
|
||||
...newMetadata,
|
||||
paymentId: checkoutSession.id,
|
||||
subscriptionId: subscription.id || null,
|
||||
subscriptionItemId: subscription.items.data[0].id || null,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const slug = prevTeam.slug || requestedSlug;
|
||||
if (slug) {
|
||||
try {
|
||||
team = await prisma.team.update({ where: { id }, data: { slug } });
|
||||
} catch (error) {
|
||||
const { message, statusCode } = getRequestedSlugError(error, slug);
|
||||
return NextResponse.json({ message }, { status: statusCode });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!metadata) {
|
||||
metadata = teamMetadataSchema.safeParse(team.metadata);
|
||||
if (!metadata.success) {
|
||||
throw new HttpError({ statusCode: 400, message: "Invalid team metadata" });
|
||||
}
|
||||
}
|
||||
|
||||
const session = await getServerSession({ req: buildLegacyRequest(headers(), cookies()) });
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.json({ message: "Team upgraded successfully" });
|
||||
}
|
||||
|
||||
const redirectUrl = team?.isOrganization
|
||||
? `${WEBAPP_URL}/settings/organizations/profile?upgraded=true`
|
||||
: `${WEBAPP_URL}/settings/teams/${team.id}/profile?upgraded=true`;
|
||||
|
||||
return NextResponse.redirect(redirectUrl);
|
||||
} catch (error) {
|
||||
if (error instanceof HttpError) {
|
||||
throw error;
|
||||
}
|
||||
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export { default } from "@calcom/features/ee/teams/api/upgrade";
|
||||
@@ -1,89 +0,0 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import type Stripe from "stripe";
|
||||
import { z } from "zod";
|
||||
|
||||
import { getRequestedSlugError } from "@calcom/app-store/stripepayment/lib/team-billing";
|
||||
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
|
||||
import stripe from "@calcom/features/ee/payments/server/stripe";
|
||||
import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
const querySchema = z.object({
|
||||
team: z.string().transform((val) => parseInt(val)),
|
||||
session_id: z.string().min(1),
|
||||
});
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { team: id, session_id } = querySchema.parse(req.query);
|
||||
|
||||
const checkoutSession = await stripe.checkout.sessions.retrieve(session_id, {
|
||||
expand: ["subscription"],
|
||||
});
|
||||
if (!checkoutSession) throw new HttpError({ statusCode: 404, message: "Checkout session not found" });
|
||||
|
||||
const subscription = checkoutSession.subscription as Stripe.Subscription;
|
||||
if (checkoutSession.payment_status !== "paid")
|
||||
throw new HttpError({ statusCode: 402, message: "Payment required" });
|
||||
|
||||
/* Check if a team was already upgraded with this payment intent */
|
||||
let team = await prisma.team.findFirst({
|
||||
where: { metadata: { path: ["paymentId"], equals: checkoutSession.id } },
|
||||
});
|
||||
|
||||
let metadata;
|
||||
|
||||
if (!team) {
|
||||
const prevTeam = await prisma.team.findFirstOrThrow({ where: { id } });
|
||||
|
||||
metadata = teamMetadataSchema.safeParse(prevTeam.metadata);
|
||||
if (!metadata.success) throw new HttpError({ statusCode: 400, message: "Invalid team metadata" });
|
||||
|
||||
const { requestedSlug, ...newMetadata } = metadata.data || {};
|
||||
/** We save the metadata first to prevent duplicate payments */
|
||||
team = await prisma.team.update({
|
||||
where: { id },
|
||||
data: {
|
||||
metadata: {
|
||||
...newMetadata,
|
||||
paymentId: checkoutSession.id,
|
||||
subscriptionId: subscription.id || null,
|
||||
subscriptionItemId: subscription.items.data[0].id || null,
|
||||
},
|
||||
},
|
||||
});
|
||||
/** Legacy teams already have a slug, this will allow them to upgrade as well */
|
||||
const slug = prevTeam.slug || requestedSlug;
|
||||
if (slug) {
|
||||
try {
|
||||
/** Then we try to upgrade the slug, which may fail if a conflict came up since team creation */
|
||||
team = await prisma.team.update({ where: { id }, data: { slug } });
|
||||
} catch (error) {
|
||||
const { message, statusCode } = getRequestedSlugError(error, slug);
|
||||
return res.status(statusCode).json({ message });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!metadata) {
|
||||
metadata = teamMetadataSchema.safeParse(team.metadata);
|
||||
if (!metadata.success) throw new HttpError({ statusCode: 400, message: "Invalid team metadata" });
|
||||
}
|
||||
|
||||
const session = await getServerSession({ req });
|
||||
|
||||
if (!session) return { message: "Team upgraded successfully" };
|
||||
|
||||
const redirectUrl = team?.isOrganization
|
||||
? `${WEBAPP_URL}/settings/organizations/profile?upgraded=true`
|
||||
: `${WEBAPP_URL}/settings/teams/${team.id}/profile?upgraded=true`;
|
||||
|
||||
// redirect to team screen
|
||||
res.redirect(302, redirectUrl);
|
||||
}
|
||||
|
||||
export default defaultHandler({
|
||||
GET: Promise.resolve({ default: defaultResponder(handler) }),
|
||||
});
|
||||
Reference in New Issue
Block a user