mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix(api): align eventEmitter update events
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Document, Model, Query } from 'mongoose';
|
||||
import { Model } from 'mongoose';
|
||||
|
||||
import { BaseRepository } from '@/utils/generics/base-repository';
|
||||
|
||||
@@ -30,28 +30,4 @@ export class NlpEntityRepository extends BaseRepository<
|
||||
constructor(@InjectModel(NlpEntity.name) readonly model: Model<NlpEntity>) {
|
||||
super(model, NlpEntity, NLP_ENTITY_POPULATE, NlpEntityFull);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post-update hook that triggers after an NLP entity is updated.
|
||||
* Emits an event to notify other parts of the system about the update.
|
||||
* Bypasses built-in entities.
|
||||
*
|
||||
* @param query - The query used to find and update the entity.
|
||||
* @param updated - The updated NLP entity document.
|
||||
*/
|
||||
async postUpdate(
|
||||
_query: Query<
|
||||
Document<NlpEntity, any, any>,
|
||||
Document<NlpEntity, any, any>,
|
||||
unknown,
|
||||
NlpEntity,
|
||||
'findOneAndUpdate'
|
||||
>,
|
||||
updated: NlpEntity,
|
||||
): Promise<void> {
|
||||
if (!updated?.builtin) {
|
||||
// Bypass builtin entities (probably fixtures)
|
||||
this.eventEmitter.emit('hook:nlpEntity:update', updated);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,14 +9,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
import {
|
||||
Document,
|
||||
Model,
|
||||
PipelineStage,
|
||||
Query,
|
||||
SortOrder,
|
||||
Types,
|
||||
} from 'mongoose';
|
||||
import { Model, PipelineStage, SortOrder, Types } from 'mongoose';
|
||||
|
||||
import { BaseRepository } from '@/utils/generics/base-repository';
|
||||
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
|
||||
@@ -45,28 +38,6 @@ export class NlpValueRepository extends BaseRepository<
|
||||
super(model, NlpValue, NLP_VALUE_POPULATE, NlpValueFull);
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits an event after an NLP value is updated, bypassing built-in values.
|
||||
*
|
||||
* @param query - The query that was used to update the NLP value.
|
||||
* @param updated - The updated NLP value document.
|
||||
*/
|
||||
async postUpdate(
|
||||
_query: Query<
|
||||
Document<NlpValue, any, any>,
|
||||
Document<NlpValue, any, any>,
|
||||
unknown,
|
||||
NlpValue,
|
||||
'findOneAndUpdate'
|
||||
>,
|
||||
updated: NlpValue,
|
||||
): Promise<void> {
|
||||
if (!updated?.builtin) {
|
||||
// Bypass builtin entities (probably fixtures)
|
||||
this.eventEmitter.emit('hook:nlpValue:update', updated);
|
||||
}
|
||||
}
|
||||
|
||||
private getSortDirection(sortOrder: SortOrder) {
|
||||
return typeof sortOrder === 'number'
|
||||
? sortOrder
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { Document, Query } from 'mongoose';
|
||||
|
||||
import { HelperService } from '@/helper/helper.service';
|
||||
import { HelperType, NLU } from '@/helper/types';
|
||||
@@ -96,15 +97,28 @@ export class NlpService {
|
||||
*
|
||||
* @param entity - The NLP entity to be updated.
|
||||
*/
|
||||
@OnEvent('hook:nlpEntity:update')
|
||||
async handleEntityUpdate(entity: NlpEntity) {
|
||||
// Synchonize new entity with NLP provider
|
||||
try {
|
||||
const helper = await this.helperService.getDefaultNluHelper();
|
||||
await helper.updateEntity(entity);
|
||||
this.logger.debug('Updated entity successfully synced!', entity);
|
||||
} catch (err) {
|
||||
this.logger.error('Unable to sync updated entity', err);
|
||||
@OnEvent('hook:nlpEntity:postUpdate')
|
||||
async handleEntityPostUpdate(
|
||||
_query: Query<
|
||||
Document<NlpEntity>,
|
||||
Document<NlpEntity>,
|
||||
unknown,
|
||||
NlpEntity,
|
||||
'findOneAndUpdate'
|
||||
>,
|
||||
updated: NlpEntity,
|
||||
) {
|
||||
if (!updated?.builtin) {
|
||||
// Synchonize new entity with NLP provider
|
||||
try {
|
||||
const helper = await this.helperService.getDefaultHelper(
|
||||
HelperType.NLU,
|
||||
);
|
||||
await helper.updateEntity(updated);
|
||||
this.logger.debug('Updated entity successfully synced!', updated);
|
||||
} catch (err) {
|
||||
this.logger.error('Unable to sync updated entity', err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,15 +188,28 @@ export class NlpService {
|
||||
*
|
||||
* @param value - The NLP value to be updated.
|
||||
*/
|
||||
@OnEvent('hook:nlpValue:update')
|
||||
async handleValueUpdate(value: NlpValue) {
|
||||
// Synchonize new value with NLP provider
|
||||
try {
|
||||
const helper = await this.helperService.getDefaultNluHelper();
|
||||
await helper.updateValue(value);
|
||||
this.logger.debug('Updated value successfully synced!', value);
|
||||
} catch (err) {
|
||||
this.logger.error('Unable to sync updated value', err);
|
||||
@OnEvent('hook:nlpValue:postUpdate')
|
||||
async handleValueUpdate(
|
||||
_query: Query<
|
||||
Document<NlpValue, any, any>,
|
||||
Document<NlpValue, any, any>,
|
||||
unknown,
|
||||
NlpValue,
|
||||
'findOneAndUpdate'
|
||||
>,
|
||||
updated: NlpValue,
|
||||
) {
|
||||
if (!updated?.builtin) {
|
||||
// Synchonize new value with NLP provider
|
||||
try {
|
||||
const helper = await this.helperService.getDefaultHelper(
|
||||
HelperType.NLU,
|
||||
);
|
||||
await helper.updateValue(updated);
|
||||
this.logger.debug('Updated value successfully synced!', updated);
|
||||
} catch (err) {
|
||||
this.logger.error('Unable to sync updated value', err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user