Total Complexity | 3 |
Complexity/F | 3 |
Lines of Code | 45 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import {QueryHandler} from '@nestjs/cqrs'; |
||
2 | import {Inject} from '@nestjs/common'; |
||
3 | import {LoginQuery} from './LoginQuery'; |
||
4 | import {AuthenticatedView} from '../View/AuthenticatedView'; |
||
5 | import {IUserRepository} from 'src/Domain/HumanResource/User/Repository/IUserRepository'; |
||
6 | import {IPasswordEncoder} from 'src/Application/IPasswordEncoder'; |
||
7 | import {PasswordNotMatchException} from 'src/Domain/HumanResource/User/Exception/PasswordNotMatchException'; |
||
8 | import {UserNotFoundException} from 'src/Domain/HumanResource/User/Exception/UserNotFoundException'; |
||
9 | |||
10 | @QueryHandler(LoginQuery) |
||
11 | export class LoginQueryHandler { |
||
12 | constructor( |
||
13 | @Inject('IUserRepository') |
||
14 | private readonly userRepository: IUserRepository, |
||
15 | @Inject('IPasswordEncoder') |
||
16 | private readonly passwordEncoder: IPasswordEncoder |
||
17 | ) {} |
||
18 | |||
19 | public async execute(query: LoginQuery): Promise<AuthenticatedView> { |
||
20 | const {password} = query; |
||
21 | const email = query.email.toLowerCase(); |
||
22 | const user = await this.userRepository.findOneByEmail(email); |
||
23 | |||
24 | if (!user) { |
||
25 | throw new UserNotFoundException(); |
||
26 | } |
||
27 | |||
28 | if ( |
||
29 | false === |
||
30 | (await this.passwordEncoder.compare(user.getPassword(), password)) |
||
31 | ) { |
||
32 | throw new PasswordNotMatchException(); |
||
33 | } |
||
34 | |||
35 | return new AuthenticatedView( |
||
36 | user.getId(), |
||
37 | user.getFirstName(), |
||
38 | user.getLastName(), |
||
39 | user.getEmail(), |
||
40 | user.getRole(), |
||
41 | user.getApiToken() |
||
42 | ); |
||
43 | } |
||
44 | } |
||
45 |