add auth and membership check to fetchSubscription

This commit is contained in:
msukkari 2025-02-13 15:11:48 -08:00
parent 4c0805c5e9
commit 0f43c00aa1
3 changed files with 25 additions and 54 deletions

View file

@ -329,9 +329,9 @@ export const redeemInvite = async (invite: Invite, userId: string): Promise<{ su
return notFound(); return notFound();
} }
// Incrememnt the seat count. We check if the subscription is valid in the redeem page so we return an error if that's not the case here // Incrememnt the seat count
if (org.stripeCustomerId) { if (org.stripeCustomerId) {
const subscription = await fetchSubscription(org.id); const subscription = await fetchSubscription(org.domain);
if (isServiceError(subscription)) { if (isServiceError(subscription)) {
return orgInvalidSubscription(); return orgInvalidSubscription();
} }
@ -422,6 +422,7 @@ export const setupInitialStripeCustomer = async (name: string, domain: string) =
const origin = (await headers()).get('origin') const origin = (await headers()).get('origin')
@nocheckin
const test_clock = await stripe.testHelpers.testClocks.create({ const test_clock = await stripe.testHelpers.testClocks.create({
frozen_time: Math.floor(Date.now() / 1000) frozen_time: Math.floor(Date.now() / 1000)
}) })
@ -541,26 +542,28 @@ export const getCustomerPortalSessionLink = async (domain: string): Promise<stri
return portalSession.url; return portalSession.url;
})); }));
export async function fetchSubscription(orgId: number) { export const fetchSubscription = (domain: string): Promise<any | ServiceError> =>
const org = await prisma.org.findUnique({ withAuth((session) =>
where: { withOrgMembership(session, domain, async (orgId) => {
id: orgId, const org = await prisma.org.findUnique({
}, where: {
}); id: orgId,
},
});
if (!org || !org.stripeCustomerId) { if (!org || !org.stripeCustomerId) {
return notFound(); return notFound();
} }
const subscriptions = await stripe.subscriptions.list({ const subscriptions = await stripe.subscriptions.list({
customer: org.stripeCustomerId! customer: org.stripeCustomerId
}) });
if (subscriptions.data.length === 0) { if (subscriptions.data.length === 0) {
return notFound(); return notFound();
} }
return subscriptions.data[0]; return subscriptions.data[0];
} }));
export const checkIfUserHasOrg = async (userId: string): Promise<boolean | ServiceError> => { export const checkIfUserHasOrg = async (userId: string): Promise<boolean | ServiceError> => {
const orgs = await prisma.userToOrg.findMany({ const orgs = await prisma.userToOrg.findMany({
@ -610,7 +613,7 @@ export const removeMember = async (memberId: string, domain: string): Promise<{
} }
if (org.stripeCustomerId) { if (org.stripeCustomerId) {
const subscription = await fetchSubscription(orgId); const subscription = await fetchSubscription(domain);
if (isServiceError(subscription)) { if (isServiceError(subscription)) {
return orgInvalidSubscription(); return orgInvalidSubscription();
} }
@ -645,7 +648,7 @@ export const removeMember = async (memberId: string, domain: string): Promise<{
export const getSubscriptionData = async (domain: string) => export const getSubscriptionData = async (domain: string) =>
withAuth(async (session) => withAuth(async (session) =>
withOrgMembership(session, domain, async (orgId) => { withOrgMembership(session, domain, async (orgId) => {
const subscription = await fetchSubscription(orgId); const subscription = await fetchSubscription(domain);
if (isServiceError(subscription)) { if (isServiceError(subscription)) {
return orgInvalidSubscription(); return orgInvalidSubscription();
} }

View file

@ -43,7 +43,7 @@ export default async function Layout({
return <PageNotFound /> return <PageNotFound />
} }
const subscription = await fetchSubscription(org.id); const subscription = await fetchSubscription(domain);
if (isServiceError(subscription) || (subscription.status !== "active" && subscription.status !== "trialing")) { if (isServiceError(subscription) || (subscription.status !== "active" && subscription.status !== "trialing")) {
return ( return (
<div className="flex flex-col items-center overflow-hidden min-h-screen"> <div className="flex flex-col items-center overflow-hidden min-h-screen">

View file

@ -6,8 +6,6 @@ import { AcceptInviteButton } from "./components/acceptInviteButton"
import Image from "next/image"; import Image from "next/image";
import logoDark from "@/public/sb_logo_dark_large.png"; import logoDark from "@/public/sb_logo_dark_large.png";
import logoLight from "@/public/sb_logo_light_large.png"; import logoLight from "@/public/sb_logo_light_large.png";
import { fetchSubscription } from "@/actions";
import { isServiceError } from "@/lib/utils";
interface RedeemPageProps { interface RedeemPageProps {
searchParams?: { searchParams?: {
@ -110,36 +108,6 @@ export default async function RedeemPage({ searchParams }: RedeemPageProps) {
) )
} }
const stripeCustomerId = org.stripeCustomerId;
if (stripeCustomerId) {
const subscription = await fetchSubscription(org.id);
console.log(org);
console.log(subscription);
if (isServiceError(subscription)) {
return (
<div className="flex flex-col justify-center items-center mt-8 mb-8 md:mt-18 w-full px-5">
<div className="max-h-44 w-auto mb-4">
<Image
src={logoDark}
className="h-18 md:h-40 w-auto hidden dark:block"
alt={"Sourcebot logo"}
priority={true}
/>
<Image
src={logoLight}
className="h-18 md:h-40 w-auto block dark:hidden"
alt={"Sourcebot logo"}
priority={true}
/>
</div>
<div className="flex justify-center items-center">
<h1>This organization's subscription has expired. Please renew the subscription and try again.</h1>
</div>
</div>
)
}
}
return ( return (
<div className="flex flex-col justify-center items-center mt-8 mb-8 md:mt-18 w-full px-5"> <div className="flex flex-col justify-center items-center mt-8 mb-8 md:mt-18 w-full px-5">
<div className="max-h-44 w-auto mb-4"> <div className="max-h-44 w-auto mb-4">