2022-02-07 23:35:26 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2023-03-02 18:15:28 +00:00
|
|
|
import type Stripe from "stripe";
|
2022-11-10 20:23:56 +00:00
|
|
|
import { z } from "zod";
|
2022-02-07 23:35:26 +00:00
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
import { getRequestedSlugError } from "@calcom/app-store/stripepayment/lib/team-billing";
|
2023-03-10 23:45:24 +00:00
|
|
|
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
|
2022-11-10 20:23:56 +00:00
|
|
|
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 { closeComUpdateTeam } from "@calcom/lib/sync/SyncServiceManager";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
|
2022-02-07 23:35:26 +00:00
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
const querySchema = z.object({
|
|
|
|
team: z.string().transform((val) => parseInt(val)),
|
|
|
|
session_id: z.string().min(1),
|
|
|
|
});
|
2022-02-07 23:35:26 +00:00
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
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 } },
|
|
|
|
});
|
2022-02-07 23:35:26 +00:00
|
|
|
|
2023-08-10 15:06:31 +00:00
|
|
|
let metadata;
|
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
if (!team) {
|
|
|
|
const prevTeam = await prisma.team.findFirstOrThrow({ where: { id } });
|
2023-08-09 16:56:53 +00:00
|
|
|
|
2023-08-10 15:06:31 +00:00
|
|
|
metadata = teamMetadataSchema.safeParse(prevTeam.metadata);
|
2023-08-09 16:56:53 +00:00
|
|
|
if (!metadata.success) throw new HttpError({ statusCode: 400, message: "Invalid team metadata" });
|
|
|
|
|
|
|
|
if (!metadata.data?.requestedSlug) {
|
|
|
|
throw new HttpError({
|
|
|
|
statusCode: 400,
|
|
|
|
message: "Can't publish team/org without `requestedSlug`",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const { requestedSlug, ...newMetadata } = metadata.data;
|
2023-02-17 23:45:50 +00:00
|
|
|
/** We save the metadata first to prevent duplicate payments */
|
|
|
|
team = await prisma.team.update({
|
|
|
|
where: { id },
|
|
|
|
data: {
|
|
|
|
metadata: {
|
2023-08-09 16:56:53 +00:00
|
|
|
...newMetadata,
|
2023-02-17 23:45:50 +00:00
|
|
|
paymentId: checkoutSession.id,
|
|
|
|
subscriptionId: subscription.id || null,
|
|
|
|
subscriptionItemId: subscription.items.data[0].id || null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2022-11-16 21:07:20 +00:00
|
|
|
/** Legacy teams already have a slug, this will allow them to upgrade as well */
|
2023-08-09 16:56:53 +00:00
|
|
|
const slug = prevTeam.slug || requestedSlug;
|
2023-02-17 23:45:50 +00:00
|
|
|
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 });
|
|
|
|
}
|
2022-11-10 20:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sync Services: Close.com
|
|
|
|
closeComUpdateTeam(prevTeam, team);
|
2022-02-07 23:35:26 +00:00
|
|
|
}
|
2022-11-10 20:23:56 +00:00
|
|
|
|
2023-08-10 15:06:31 +00:00
|
|
|
if (!metadata) {
|
|
|
|
metadata = teamMetadataSchema.safeParse(team.metadata);
|
|
|
|
if (!metadata.success) throw new HttpError({ statusCode: 400, message: "Invalid team metadata" });
|
|
|
|
}
|
|
|
|
|
2023-03-10 23:45:24 +00:00
|
|
|
const session = await getServerSession({ req, res });
|
2022-11-18 23:55:34 +00:00
|
|
|
|
|
|
|
if (!session) return { message: "Team upgraded successfully" };
|
|
|
|
|
2023-08-10 15:06:31 +00:00
|
|
|
const redirectUrl = metadata?.data?.isOrganization
|
|
|
|
? `${WEBAPP_URL}/settings/organizations/profile?upgraded=true`
|
|
|
|
: `${WEBAPP_URL}/settings/teams/${team.id}/profile?upgraded=true`;
|
|
|
|
|
2022-11-10 20:23:56 +00:00
|
|
|
// redirect to team screen
|
2023-08-10 15:06:31 +00:00
|
|
|
res.redirect(302, redirectUrl);
|
2022-02-07 23:35:26 +00:00
|
|
|
}
|
2022-11-10 20:23:56 +00:00
|
|
|
|
|
|
|
export default defaultHandler({
|
|
|
|
GET: Promise.resolve({ default: defaultResponder(handler) }),
|
|
|
|
});
|