From e56bd8810429081d40f440ba13eed6c16f184208 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Mon, 30 Dec 2024 22:22:28 +0400 Subject: [PATCH] updated error message for update community with existing name --- src/community/services/community.service.ts | 28 +++++++++++++-------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/community/services/community.service.ts b/src/community/services/community.service.ts index eee72d1..7bfc9bc 100644 --- a/src/community/services/community.service.ts +++ b/src/community/services/community.service.ts @@ -31,15 +31,7 @@ export class CommunityService { const project = await this.validateProject(param.projectUuid); - const existingCommunity = await this.communityRepository.findOneBy({ - name, - }); - if (existingCommunity) { - throw new HttpException( - `A community with the name '${name}' already exists.`, - HttpStatus.BAD_REQUEST, - ); - } + await this.validateName(name); // Create the new community entity const community = this.communityRepository.create({ @@ -148,6 +140,7 @@ export class CommunityService { try { const { name } = updateCommunityDto; + if (name != community.name) await this.validateName(name); community.name = name; @@ -163,7 +156,10 @@ export class CommunityService { throw err; // If it's an HttpException, rethrow it } else { // Throw a generic 404 error if anything else goes wrong - throw new HttpException('Community not found', HttpStatus.NOT_FOUND); + throw new HttpException( + `An Internal exception has been occured ${err}`, + HttpStatus.INTERNAL_SERVER_ERROR, + ); } } } @@ -234,4 +230,16 @@ export class CommunityService { } return project; } + + private async validateName(name: string) { + const existingCommunity = await this.communityRepository.findOneBy({ + name, + }); + if (existingCommunity) { + throw new HttpException( + `A community with the name '${name}' already exists.`, + HttpStatus.BAD_REQUEST, + ); + } + } }