Passed
Push — master ( 047d2d...6c6d4f )
by
unknown
20:36 queued 17:22
created

AddProjectController.post   A

Complexity

Conditions 2

Size

Total Lines 13
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 13
rs 9.8
c 0
b 0
f 0
cc 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(null)
42
    );
43
44
    return {
45
      active: true,
46
      customers: customers.items
47
    };
48
  }
49
50
  @Post()
51
  public async post(@Body() projectDto: ProjectDTO, @Res() res: Response) {
52
    const { name, customerId, active } = projectDto;
53
54
    try {
55
      await this.commandBus.execute(
56
        new CreateProjectCommand(name, InvoiceUnits.DAY, active, customerId)
57
      );
58
59
      res.redirect(303, this.resolver.resolve('crm_projects_list'));
60
    } catch (e) {
61
      throw new BadRequestException(e.message);
62
    }
63
  }
64
}
65