updated community service to manage orphan community

This commit is contained in:
hannathkadher
2024-12-18 08:23:42 +04:00
parent a5ab883957
commit e4a5068f0d
3 changed files with 1665 additions and 63 deletions

1675
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -23,6 +23,7 @@
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.0",
"@nestjs/core": "^10.0.0",
"@nestjs/cqrs": "^10.2.8",
"@nestjs/jwt": "^10.2.0",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.0.0",

View File

@ -12,6 +12,8 @@ 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';
import { ORPHAN_COMMUNITY_NAME } from '@app/common/constants/orphan-constant';
import { Not } from 'typeorm';
@Injectable()
export class CommunityService {
@ -90,20 +92,31 @@ export class CommunityService {
param: ProjectParam,
pageable: Partial<TypeORMCustomModelFindAllQuery>,
): Promise<BaseResponseDto> {
await this.validateProject(param.projectUuid);
try {
const project = await this.validateProject(param.projectUuid);
pageable.modelName = 'community';
pageable.where = { project: { uuid: param.projectUuid } };
pageable.modelName = 'community';
pageable.where = {
project: { uuid: param.projectUuid },
name: Not(`${ORPHAN_COMMUNITY_NAME}-${project.name}`),
};
const customModel = TypeORMCustomModel(this.communityRepository);
const customModel = TypeORMCustomModel(this.communityRepository);
const { baseResponseDto, paginationResponseDto } =
await customModel.findAll(pageable);
const { baseResponseDto, paginationResponseDto } =
await customModel.findAll(pageable);
return new PageResponse<CommunityDto>(
baseResponseDto,
paginationResponseDto,
);
return new PageResponse<CommunityDto>(
baseResponseDto,
paginationResponseDto,
);
} catch (error) {
// Generic error handling
throw new HttpException(
error.message || 'An error occurred while fetching communities.',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async updateCommunity(
@ -112,7 +125,7 @@ export class CommunityService {
): Promise<BaseResponseDto> {
const { communityUuid, projectUuid } = params;
await this.validateProject(projectUuid);
const project = await this.validateProject(projectUuid);
const community = await this.communityRepository.findOne({
where: { uuid: communityUuid },
@ -126,6 +139,13 @@ export class CommunityService {
);
}
if (community.name === `${ORPHAN_COMMUNITY_NAME}-${project.name}`) {
throw new HttpException(
`Community with ID ${communityUuid} cannot be updated`,
HttpStatus.BAD_REQUEST,
);
}
try {
const { name } = updateCommunityDto;
@ -151,7 +171,7 @@ export class CommunityService {
async deleteCommunity(params: GetCommunityParams): Promise<BaseResponseDto> {
const { communityUuid, projectUuid } = params;
await this.validateProject(projectUuid);
const project = await this.validateProject(projectUuid);
const community = await this.communityRepository.findOne({
where: { uuid: communityUuid },
@ -164,6 +184,14 @@ export class CommunityService {
HttpStatus.NOT_FOUND,
);
}
if (community.name === `${ORPHAN_COMMUNITY_NAME}-${project.name}`) {
throw new HttpException(
`Community with ID ${communityUuid} cannot be deleted`,
HttpStatus.BAD_REQUEST,
);
}
try {
await this.communityRepository.remove(community);