import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Patch, Post, Query, } from '@nestjs/common'; import { ApiOkResponse, ApiTags } from '@nestjs/swagger'; import { type PageDto } from '../../common/dto/page.dto'; import { RoleType } from '../../constants'; import { ApiPageOkResponse, Auth, UUIDParam, Whitelist, } from '../../decorators'; import { SectorNotFoundException } from '../../exceptions'; import { CreateSectorDto } from './dtos/create-sector.dto'; import { SectorPageOptionsDto } from './dtos/get-sector-page.dto'; import { SectorDto } from './dtos/sector.dto'; import { UpdateSectorDto } from './dtos/update-sector.dto'; import { SectorService } from './sector.service'; @Controller('sector') @ApiTags('sector') export class SectorController { constructor(private sectorService: SectorService) {} @Get() @Whitelist() @Auth([RoleType.ADMIN, RoleType.USER]) @HttpCode(HttpStatus.OK) @ApiPageOkResponse({ type: SectorDto, description: 'Get sector list', }) async getSector( @Query() query: SectorPageOptionsDto, ): Promise> { return this.sectorService.findMany(query); } @Post() @Whitelist() @Auth([RoleType.ADMIN]) @HttpCode(HttpStatus.OK) @ApiOkResponse() async createSector(@Body() body: CreateSectorDto): Promise { const sectorEntity = await this.sectorService.createSector(body); return sectorEntity.toDto(); } @Patch(':id') @Whitelist() @Auth([RoleType.ADMIN]) @HttpCode(HttpStatus.OK) @ApiOkResponse() async updateSector( @UUIDParam('id') id: Uuid, @Body() body: UpdateSectorDto, ): Promise { const toPatchEntity = await this.sectorService.findOne({ id, }); if (!toPatchEntity) { throw new SectorNotFoundException(); } const sectorEntity = await this.sectorService.updateSector( toPatchEntity, body, ); return sectorEntity.toDto(); } @Delete(':id') @Whitelist() @Auth([RoleType.ADMIN]) @HttpCode(HttpStatus.OK) @ApiOkResponse() async delete(@UUIDParam('id') id: Uuid) { const toDeleteEntity = await this.sectorService.findOne({ id, }); if (!toDeleteEntity) { throw new SectorNotFoundException(); } await this.sectorService.delete(toDeleteEntity); return { status: 'success' }; } }