calcom/pages/api/upgrade.ts
Omar López 4cd7a4ce5b
Adds trial banner and in-app upgrade (#1402)
* WIP trial banner

* Fixes days left count

* Defers stripe loading until needed

* Fixes auth issues

* Checkout fixes

* Allows for signup testing

* Debugging checkout

* Adds tests for trial banner

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2022-01-03 22:50:59 +00:00

50 lines
1.3 KiB
TypeScript

import { Prisma } from "@prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { getSession } from "@lib/auth";
import { WEBSITE_URL } from "@lib/config/constants";
import { HttpError as HttpCode } from "@lib/core/http/error";
import prisma from "@lib/prisma";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const session = await getSession({ req });
if (!session?.user?.id) {
return res.status(401).json({ message: "Not authenticated" });
}
if (req.method !== "GET") {
throw new HttpCode({ statusCode: 405, message: "Method Not Allowed" });
}
const user = await prisma.user.findUnique({
rejectOnNotFound: true,
where: {
id: session.user.id,
},
select: {
email: true,
metadata: true,
},
});
try {
const response = await fetch(`${WEBSITE_URL}/api/upgrade`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
stripeCustomerId: (user.metadata as Prisma.JsonObject)?.stripeCustomerId,
email: user.email,
fromApp: true,
}),
});
const data = await response.json();
res.redirect(303, data.url);
} catch (error) {
console.error(`error`, error);
res.redirect(303, req.headers.origin || "/");
}
}