mirror of
https://github.com/documenso/documenso.git
synced 2025-11-16 09:41:35 +10:00
## Description Previously we assumed that there can only be 1 subscription per user. However, that will soon no longer the case with the introduction of the Teams subscription. This PR will apply the required migrations to support multiple subscriptions. ## Changes Made - Updated the Prisma schema to allow for multiple `Subscriptions` per `User` - Added a Stripe `customerId` field to the `User` model - Updated relevant billing sections to support multiple subscriptions ## Testing Performed - Tested running the Prisma migration on a demo database created on the main branch Will require a lot of additional testing. ## Checklist - [ ] I have tested these changes locally and they work as expected. - [ ] I have added/updated tests that prove the effectiveness of these changes. - [X] I have followed the project's coding style guidelines. ## Additional Notes Added the following custom SQL statement to the migration: > DELETE FROM "Subscription" WHERE "planId" IS NULL OR "priceId" IS NULL; Prior to deployment this will require changes to Stripe products: - Adding `type` meta attribute --------- Co-authored-by: Lucas Smith <me@lucasjamessmith.me>
40 lines
1.4 KiB
TypeScript
40 lines
1.4 KiB
TypeScript
'use server';
|
|
|
|
import { getCheckoutSession } from '@documenso/ee/server-only/stripe/get-checkout-session';
|
|
import { getStripeCustomerByUser } from '@documenso/ee/server-only/stripe/get-customer';
|
|
import { getPortalSession } from '@documenso/ee/server-only/stripe/get-portal-session';
|
|
import { getRequiredServerComponentSession } from '@documenso/lib/next-auth/get-server-component-session';
|
|
import { getSubscriptionsByUserId } from '@documenso/lib/server-only/subscription/get-subscriptions-by-user-id';
|
|
|
|
export type CreateCheckoutOptions = {
|
|
priceId: string;
|
|
};
|
|
|
|
export const createCheckout = async ({ priceId }: CreateCheckoutOptions) => {
|
|
const session = await getRequiredServerComponentSession();
|
|
|
|
const { user, stripeCustomer } = await getStripeCustomerByUser(session.user);
|
|
|
|
const existingSubscriptions = await getSubscriptionsByUserId({ userId: user.id });
|
|
|
|
const foundSubscription = existingSubscriptions.find(
|
|
(subscription) =>
|
|
subscription.priceId === priceId &&
|
|
subscription.periodEnd &&
|
|
subscription.periodEnd >= new Date(),
|
|
);
|
|
|
|
if (foundSubscription) {
|
|
return getPortalSession({
|
|
customerId: stripeCustomer.id,
|
|
returnUrl: `${process.env.NEXT_PUBLIC_WEBAPP_URL}/settings/billing`,
|
|
});
|
|
}
|
|
|
|
return getCheckoutSession({
|
|
customerId: stripeCustomer.id,
|
|
priceId,
|
|
returnUrl: `${process.env.NEXT_PUBLIC_WEBAPP_URL}/settings/billing`,
|
|
});
|
|
};
|