Total Complexity | 3 |
Complexity/F | 3 |
Lines of Code | 48 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |