From 19825540ddfc521ae835462f8723875008104d17 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Mon, 9 Dec 2024 16:22:54 +0400 Subject: [PATCH] updated endpoint and validation to manage project entity in communty CRUD --- src/community/community.module.ts | 2 + .../controllers/community.controller.ts | 20 ++++---- src/community/dtos/get.community.dto.ts | 3 +- src/community/dtos/index.ts | 2 + src/community/dtos/project.param.dto.ts | 11 +++++ src/community/services/community.service.ts | 47 +++++++++++++++++-- 6 files changed, 69 insertions(+), 16 deletions(-) create mode 100644 src/community/dtos/project.param.dto.ts diff --git a/src/community/community.module.ts b/src/community/community.module.ts index 106d9d1..a87ef85 100644 --- a/src/community/community.module.ts +++ b/src/community/community.module.ts @@ -9,6 +9,7 @@ import { UserRepositoryModule } from '@app/common/modules/user/user.repository.m import { SpacePermissionService } from '@app/common/helper/services'; import { CommunityRepository } from '@app/common/modules/community/repositories'; import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service'; +import { ProjectRepository } from '@app/common/modules/project/repositiories'; @Module({ imports: [ConfigModule, SpaceRepositoryModule, UserRepositoryModule], @@ -20,6 +21,7 @@ import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service TuyaService, CommunityRepository, SpacePermissionService, + ProjectRepository, ], exports: [CommunityService, SpacePermissionService], }) diff --git a/src/community/controllers/community.controller.ts b/src/community/controllers/community.controller.ts index 86bad74..5548e84 100644 --- a/src/community/controllers/community.controller.ts +++ b/src/community/controllers/community.controller.ts @@ -19,6 +19,7 @@ import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard'; import { ControllerRoute } from '@app/common/constants/controller-route'; import { BaseResponseDto } from '@app/common/dto/base.response.dto'; import { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.dto'; +import { ProjectParam } from '../dtos'; @ApiTags('Community Module') @Controller({ @@ -36,9 +37,10 @@ export class CommunityController { description: ControllerRoute.COMMUNITY.ACTIONS.CREATE_COMMUNITY_DESCRIPTION, }) async createCommunity( + @Param() param: ProjectParam, @Body() addCommunityDto: AddCommunityDto, ): Promise { - return await this.communityService.createCommunity(addCommunityDto); + return await this.communityService.createCommunity(param, addCommunityDto); } @ApiBearerAuth() @@ -52,7 +54,7 @@ export class CommunityController { async getCommunityByUuid( @Param() params: GetCommunityParams, ): Promise { - return await this.communityService.getCommunityById(params.communityUuid); + return await this.communityService.getCommunityById(params); } @ApiBearerAuth() @@ -63,9 +65,10 @@ export class CommunityController { }) @Get() async getCommunities( + @Param() param: ProjectParam, @Query() query: PaginationRequestGetListDto, ): Promise { - return this.communityService.getCommunities(query); + return this.communityService.getCommunities(param, query); } @ApiBearerAuth() @@ -76,13 +79,10 @@ export class CommunityController { }) @Put('/:communityUuid') async updateCommunity( - @Param() param: GetCommunityParams, + @Param() params: GetCommunityParams, @Body() updateCommunityDto: UpdateCommunityNameDto, ) { - return this.communityService.updateCommunity( - param.communityUuid, - updateCommunityDto, - ); + return this.communityService.updateCommunity(params, updateCommunityDto); } @ApiBearerAuth() @@ -93,8 +93,8 @@ export class CommunityController { description: ControllerRoute.COMMUNITY.ACTIONS.DELETE_COMMUNITY_DESCRIPTION, }) async deleteCommunity( - @Param() param: GetCommunityParams, + @Param() params: GetCommunityParams, ): Promise { - return this.communityService.deleteCommunity(param.communityUuid); + return this.communityService.deleteCommunity(params); } } diff --git a/src/community/dtos/get.community.dto.ts b/src/community/dtos/get.community.dto.ts index fe2cf46..d6da53a 100644 --- a/src/community/dtos/get.community.dto.ts +++ b/src/community/dtos/get.community.dto.ts @@ -10,6 +10,7 @@ import { IsUUID, Min, } from 'class-validator'; +import { ProjectParam } from './project.param.dto'; export class GetCommunityDto { @ApiProperty({ @@ -21,7 +22,7 @@ export class GetCommunityDto { public communityUuid: string; } -export class GetCommunityParams { +export class GetCommunityParams extends ProjectParam { @ApiProperty({ description: 'Community id of the specific community', required: true, diff --git a/src/community/dtos/index.ts b/src/community/dtos/index.ts index 7119b23..34d8bbb 100644 --- a/src/community/dtos/index.ts +++ b/src/community/dtos/index.ts @@ -1 +1,3 @@ export * from './add.community.dto'; +export * from './project.param.dto'; +export * from './get.community.dto'; diff --git a/src/community/dtos/project.param.dto.ts b/src/community/dtos/project.param.dto.ts new file mode 100644 index 0000000..8bb2929 --- /dev/null +++ b/src/community/dtos/project.param.dto.ts @@ -0,0 +1,11 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsUUID } from 'class-validator'; + +export class ProjectParam { + @ApiProperty({ + description: 'UUID of the project this community belongs to', + example: 'd290f1ee-6c54-4b01-90e6-d701748f0851', + }) + @IsUUID() + projectUuid: string; +} diff --git a/src/community/services/community.service.ts b/src/community/services/community.service.ts index e833416..c21d20a 100644 --- a/src/community/services/community.service.ts +++ b/src/community/services/community.service.ts @@ -1,5 +1,5 @@ import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; -import { AddCommunityDto } from '../dtos'; +import { AddCommunityDto, GetCommunityParams, ProjectParam } from '../dtos'; import { UpdateCommunityNameDto } from '../dtos/update.community.dto'; import { BaseResponseDto } from '@app/common/dto/base.response.dto'; import { @@ -11,17 +11,24 @@ import { CommunityRepository } from '@app/common/modules/community/repositories' import { CommunityDto } from '@app/common/modules/community/dtos'; import { SuccessResponseDto } from '@app/common/dto/success.response.dto'; import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service'; +import { ProjectRepository } from '@app/common/modules/project/repositiories'; @Injectable() export class CommunityService { constructor( private readonly communityRepository: CommunityRepository, + private readonly projectRepository: ProjectRepository, private readonly tuyaService: TuyaService, ) {} - async createCommunity(dto: AddCommunityDto): Promise { + async createCommunity( + param: ProjectParam, + dto: AddCommunityDto, + ): Promise { const { name, description } = dto; + const project = await this.validateProject(param.projectUuid); + const existingCommunity = await this.communityRepository.findOneBy({ name, }); @@ -36,6 +43,7 @@ export class CommunityService { const community = this.communityRepository.create({ name: name, description: description, + project: project, }); // Save the community to the database @@ -54,7 +62,11 @@ export class CommunityService { } } - async getCommunityById(communityUuid: string): Promise { + async getCommunityById(params: GetCommunityParams): Promise { + const { communityUuid, projectUuid } = params; + + await this.validateProject(projectUuid); + const community = await this.communityRepository.findOneBy({ uuid: communityUuid, }); @@ -75,9 +87,13 @@ export class CommunityService { } async getCommunities( + param: ProjectParam, pageable: Partial, ): Promise { + await this.validateProject(param.projectUuid); + pageable.modelName = 'community'; + pageable.where = { project: { uuid: param.projectUuid } }; const customModel = TypeORMCustomModel(this.communityRepository); @@ -91,9 +107,13 @@ export class CommunityService { } async updateCommunity( - communityUuid: string, + params: GetCommunityParams, updateCommunityDto: UpdateCommunityNameDto, ): Promise { + const { communityUuid, projectUuid } = params; + + await this.validateProject(projectUuid); + const community = await this.communityRepository.findOne({ where: { uuid: communityUuid }, }); @@ -128,7 +148,11 @@ export class CommunityService { } } - async deleteCommunity(communityUuid: string): Promise { + async deleteCommunity(params: GetCommunityParams): Promise { + const { communityUuid, projectUuid } = params; + + await this.validateProject(projectUuid); + const community = await this.communityRepository.findOne({ where: { uuid: communityUuid }, }); @@ -169,4 +193,17 @@ export class CommunityService { ); } } + + private async validateProject(uuid: string) { + const project = await this.projectRepository.findOne({ + where: { uuid }, + }); + if (!project) { + throw new HttpException( + `A project with the uuid '${uuid}' doesn't exists.`, + HttpStatus.BAD_REQUEST, + ); + } + return project; + } }