Skip to content

The Open Source DocuSign Alternative.

fix: create customer on signup (#1964)

David Nguyenea7a2c27125d
diff --git a/packages/lib/server-only/organisation/create-organisation.ts b/packages/lib/server-only/organisation/create-organisation.ts
index c00ebd82c..32f6510d9 100644
--- a/packages/lib/server-only/organisation/create-organisation.ts
+++ b/packages/lib/server-only/organisation/create-organisation.ts
@@ -2,8 +2,10 @@ import type { Prisma } from '@prisma/client';
import { OrganisationType } from '@prisma/client';
import { OrganisationMemberRole } from '@prisma/client';
+import { createCustomer } from '@documenso/ee/server-only/stripe/create-customer';
import { prisma } from '@documenso/prisma';
+import { IS_BILLING_ENABLED } from '../../constants/app';
import { ORGANISATION_INTERNAL_GROUPS } from '../../constants/organisations';
import { AppErrorCode } from '../../errors/app-error';
import { AppError } from '../../errors/app-error';
@@ -30,6 +32,33 @@ export const createOrganisation = async ({
customerId,
claim,
}: 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) => {
const organisationSetting = await tx.organisationGlobalSettings.create({
data: {
@@ -64,7 +93,7 @@ export const createOrganisation = async ({
id: generateDatabaseId('org_group'),
})),
},
- customerId,
+ customerId: customerIdToUse,
},
include: {
groups: true,