Merge pull request #186 from SyncrowIOT/bugfix/fix-error-on-community-update-error

updated error message for update community with existing name
This commit is contained in:
hannathkadher
2024-12-31 10:15:26 +04:00
committed by GitHub

View File

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