updated endpoint and validation to manage project entity in communty CRUD

This commit is contained in:
hannathkadher
2024-12-09 16:22:54 +04:00
parent fc28a91b82
commit 19825540dd
6 changed files with 69 additions and 16 deletions

View File

@ -9,6 +9,7 @@ import { UserRepositoryModule } from '@app/common/modules/user/user.repository.m
import { SpacePermissionService } from '@app/common/helper/services'; import { SpacePermissionService } from '@app/common/helper/services';
import { CommunityRepository } from '@app/common/modules/community/repositories'; import { CommunityRepository } from '@app/common/modules/community/repositories';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service'; import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
@Module({ @Module({
imports: [ConfigModule, SpaceRepositoryModule, UserRepositoryModule], imports: [ConfigModule, SpaceRepositoryModule, UserRepositoryModule],
@ -20,6 +21,7 @@ import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service
TuyaService, TuyaService,
CommunityRepository, CommunityRepository,
SpacePermissionService, SpacePermissionService,
ProjectRepository,
], ],
exports: [CommunityService, SpacePermissionService], exports: [CommunityService, SpacePermissionService],
}) })

View File

@ -19,6 +19,7 @@ import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
import { ControllerRoute } from '@app/common/constants/controller-route'; import { ControllerRoute } from '@app/common/constants/controller-route';
import { BaseResponseDto } from '@app/common/dto/base.response.dto'; import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.dto'; import { PaginationRequestGetListDto } from '@app/common/dto/pagination.request.dto';
import { ProjectParam } from '../dtos';
@ApiTags('Community Module') @ApiTags('Community Module')
@Controller({ @Controller({
@ -36,9 +37,10 @@ export class CommunityController {
description: ControllerRoute.COMMUNITY.ACTIONS.CREATE_COMMUNITY_DESCRIPTION, description: ControllerRoute.COMMUNITY.ACTIONS.CREATE_COMMUNITY_DESCRIPTION,
}) })
async createCommunity( async createCommunity(
@Param() param: ProjectParam,
@Body() addCommunityDto: AddCommunityDto, @Body() addCommunityDto: AddCommunityDto,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
return await this.communityService.createCommunity(addCommunityDto); return await this.communityService.createCommunity(param, addCommunityDto);
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -52,7 +54,7 @@ export class CommunityController {
async getCommunityByUuid( async getCommunityByUuid(
@Param() params: GetCommunityParams, @Param() params: GetCommunityParams,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
return await this.communityService.getCommunityById(params.communityUuid); return await this.communityService.getCommunityById(params);
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -63,9 +65,10 @@ export class CommunityController {
}) })
@Get() @Get()
async getCommunities( async getCommunities(
@Param() param: ProjectParam,
@Query() query: PaginationRequestGetListDto, @Query() query: PaginationRequestGetListDto,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
return this.communityService.getCommunities(query); return this.communityService.getCommunities(param, query);
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -76,13 +79,10 @@ export class CommunityController {
}) })
@Put('/:communityUuid') @Put('/:communityUuid')
async updateCommunity( async updateCommunity(
@Param() param: GetCommunityParams, @Param() params: GetCommunityParams,
@Body() updateCommunityDto: UpdateCommunityNameDto, @Body() updateCommunityDto: UpdateCommunityNameDto,
) { ) {
return this.communityService.updateCommunity( return this.communityService.updateCommunity(params, updateCommunityDto);
param.communityUuid,
updateCommunityDto,
);
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -93,8 +93,8 @@ export class CommunityController {
description: ControllerRoute.COMMUNITY.ACTIONS.DELETE_COMMUNITY_DESCRIPTION, description: ControllerRoute.COMMUNITY.ACTIONS.DELETE_COMMUNITY_DESCRIPTION,
}) })
async deleteCommunity( async deleteCommunity(
@Param() param: GetCommunityParams, @Param() params: GetCommunityParams,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
return this.communityService.deleteCommunity(param.communityUuid); return this.communityService.deleteCommunity(params);
} }
} }

View File

@ -10,6 +10,7 @@ import {
IsUUID, IsUUID,
Min, Min,
} from 'class-validator'; } from 'class-validator';
import { ProjectParam } from './project.param.dto';
export class GetCommunityDto { export class GetCommunityDto {
@ApiProperty({ @ApiProperty({
@ -21,7 +22,7 @@ export class GetCommunityDto {
public communityUuid: string; public communityUuid: string;
} }
export class GetCommunityParams { export class GetCommunityParams extends ProjectParam {
@ApiProperty({ @ApiProperty({
description: 'Community id of the specific community', description: 'Community id of the specific community',
required: true, required: true,

View File

@ -1 +1,3 @@
export * from './add.community.dto'; export * from './add.community.dto';
export * from './project.param.dto';
export * from './get.community.dto';

View File

@ -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;
}

View File

@ -1,5 +1,5 @@
import { Injectable, HttpException, HttpStatus } from '@nestjs/common'; 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 { UpdateCommunityNameDto } from '../dtos/update.community.dto';
import { BaseResponseDto } from '@app/common/dto/base.response.dto'; import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { import {
@ -11,17 +11,24 @@ import { CommunityRepository } from '@app/common/modules/community/repositories'
import { CommunityDto } from '@app/common/modules/community/dtos'; import { CommunityDto } from '@app/common/modules/community/dtos';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto'; import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service'; import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
@Injectable() @Injectable()
export class CommunityService { export class CommunityService {
constructor( constructor(
private readonly communityRepository: CommunityRepository, private readonly communityRepository: CommunityRepository,
private readonly projectRepository: ProjectRepository,
private readonly tuyaService: TuyaService, private readonly tuyaService: TuyaService,
) {} ) {}
async createCommunity(dto: AddCommunityDto): Promise<BaseResponseDto> { async createCommunity(
param: ProjectParam,
dto: AddCommunityDto,
): Promise<BaseResponseDto> {
const { name, description } = dto; const { name, description } = dto;
const project = await this.validateProject(param.projectUuid);
const existingCommunity = await this.communityRepository.findOneBy({ const existingCommunity = await this.communityRepository.findOneBy({
name, name,
}); });
@ -36,6 +43,7 @@ export class CommunityService {
const community = this.communityRepository.create({ const community = this.communityRepository.create({
name: name, name: name,
description: description, description: description,
project: project,
}); });
// Save the community to the database // Save the community to the database
@ -54,7 +62,11 @@ export class CommunityService {
} }
} }
async getCommunityById(communityUuid: string): Promise<BaseResponseDto> { async getCommunityById(params: GetCommunityParams): Promise<BaseResponseDto> {
const { communityUuid, projectUuid } = params;
await this.validateProject(projectUuid);
const community = await this.communityRepository.findOneBy({ const community = await this.communityRepository.findOneBy({
uuid: communityUuid, uuid: communityUuid,
}); });
@ -75,9 +87,13 @@ export class CommunityService {
} }
async getCommunities( async getCommunities(
param: ProjectParam,
pageable: Partial<TypeORMCustomModelFindAllQuery>, pageable: Partial<TypeORMCustomModelFindAllQuery>,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
await this.validateProject(param.projectUuid);
pageable.modelName = 'community'; pageable.modelName = 'community';
pageable.where = { project: { uuid: param.projectUuid } };
const customModel = TypeORMCustomModel(this.communityRepository); const customModel = TypeORMCustomModel(this.communityRepository);
@ -91,9 +107,13 @@ export class CommunityService {
} }
async updateCommunity( async updateCommunity(
communityUuid: string, params: GetCommunityParams,
updateCommunityDto: UpdateCommunityNameDto, updateCommunityDto: UpdateCommunityNameDto,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
const { communityUuid, projectUuid } = params;
await this.validateProject(projectUuid);
const community = await this.communityRepository.findOne({ const community = await this.communityRepository.findOne({
where: { uuid: communityUuid }, where: { uuid: communityUuid },
}); });
@ -128,7 +148,11 @@ export class CommunityService {
} }
} }
async deleteCommunity(communityUuid: string): Promise<BaseResponseDto> { async deleteCommunity(params: GetCommunityParams): Promise<BaseResponseDto> {
const { communityUuid, projectUuid } = params;
await this.validateProject(projectUuid);
const community = await this.communityRepository.findOne({ const community = await this.communityRepository.findOne({
where: { uuid: communityUuid }, 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;
}
} }