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

server/src/Infrastructure/Project/Controller/AddProjectController.ts   A

Complexity

Total Complexity 3
Complexity/F 1.5

Size

Lines of Code 64
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 64
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 AddProjectController.get 0 11 1
A AddProjectController.poqr 0 13 2
1
import {
2
  BadRequestException,
3
  Body,
4
  Controller,
5
  Get,
6
  Inject,
7
  Post,
8
  Render,
9
  Res,
10
  UseGuards
11
} from '@nestjs/common';
12
import { Response } from 'express';
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 { ProjectDTO } from '../DTO/ProjectDTO';
17
import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver';
18
import { CreateProjectCommand } from 'src/Application/Project/Command/CreateProjectCommand';
19
import { InvoiceUnits } from 'src/Domain/Project/Project.entity';
20
import { IQueryBus } from 'src/Application/IQueryBus';
21
import { GetCustomersQuery } from 'src/Application/Customer/Query/GetCustomersQuery';
22
import { Pagination } from 'src/Application/Common/Pagination';
23
import { CustomerView } from 'src/Application/Customer/View/CustomerView';
24
25
@Controller('app/projects/add')
26
@UseGuards(IsAuthenticatedGuard)
27
export class AddProjectController {
28
  constructor(
29
    @Inject('ICommandBus')
30
    private readonly commandBus: ICommandBus,
31
    @Inject('IQueryBus')
32
    private readonly queryBus: IQueryBus,
33
    private readonly resolver: RouteNameResolver
34
  ) {}
35
36
  @Get()
37
  @WithName('crm_projects_add')
38
  @Render('pages/projects/add.njk')
39
  public async get() {
40
    const customers: Pagination<CustomerView> = await this.queryBus.execute(
41
      new GetCustomersQuery(1)
42
    );
43
44
    return {
45
      customers: customers.items
46
    };
47
  }
48
49
  @Post()
50
  public async poqr(@Body() projectDto: ProjectDTO, @Res() res: Response) {
51
    const { name, customerId } = projectDto;
52
53
    try {
54
      await this.commandBus.execute(
55
        new CreateProjectCommand(name, InvoiceUnits.DAY, customerId)
56
      );
57
58
      res.redirect(303, this.resolver.resolve('crm_projects_list'));
59
    } catch (e) {
60
      throw new BadRequestException(e.message);
61
    }
62
  }
63
}
64