mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix(api): resolve predefined create hooks bug
This commit is contained in:
parent
daa2bb61d9
commit
b92c34e373
@ -16,7 +16,6 @@ import { LabelDto } from '../dto/label.dto';
|
||||
import {
|
||||
Label,
|
||||
LABEL_POPULATE,
|
||||
LabelDocument,
|
||||
LabelFull,
|
||||
LabelPopulate,
|
||||
} from '../schemas/label.schema';
|
||||
@ -31,31 +30,4 @@ export class LabelRepository extends BaseRepository<
|
||||
constructor(@InjectModel(Label.name) readonly model: Model<Label>) {
|
||||
super(model, Label, LABEL_POPULATE, LabelFull);
|
||||
}
|
||||
|
||||
/**
|
||||
* After creating a `Label`, this method emits an event and updates the `label_id` field.
|
||||
*
|
||||
* @param created - The created label document instance.
|
||||
*
|
||||
* @returns A promise that resolves when the update operation is complete.
|
||||
*/
|
||||
async postCreate(created: LabelDocument): Promise<void> {
|
||||
this.eventEmitter.emit(
|
||||
'hook:label:create',
|
||||
created,
|
||||
async (result: Record<string, any>) => {
|
||||
await this.model.updateOne(
|
||||
{ _id: created._id },
|
||||
{
|
||||
$set: {
|
||||
label_id: {
|
||||
...(created.label_id || {}),
|
||||
...result,
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,6 @@ import { NlpEntityDto } from '../dto/nlp-entity.dto';
|
||||
import {
|
||||
NLP_ENTITY_POPULATE,
|
||||
NlpEntity,
|
||||
NlpEntityDocument,
|
||||
NlpEntityFull,
|
||||
NlpEntityPopulate,
|
||||
} from '../schemas/nlp-entity.schema';
|
||||
@ -32,20 +31,6 @@ export class NlpEntityRepository extends BaseRepository<
|
||||
super(model, NlpEntity, NLP_ENTITY_POPULATE, NlpEntityFull);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post-create hook that triggers after an NLP entity is created.
|
||||
* Emits an event to notify other parts of the system about the creation.
|
||||
* Bypasses built-in entities.
|
||||
*
|
||||
* @param created - The newly created NLP entity document.
|
||||
*/
|
||||
async postCreate(_created: NlpEntityDocument): Promise<void> {
|
||||
if (!_created.builtin) {
|
||||
// Bypass builtin entities (probably fixtures)
|
||||
this.eventEmitter.emit('hook:nlpEntity:create', _created);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Post-update hook that triggers after an NLP entity is updated.
|
||||
* Emits an event to notify other parts of the system about the update.
|
||||
|
@ -27,7 +27,6 @@ import { NlpValueDto } from '../dto/nlp-value.dto';
|
||||
import {
|
||||
NLP_VALUE_POPULATE,
|
||||
NlpValue,
|
||||
NlpValueDocument,
|
||||
NlpValueFull,
|
||||
NlpValueFullWithCount,
|
||||
NlpValuePopulate,
|
||||
@ -46,18 +45,6 @@ export class NlpValueRepository extends BaseRepository<
|
||||
super(model, NlpValue, NLP_VALUE_POPULATE, NlpValueFull);
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits an event after a new NLP value is created, bypassing built-in values.
|
||||
*
|
||||
* @param created - The newly created NLP value document.
|
||||
*/
|
||||
async postCreate(created: NlpValueDocument): Promise<void> {
|
||||
if (!created.builtin) {
|
||||
// Bypass builtin entities (probably fixtures)
|
||||
this.eventEmitter.emit('hook:nlpValue:create', created);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits an event after an NLP value is updated, bypassing built-in values.
|
||||
*
|
||||
|
@ -69,15 +69,18 @@ export class NlpService {
|
||||
*
|
||||
* @param entity - The NLP entity to be created.
|
||||
*/
|
||||
@OnEvent('hook:nlpEntity:create')
|
||||
async handleEntityCreate(entity: NlpEntityDocument) {
|
||||
@OnEvent('hook:nlpEntity:postCreate')
|
||||
async handleEntityPostCreate(created: NlpEntityDocument) {
|
||||
if (!created.builtin) {
|
||||
// Synchonize new entity with NLP
|
||||
try {
|
||||
const helper = await this.helperService.getDefaultHelper(HelperType.NLU);
|
||||
const foreignId = await helper.addEntity(entity);
|
||||
const helper = await this.helperService.getDefaultHelper(
|
||||
HelperType.NLU,
|
||||
);
|
||||
const foreignId = await helper.addEntity(created);
|
||||
this.logger.debug('New entity successfully synced!', foreignId);
|
||||
await this.nlpEntityService.updateOne(
|
||||
{ _id: entity._id },
|
||||
{ _id: created._id },
|
||||
{
|
||||
foreign_id: foreignId,
|
||||
},
|
||||
@ -86,6 +89,7 @@ export class NlpService {
|
||||
this.logger.error('Unable to sync a new entity', err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the event triggered when an NLP entity is updated. Synchronizes the updated entity with the external NLP provider.
|
||||
@ -145,15 +149,16 @@ export class NlpService {
|
||||
*
|
||||
* @param value - The NLP value to be created.
|
||||
*/
|
||||
@OnEvent('hook:nlpValue:create')
|
||||
async handleValueCreate(value: NlpValueDocument) {
|
||||
@OnEvent('hook:nlpValue:postCreate')
|
||||
async handleValuePostCreate(created: NlpValueDocument) {
|
||||
if (!created.builtin) {
|
||||
// Synchonize new value with NLP provider
|
||||
try {
|
||||
const helper = await this.helperService.getDefaultNluHelper();
|
||||
const foreignId = await helper.addValue(value);
|
||||
const foreignId = await helper.addValue(created);
|
||||
this.logger.debug('New value successfully synced!', foreignId);
|
||||
await this.nlpValueService.updateOne(
|
||||
{ _id: value._id },
|
||||
{ _id: created._id },
|
||||
{
|
||||
foreign_id: foreignId,
|
||||
},
|
||||
@ -162,6 +167,7 @@ export class NlpService {
|
||||
this.logger.error('Unable to sync a new value', err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the event triggered when an NLP value is updated. Synchronizes the updated value with the external NLP provider.
|
||||
|
Loading…
Reference in New Issue
Block a user