mirror of
https://github.com/documenso/documenso.git
synced 2025-11-21 20:21:38 +10:00
Merge branch 'main' into feat/org-insights
This commit is contained in:
@ -2,8 +2,10 @@ import type { Prisma } from '@prisma/client';
|
|||||||
import { OrganisationType } from '@prisma/client';
|
import { OrganisationType } from '@prisma/client';
|
||||||
import { OrganisationMemberRole } from '@prisma/client';
|
import { OrganisationMemberRole } from '@prisma/client';
|
||||||
|
|
||||||
|
import { createCustomer } from '@documenso/ee/server-only/stripe/create-customer';
|
||||||
import { prisma } from '@documenso/prisma';
|
import { prisma } from '@documenso/prisma';
|
||||||
|
|
||||||
|
import { IS_BILLING_ENABLED } from '../../constants/app';
|
||||||
import { ORGANISATION_INTERNAL_GROUPS } from '../../constants/organisations';
|
import { ORGANISATION_INTERNAL_GROUPS } from '../../constants/organisations';
|
||||||
import { AppErrorCode } from '../../errors/app-error';
|
import { AppErrorCode } from '../../errors/app-error';
|
||||||
import { AppError } from '../../errors/app-error';
|
import { AppError } from '../../errors/app-error';
|
||||||
@ -30,6 +32,33 @@ export const createOrganisation = async ({
|
|||||||
customerId,
|
customerId,
|
||||||
claim,
|
claim,
|
||||||
}: CreateOrganisationOptions) => {
|
}: CreateOrganisationOptions) => {
|
||||||
|
let customerIdToUse = customerId;
|
||||||
|
|
||||||
|
if (!customerId && IS_BILLING_ENABLED()) {
|
||||||
|
const user = await prisma.user.findUnique({
|
||||||
|
where: {
|
||||||
|
id: userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||||
|
message: 'User not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
customerIdToUse = await createCustomer({
|
||||||
|
name: user.name || user.email,
|
||||||
|
email: user.email,
|
||||||
|
})
|
||||||
|
.then((customer) => customer.id)
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(err);
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return await prisma.$transaction(async (tx) => {
|
return await prisma.$transaction(async (tx) => {
|
||||||
const organisationSetting = await tx.organisationGlobalSettings.create({
|
const organisationSetting = await tx.organisationGlobalSettings.create({
|
||||||
data: {
|
data: {
|
||||||
@ -64,7 +93,7 @@ export const createOrganisation = async ({
|
|||||||
id: generateDatabaseId('org_group'),
|
id: generateDatabaseId('org_group'),
|
||||||
})),
|
})),
|
||||||
},
|
},
|
||||||
customerId,
|
customerId: customerIdToUse,
|
||||||
},
|
},
|
||||||
include: {
|
include: {
|
||||||
groups: true,
|
groups: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user