diff --git a/apps/builder/src/features/typebot/api/createTypebot.ts b/apps/builder/src/features/typebot/api/createTypebot.ts index 07d90d176..097640a87 100644 --- a/apps/builder/src/features/typebot/api/createTypebot.ts +++ b/apps/builder/src/features/typebot/api/createTypebot.ts @@ -67,7 +67,10 @@ export const createTypebot = authenticatedProcedure if ( typebot.customDomain && - (await isCustomDomainNotAvailable(typebot.customDomain)) + (await isCustomDomainNotAvailable({ + customDomain: typebot.customDomain, + workspaceId, + })) ) throw new TRPCError({ code: 'BAD_REQUEST', diff --git a/apps/builder/src/features/typebot/api/updateTypebot.ts b/apps/builder/src/features/typebot/api/updateTypebot.ts index ce53d5cb3..50e815053 100644 --- a/apps/builder/src/features/typebot/api/updateTypebot.ts +++ b/apps/builder/src/features/typebot/api/updateTypebot.ts @@ -131,7 +131,10 @@ export const updateTypebot = authenticatedProcedure if ( typebot.customDomain && existingTypebot.customDomain !== typebot.customDomain && - (await isCustomDomainNotAvailable(typebot.customDomain)) + (await isCustomDomainNotAvailable({ + customDomain: typebot.customDomain, + workspaceId: existingTypebot.workspace.id, + })) ) throw new TRPCError({ code: 'BAD_REQUEST', diff --git a/apps/builder/src/features/typebot/helpers/sanitizers.ts b/apps/builder/src/features/typebot/helpers/sanitizers.ts index fcf272f47..9098cdb73 100644 --- a/apps/builder/src/features/typebot/helpers/sanitizers.ts +++ b/apps/builder/src/features/typebot/helpers/sanitizers.ts @@ -123,7 +123,21 @@ export const isPublicIdNotAvailable = async (publicId: string) => { return typebotWithSameIdCount > 0 } -export const isCustomDomainNotAvailable = async (customDomain: string) => { +export const isCustomDomainNotAvailable = async ({ + customDomain, + workspaceId, +}: { + customDomain: string + workspaceId: string +}) => { + const domainCount = await prisma.customDomain.count({ + where: { + workspaceId, + name: customDomain.split('/')[0], + }, + }) + if (domainCount === 0) return true + const typebotWithSameDomainCount = await prisma.typebot.count({ where: { customDomain,