File tree 3 files changed +19
-0
lines changed
src/modules/user/src/core
3 files changed +19
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -32,4 +32,8 @@ export class UserRepository {
32
32
33
33
return user ;
34
34
}
35
+
36
+ async findByEmail ( email : string ) {
37
+ return this . deps . dbConnection . em . getRepository ( User ) . findOne ( { email } ) ;
38
+ }
35
39
}
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import { UserDto } from "../dto/user.dto";
7
7
import { Profile } from "../entities/profile" ;
8
8
import { User } from "../entities/user" ;
9
9
import { InvalidEmailOrPasswordError } from "../error/invalid-email-or-password.error" ;
10
+ import { UserExistsError } from "../error/user-exists.error" ;
10
11
import { UserCreated } from "../events/user-created.event" ;
11
12
import { UserRepository } from "../repositories/user.repository" ;
12
13
import { AuthService } from "./auth.service" ;
@@ -23,6 +24,12 @@ export class UserService {
23
24
constructor ( private readonly deps : UserServiceDependencies ) { }
24
25
25
26
async register ( dto : RegisterDto ) {
27
+ const existingUser = await this . deps . userRepository . findByEmail ( dto . email ) ;
28
+
29
+ if ( existingUser ) {
30
+ throw new UserExistsError ( ) ;
31
+ }
32
+
26
33
const userId = Guid . create ( ) ;
27
34
const profileId = Guid . create ( ) ;
28
35
const password = await this . deps . passwordManager . hashPassword ( dto . password ) ;
You can’t perform that action at this time.
0 commit comments