Passed
Pull Request — master (#406)
by
unknown
02:02 queued 14s
created

server/src/Infrastructure/HumanResource/Leave/Controller/EditLeaveRequestController.ts   A

Complexity

Total Complexity 3
Complexity/F 1.5

Size

Lines of Code 83
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

2 Functions

Rating   Name   Duplication   Size   Complexity  
A EditLeaveRequestController.post 0 27 2
A EditLeaveRequestController.get 0 14 1
1
import {
2
  Body,
3
  Post,
4
  Controller,
5
  Inject,
6
  BadRequestException,
7
  UseGuards,
8
  Render,
9
  Get,
10
  Param,
11
  Res
12
} from '@nestjs/common';
13
import { ICommandBus } from 'src/Application/ICommandBus';
14
import { Response } from 'express';
15
import { LeaveRequestDTO } from '../DTO/LeaveRequestDTO';
16
import { LoggedUser } from '../../User/Decorator/LoggedUser';
17
import { IsAuthenticatedGuard } from '../../User/Security/IsAuthenticatedGuard';
18
import { WithName } from 'src/Infrastructure/Common/ExtendedRouting/WithName';
19
import { User } from 'src/Domain/HumanResource/User/User.entity';
20
import { Type } from 'src/Domain/HumanResource/Leave/LeaveRequest.entity';
21
import { UpdateLeaveRequestCommand } from 'src/Application/HumanResource/Leave/Command/UpdateLeaveRequestCommand';
22
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO';
23
import { GetLeaveRequestByIdQuery } from 'src/Application/HumanResource/Leave/Query/GetLeaveRequestByIdQuery';
24
import { IQueryBus } from 'src/Application/IQueryBus';
25
import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver';
26
27
@Controller('app/people/leave-requests/edit')
28
@UseGuards(IsAuthenticatedGuard)
29
export class EditLeaveRequestController {
30
  constructor(
31
    @Inject('ICommandBus')
32
    private readonly commandBus: ICommandBus,
33
    @Inject('IQueryBus')
34
    private readonly queryBus: IQueryBus,
35
    private readonly resolver: RouteNameResolver
36
  ) {}
37
38
  @Get(':id')
39
  @WithName('people_leave_requests_edit')
40
  @Render('pages/leave_requests/edit.njk')
41
  public async get(@Param() { id }: IdDTO) {
42
    const leaveRequest = await this.queryBus.execute(
43
      new GetLeaveRequestByIdQuery(id)
44
    );
45
46
    const types = Object.values(Type);
47
48
    return {
49
      leaveRequest,
50
      types
51
    };
52
  }
53
54
  @Post(':id')
55
  public async post(
56
    @Param() { id }: IdDTO,
57
    @Body() dto: LeaveRequestDTO,
58
    @LoggedUser() user: User,
59
    @Res() res: Response
60
  ) {
61
    const { type, startDate, startsAllDay, endDate, endsAllDay, comment } = dto;
62
63
    try {
64
      await this.commandBus.execute(
65
        new UpdateLeaveRequestCommand(
66
          id,
67
          type,
68
          startDate,
69
          startsAllDay,
70
          endDate,
71
          endsAllDay,
72
          user,
73
          comment
74
        )
75
      );
76
77
      res.redirect(303, this.resolver.resolve('people_leave_requests_list'));
78
    } catch (e) {
79
      throw new BadRequestException(e.message);
80
    }
81
  }
82
}
83