Fix type errors

This commit is contained in:
Joe Au-Yeung 2022-10-24 14:31:27 -04:00
parent 235e419467
commit 90e1cbc2cd
2 changed files with 14 additions and 4 deletions

View File

@ -9,6 +9,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if (req.method === "POST" || req.method === "GET") {
const referer = req.headers.referer;
if (!referer) {
res.status(500).json({ message: "Missing referer" });
return;
}
// If accessing a user's portal
if (referer.includes("/settings/billing")) {
const customerId = await getStripeCustomerIdFromUserId(req.session!.user.id);
@ -28,15 +33,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
// If accessing a team's portal if referer has /settings/team/[:teamId]/billing
if (/settings\/teams\/\d+\/billing/g.test(referer)) {
// Grab the teamId by just matching /settings/teams/[:teamId]/billing and getting third item in array after split
const teamId = parseInt(referer.match(/\/(settings.+)/g)[0].split("/")[3]);
const teamId = referer.match(/\/(settings.+)/g) || "";
const team = await getStripeIdsForTeam(parseInt(teamId[0].split("/")[3]));
const { stripeCustomerId } = await getStripeIdsForTeam(teamId);
if (!stripeCustomerId) {
if (!team?.stripeCustomerId) {
res.status(500).json({ message: "Missing customer id" });
return;
}
const stripeSession = await stripe.billingPortal.sessions.create({
customer: stripeCustomerId,
customer: team.stripeCustomerId,
return_url: `${process.env.NEXT_PUBLIC_WEBAPP_URL}/settings/teams/${teamId}/billing`,
});
res.redirect(302, stripeSession.url);

View File

@ -57,6 +57,11 @@ export const deleteTeamFromStripe = async (teamId: number) => {
select: { stripeCustomerId: true },
});
if (!stripeCustomerId) {
throw new Error("Team not found");
return;
}
if (stripeCustomerId) {
await stripe.customers.del(stripeCustomerId.stripeCustomerId);
return;