Passed
Pull Request — master (#220)
by
unknown
02:21
created

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

Complexity

Total Complexity 2
Complexity/F 1

Size

Lines of Code 75
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

2 Functions

Rating   Name   Duplication   Size   Complexity  
A DownloadPayrollElementsAction.respondDocx 0 18 1
A DownloadPayrollElementsAction.index 0 19 1
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 { UserRole } from 'src/Domain/HumanResource/User/User.entity';
8
import { RolesGuard } from 'src/Infrastructure/HumanResource/User/Security/RolesGuard';
9
import { Roles } from 'src/Infrastructure/HumanResource/User/Decorator/Roles';
10
import { IDateUtils } from 'src/Application/IDateUtils';
11
import { IDocxService } from 'src/Application/IDocxService';
12
import { GetPayrollElementsQuery } from 'src/Application/HumanResource/PayrollElements/Query/GetPayrollElementsQuery';
13
import { PayrollElementsLocals } from '../DTO/PayrollElementsDTO';
14
15
const formatMoney = (amount: number): string => {
16
  return new Intl.NumberFormat('fr-FR', {
17
    style: 'currency',
18
    currency: 'EUR'
19
  }).format(amount);
20
};
21
22
@Controller('payroll_elements')
23
@ApiTags('Human Resource')
24
@ApiBearerAuth()
25
@UseGuards(AuthGuard('bearer'), RolesGuard)
26
export class DownloadPayrollElementsAction {
27
  constructor(
28
    @Inject('IQueryBus')
29
    private readonly queryBus: IQueryBus,
30
    @Inject('IDocxService')
31
    private readonly docxService: IDocxService,
32
    @Inject('IDateUtils')
33
    private readonly dateUtils: IDateUtils
34
  ) {}
35
36
  @Get()
37
  @Roles(UserRole.COOPERATOR, UserRole.ACCOUNTANT)
38
  @ApiOperation({ summary: 'Download payroll elements' })
39
  public async index(@Res() res: Response) {
40
    const elements = await this.queryBus.execute(new GetPayrollElementsQuery());
41
    const now = new Date();
42
43
    const locals: PayrollElementsLocals = {
44
      elements,
45
      now,
46
      formatMoney,
47
      dateUtils: this.dateUtils
48
    };
49
50
    const yearMonth = this.dateUtils.format(now, 'y-MM');
51
    const fileStem = `${yearMonth}-elements-paie-fairness`;
52
53
    await this.respondDocx(locals, fileStem, res);
54
  }
55
56
  private async respondDocx(
57
    locals: any,
58
    fileStem: string,
59
    res: Response
60
  ): Promise<void> {
61
    const buffer = await this.docxService.toBuffer('PayrollElements', locals);
62
63
    res.set('Content-Disposition', `attachment; filename=${fileStem}.docx`);
64
    res.set(
65
      'Content-Type',
66
      'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
67
    );
68
    res.set('Content-Length', buffer.length.toString());
69
70
    const stream = new PassThrough();
71
    stream.end(buffer);
72
    stream.pipe(res);
73
  }
74
}
75