diff --git a/api/src/nlp/controllers/nlp-entity.controller.spec.ts b/api/src/nlp/controllers/nlp-entity.controller.spec.ts index 484f41c8..db405e63 100644 --- a/api/src/nlp/controllers/nlp-entity.controller.spec.ts +++ b/api/src/nlp/controllers/nlp-entity.controller.spec.ts @@ -9,7 +9,6 @@ import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { BadRequestException, - ConflictException, MethodNotAllowedException, NotFoundException, } from '@nestjs/common'; @@ -29,7 +28,7 @@ import { import { TFixtures } from '@/utils/test/types'; import { buildTestingMocks } from '@/utils/test/utils'; -import { NlpEntityCreateDto } from '../dto/nlp-entity.dto'; +import { NlpEntityCreateDto, NlpEntityUpdateDto } from '../dto/nlp-entity.dto'; import { NlpEntityRepository } from '../repositories/nlp-entity.repository'; import { NlpSampleEntityRepository } from '../repositories/nlp-sample-entity.repository'; import { NlpValueRepository } from '../repositories/nlp-value.repository'; @@ -270,24 +269,8 @@ describe('NlpEntityController', () => { ).rejects.toThrow(NotFoundException); }); - it('should throw an exception if entity is builtin but weight not provided', async () => { - const updateNlpEntity: NlpEntityCreateDto = { - name: 'updated', - doc: '', - lookups: ['trait'], - builtin: false, - }; - await expect( - nlpEntityController.updateOne(buitInEntityId!, updateNlpEntity), - ).rejects.toThrow(ConflictException); - }); - it('should update weight if entity is builtin and weight is provided', async () => { - const updatedNlpEntity: NlpEntityCreateDto = { - name: 'updated', - doc: '', - lookups: ['trait'], - builtin: false, + const updatedNlpEntity: NlpEntityUpdateDto = { weight: 4, }; const findOneSpy = jest.spyOn(nlpEntityService, 'findOne'); diff --git a/api/src/nlp/controllers/nlp-entity.controller.ts b/api/src/nlp/controllers/nlp-entity.controller.ts index af549526..32d22b11 100644 --- a/api/src/nlp/controllers/nlp-entity.controller.ts +++ b/api/src/nlp/controllers/nlp-entity.controller.ts @@ -9,7 +9,6 @@ import { BadRequestException, Body, - ConflictException, Controller, Delete, Get, @@ -34,7 +33,7 @@ import { PopulatePipe } from '@/utils/pipes/populate.pipe'; import { SearchFilterPipe } from '@/utils/pipes/search-filter.pipe'; import { TFilterQuery } from '@/utils/types/filter.types'; -import { NlpEntityCreateDto } from '../dto/nlp-entity.dto'; +import { NlpEntityCreateDto, NlpEntityUpdateDto } from '../dto/nlp-entity.dto'; import { NlpEntity, NlpEntityFull, @@ -143,7 +142,7 @@ export class NlpEntityController extends BaseController< * This endpoint allows updating an existing NLP entity. The entity must not be a built-in entity. * * @param id - The ID of the NLP entity to update. - * @param updateNlpEntityDto - The new data for the NLP entity. + * @param nlpEntityDto - The new data for the NLP entity. * * @returns The updated NLP entity. */ @@ -151,7 +150,7 @@ export class NlpEntityController extends BaseController< @Patch(':id') async updateOne( @Param('id') id: string, - @Body() updateNlpEntityDto: NlpEntityCreateDto, + @Body() nlpEntityDto: NlpEntityUpdateDto, ): Promise { const nlpEntity = await this.nlpEntityService.findOne(id); if (!nlpEntity) { @@ -159,21 +158,12 @@ export class NlpEntityController extends BaseController< throw new NotFoundException(`NLP Entity with ID ${id} not found`); } - if (nlpEntity.builtin) { + if (nlpEntity.builtin && nlpEntityDto.weight) { // Only allow weight update for builtin entities - if (updateNlpEntityDto.weight) { - return await this.nlpEntityService.updateWeight( - id, - updateNlpEntityDto.weight, - ); - } else { - throw new ConflictException( - `Cannot update builtin NLP Entity ${nlpEntity.name} except for weight`, - ); - } + return await this.nlpEntityService.updateWeight(id, nlpEntityDto.weight); } - return await this.nlpEntityService.updateOne(id, updateNlpEntityDto); + return await this.nlpEntityService.updateOne(id, nlpEntityDto); } /** diff --git a/api/src/nlp/dto/nlp-entity.dto.ts b/api/src/nlp/dto/nlp-entity.dto.ts index 86ddf7d8..d82b6689 100644 --- a/api/src/nlp/dto/nlp-entity.dto.ts +++ b/api/src/nlp/dto/nlp-entity.dto.ts @@ -55,7 +55,25 @@ export class NlpEntityCreateDto { type: Number, }) @IsOptional() - @Validate((value) => value > 0, { + @Validate((value: number) => value > 0, { + message: 'Weight must be a strictly positive number', + }) + @IsNumber({ allowNaN: false, allowInfinity: false }) + weight?: number; +} + +export class NlpEntityUpdateDto { + @ApiPropertyOptional({ type: String }) + @IsString() + @IsOptional() + foreign_id?: string; + + @ApiPropertyOptional({ + description: 'Nlp entity associated weight for next block triggering', + type: Number, + }) + @IsOptional() + @Validate((value: number) => value > 0, { message: 'Weight must be a strictly positive number', }) @IsNumber({ allowNaN: false, allowInfinity: false }) @@ -64,4 +82,5 @@ export class NlpEntityCreateDto { export type NlpEntityDto = DtoConfig<{ create: NlpEntityCreateDto; + update: NlpEntityUpdateDto; }>; diff --git a/api/src/nlp/services/nlp-entity.service.ts b/api/src/nlp/services/nlp-entity.service.ts index 2704dabd..0f38c920 100644 --- a/api/src/nlp/services/nlp-entity.service.ts +++ b/api/src/nlp/services/nlp-entity.service.ts @@ -7,7 +7,7 @@ */ import { CACHE_MANAGER } from '@nestjs/cache-manager'; -import { Inject, Injectable } from '@nestjs/common'; +import { BadRequestException, Inject, Injectable } from '@nestjs/common'; import { OnEvent } from '@nestjs/event-emitter'; import { Cache } from 'cache-manager'; @@ -64,7 +64,9 @@ export class NlpEntityService extends BaseService< */ async updateWeight(id: string, updatedWeight: number): Promise { if (updatedWeight <= 0) { - throw new Error('Weight must be a strictly positive number'); + throw new BadRequestException( + 'Weight must be a strictly positive number', + ); } return await this.repository.updateOne(id, { weight: updatedWeight }); diff --git a/api/src/nlp/services/nlp.service.ts b/api/src/nlp/services/nlp.service.ts index 905a9465..6a779648 100644 --- a/api/src/nlp/services/nlp.service.ts +++ b/api/src/nlp/services/nlp.service.ts @@ -10,7 +10,7 @@ import { Injectable, NotFoundException } from '@nestjs/common'; import { OnEvent } from '@nestjs/event-emitter'; import { HelperService } from '@/helper/helper.service'; -import { NLU } from '@/helper/types'; +import { HelperType, NLU } from '@/helper/types'; import { LoggerService } from '@/logger/logger.service'; import { NlpEntity, NlpEntityDocument } from '../schemas/nlp-entity.schema'; @@ -70,7 +70,7 @@ export class NlpService { async handleEntityCreate(entity: NlpEntityDocument) { // Synchonize new entity with NLP try { - const helper = await this.helperService.getDefaultNluHelper(); + const helper = await this.helperService.getDefaultHelper(HelperType.NLU); const foreignId = await helper.addEntity(entity); this.logger.debug('New entity successfully synced!', foreignId); return await this.nlpEntityService.updateOne(