@@ -2,10 +2,24 @@ import { NotFoundError } from '../../utils/errors.js'
2
2
import { AuthenticateUserInput , User , UserRepository , UserUseCases } from '../domain/user.js'
3
3
import { UserPrismaRepository } from '../infrastructure/user-repository.js'
4
4
5
+ /**
6
+ * The `UserSevice` represents the use case implementations for the `User` entity.
7
+ * It holds all the operations around the users including the get user details and part of the authentication process.
8
+ * This service depends on a user repository to handle users.
9
+ */
5
10
export class UserService implements UserUseCases {
11
+ /**
12
+ * Instance a new `UserService` used to handle users.
13
+ * @param userRepository Abstract user repository implementation to handle user data.
14
+ */
6
15
constructor ( private readonly userRepository : UserRepository = new UserPrismaRepository ( ) ) { }
7
16
8
- async getUserById ( id : string ) : Promise < User > {
17
+ /**
18
+ * Get a User by a given id.
19
+ * @param id User id.
20
+ * @returns Async result containing a User instance. This operation can also throw an error when the user does not exists.
21
+ */
22
+ public async getUserById ( id : string ) : Promise < User > {
9
23
const user = await this . userRepository . get ( id )
10
24
11
25
if ( ! user ) {
@@ -15,16 +29,36 @@ export class UserService implements UserUseCases {
15
29
return user
16
30
}
17
31
18
- async authenticateUser ( input : AuthenticateUserInput ) : Promise < User > {
32
+ /**
33
+ * Validate a user input model for authentication process.
34
+ * @param input User authentication model.
35
+ * @returns Async result containing the user when the operation succeed. This operation can also throw an error when the user does not exists or the credentials does not matches.
36
+ */
37
+ public async authenticateUser ( input : AuthenticateUserInput ) : Promise < User > {
19
38
const user = await this . userRepository . findUserByUsername ( input . username )
39
+
40
+ // When the user does not exists.
20
41
if ( ! user ) {
21
- throw new NotFoundError ( 'User not available for authentication. ' )
42
+ throw new NotFoundError ( 'User credentials does not match or it is not available for authentication' )
22
43
}
23
44
24
- if ( user . password !== input . password ) {
45
+ const password = await this . encryptPassword ( input . password )
46
+
47
+ // When the user exists but the password does not match.
48
+ if ( user . password !== password ) {
25
49
throw new NotFoundError ( 'User credentials does not match or it is not available for authentication' )
26
50
}
27
51
28
52
return user
29
53
}
54
+
55
+ /**
56
+ * Encrypt a hash string to be used as a password
57
+ * @param password String to be encrypted.
58
+ * @returns An encrypt string using a hash strategy.
59
+ */
60
+ private async encryptPassword ( password : string ) : Promise < string > {
61
+ // TODO: implement crypto here for password ... in progress
62
+ return password
63
+ }
30
64
}
0 commit comments