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

FairCalendarController.get   D

Complexity

Conditions 11

Size

Total Lines 72
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 66
dl 0
loc 72
rs 4.8926
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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:

Complexity

Complex classes like FairCalendarController.get often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import {
2
  Controller,
3
  Get,
4
  Inject,
5
  Query,
6
  Render,
7
  UseGuards
8
} from '@nestjs/common';
9
import { User } from 'src/Domain/HumanResource/User/User.entity';
10
import { GetMonthlyFairCalendarQuery } from 'src/Application/FairCalendar/Query/GetMonthlyFairCalendarQuery';
11
import { IQueryBus } from 'src/Application/IQueryBus';
12
import { IsAuthenticatedGuard } from 'src/Infrastructure/HumanResource/User/Security/IsAuthenticatedGuard';
13
import { WithName } from 'src/Infrastructure/Common/ExtendedRouting/WithName';
14
import { FairCalendarControllerDTO } from '../DTO/FairCalendarControllerDTO';
15
import { ITranslator } from 'src/Infrastructure/Translations/ITranslator';
16
import { minutesToHours } from 'src/Infrastructure/Common/Utils/dateUtils';
17
import { LoggedUser } from 'src/Infrastructure/HumanResource/User/Decorator/LoggedUser';
18
import { UserView } from 'src/Application/HumanResource/User/View/UserView';
19
import { GetUsersQuery } from 'src/Application/HumanResource/User/Query/GetUsersQuery';
20
import { FairCalendarView } from 'src/Application/FairCalendar/View/FairCalendarView';
21
import { FairCalendarOverviewFactory } from 'src/Domain/FairCalendar/FairCalendarOverviewFactory';
22
import { FairCalendarOverviewTableFactory } from '../Table/FairCalendarOverviewTableFactory';
23
24
@Controller('app/faircalendar')
25
@UseGuards(IsAuthenticatedGuard)
26
export class FairCalendarController {
27
  constructor(
28
    @Inject('IQueryBus')
29
    private readonly queryBus: IQueryBus,
30
    @Inject('ITranslator')
31
    private readonly translator: ITranslator,
32
    private overviewFactory: FairCalendarOverviewFactory,
33
    private overviewTableFactory: FairCalendarOverviewTableFactory
34
  ) {}
35
36
  @Get()
37
  @WithName('faircalendar_index')
38
  @Render('pages/faircalendar/index.njk')
39
  public async get(
40
    @Query() dto: FairCalendarControllerDTO,
41
    @LoggedUser() user: User
42
  ) {
43
    const date =
44
      dto.month !== undefined && dto.year !== undefined
45
        ? new Date(dto.year, dto.month, 15)
46
        : new Date();
47
48
    const userId = dto.userId ? dto.userId : user['id'];
49
50
    const users: UserView[] = await this.queryBus.execute(new GetUsersQuery());
51
52
    const events: FairCalendarView[] = await this.queryBus.execute(
53
      new GetMonthlyFairCalendarQuery(date, userId)
54
    );
55
56
    const overview = await this.overviewFactory.create(events);
57
    const overviewTable = this.overviewTableFactory.create(overview);
58
59
    const fullCalendarEvents = events.map(event => {
60
      let title = `${minutesToHours(event.time)} - `;
61
62
      const fcEventType = event.type.startsWith('leave_')
63
        ? 'leave'
64
        : event.type;
65
66
      if (fcEventType === 'mission' && event.task && event.project) {
67
        title += `${event.project.name} (${event.task.name})`;
68
      } else if (fcEventType === 'leave') {
69
        title += `${this.translator.translate('leaves-type-value', {
70
          type: event.type.slice(6)
71
        })}`;
72
      } else {
73
        title += `${this.translator.translate('faircalendar-type-option', {
74
          type: event.type
75
        })}`;
76
      }
77
78
      return {
79
        // See: https://fullcalendar.io/docs/event-object
80
        type: fcEventType,
81
        start: event.date,
82
        end: event.date,
83
        title,
84
        ...(event.id
85
          ? { url: `/app/faircalendar/events/edit/${event.id}` }
86
          : {}),
87
        textColor: `var(--event-${fcEventType}-text)`,
88
        backgroundColor: `var(--event-${fcEventType}-background)`,
89
        borderColor: `var(--event-${fcEventType}-border)`
90
      };
91
    });
92
93
    const currentYear = date.getFullYear();
94
    const minYear = dto.minYear ?? currentYear - 5;
95
    const maxYear = dto.maxYear ?? currentYear;
96
97
    return {
98
      users,
99
      overviewTable,
100
      fullCalendarEvents,
101
      date,
102
      currentMonth: date.getMonth(),
103
      currentYear: date.getFullYear(),
104
      minYear,
105
      maxYear,
106
      userId
107
    };
108
  }
109
}
110