api/src/Infrastructure/School/Action/CreateSchoolAction.ts   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 71
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 53
mnd 1
bc 1
fnc 1
dl 0
loc 71
rs 10
bpm 1
cpm 2
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A CreateSchoolAction.index 0 41 2
1
import {
2
  Controller,
3
  Inject,
4
  Post,
5
  Body,
6
  BadRequestException,
7
  UseGuards
8
} from '@nestjs/common';
9
import { AuthGuard } from '@nestjs/passport';
10
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
11
import { ICommandBus } from 'src/Application/ICommandBus';
12
import { CreateSchoolCommand } from 'src/Application/School/Command/CreateSchoolCommand';
13
import { UserRole } from 'src/Domain/User/User.entity';
14
import { Roles } from 'src/Infrastructure/User/Decorator/Roles';
15
import { RolesGuard } from 'src/Infrastructure/User/Security/RolesGuard';
16
import { SchoolDTO } from '../DTO/SchoolDTO';
17
18
@Controller('schools')
19
@ApiTags('School')
20
@ApiBearerAuth()
21
@UseGuards(AuthGuard('bearer'), RolesGuard)
22
export class CreateSchoolAction {
23
  constructor(
24
    @Inject('ICommandBus')
25
    private readonly commandBus: ICommandBus
26
  ) {}
27
28
  @Post()
29
  @Roles(UserRole.PHOTOGRAPHER)
30
  @ApiOperation({ summary: 'Create new school' })
31
  public async index(@Body() dto: SchoolDTO) {
32
    const {
33
      reference,
34
      name,
35
      address,
36
      zipCode,
37
      city,
38
      email,
39
      phoneNumber,
40
      numberOfClasses,
41
      numberOfStudents,
42
      observation,
43
      status,
44
      type
45
    } = dto;
46
47
    try {
48
      const id = await this.commandBus.execute(
49
        new CreateSchoolCommand(
50
          reference,
51
          name,
52
          address,
53
          zipCode,
54
          city,
55
          status,
56
          type,
57
          email,
58
          phoneNumber,
59
          numberOfStudents,
60
          numberOfClasses,
61
          observation
62
        )
63
      );
64
65
      return { id };
66
    } catch (e) {
67
      throw new BadRequestException(e.message);
68
    }
69
  }
70
}
71