2022-01-14 13:49:15 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
2022-03-09 22:56:05 +00:00
|
|
|
import { deleteStripeCustomer } from "@calcom/stripe/customer";
|
2022-01-14 13:49:15 +00:00
|
|
|
|
|
|
|
import { getSession } from "@lib/auth";
|
|
|
|
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 !== "DELETE") {
|
|
|
|
return res.status(405).json({ message: "Method Not Allowed" });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === "DELETE") {
|
|
|
|
// Get user
|
|
|
|
const user = await prisma.user.findUnique({
|
|
|
|
rejectOnNotFound: true,
|
|
|
|
where: {
|
|
|
|
id: session.user?.id,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
email: true,
|
|
|
|
metadata: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
// Delete from stripe
|
|
|
|
await deleteStripeCustomer(user).catch(console.warn);
|
|
|
|
// Delete from Cal
|
|
|
|
await prisma.user.delete({
|
|
|
|
where: {
|
|
|
|
id: session?.user.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.status(204).end();
|
|
|
|
}
|
|
|
|
}
|