mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 00:32:43 +10:00
## Description Add support for teams which will allow users to collaborate on documents. Teams features allows users to: - Create, manage and transfer teams - Manage team members - Manage team emails - Manage a shared team inbox and documents These changes do NOT include the following, which are planned for a future release: - Team templates - Team API - Search menu integration ## Testing Performed - Added E2E tests for general team management - Added E2E tests to validate document counts ## Checklist - [X] I have tested these changes locally and they work as expected. - [X] I have added/updated tests that prove the effectiveness of these changes. - [ ] I have updated the documentation to reflect these changes, if applicable. - [X] I have followed the project's coding style guidelines.
60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import { match } from 'ts-pattern';
|
|
|
|
import type { Stripe } from '@documenso/lib/server-only/stripe';
|
|
import { prisma } from '@documenso/prisma';
|
|
import type { Prisma } from '@documenso/prisma/client';
|
|
import { SubscriptionStatus } from '@documenso/prisma/client';
|
|
|
|
export type OnSubscriptionUpdatedOptions = {
|
|
userId?: number;
|
|
teamId?: number;
|
|
subscription: Stripe.Subscription;
|
|
};
|
|
|
|
export const onSubscriptionUpdated = async ({
|
|
userId,
|
|
teamId,
|
|
subscription,
|
|
}: OnSubscriptionUpdatedOptions) => {
|
|
await prisma.subscription.upsert(
|
|
mapStripeSubscriptionToPrismaUpsertAction(subscription, userId, teamId),
|
|
);
|
|
};
|
|
|
|
export const mapStripeSubscriptionToPrismaUpsertAction = (
|
|
subscription: Stripe.Subscription,
|
|
userId?: number,
|
|
teamId?: number,
|
|
): Prisma.SubscriptionUpsertArgs => {
|
|
if ((!userId && !teamId) || (userId && teamId)) {
|
|
throw new Error('Either userId or teamId must be provided.');
|
|
}
|
|
|
|
const status = match(subscription.status)
|
|
.with('active', () => SubscriptionStatus.ACTIVE)
|
|
.with('past_due', () => SubscriptionStatus.PAST_DUE)
|
|
.otherwise(() => SubscriptionStatus.INACTIVE);
|
|
|
|
return {
|
|
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: userId ?? null,
|
|
teamId: teamId ?? null,
|
|
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,
|
|
},
|
|
};
|
|
};
|