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 { 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],
})

View File

@ -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<BaseResponseDto> {
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<BaseResponseDto> {
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<BaseResponseDto> {
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<BaseResponseDto> {
return this.communityService.deleteCommunity(param.communityUuid);
return this.communityService.deleteCommunity(params);
}
}

View File

@ -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,

View File

@ -1 +1,3 @@
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 { 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<BaseResponseDto> {
async createCommunity(
param: ProjectParam,
dto: AddCommunityDto,
): Promise<BaseResponseDto> {
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<BaseResponseDto> {
async getCommunityById(params: GetCommunityParams): Promise<BaseResponseDto> {
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<TypeORMCustomModelFindAllQuery>,
): Promise<BaseResponseDto> {
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<BaseResponseDto> {
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<BaseResponseDto> {
async deleteCommunity(params: GetCommunityParams): Promise<BaseResponseDto> {
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;
}
}