Fixes upgrade for users without customer id (#2059)
This commit is contained in:
parent
eeb0cd7e4d
commit
b6a20cc4d7
2 changed files with 7 additions and 2 deletions
|
@ -34,7 +34,8 @@ const userType = Prisma.validator<Prisma.UserArgs>()({
|
||||||
});
|
});
|
||||||
|
|
||||||
type UserType = Prisma.UserGetPayload<typeof userType>;
|
type UserType = Prisma.UserGetPayload<typeof userType>;
|
||||||
export async function getStripeCustomerId(user: UserType): Promise<string | null> {
|
/** This will retrieve the customer ID from Stripe or create it if it doesn't exists yet. */
|
||||||
|
export async function getStripeCustomerId(user: UserType): Promise<string> {
|
||||||
let customerId: string | null = null;
|
let customerId: string | null = null;
|
||||||
|
|
||||||
if (user?.metadata && typeof user.metadata === "object" && "stripeCustomerId" in user.metadata) {
|
if (user?.metadata && typeof user.metadata === "object" && "stripeCustomerId" in user.metadata) {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import { Prisma } from "@prisma/client";
|
import { Prisma } from "@prisma/client";
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
|
|
||||||
|
import { getStripeCustomerId } from "@ee/lib/stripe/customer";
|
||||||
|
|
||||||
import { getSession } from "@lib/auth";
|
import { getSession } from "@lib/auth";
|
||||||
import { WEBSITE_URL } from "@lib/config/constants";
|
import { WEBSITE_URL } from "@lib/config/constants";
|
||||||
import { HttpError as HttpCode } from "@lib/core/http/error";
|
import { HttpError as HttpCode } from "@lib/core/http/error";
|
||||||
|
@ -27,6 +29,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const stripeCustomerId = await getStripeCustomerId(user);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${WEBSITE_URL}/api/upgrade`, {
|
const response = await fetch(`${WEBSITE_URL}/api/upgrade`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
@ -35,7 +39,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
stripeCustomerId: (user.metadata as Prisma.JsonObject)?.stripeCustomerId,
|
stripeCustomerId,
|
||||||
email: user.email,
|
email: user.email,
|
||||||
fromApp: true,
|
fromApp: true,
|
||||||
}),
|
}),
|
||||||
|
|
Loading…
Reference in a new issue