Passed
Push — master ( 09dedb...60e7ed )
by
unknown
02:10
created

GetUsersElementsCsvAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 72
dl 0
loc 74
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
B index 0 53 1
A formatLeaves 0 11 2
1
import { Controller, Inject, Get, UseGuards, Res } from '@nestjs/common';
2
import { AuthGuard } from '@nestjs/passport';
3
import { ApiCookieAuth } from '@nestjs/swagger';
4
import { IQueryBus } from 'src/Application/IQueryBus';
5
import { GetUsersElementsQuery } from 'src/Application/HumanResource/Payslip/Query/GetUsersElementsQuery';
6
import { UserElementsView } from 'src/Application/HumanResource/Payslip/View/UserElementsView';
7
import { Response } from 'express';
8
import { UserLeavesView } from 'src/Application/HumanResource/Payslip/View/UserLeavesView';
9
import { format } from 'date-fns';
10
11
@Controller('payslips.csv')
12
@ApiCookieAuth()
13
@UseGuards(AuthGuard('bearer'))
14
export class GetUsersElementsCsvAction {
15
  constructor(
16
    @Inject('IQueryBus')
17
    private readonly queryBus: IQueryBus
18
  ) {}
19
20
  @Get()
21
  public async index(@Res() res: Response) {
22
    res.header('Content-Type', 'text/csv');
23
24
    const date = new Date();
25
26
    const month = date.toISOString().substring(0, 7);
27
28
    res.attachment(`playslips-${month}.csv`);
29
30
    const payslips = await this.queryBus.execute(new GetUsersElementsQuery(date));
31
32
    const rows: string[] = payslips.map((payslip: UserElementsView) => [
33
        payslip.firstName,
34
        payslip.lastName,
35
        payslip.contract,
36
        payslip.joiningDate,
37
        payslip.annualEarnings,
38
        payslip.monthlyEarnings.toFixed(2),
39
        payslip.workingTime,
40
        payslip.transportFee,
41
        payslip.mealTickets,
42
        payslip.healthInsurance,
43
        this.formatLeaves(payslip.paidLeaves),
44
        payslip.unpaidLeaves.totalDays,
45
        payslip.sickLeaves.totalDays,
46
        payslip.exceptionalLeaves.totalDays,
47
    ].join(','));
48
49
    const headers = [
50
        'firstName',
51
        'lastName',
52
        'contract',
53
        'joiningDate',
54
        'annualEarnings',
55
        'monthlyEarnings',
56
        'workingTime',
57
        'transportFee',
58
        'mealTickets',
59
        'healthInsurance',
60
        'paidLeaves',
61
        'unpaidLeaves',
62
        'sickLeaves',
63
        'exceptionalLeaves',
64
    ];
65
66
    const csv: string = [
67
        headers.join(','),
68
        ...rows
69
    ].join("\n");
70
71
    return res.send(csv);
72
  }
73
74
  private formatLeaves(leaves: UserLeavesView): string {
75
    if (leaves.totalDays === 0) return '';
76
77
    const formatDate = (dateString: string): string => format(new Date(dateString), 'dd/MM/yyyy');
78
79
    const formattedSlots = leaves.leaves.map((leave) => formatDate(leave.startDate) + ' - ' + formatDate(leave.endDate));
80
81
    const row = leaves.totalDays + "\n" + formattedSlots.join("\n");
82
83
    return `"${row}"`;
84
  }
85
}
86