api/src/Application/User/Query/UserLoginQueryHandler.ts   A
last analyzed

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 44
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 37
mnd 2
bc 2
fnc 1
dl 0
loc 44
rs 10
bpm 2
cpm 3
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A UserLoginQueryHandler.execute 0 23 3
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