Passed
Pull Request — master (#256)
by Mathieu
02:22
created

AddUserSavingsRecordAction.index   A

Complexity

Conditions 2

Size

Total Lines 15
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 15
rs 9.7
c 0
b 0
f 0
cc 2
1
import {
2
  Body,
3
  Post,
4
  Controller,
5
  Inject,
6
  BadRequestException,
7
  UseGuards
8
} from '@nestjs/common';
9
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
10
import { AuthGuard } from '@nestjs/passport';
11
import { ICommandBus } from 'src/Application/ICommandBus';
12
import { UserRole } from 'src/Domain/HumanResource/User/User.entity';
13
import { RolesGuard } from 'src/Infrastructure/HumanResource/User/Security/RolesGuard';
14
import { Roles } from 'src/Infrastructure/HumanResource/User/Decorator/Roles';
15
import { AddUserSavingsRecordCommand } from 'src/Application/HumanResource/Savings/Command/AddUserSavingsRecordCommand';
16
import { UserSavingsRecordDTO } from '../DTO/UserSavingsRecordDTO';
17
18
@Controller('savings-record')
19
@ApiTags('Human Resource')
20
@ApiBearerAuth()
21
@UseGuards(AuthGuard('bearer'), RolesGuard)
22
export class AddUserSavingsRecordAction {
23
  constructor(
24
    @Inject('ICommandBus')
25
    private readonly commandBus: ICommandBus
26
  ) {}
27
28
  @Post()
29
  @Roles(UserRole.COOPERATOR, UserRole.EMPLOYEE)
30
  @ApiOperation({ summary: 'Add new user savings record' })
31
  public async index(
32
    @Body() { type, userId, amount }: UserSavingsRecordDTO,
33
  ) {
34
    try {
35
      const id = await this.commandBus.execute(
36
        new AddUserSavingsRecordCommand(type, amount, userId)
37
      );
38
39
      return { id };
40
    } catch (e) {
41
      throw new BadRequestException(e.message);
42
    }
43
  }
44
}
45