updated error message for update community with existing name

This commit is contained in:
hannathkadher
2024-12-30 22:22:28 +04:00
parent 5220722579
commit e56bd88104

View File

@ -31,15 +31,7 @@ export class CommunityService {
const project = await this.validateProject(param.projectUuid); const project = await this.validateProject(param.projectUuid);
const existingCommunity = await this.communityRepository.findOneBy({ await this.validateName(name);
name,
});
if (existingCommunity) {
throw new HttpException(
`A community with the name '${name}' already exists.`,
HttpStatus.BAD_REQUEST,
);
}
// Create the new community entity // Create the new community entity
const community = this.communityRepository.create({ const community = this.communityRepository.create({
@ -148,6 +140,7 @@ export class CommunityService {
try { try {
const { name } = updateCommunityDto; const { name } = updateCommunityDto;
if (name != community.name) await this.validateName(name);
community.name = name; community.name = name;
@ -163,7 +156,10 @@ export class CommunityService {
throw err; // If it's an HttpException, rethrow it throw err; // If it's an HttpException, rethrow it
} else { } else {
// Throw a generic 404 error if anything else goes wrong // 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; 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,
);
}
}
} }