Skip to content

Commit 58e2364

Browse files
committed
Add docs in Use Cases
1 parent fdf7267 commit 58e2364

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

src/core/use-cases/user-use-cases.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,24 @@ import { NotFoundError } from '../../utils/errors.js'
22
import { AuthenticateUserInput, User, UserRepository, UserUseCases } from '../domain/user.js'
33
import { UserPrismaRepository } from '../infrastructure/user-repository.js'
44

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+
*/
510
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+
*/
615
constructor(private readonly userRepository: UserRepository = new UserPrismaRepository()) {}
716

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> {
923
const user = await this.userRepository.get(id)
1024

1125
if (!user) {
@@ -15,16 +29,36 @@ export class UserService implements UserUseCases {
1529
return user
1630
}
1731

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> {
1938
const user = await this.userRepository.findUserByUsername(input.username)
39+
40+
// When the user does not exists.
2041
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')
2243
}
2344

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) {
2549
throw new NotFoundError('User credentials does not match or it is not available for authentication')
2650
}
2751

2852
return user
2953
}
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+
}
3064
}

0 commit comments

Comments
 (0)