Skip to content

Commit f1e25d3

Browse files
author
Mateusz Gajda
committed
athrow error if user with provided email exists
1 parent 0c39c71 commit f1e25d3

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { TravelhoopError } from "@travelhoop/infrastructure";
2+
import StatusCodes from "http-status-codes";
3+
4+
export class UserExistsError extends TravelhoopError {
5+
constructor() {
6+
super("User with provided email exists", StatusCodes.BAD_REQUEST);
7+
}
8+
}

src/modules/user/src/core/repositories/user.repository.ts

+4
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ export class UserRepository {
3232

3333
return user;
3434
}
35+
36+
async findByEmail(email: string) {
37+
return this.deps.dbConnection.em.getRepository(User).findOne({ email });
38+
}
3539
}

src/modules/user/src/core/services/user.service.ts

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { UserDto } from "../dto/user.dto";
77
import { Profile } from "../entities/profile";
88
import { User } from "../entities/user";
99
import { InvalidEmailOrPasswordError } from "../error/invalid-email-or-password.error";
10+
import { UserExistsError } from "../error/user-exists.error";
1011
import { UserCreated } from "../events/user-created.event";
1112
import { UserRepository } from "../repositories/user.repository";
1213
import { AuthService } from "./auth.service";
@@ -23,6 +24,12 @@ export class UserService {
2324
constructor(private readonly deps: UserServiceDependencies) {}
2425

2526
async register(dto: RegisterDto) {
27+
const existingUser = await this.deps.userRepository.findByEmail(dto.email);
28+
29+
if (existingUser) {
30+
throw new UserExistsError();
31+
}
32+
2633
const userId = Guid.create();
2734
const profileId = Guid.create();
2835
const password = await this.deps.passwordManager.hashPassword(dto.password);

0 commit comments

Comments
 (0)