Completed
Push — master ( 7c7ab7...45cb8f )
by Mathieu
19s queued 11s
created

server/src/Infrastructure/HumanResource/User/Action/UpdateMeAction.ts   A

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 50
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A UpdateMeAction.index 0 16 2
1
import {
2
  Controller,
3
  Inject,
4
  Put,
5
  Body,
6
  BadRequestException,
7
  UseGuards
8
} from '@nestjs/common';
9
import {AuthGuard} from '@nestjs/passport';
10
import {ApiUseTags, ApiOperation, ApiBearerAuth} from '@nestjs/swagger';
11
import {ICommandBus} from 'src/Application/ICommandBus';
12
import {UserView} from 'src/Application/HumanResource/User/View/UserView';
13
import {UpdateProfileCommand} from 'src/Application/HumanResource/User/Command/UpdateProfileCommand';
14
import {LoggedUser} from '../Decorator/LoggedUser';
15
import {User} from 'src/Domain/HumanResource/User/User.entity';
16
import {IQueryBus} from 'src/Application/IQueryBus';
17
import {GetUserByIdQuery} from 'src/Application/HumanResource/User/Query/GetUserByIdQuery';
18
import {ProfileDTO} from '../DTO/ProfileDTO';
19
20
@Controller('users')
21
@ApiUseTags('User')
22
@ApiBearerAuth()
23
@UseGuards(AuthGuard('bearer'))
24
export class UpdateMeAction {
25
  constructor(
26
    @Inject('ICommandBus')
27
    private readonly commandBus: ICommandBus,
28
    @Inject('IQueryBus')
29
    private readonly queryBus: IQueryBus
30
  ) {}
31
32
  @Put('me')
33
  @ApiOperation({title: 'Update current user'})
34
  public async index(
35
    @Body() dto: ProfileDTO,
36
    @LoggedUser() user: User
37
  ): Promise<UserView> {
38
    try {
39
      const {firstName, lastName, email, password} = dto;
40
      await this.commandBus.execute(
41
        new UpdateProfileCommand(user, firstName, lastName, email, password)
42
      );
43
44
      return await this.queryBus.execute(new GetUserByIdQuery(user.getId()));
45
    } catch (e) {
46
      throw new BadRequestException(e.message);
47
    }
48
  }
49
}
50