Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 50 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { |
||
2 | Controller, |
||
3 | Inject, |
||
4 | Put, |
||
5 | Body, |
||
6 | BadRequestException, |
||
7 | UseGuards |
||
8 | } from '@nestjs/common'; |
||
9 | import {AuthGuard} from '@nestjs/passport'; |
||
10 | import {ApiUseTags, ApiOperation, ApiBearerAuth} from '@nestjs/swagger'; |
||
11 | import {ICommandBus} from 'src/Application/ICommandBus'; |
||
12 | import {UserView} from 'src/Application/HumanResource/User/View/UserView'; |
||
13 | import {UpdateProfileCommand} from 'src/Application/HumanResource/User/Command/UpdateProfileCommand'; |
||
14 | import {LoggedUser} from '../Decorator/LoggedUser'; |
||
15 | import {User} from 'src/Domain/HumanResource/User/User.entity'; |
||
16 | import {IQueryBus} from 'src/Application/IQueryBus'; |
||
17 | import {GetUserByIdQuery} from 'src/Application/HumanResource/User/Query/GetUserByIdQuery'; |
||
18 | import {ProfileDTO} from '../DTO/ProfileDTO'; |
||
19 | |||
20 | @Controller('users') |
||
21 | @ApiUseTags('User') |
||
22 | @ApiBearerAuth() |
||
23 | @UseGuards(AuthGuard('bearer')) |
||
24 | export class UpdateMeAction { |
||
25 | constructor( |
||
26 | @Inject('ICommandBus') |
||
27 | private readonly commandBus: ICommandBus, |
||
28 | @Inject('IQueryBus') |
||
29 | private readonly queryBus: IQueryBus |
||
30 | ) {} |
||
31 | |||
32 | @Put('me') |
||
33 | @ApiOperation({title: 'Update current user'}) |
||
34 | public async index( |
||
35 | @Body() dto: ProfileDTO, |
||
36 | @LoggedUser() user: User |
||
37 | ): Promise<UserView> { |
||
38 | try { |
||
39 | const {firstName, lastName, email, password} = dto; |
||
40 | await this.commandBus.execute( |
||
41 | new UpdateProfileCommand(user, firstName, lastName, email, password) |
||
42 | ); |
||
43 | |||
44 | return await this.queryBus.execute(new GetUserByIdQuery(user.getId())); |
||
45 | } catch (e) { |
||
46 | throw new BadRequestException(e.message); |
||
47 | } |
||
48 | } |
||
49 | } |
||
50 |