Conditions | 1 |
Total Lines | 53 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import { Controller, Inject, Get, UseGuards, Res } from '@nestjs/common'; |
||
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 | } |
||
86 |