src/Infrastructure/FairCalendar/Controller/AddEventController.ts   A
last analyzed

Complexity

Total Complexity 3
Complexity/F 1.5

Size

Lines of Code 109
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 91
mnd 1
bc 1
fnc 2
dl 0
loc 109
rs 10
bpm 0.5
cpm 1.5
noi 0
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A AddEventController.post 0 33 2
A AddEventController.get 0 31 1
1
import {
2
  BadRequestException,
3
  Body,
4
  Controller,
5
  Get,
6
  Inject,
7
  Param,
8
  Post,
9
  Render,
10
  Res,
11
  UseGuards
12
} from '@nestjs/common';
13
import { Response } from 'express';
14
import { ICommandBus } from 'src/Application/ICommandBus';
15
import { IsAuthenticatedGuard } from 'src/Infrastructure/HumanResource/User/Security/IsAuthenticatedGuard';
16
import { WithName } from 'src/Infrastructure/Common/ExtendedRouting/WithName';
17
import { AddEventControllerDTO } from '../DTO/AddEventControllerDTO';
18
import { AddEventDTO } from '../DTO/AddEventDTO';
19
import { AddEventCommand } from 'src/Application/FairCalendar/Command/AddEventCommand';
20
import { LoggedUser } from 'src/Infrastructure/HumanResource/User/Decorator/LoggedUser';
21
import { User } from 'src/Domain/HumanResource/User/User.entity';
22
import { IQueryBus } from 'src/Application/IQueryBus';
23
import { GetTasksQuery } from 'src/Application/Task/Query/GetTasksQuery';
24
import { GetProjectsQuery } from 'src/Application/Project/Query/GetProjectsQuery';
25
import { GetCooperativeQuery } from 'src/Application/Settings/Query/GetCooperativeQuery';
26
import { ArrayUtils } from 'src/Infrastructure/Common/Utils/ArrayUtils';
27
import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver';
28
import { makeMonthUrl } from '../Routing/urls';
29
30
@Controller('app/faircalendar/events/add')
31
@UseGuards(IsAuthenticatedGuard)
32
export class AddEventController {
33
  constructor(
34
    @Inject('ICommandBus')
35
    private readonly commandBus: ICommandBus,
36
    @Inject('IQueryBus')
37
    private readonly queryBus: IQueryBus,
38
    private readonly resolver: RouteNameResolver
39
  ) {}
40
41
  @Get(':startDate--:endDate')
42
  @WithName('faircalendar_events_add')
43
  @Render('pages/faircalendar/events/add.njk')
44
  public async get(@Param() dto: AddEventControllerDTO) {
45
    const types = [
46
      'mission',
47
      'dojo',
48
      'support',
49
      'formationConference',
50
      'other'
51
    ];
52
53
    const tasksPagination = await this.queryBus.execute(new GetTasksQuery(1));
54
55
    const projectsPagination = await this.queryBus.execute(
56
      new GetProjectsQuery(null, true)
57
    );
58
59
    const { dayDuration } = await this.queryBus.execute(
60
      new GetCooperativeQuery()
61
    );
62
    const times = [...ArrayUtils.range(30, dayDuration, 30)].reverse();
63
64
    return {
65
      startDate: dto.startDate,
66
      endDate: dto.endDate,
67
      types,
68
      tasks: tasksPagination.items,
69
      projects: projectsPagination.items,
70
      times
71
    };
72
  }
73
74
  @Post(':date')
75
  public async post(
76
    @Body() dto: AddEventDTO,
77
    @LoggedUser() user: User,
78
    @Res() res: Response
79
  ) {
80
    try {
81
      const {
82
        type,
83
        startDate,
84
        endDate,
85
        projectId,
86
        taskId,
87
        summary,
88
        time
89
      } = dto;
90
      await this.commandBus.execute(
91
        new AddEventCommand(
92
          type,
93
          user,
94
          time,
95
          new Date(startDate),
96
          new Date(endDate),
97
          projectId,
98
          taskId,
99
          summary
100
        )
101
      );
102
103
      res.redirect(303, makeMonthUrl(this.resolver, new Date(startDate)));
104
    } catch (e) {
105
      throw new BadRequestException(e.message);
106
    }
107
  }
108
}
109