diff --git a/servers/fdr/src/controllers/docs/v2/getDocsWriteV2Service.ts b/servers/fdr/src/controllers/docs/v2/getDocsWriteV2Service.ts index 6daa069759..6cf25999c3 100644 --- a/servers/fdr/src/controllers/docs/v2/getDocsWriteV2Service.ts +++ b/servers/fdr/src/controllers/docs/v2/getDocsWriteV2Service.ts @@ -55,12 +55,16 @@ export function getDocsWriteV2Service(app: FdrApplication): DocsV2WriteService { const customUrls = parseCustomDomainUrls({ customUrls: req.body.customDomains }); // ensure that the domains are not already registered by another org - const hasOwnership = await app.dao.docsV2().checkDomainsDontBelongToAnotherOrg( - [fernUrl, ...customUrls].map((url) => url.getFullUrl()), - req.body.orgId, - ); + const { allDomainsOwned: hasOwnership, unownedDomains } = await app.dao + .docsV2() + .checkDomainsDontBelongToAnotherOrg( + [fernUrl, ...customUrls].map((url) => url.getFullUrl()), + req.body.orgId, + ); if (!hasOwnership) { - throw new DomainBelongsToAnotherOrgError("Domain belongs to another org"); + throw new DomainBelongsToAnotherOrgError( + `The following domains belong to another organization: ${unownedDomains.join(", ")}`, + ); } const docsRegistrationId = DocsV1Write.DocsRegistrationId(uuidv4()); diff --git a/servers/fdr/src/db/docs/DocsV2Dao.ts b/servers/fdr/src/db/docs/DocsV2Dao.ts index 61ec735ab0..5e17891b5f 100644 --- a/servers/fdr/src/db/docs/DocsV2Dao.ts +++ b/servers/fdr/src/db/docs/DocsV2Dao.ts @@ -33,8 +33,13 @@ export interface LoadDocsConfigResponse { referencedApis: string[]; } +export interface CheckDomainOwnershipResponse { + allDomainsOwned: boolean; + unownedDomains: string[]; +} + export interface DocsV2Dao { - checkDomainsDontBelongToAnotherOrg(domains: string[], orgId: string): Promise; + checkDomainsDontBelongToAnotherOrg(domains: string[], orgId: string): Promise; loadDocsForURL(url: URL): Promise; @@ -88,10 +93,14 @@ export class DocsV2DaoImpl implements DocsV2Dao { }); } - public async checkDomainsDontBelongToAnotherOrg(domains: string[], orgId: string): Promise { + public async checkDomainsDontBelongToAnotherOrg( + domains: string[], + orgId: string, + ): Promise { const matchedDomains = await this.prisma.docsV2.findMany({ select: { orgID: true, + domain: true, }, where: { domain: { @@ -101,7 +110,14 @@ export class DocsV2DaoImpl implements DocsV2Dao { distinct: ["orgID", "domain"], }); - return matchedDomains.every((matchedDomain) => matchedDomain.orgID === orgId); + const allDomainsOwned = matchedDomains.every((matchedDomain) => matchedDomain.orgID === orgId); + const unownedDomains = matchedDomains + .filter((matchedDomain) => matchedDomain.orgID !== orgId) + .map((matchedDomain) => matchedDomain.domain); + return { + allDomainsOwned, + unownedDomains, + }; } public async loadDocsForURL(url: URL): Promise | undefined> {