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 {ApiUseTags, ApiOperation, ApiBearerAuth} from '@nestjs/swagger'; |
11
|
|
|
import {ICommandBus} from 'src/Application/ICommandBus'; |
12
|
|
|
import {CreateUserCommand} from 'src/Application/HumanResource/User/Command/CreateUserCommand'; |
13
|
|
|
import {UserView} from 'src/Application/HumanResource/User/View/UserView'; |
14
|
|
|
import {UserDTO} from '../DTO/UserDTO'; |
15
|
|
|
import {IQueryBus} from 'src/Application/IQueryBus'; |
16
|
|
|
import {GetUserByIdQuery} from 'src/Application/HumanResource/User/Query/GetUserByIdQuery'; |
17
|
|
|
import {Roles} from '../Decorator/Roles'; |
18
|
|
|
import {RolesGuard} from '../Security/RolesGuard'; |
19
|
|
|
import {UserRole} from 'src/Domain/HumanResource/User/User.entity'; |
20
|
|
|
|
21
|
|
|
@Controller('users') |
22
|
|
|
@ApiUseTags('User') |
23
|
|
|
@ApiBearerAuth() |
24
|
|
|
@UseGuards(AuthGuard('bearer'), RolesGuard) |
25
|
|
|
export class CreateUserAction { |
26
|
|
|
constructor( |
27
|
|
|
@Inject('ICommandBus') |
28
|
|
|
private readonly commandBus: ICommandBus, |
29
|
|
|
@Inject('IQueryBus') |
30
|
|
|
private readonly queryBus: IQueryBus |
31
|
|
|
) {} |
32
|
|
|
|
33
|
|
|
@Post() |
34
|
|
|
@Roles(UserRole.COOPERATOR) |
35
|
|
|
@ApiOperation({title: 'Create new user account'}) |
36
|
|
|
public async index(@Body() userDto: UserDTO): Promise<UserView> { |
37
|
|
|
try { |
38
|
|
|
const {firstName, lastName, email, password, entryDate, role} = userDto; |
39
|
|
|
const id = await this.commandBus.execute( |
40
|
|
|
new CreateUserCommand( |
41
|
|
|
firstName, |
42
|
|
|
lastName, |
43
|
|
|
email, |
44
|
|
|
password, |
45
|
|
|
role, |
46
|
|
|
entryDate ? new Date(entryDate) : null |
47
|
|
|
) |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
return await this.queryBus.execute(new GetUserByIdQuery(id)); |
51
|
|
|
} catch (e) { |
52
|
|
|
throw new BadRequestException(e.message); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|