Files
calendar/packages/lib/server/checkRegularUsername.ts
T
a677d0d2c4 fix: Organization Migration - Prevent the redirected username from being claimed by others. (#13675)
* Check for migrated username

* Add e2e for reserved username due to migration

* Add username change test

* Update username.ts

* fixed type checks

* Refactor to use isOrganization

* Typing

* Typing

* Removed the isOrganization function

---------

Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
Co-authored-by: Keith Williams <keithwillcode@gmail.com>
2024-03-05 17:08:44 +00:00

38 lines
1.0 KiB
TypeScript

import slugify from "@calcom/lib/slugify";
import { ProfileRepository } from "./repository/profile";
import { isUsernameReservedDueToMigration } from "./username";
export async function checkRegularUsername(_username: string, currentOrgDomain?: string | null) {
const isCheckingUsernameInGlobalNamespace = !currentOrgDomain;
const username = slugify(_username);
const premium = !!process.env.NEXT_PUBLIC_IS_E2E && username.length < 5;
const profiles = currentOrgDomain
? await ProfileRepository.findManyByOrgSlugOrRequestedSlug({
orgSlug: currentOrgDomain,
usernames: [username],
})
: null;
const user = profiles?.length ? profiles[0].user : null;
if (user) {
return {
available: false as const,
premium,
message: "A user exists with that username",
};
}
const isUsernameAvailable = isCheckingUsernameInGlobalNamespace
? !(await isUsernameReservedDueToMigration(username))
: true;
return {
available: isUsernameAvailable,
premium,
};
}