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

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