mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix: update dto
This commit is contained in:
parent
a054ee542e
commit
44a42e474d
@ -9,7 +9,6 @@
|
|||||||
import { CACHE_MANAGER } from '@nestjs/cache-manager';
|
import { CACHE_MANAGER } from '@nestjs/cache-manager';
|
||||||
import {
|
import {
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
ConflictException,
|
|
||||||
MethodNotAllowedException,
|
MethodNotAllowedException,
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
@ -29,7 +28,7 @@ import {
|
|||||||
import { TFixtures } from '@/utils/test/types';
|
import { TFixtures } from '@/utils/test/types';
|
||||||
import { buildTestingMocks } from '@/utils/test/utils';
|
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 { NlpEntityRepository } from '../repositories/nlp-entity.repository';
|
||||||
import { NlpSampleEntityRepository } from '../repositories/nlp-sample-entity.repository';
|
import { NlpSampleEntityRepository } from '../repositories/nlp-sample-entity.repository';
|
||||||
import { NlpValueRepository } from '../repositories/nlp-value.repository';
|
import { NlpValueRepository } from '../repositories/nlp-value.repository';
|
||||||
@ -270,24 +269,8 @@ describe('NlpEntityController', () => {
|
|||||||
).rejects.toThrow(NotFoundException);
|
).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 () => {
|
it('should update weight if entity is builtin and weight is provided', async () => {
|
||||||
const updatedNlpEntity: NlpEntityCreateDto = {
|
const updatedNlpEntity: NlpEntityUpdateDto = {
|
||||||
name: 'updated',
|
|
||||||
doc: '',
|
|
||||||
lookups: ['trait'],
|
|
||||||
builtin: false,
|
|
||||||
weight: 4,
|
weight: 4,
|
||||||
};
|
};
|
||||||
const findOneSpy = jest.spyOn(nlpEntityService, 'findOne');
|
const findOneSpy = jest.spyOn(nlpEntityService, 'findOne');
|
||||||
|
@ -9,7 +9,6 @@
|
|||||||
import {
|
import {
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
Body,
|
Body,
|
||||||
ConflictException,
|
|
||||||
Controller,
|
Controller,
|
||||||
Delete,
|
Delete,
|
||||||
Get,
|
Get,
|
||||||
@ -34,7 +33,7 @@ import { PopulatePipe } from '@/utils/pipes/populate.pipe';
|
|||||||
import { SearchFilterPipe } from '@/utils/pipes/search-filter.pipe';
|
import { SearchFilterPipe } from '@/utils/pipes/search-filter.pipe';
|
||||||
import { TFilterQuery } from '@/utils/types/filter.types';
|
import { TFilterQuery } from '@/utils/types/filter.types';
|
||||||
|
|
||||||
import { NlpEntityCreateDto } from '../dto/nlp-entity.dto';
|
import { NlpEntityCreateDto, NlpEntityUpdateDto } from '../dto/nlp-entity.dto';
|
||||||
import {
|
import {
|
||||||
NlpEntity,
|
NlpEntity,
|
||||||
NlpEntityFull,
|
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.
|
* 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 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.
|
* @returns The updated NLP entity.
|
||||||
*/
|
*/
|
||||||
@ -151,7 +150,7 @@ export class NlpEntityController extends BaseController<
|
|||||||
@Patch(':id')
|
@Patch(':id')
|
||||||
async updateOne(
|
async updateOne(
|
||||||
@Param('id') id: string,
|
@Param('id') id: string,
|
||||||
@Body() updateNlpEntityDto: NlpEntityCreateDto,
|
@Body() nlpEntityDto: NlpEntityUpdateDto,
|
||||||
): Promise<NlpEntity> {
|
): Promise<NlpEntity> {
|
||||||
const nlpEntity = await this.nlpEntityService.findOne(id);
|
const nlpEntity = await this.nlpEntityService.findOne(id);
|
||||||
if (!nlpEntity) {
|
if (!nlpEntity) {
|
||||||
@ -159,21 +158,12 @@ export class NlpEntityController extends BaseController<
|
|||||||
throw new NotFoundException(`NLP Entity with ID ${id} not found`);
|
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
|
// Only allow weight update for builtin entities
|
||||||
if (updateNlpEntityDto.weight) {
|
return await this.nlpEntityService.updateWeight(id, nlpEntityDto.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.updateOne(id, updateNlpEntityDto);
|
return await this.nlpEntityService.updateOne(id, nlpEntityDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -55,7 +55,25 @@ export class NlpEntityCreateDto {
|
|||||||
type: Number,
|
type: Number,
|
||||||
})
|
})
|
||||||
@IsOptional()
|
@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',
|
message: 'Weight must be a strictly positive number',
|
||||||
})
|
})
|
||||||
@IsNumber({ allowNaN: false, allowInfinity: false })
|
@IsNumber({ allowNaN: false, allowInfinity: false })
|
||||||
@ -64,4 +82,5 @@ export class NlpEntityCreateDto {
|
|||||||
|
|
||||||
export type NlpEntityDto = DtoConfig<{
|
export type NlpEntityDto = DtoConfig<{
|
||||||
create: NlpEntityCreateDto;
|
create: NlpEntityCreateDto;
|
||||||
|
update: NlpEntityUpdateDto;
|
||||||
}>;
|
}>;
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { CACHE_MANAGER } from '@nestjs/cache-manager';
|
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 { OnEvent } from '@nestjs/event-emitter';
|
||||||
import { Cache } from 'cache-manager';
|
import { Cache } from 'cache-manager';
|
||||||
|
|
||||||
@ -64,7 +64,9 @@ export class NlpEntityService extends BaseService<
|
|||||||
*/
|
*/
|
||||||
async updateWeight(id: string, updatedWeight: number): Promise<NlpEntity> {
|
async updateWeight(id: string, updatedWeight: number): Promise<NlpEntity> {
|
||||||
if (updatedWeight <= 0) {
|
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 });
|
return await this.repository.updateOne(id, { weight: updatedWeight });
|
||||||
|
@ -10,7 +10,7 @@ import { Injectable, NotFoundException } from '@nestjs/common';
|
|||||||
import { OnEvent } from '@nestjs/event-emitter';
|
import { OnEvent } from '@nestjs/event-emitter';
|
||||||
|
|
||||||
import { HelperService } from '@/helper/helper.service';
|
import { HelperService } from '@/helper/helper.service';
|
||||||
import { NLU } from '@/helper/types';
|
import { HelperType, NLU } from '@/helper/types';
|
||||||
import { LoggerService } from '@/logger/logger.service';
|
import { LoggerService } from '@/logger/logger.service';
|
||||||
|
|
||||||
import { NlpEntity, NlpEntityDocument } from '../schemas/nlp-entity.schema';
|
import { NlpEntity, NlpEntityDocument } from '../schemas/nlp-entity.schema';
|
||||||
@ -70,7 +70,7 @@ export class NlpService {
|
|||||||
async handleEntityCreate(entity: NlpEntityDocument) {
|
async handleEntityCreate(entity: NlpEntityDocument) {
|
||||||
// Synchonize new entity with NLP
|
// Synchonize new entity with NLP
|
||||||
try {
|
try {
|
||||||
const helper = await this.helperService.getDefaultNluHelper();
|
const helper = await this.helperService.getDefaultHelper(HelperType.NLU);
|
||||||
const foreignId = await helper.addEntity(entity);
|
const foreignId = await helper.addEntity(entity);
|
||||||
this.logger.debug('New entity successfully synced!', foreignId);
|
this.logger.debug('New entity successfully synced!', foreignId);
|
||||||
return await this.nlpEntityService.updateOne(
|
return await this.nlpEntityService.updateOne(
|
||||||
|
Loading…
Reference in New Issue
Block a user