fix: restore conflict exception

This commit is contained in:
Mohamed Marrouchi 2025-05-12 14:55:36 +01:00
parent f2fede7e68
commit 6ba4c76440
2 changed files with 25 additions and 3 deletions

View File

@ -9,6 +9,7 @@
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import {
BadRequestException,
ConflictException,
MethodNotAllowedException,
NotFoundException,
} from '@nestjs/common';
@ -289,6 +290,17 @@ describe('NlpEntityController', () => {
expect(result.weight).toBe(updatedNlpEntity.weight);
});
it('should throw an exception if entity is builtin but weight not provided', async () => {
await expect(
nlpEntityController.updateOne(buitInEntityId!, {
name: 'updated',
doc: '',
lookups: ['trait'],
builtin: false,
} as any),
).rejects.toThrow(ConflictException);
});
it('should update only the weight of the builtin entity', async () => {
const updatedNlpEntity: NlpEntityUpdateDto = {
weight: 8,

View File

@ -9,6 +9,7 @@
import {
BadRequestException,
Body,
ConflictException,
Controller,
Delete,
Get,
@ -158,9 +159,18 @@ export class NlpEntityController extends BaseController<
throw new NotFoundException(`NLP Entity with ID ${id} not found`);
}
if (nlpEntity.builtin && nlpEntityDto.weight) {
// Only allow weight update for builtin entities
return await this.nlpEntityService.updateWeight(id, nlpEntityDto.weight);
if (nlpEntity.builtin) {
if (nlpEntityDto.weight) {
// Only allow weight update for builtin entities
return await this.nlpEntityService.updateWeight(
id,
nlpEntityDto.weight,
);
} else {
throw new ConflictException(
`Cannot update builtin NLP Entity ${nlpEntity.name} except for weight`,
);
}
}
return await this.nlpEntityService.updateOne(id, nlpEntityDto);