This repository was archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromotions.controller.ts
More file actions
116 lines (104 loc) · 4.83 KB
/
Copy pathpromotions.controller.ts
File metadata and controls
116 lines (104 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { Controller, Delete, Get, Param, Post, Req, UploadedFile, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiOkResponse, ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger';
import { GuardPermissions } from '@modules/auth/decorators/permissions.decorator';
import { PermissionGuard } from '@modules/auth/guards/permission.guard';
import { ApiNotOkResponses } from '@modules/base/decorators/api-not-ok.decorator';
import { OutputMessageDTO } from '@modules/base/dto/output.dto';
import { i18nBadRequestException } from '@modules/base/http-errors';
import { ApiDownloadFile } from '@modules/files/decorators/download.decorator';
import { ApiUploadFile } from '@modules/files/decorators/upload.decorator';
import { OutputFileDTO } from '@modules/files/dto/output.dto';
import { FilesService } from '@modules/files/files.service';
import { Request } from '@modules/users/entities/user.entity';
import { InputPromotionNumberParamDTO } from './dto/input.dto';
import { OutputPromotionDTO } from './dto/output.dto';
import { PromotionsService } from './promotions.service';
import { OutputBaseUserDTO } from '../users/dto/output.dto';
@ApiTags('Promotions')
@Controller('promotions')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
export class PromotionsController {
constructor(private readonly promotionsService: PromotionsService, private readonly filesService: FilesService) {}
@Get()
@UseGuards(PermissionGuard)
@GuardPermissions('CAN_READ_PROMOTION')
@ApiOperation({ summary: 'Get all existing promotions' })
@ApiOkResponse({ type: [OutputPromotionDTO] })
async getAll(): Promise<OutputPromotionDTO[]> {
return this.promotionsService.findAll();
}
@Get('latest')
@UseGuards(PermissionGuard)
@GuardPermissions('CAN_READ_PROMOTION')
@ApiOperation({ summary: 'Get the latest promotion that has been created' })
@ApiOkResponse({ type: OutputPromotionDTO })
async getLatest(): Promise<OutputPromotionDTO> {
return this.promotionsService.findLatest();
}
@Get('current')
@UseGuards(PermissionGuard)
@GuardPermissions('CAN_READ_PROMOTION')
@ApiOperation({ summary: 'Get promotions currently active' })
@ApiOkResponse({ type: [OutputPromotionDTO] })
async getCurrent(): Promise<OutputPromotionDTO[]> {
return this.promotionsService.findCurrent();
}
@Post(':number/logo')
@UseGuards(PermissionGuard)
@GuardPermissions('CAN_EDIT_PROMOTION')
@ApiOperation({ summary: 'Update the promotion logo' })
@ApiUploadFile()
@ApiParam({ name: 'number', description: 'The promotion number (eg: 21)' })
@ApiOkResponse({ type: OutputFileDTO })
@ApiNotOkResponses({ 400: 'Invalid file', 404: 'Promotion not found' })
async editLogo(
@UploadedFile() file: Express.Multer.File,
@Param() params: InputPromotionNumberParamDTO,
): Promise<OutputFileDTO> {
if (!file) throw new i18nBadRequestException('validations.file.invalid.not_provided');
return this.promotionsService.updateLogo(params.number, file);
}
@Get(':number/logo')
@UseGuards(PermissionGuard)
@GuardPermissions('CAN_READ_PROMOTION')
@ApiOperation({ summary: 'Get the promotion logo' })
@ApiParam({ name: 'number', description: 'The promotion number (eg: 21)' })
@ApiDownloadFile('The promotion logo')
@ApiNotOkResponses({ 404: 'Promotion not found or promotion has no logo' })
async getLogo(@Req() req: Request, @Param() params: InputPromotionNumberParamDTO) {
const logo = await this.promotionsService.getLogo(params.number);
return this.filesService.getAsStreamable(logo, req.user.id);
}
@Delete(':number/logo')
@UseGuards(PermissionGuard)
@GuardPermissions('CAN_EDIT_PROMOTION')
@ApiParam({ name: 'number', description: 'The promotion number (eg: 21)' })
@ApiOperation({ summary: 'Delete the promotion logo' })
@ApiOkResponse({ type: OutputMessageDTO })
@ApiNotOkResponses({ 404: 'Promotion not found or promotion has no logo' })
async deleteLogo(@Param() params: InputPromotionNumberParamDTO) {
return this.promotionsService.deleteLogo(params.number);
}
@Get(':number')
@UseGuards(PermissionGuard)
@GuardPermissions('CAN_READ_PROMOTION')
@ApiOperation({ summary: 'Get the specified promotion' })
@ApiParam({ name: 'number', description: 'The promotion number (eg: 21)' })
@ApiOkResponse({ type: OutputPromotionDTO })
@ApiNotOkResponses({ 404: 'Promotion not found' })
async get(@Param() params: InputPromotionNumberParamDTO) {
return this.promotionsService.findOne(params.number);
}
@Get(':number/users')
@UseGuards(PermissionGuard)
@GuardPermissions('CAN_READ_PROMOTION')
@ApiParam({ name: 'number', description: 'The promotion number (eg: 21)' })
@ApiOperation({ summary: 'Get users of the specified promotions' })
@ApiOkResponse({ type: [OutputBaseUserDTO] })
@ApiNotOkResponses({ 404: 'Promotion not found' })
async getUsers(@Param() params: InputPromotionNumberParamDTO) {
return this.promotionsService.getUsers(params.number);
}
}