Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 53 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { |
||
2 | Body, |
||
3 | Post, |
||
4 | Controller, |
||
5 | Inject, |
||
6 | BadRequestException, |
||
7 | UseGuards |
||
8 | } from '@nestjs/common'; |
||
9 | import {AuthGuard} from '@nestjs/passport'; |
||
10 | import {ApiTags, ApiBearerAuth, ApiOperation} from '@nestjs/swagger'; |
||
11 | import {ICommandBus} from 'src/Application/ICommandBus'; |
||
12 | import {LoggedUser} from 'src/Infrastructure/HumanResource/User/Decorator/LoggedUser'; |
||
13 | import {User, UserRole} from 'src/Domain/HumanResource/User/User.entity'; |
||
14 | import {AddEventCommand} from 'src/Application/FairCalendar/Command/AddEventCommand'; |
||
15 | import {EventDTO} from '../DTO/EventDTO'; |
||
16 | import {RolesGuard} from 'src/Infrastructure/HumanResource/User/Security/RolesGuard'; |
||
17 | import {Roles} from 'src/Infrastructure/HumanResource/User/Decorator/Roles'; |
||
18 | |||
19 | @Controller('events') |
||
20 | @ApiTags('Event') |
||
21 | @ApiBearerAuth() |
||
22 | @UseGuards(AuthGuard('bearer'), RolesGuard) |
||
23 | export class AddEventsAction { |
||
24 | constructor( |
||
25 | @Inject('ICommandBus') |
||
26 | private readonly commandBus: ICommandBus |
||
27 | ) {} |
||
28 | |||
29 | @Post() |
||
30 | @Roles(UserRole.COOPERATOR, UserRole.EMPLOYEE) |
||
31 | @ApiOperation({summary: 'Add new event(s)'}) |
||
32 | public async index(@Body() dto: EventDTO, @LoggedUser() user: User) { |
||
33 | try { |
||
34 | const {type, startDate, projectId, taskId, summary, time} = dto; |
||
35 | const id = await this.commandBus.execute( |
||
36 | new AddEventCommand( |
||
37 | type, |
||
38 | user, |
||
39 | Number(time), |
||
40 | new Date(startDate), |
||
41 | projectId, |
||
42 | taskId, |
||
43 | summary |
||
44 | ) |
||
45 | ); |
||
46 | |||
47 | return {id}; |
||
48 | } catch (e) { |
||
49 | throw new BadRequestException(e.message); |
||
50 | } |
||
51 | } |
||
52 | } |
||
53 |