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
|
|
|
|