mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 08:54:54 +00:00
updated community service to manage orphan community
This commit is contained in:
1675
package-lock.json
generated
1675
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -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",
|
||||
|
||||
@ -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,10 +92,14 @@ 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.where = {
|
||||
project: { uuid: param.projectUuid },
|
||||
name: Not(`${ORPHAN_COMMUNITY_NAME}-${project.name}`),
|
||||
};
|
||||
|
||||
const customModel = TypeORMCustomModel(this.communityRepository);
|
||||
|
||||
@ -104,6 +110,13 @@ export class CommunityService {
|
||||
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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user