Files
outline/server/commands/teamCreator.ts
T
Tom MoorandClaude Opus 5 34383b1690 fix: Duplicate team subdomain surfaced as a 500 error (#13329)
* fix: Friendly error and retry for duplicate team subdomains

Picking a subdomain already in use in settings surfaced the raw unique
constraint violation as a 500, and concurrent signups could race between
the availability check and the insert.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* test: Use setCloudHosted helper rather than spying on env

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* refactor: Move retryOnUniqueConstraintError to storage/database

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 08:03:56 -04:00

90 lines
2.3 KiB
TypeScript

import type { InferCreationAttributes } from "sequelize";
import slugify from "slugify";
import { RESERVED_SUBDOMAINS } from "@shared/utils/domains";
import { traceFunction } from "@server/logging/tracing";
import { Team } from "@server/models";
import type { APIContext } from "@server/types";
type Props = {
/** The displayed name of the team */
name: string;
/** The domain name from the email of the user logging in */
domain?: string;
/** The preferred subdomain to provision for the team if not yet created */
subdomain: string;
/** The public url of an image representing the team */
avatarUrl?: string | null;
/** Details of the authentication provider being used */
authenticationProviders: {
/** The name of the authentication provider, eg "google" */
name: string;
/** External identifier of the authentication provider */
providerId: string;
}[];
};
async function teamCreator(
ctx: APIContext,
{ name, subdomain, avatarUrl, authenticationProviders }: Props
): Promise<Team> {
if (!avatarUrl?.startsWith("http")) {
avatarUrl = null;
}
const availableSubdomain = await findAvailableSubdomain(ctx, subdomain);
return await Team.createWithCtx(
ctx,
{
name,
subdomain: availableSubdomain,
avatarUrl,
authenticationProviders,
} as Partial<InferCreationAttributes<Team>>,
undefined,
{
include: ["authenticationProviders"],
}
);
}
async function findAvailableSubdomain(
ctx: APIContext,
requestedSubdomain: string
) {
// filter subdomain to only valid characters
// if there are less than the minimum length, use a default subdomain
const normalizedSubdomain = slugify(requestedSubdomain, {
lower: true,
strict: true,
});
const baseSubdomain =
normalizedSubdomain.length < 3 ||
RESERVED_SUBDOMAINS.includes(normalizedSubdomain)
? "team"
: normalizedSubdomain;
let subdomain = baseSubdomain;
let append = 0;
for (;;) {
const existing = await Team.findOne({
where: { subdomain },
paranoid: false,
transaction: ctx.state.transaction,
});
if (existing) {
// subdomain was invalid or already used, try another
subdomain = `${baseSubdomain}${++append}`;
} else {
break;
}
}
return subdomain;
}
export default traceFunction({
spanName: "teamCreator",
})(teamCreator);