src/Infrastructure/Customer/Controller/EditCustomerController.ts   A
last analyzed

Complexity

Total Complexity 3
Complexity/F 1.5

Size

Lines of Code 65
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

2 Functions

Rating   Name   Duplication   Size   Complexity  
A EditCustomerController.get 0 11 1
A EditCustomerController.post 0 15 2
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 { ICommandBus } from 'src/Application/ICommandBus';
14
import { IsAuthenticatedGuard } from 'src/Infrastructure/HumanResource/User/Security/IsAuthenticatedGuard';
15
import { WithName } from 'src/Infrastructure/Common/ExtendedRouting/WithName';
16
import { IQueryBus } from 'src/Application/IQueryBus';
17
import { Response } from 'express';
18
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO';
19
import { GetCustomerByIdQuery } from 'src/Application/Customer/Query/GetCustomerByIdQuery';
20
import { CustomerDTO } from 'src/Infrastructure/Customer/DTO/CustomerDTO';
21
import { UpdateCustomerCommand } from 'src/Application/Customer/Command/UpdateCustomerCommand';
22
import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver';
23
24
@Controller('app/customers/edit')
25
@UseGuards(IsAuthenticatedGuard)
26
export class EditCustomerController {
27
  constructor(
28
    @Inject('ICommandBus')
29
    private readonly commandBus: ICommandBus,
30
    @Inject('IQueryBus')
31
    private readonly queryBus: IQueryBus,
32
    private readonly resolver: RouteNameResolver
33
  ) {}
34
35
  @Get(':id')
36
  @WithName('crm_customers_edit')
37
  @Render('pages/customers/edit.njk')
38
  public async get(@Param() idDto: IdDTO) {
39
    const customer = await this.queryBus.execute(
40
      new GetCustomerByIdQuery(idDto.id)
41
    );
42
43
    return {
44
      customer
45
    };
46
  }
47
48
  @Post(':id')
49
  public async post(
50
    @Param() idDto: IdDTO,
51
    @Body() dto: CustomerDTO,
52
    @Res() res: Response
53
  ) {
54
    const { name } = dto;
55
56
    try {
57
      await this.commandBus.execute(new UpdateCustomerCommand(idDto.id, name));
58
59
      res.redirect(303, this.resolver.resolve('crm_customers_list'));
60
    } catch (e) {
61
      throw new BadRequestException(e.message);
62
    }
63
  }
64
}
65