Passed
Pull Request — master (#220)
by
unknown
01:36
created

server/src/Infrastructure/HumanResource/PayrollElements/Action/DownloadPayrollElementsAction.ts   A

Complexity

Total Complexity 4
Complexity/F 1.33

Size

Lines of Code 103
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 93
mnd 1
bc 1
fnc 3
dl 0
loc 103
rs 10
bpm 0.3333
cpm 1.3333
noi 0
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A DownloadPayrollElementsAction.respondDocx 0 18 1
A DownloadPayrollElementsAction.respondPdf 0 16 1
A DownloadPayrollElementsAction.index 0 24 2
1
import { Get, Controller, Inject, UseGuards, Res, Query } from '@nestjs/common';
2
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
3
import { IQueryBus } from '@nestjs/cqrs';
4
import { AuthGuard } from '@nestjs/passport';
5
import { PassThrough } from 'stream';
6
import { Response } from 'express';
7
import { PDFService } from '@t00nday/nestjs-pdf';
8
import { UserRole } from 'src/Domain/HumanResource/User/User.entity';
9
import { RolesGuard } from 'src/Infrastructure/HumanResource/User/Security/RolesGuard';
10
import { Roles } from 'src/Infrastructure/HumanResource/User/Decorator/Roles';
11
import { IDateUtils } from 'src/Application/IDateUtils';
12
import { IDocxService } from 'src/Application/IDocxService';
13
import { GetPayrollElementsQuery } from 'src/Application/HumanResource/PayrollElements/Query/GetPayrollElementsQuery';
14
import {
15
  PayrollElementsDTO,
16
  PayrollElementsLocals
17
} from '../DTO/PayrollElementsDTO';
18
19
const formatMoney = (amount: number): string => {
20
  return new Intl.NumberFormat('fr-FR', {
21
    style: 'currency',
22
    currency: 'EUR'
23
  }).format(amount);
24
};
25
26
@Controller('payroll_elements')
27
@ApiTags('Human Resource')
28
@ApiBearerAuth()
29
@UseGuards(AuthGuard('bearer'), RolesGuard)
30
export class DownloadPayrollElementsAction {
31
  constructor(
32
    @Inject('IQueryBus')
33
    private readonly queryBus: IQueryBus,
34
    private readonly pdfService: PDFService,
35
    @Inject('IDocxService')
36
    private readonly docxService: IDocxService,
37
    @Inject('IDateUtils')
38
    private readonly dateUtils: IDateUtils
39
  ) {}
40
41
  @Get()
42
  @Roles(UserRole.COOPERATOR, UserRole.ACCOUNTANT)
43
  @ApiOperation({ summary: 'Download payroll elements' })
44
  public async index(@Query() dto: PayrollElementsDTO, @Res() res: Response) {
45
    const elements = await this.queryBus.execute(new GetPayrollElementsQuery());
46
    const now = new Date();
47
48
    const locals: PayrollElementsLocals = {
49
      elements,
50
      now,
51
      formatMoney,
52
      dateUtils: this.dateUtils
53
    };
54
55
    const yearMonth = this.dateUtils.format(now, 'y-MM');
56
    const fileStem = `${yearMonth}-elements-paie-fairness`;
57
58
    const { format } = dto;
59
60
    if (format == 'docx') {
61
      await this.respondDocx(locals, fileStem, res);
62
    } else {
63
      await this.respondPdf(locals, fileStem, res);
64
    }
65
  }
66
67
  private async respondPdf(
68
    locals: any,
69
    fileStem: string,
70
    res: Response
71
  ): Promise<void> {
72
    const buffer = await this.pdfService
73
      .toBuffer('PayrollElements', { locals })
74
      .toPromise();
75
    res.set('Content-Disposition', `attachment; filename=${fileStem}.pdf`);
76
    res.set('Content-Type', 'application/pdf');
77
    res.set('Content-Length', buffer.length.toString());
78
79
    const stream = new PassThrough();
80
    stream.end(buffer);
81
    stream.pipe(res);
82
  }
83
84
  private async respondDocx(
85
    locals: any,
86
    fileStem: string,
87
    res: Response
88
  ): Promise<void> {
89
    const buffer = await this.docxService.toBuffer('PayrollElements', locals);
90
91
    res.set('Content-Disposition', `attachment; filename=${fileStem}.docx`);
92
    res.set(
93
      'Content-Type',
94
      'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
95
    );
96
    res.set('Content-Length', buffer.length.toString());
97
98
    const stream = new PassThrough();
99
    stream.end(buffer);
100
    stream.pipe(res);
101
  }
102
}
103