mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 16:51:38 +10:00
feat: add organisations (#1820)
This commit is contained in:
42
packages/ee/server-only/stripe/get-subscription.ts
Normal file
42
packages/ee/server-only/stripe/get-subscription.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { ORGANISATION_MEMBER_ROLE_PERMISSIONS_MAP } from '@documenso/lib/constants/organisations';
|
||||
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||
import { stripe } from '@documenso/lib/server-only/stripe';
|
||||
import { buildOrganisationWhereQuery } from '@documenso/lib/utils/organisations';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
export type GetSubscriptionOptions = {
|
||||
userId: number;
|
||||
organisationId: string;
|
||||
};
|
||||
|
||||
export const getSubscription = async ({ organisationId, userId }: GetSubscriptionOptions) => {
|
||||
const organisation = await prisma.organisation.findFirst({
|
||||
where: buildOrganisationWhereQuery({
|
||||
organisationId,
|
||||
userId,
|
||||
roles: ORGANISATION_MEMBER_ROLE_PERMISSIONS_MAP['MANAGE_ORGANISATION'],
|
||||
}),
|
||||
include: {
|
||||
subscription: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!organisation) {
|
||||
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||
message: 'Organisation not found',
|
||||
});
|
||||
}
|
||||
|
||||
if (!organisation.subscription) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const stripeSubscription = await stripe.subscriptions.retrieve(organisation.subscription.planId, {
|
||||
expand: ['items.data.price.product'],
|
||||
});
|
||||
|
||||
return {
|
||||
organisationSubscription: organisation.subscription,
|
||||
stripeSubscription,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user