45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
import { ValidationError, ConflictError } from "../errors/httpErrors.js";
|
|
import { CreateUserRequestDto } from "../dtos/CreateUserRequestDto.js";
|
|
import { UserDto } from "../dtos/UserDto.js";
|
|
import { encrypt } from "../utils/encryptionUtils.js";
|
|
import { UserRepository } from "../repositories/UserRepository.js";
|
|
import { UserDtoEntityMapper } from "../mappers/UserDtoEntityMapper.js";
|
|
|
|
export class UserController {
|
|
constructor(
|
|
private userRepository: UserRepository,
|
|
private mapper: UserDtoEntityMapper
|
|
) {}
|
|
|
|
async createUser(dto: CreateUserRequestDto): Promise<UserDto> {
|
|
// check mandatory fields
|
|
if(!dto.userData){
|
|
throw new ValidationError("User data is required")
|
|
}
|
|
const email = dto.userData.email;
|
|
if (!email || (email && email.length == 0)) {
|
|
throw new ValidationError("Email is required");
|
|
}
|
|
const password = dto.password;
|
|
if(!password || (password && password.length == 0)){
|
|
throw new ValidationError("Password is required");
|
|
}
|
|
const userName = dto.userData.userName;
|
|
if (!userName|| (userName && userName.length == 0)){
|
|
throw new ValidationError("Username is required");
|
|
}
|
|
|
|
// user name must be unique
|
|
const existingUser = await this.userRepository.findByUserName(userName);
|
|
if (existingUser) {
|
|
throw new ConflictError("User with this user name already exists");
|
|
}
|
|
|
|
const userEntity = this.mapper.toEntity(dto.userData);
|
|
userEntity.password = await encrypt.encryptpass(password);
|
|
|
|
const savedUser = await this.userRepository.create(userEntity);
|
|
|
|
return this.mapper.toDto(savedUser);
|
|
}
|
|
}
|