src/Infrastructure/HumanResource/Leave/Controller/EditLeaveRequestController.ts   A
last analyzed

Complexity

Total Complexity 3
Complexity/F 1.5

Size

Lines of Code 92
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

2 Functions

Rating   Name   Duplication   Size   Complexity  
A EditLeaveRequestController.get 0 17 1
A EditLeaveRequestController.post 0 27 2
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 {
21
  getSelectableLeaveRequestTypes,
22
  Status,
23
  Type
24
} from 'src/Domain/HumanResource/Leave/LeaveRequest.entity';
25
import { UpdateLeaveRequestCommand } from 'src/Application/HumanResource/Leave/Command/UpdateLeaveRequestCommand';
26
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO';
27
import { GetLeaveRequestByIdQuery } from 'src/Application/HumanResource/Leave/Query/GetLeaveRequestByIdQuery';
28
import { IQueryBus } from 'src/Application/IQueryBus';
29
import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver';
30
import { DoesLeaveRequestBelongToUser } from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestBelongToUser';
31
32
@Controller('app/people/leave-requests/edit')
33
@UseGuards(IsAuthenticatedGuard)
34
export class EditLeaveRequestController {
35
  constructor(
36
    @Inject('ICommandBus')
37
    private readonly commandBus: ICommandBus,
38
    @Inject('IQueryBus')
39
    private readonly queryBus: IQueryBus,
40
    private readonly resolver: RouteNameResolver,
41
    private readonly doesLeaveRequestBelongToUser: DoesLeaveRequestBelongToUser
42
  ) {}
43
44
  @Get(':id')
45
  @WithName('people_leave_requests_edit')
46
  @Render('pages/leave_requests/edit.njk')
47
  public async get(@Param() { id }: IdDTO, @LoggedUser() user: User) {
48
    const leaveRequest = await this.queryBus.execute(
49
      new GetLeaveRequestByIdQuery(id, user)
50
    );
51
52
    const types = getSelectableLeaveRequestTypes();
53
54
    return {
55
      leaveRequest,
56
      types,
57
      canDelete:
58
        leaveRequest.status === Status.PENDING &&
59
        this.doesLeaveRequestBelongToUser.isSatisfiedBy(leaveRequest, user)
60
    };
61
  }
62
63
  @Post(':id')
64
  public async post(
65
    @Param() { id }: IdDTO,
66
    @Body() dto: LeaveRequestDTO,
67
    @LoggedUser() user: User,
68
    @Res() res: Response
69
  ) {
70
    const { type, startDate, startsAllDay, endDate, endsAllDay, comment } = dto;
71
72
    try {
73
      await this.commandBus.execute(
74
        new UpdateLeaveRequestCommand(
75
          id,
76
          type,
77
          startDate,
78
          startsAllDay,
79
          endDate,
80
          endsAllDay,
81
          user,
82
          comment
83
        )
84
      );
85
86
      res.redirect(303, this.resolver.resolve('people_leave_requests_list'));
87
    } catch (e) {
88
      throw new BadRequestException(e.message);
89
    }
90
  }
91
}
92