* Create `createUser` method in `userCreationService` * Refactor `UserRepository` create method to accept prisma input and remove business logic * API use `UserCreationService` * Move slugify to service * Use hashPassword instead * Type fixes in `UserCreationService` * Add `userCreationService` tests * API accept data object * Type fixes * Add user _post test * Add test for locked user * Add locked param to log * Add user repository tests * Do not return locked status * Explicitly pass `locked` prop * Fix tests when locked isn't returned * Fix tests * Pass locked prop * Edit test name * Use logger * Fix passing hashed password
55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import { hashPassword } from "@calcom/features/auth/lib/hashPassword";
|
|
import { checkIfEmailIsBlockedInWatchlistController } from "@calcom/features/watchlist/operations/check-if-email-in-watchlist.controller";
|
|
import logger from "@calcom/lib/logger";
|
|
import type { CreationSource, UserPermissionRole, IdentityProvider } from "@calcom/prisma/enums";
|
|
|
|
import slugify from "../../slugify";
|
|
import { UserRepository } from "../repository/user";
|
|
|
|
interface CreateUserInput {
|
|
email: string;
|
|
username: string;
|
|
name?: string | null;
|
|
password?: string;
|
|
brandColor?: string;
|
|
darkBrandColor?: string;
|
|
hideBranding?: boolean;
|
|
weekStart?: string;
|
|
timeZone?: string;
|
|
theme?: string | null;
|
|
timeFormat?: number;
|
|
locale?: string;
|
|
avatar?: string;
|
|
organizationId?: number | null;
|
|
creationSource: CreationSource;
|
|
role?: UserPermissionRole;
|
|
emailVerified?: Date;
|
|
identityProvider?: IdentityProvider;
|
|
}
|
|
|
|
const log = logger.getSubLogger({ prefix: ["[userCreationService]"] });
|
|
|
|
export class UserCreationService {
|
|
static async createUser({ data }: { data: CreateUserInput }) {
|
|
const { email, password, username } = data;
|
|
|
|
const shouldLockByDefault = await checkIfEmailIsBlockedInWatchlistController(email);
|
|
|
|
const hashedPassword = password ? await hashPassword(password) : null;
|
|
|
|
const user = await UserRepository.create({
|
|
...data,
|
|
username: slugify(username),
|
|
...(hashedPassword && { hashedPassword }),
|
|
organizationId: data?.organizationId ?? null,
|
|
locked: shouldLockByDefault,
|
|
});
|
|
|
|
log.info(`Created user: ${user.id} with locked status of ${user.locked}`);
|
|
|
|
const { locked, ...restUser } = user;
|
|
|
|
return restUser;
|
|
}
|
|
}
|