mirror of
https://github.com/sourcebot-dev/sourcebot.git
synced 2025-12-12 12:25:22 +00:00
add auth and membership check to fetchSubscription
This commit is contained in:
parent
4c0805c5e9
commit
0f43c00aa1
3 changed files with 25 additions and 54 deletions
|
|
@ -329,9 +329,9 @@ export const redeemInvite = async (invite: Invite, userId: string): Promise<{ su
|
|||
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) {
|
||||
const subscription = await fetchSubscription(org.id);
|
||||
const subscription = await fetchSubscription(org.domain);
|
||||
if (isServiceError(subscription)) {
|
||||
return orgInvalidSubscription();
|
||||
}
|
||||
|
|
@ -422,6 +422,7 @@ export const setupInitialStripeCustomer = async (name: string, domain: string) =
|
|||
|
||||
const origin = (await headers()).get('origin')
|
||||
|
||||
@nocheckin
|
||||
const test_clock = await stripe.testHelpers.testClocks.create({
|
||||
frozen_time: Math.floor(Date.now() / 1000)
|
||||
})
|
||||
|
|
@ -541,26 +542,28 @@ export const getCustomerPortalSessionLink = async (domain: string): Promise<stri
|
|||
return portalSession.url;
|
||||
}));
|
||||
|
||||
export async function fetchSubscription(orgId: number) {
|
||||
const org = await prisma.org.findUnique({
|
||||
where: {
|
||||
id: orgId,
|
||||
},
|
||||
});
|
||||
export const fetchSubscription = (domain: string): Promise<any | ServiceError> =>
|
||||
withAuth((session) =>
|
||||
withOrgMembership(session, domain, async (orgId) => {
|
||||
const org = await prisma.org.findUnique({
|
||||
where: {
|
||||
id: orgId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!org || !org.stripeCustomerId) {
|
||||
return notFound();
|
||||
}
|
||||
if (!org || !org.stripeCustomerId) {
|
||||
return notFound();
|
||||
}
|
||||
|
||||
const subscriptions = await stripe.subscriptions.list({
|
||||
customer: org.stripeCustomerId!
|
||||
})
|
||||
const subscriptions = await stripe.subscriptions.list({
|
||||
customer: org.stripeCustomerId
|
||||
});
|
||||
|
||||
if (subscriptions.data.length === 0) {
|
||||
return notFound();
|
||||
}
|
||||
return subscriptions.data[0];
|
||||
}
|
||||
if (subscriptions.data.length === 0) {
|
||||
return notFound();
|
||||
}
|
||||
return subscriptions.data[0];
|
||||
}));
|
||||
|
||||
export const checkIfUserHasOrg = async (userId: string): Promise<boolean | ServiceError> => {
|
||||
const orgs = await prisma.userToOrg.findMany({
|
||||
|
|
@ -610,7 +613,7 @@ export const removeMember = async (memberId: string, domain: string): Promise<{
|
|||
}
|
||||
|
||||
if (org.stripeCustomerId) {
|
||||
const subscription = await fetchSubscription(orgId);
|
||||
const subscription = await fetchSubscription(domain);
|
||||
if (isServiceError(subscription)) {
|
||||
return orgInvalidSubscription();
|
||||
}
|
||||
|
|
@ -645,7 +648,7 @@ export const removeMember = async (memberId: string, domain: string): Promise<{
|
|||
export const getSubscriptionData = async (domain: string) =>
|
||||
withAuth(async (session) =>
|
||||
withOrgMembership(session, domain, async (orgId) => {
|
||||
const subscription = await fetchSubscription(orgId);
|
||||
const subscription = await fetchSubscription(domain);
|
||||
if (isServiceError(subscription)) {
|
||||
return orgInvalidSubscription();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ export default async function Layout({
|
|||
return <PageNotFound />
|
||||
}
|
||||
|
||||
const subscription = await fetchSubscription(org.id);
|
||||
const subscription = await fetchSubscription(domain);
|
||||
if (isServiceError(subscription) || (subscription.status !== "active" && subscription.status !== "trialing")) {
|
||||
return (
|
||||
<div className="flex flex-col items-center overflow-hidden min-h-screen">
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ import { AcceptInviteButton } from "./components/acceptInviteButton"
|
|||
import Image from "next/image";
|
||||
import logoDark from "@/public/sb_logo_dark_large.png";
|
||||
import logoLight from "@/public/sb_logo_light_large.png";
|
||||
import { fetchSubscription } from "@/actions";
|
||||
import { isServiceError } from "@/lib/utils";
|
||||
|
||||
interface RedeemPageProps {
|
||||
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 (
|
||||
<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">
|
||||
|
|
|
|||
Loading…
Reference in a new issue