Passed
Pull Request — master (#105)
by Mathieu
32:46
created

server/src/Infrastructure/HumanResource/PaySlip/Action/DownloadPaySlipAction.ts   A

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 48
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A DownloadPaySlipAction.index 0 16 3
1
import {
2
  Controller,
3
  Inject,
4
  BadRequestException,
5
  UseGuards,
6
  Get,
7
  Param,
8
  ForbiddenException
9
} from '@nestjs/common';
10
import {ApiUseTags, ApiBearerAuth, ApiOperation} from '@nestjs/swagger';
11
import {AuthGuard} from '@nestjs/passport';
12
import {User} from 'src/Domain/HumanResource/User/User.entity';
13
import {IdDTO} from 'src/Infrastructure/Common/DTO/IdDTO';
14
import {LoggedUser} from '../../User/Decorator/LoggedUser';
15
import {IQueryBus} from 'src/Application/IQueryBus';
16
import {DownloadFileQuery} from 'src/Application/File/Command/DownloadFileQuery';
17
import {GetPaySlipByIdQuery} from 'src/Application/HumanResource/PaySlip/Query/GetPaySlipByIdQuery';
18
import {PaySlipView} from 'src/Application/HumanResource/PaySlip/View/PaySlipView';
19
20
@Controller('pay_slips')
21
@ApiUseTags('Human Resource')
22
@ApiBearerAuth()
23
@UseGuards(AuthGuard('bearer'))
24
export class DownloadPaySlipAction {
25
  constructor(
26
    @Inject('IQueryBus')
27
    private readonly queryBus: IQueryBus
28
  ) {}
29
30
  @Get(':id/download')
31
  @ApiOperation({title: 'Download payslip'})
32
  public async index(@Param() dto: IdDTO, @LoggedUser() loggedUser: User) {
33
    try {
34
      const {user, file}: PaySlipView = await this.queryBus.execute(
35
        new GetPaySlipByIdQuery(dto.id)
36
      );
37
38
      if (user.id !== loggedUser.getId()) {
39
        throw new ForbiddenException();
40
      }
41
42
      await this.queryBus.execute(new DownloadFileQuery(file.id));
43
    } catch (e) {
44
      throw new BadRequestException(e.message);
45
    }
46
  }
47
}
48