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 {ApiUseTags, ApiBearerAuth, ApiOperation} from '@nestjs/swagger'; |
11
|
|
|
import {ICommandBus} from 'src/Application/ICommandBus'; |
12
|
|
|
import {LoggedUser} from 'src/Infrastructure/User/Decorator/LoggedUser'; |
13
|
|
|
import {User, UserRole} from 'src/Domain/User/User.entity'; |
14
|
|
|
import {CreateQuoteCommand} from 'src/Application/Accounting/Command/Quote/CreateQuoteCommand'; |
15
|
|
|
import {QuoteDTO} from '../../DTO/QuoteDTO'; |
16
|
|
|
import {CreateQuoteItemsCommand} from 'src/Application/Accounting/Command/Quote/CreateQuoteItemsCommand'; |
17
|
|
|
import {Roles} from 'src/Infrastructure/User/Decorator/Roles'; |
18
|
|
|
import {RolesGuard} from 'src/Infrastructure/User/Security/RolesGuard'; |
19
|
|
|
|
20
|
|
|
@Controller('quotes') |
21
|
|
|
@ApiUseTags('Accounting') |
22
|
|
|
@ApiBearerAuth() |
23
|
|
|
@UseGuards(AuthGuard('bearer'), RolesGuard) |
24
|
|
|
export class CreateQuoteAction { |
25
|
|
|
constructor( |
26
|
|
|
@Inject('ICommandBus') |
27
|
|
|
private readonly commandBus: ICommandBus |
28
|
|
|
) {} |
29
|
|
|
|
30
|
|
|
@Post() |
31
|
|
|
@Roles(UserRole.COOPERATOR, UserRole.EMPLOYEE) |
32
|
|
|
@ApiOperation({title: 'Create new quote'}) |
33
|
|
|
public async index(@Body() dto: QuoteDTO, @LoggedUser() user: User) { |
34
|
|
|
try { |
35
|
|
|
const {projectId, customerId, status, items} = dto; |
36
|
|
|
const id = await this.commandBus.execute( |
37
|
|
|
new CreateQuoteCommand(user, status, customerId, projectId) |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
await this.commandBus.execute(new CreateQuoteItemsCommand(id, items)); |
41
|
|
|
|
42
|
|
|
return {id}; |
43
|
|
|
} catch (e) { |
44
|
|
|
throw new BadRequestException(e.message); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|