fix: update dto

This commit is contained in:
Mohamed Marrouchi 2025-05-12 14:49:04 +01:00
parent a054ee542e
commit 44a42e474d
5 changed files with 34 additions and 40 deletions

View File

@ -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');

View File

@ -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<NlpEntity> {
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);
}
/**

View File

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

View File

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

View File

@ -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(