From ea7a2c27125dd1b4f4fc10c1910cf42d64d9a023 Mon Sep 17 00:00:00 2001 From: David Nguyen Date: Thu, 14 Aug 2025 16:30:16 +1000 Subject: [PATCH] fix: create customer on signup (#1964) --- .../organisation/create-organisation.ts | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) 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,