Skip to content

Commit

Permalink
fix(fdr): specify which domains are not owned by the org (#1720)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Oct 25, 2024
1 parent 9270067 commit b3b8d49
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
14 changes: 9 additions & 5 deletions servers/fdr/src/controllers/docs/v2/getDocsWriteV2Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
22 changes: 19 additions & 3 deletions servers/fdr/src/db/docs/DocsV2Dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>;
checkDomainsDontBelongToAnotherOrg(domains: string[], orgId: string): Promise<CheckDomainOwnershipResponse>;

loadDocsForURL(url: URL): Promise<LoadDocsDefinitionByUrlResponse | undefined>;

Expand Down Expand Up @@ -88,10 +93,14 @@ export class DocsV2DaoImpl implements DocsV2Dao {
});
}

public async checkDomainsDontBelongToAnotherOrg(domains: string[], orgId: string): Promise<boolean> {
public async checkDomainsDontBelongToAnotherOrg(
domains: string[],
orgId: string,
): Promise<CheckDomainOwnershipResponse> {
const matchedDomains = await this.prisma.docsV2.findMany({
select: {
orgID: true,
domain: true,
},
where: {
domain: {
Expand All @@ -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<WithoutQuestionMarks<LoadDocsDefinitionByUrlResponse> | undefined> {
Expand Down

0 comments on commit b3b8d49

Please sign in to comment.