Passed
Pull Request — master (#406)
by
unknown
01:44
created

PayrollElementsTableFactory.create   B

Complexity

Conditions 4

Size

Total Lines 59
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 48
dl 0
loc 59
rs 8.7018
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
import { Injectable } from '@nestjs/common';
2
import { UserElementsView } from 'src/Application/HumanResource/Payslip/View/UserElementsView';
3
import { Table } from 'src/Infrastructure/Tables';
4
import { RowFactory } from 'src/Infrastructure/Tables/RowFactory';
5
import { formatFullName } from 'src/Infrastructure/Common/Utils/formatUtils';
6
7
@Injectable()
8
export class PayrollElementsTableFactory {
9
  constructor(private readonly cells: RowFactory) {}
10
11
  public create(elements: UserElementsView[]): Table {
12
    const columns = [
13
      'payroll-elements-user',
14
      'payroll-elements-contract',
15
      'payroll-elements-joiningDate',
16
      'payroll-elements-annualEarnings',
17
      'payroll-elements-monthlyEarnings',
18
      'payroll-elements-workingTime',
19
      'payroll-elements-transportFee',
20
      'payroll-elements-sustainableMobilityFee',
21
      'payroll-elements-mealTickets',
22
      'payroll-elements-healthInsurance',
23
      'payroll-elements-paidLeaves',
24
      'payroll-elements-unpaidLeaves',
25
      'payroll-elements-medicalLeaves',
26
      'payroll-elements-specialLeaves'
27
    ];
28
29
    const rows = [];
30
31
    for (const item of elements) {
32
      const row = this.cells
33
        .createBuilder()
34
        .value(formatFullName(item))
35
        .trans('payroll-elements-contract-value', {
36
          contract: item.contract,
37
          executivePosition: item.isExecutivePosition ? 'yes' : 'no'
38
        })
39
        .trans('common-date', { date: new Date(item.joiningDate) })
40
        .trans('common-money', { value: item.annualEarnings })
41
        .trans('common-money', { value: item.monthlyEarnings })
42
        .trans('users-workingTime-value', {
43
          workingTime: item.workingTime
44
        })
45
        .trans('common-money', { value: item.transportFee })
46
        .trans('common-money', {
47
          value: item.sustainableMobilityFee
48
        })
49
        .value(item.mealTickets)
50
        .trans(item.healthInsurance ? 'common-yes' : 'common-no')
51
        .template('pages/payroll_elements/_leaves.njk', {
52
          leaves: item.paidLeaves
53
        })
54
        .template('pages/payroll_elements/_leaves.njk', {
55
          leaves: item.unpaidLeaves
56
        })
57
        .template('pages/payroll_elements/_leaves.njk', {
58
          leaves: item.sickLeaves
59
        })
60
        .template('pages/payroll_elements/_leaves.njk', {
61
          leaves: item.exceptionalLeaves
62
        })
63
        .build();
64
65
      rows.push(row);
66
    }
67
68
    return new Table(columns, rows);
69
  }
70
}
71