mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 16:51:38 +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>
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import { match } from 'ts-pattern';
|
|
|
|
import type { Stripe } from '@documenso/lib/server-only/stripe';
|
|
import { prisma } from '@documenso/prisma';
|
|
import { SubscriptionStatus } from '@documenso/prisma/client';
|
|
|
|
export type OnSubscriptionUpdatedOptions = {
|
|
userId: number;
|
|
subscription: Stripe.Subscription;
|
|
};
|
|
|
|
export const onSubscriptionUpdated = async ({
|
|
userId,
|
|
subscription,
|
|
}: OnSubscriptionUpdatedOptions) => {
|
|
const status = match(subscription.status)
|
|
.with('active', () => SubscriptionStatus.ACTIVE)
|
|
.with('past_due', () => SubscriptionStatus.PAST_DUE)
|
|
.otherwise(() => SubscriptionStatus.INACTIVE);
|
|
|
|
await prisma.subscription.upsert({
|
|
where: {
|
|
planId: subscription.id,
|
|
},
|
|
create: {
|
|
status: status,
|
|
planId: subscription.id,
|
|
priceId: subscription.items.data[0].price.id,
|
|
periodEnd: new Date(subscription.current_period_end * 1000),
|
|
userId,
|
|
cancelAtPeriodEnd: subscription.cancel_at_period_end,
|
|
},
|
|
update: {
|
|
status: status,
|
|
planId: subscription.id,
|
|
priceId: subscription.items.data[0].price.id,
|
|
periodEnd: new Date(subscription.current_period_end * 1000),
|
|
cancelAtPeriodEnd: subscription.cancel_at_period_end,
|
|
},
|
|
});
|
|
};
|