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