fix(api): resolve predefined create hooks bug

This commit is contained in:
yassinedorbozgithub 2025-06-13 11:14:56 +01:00
parent daa2bb61d9
commit b92c34e373
4 changed files with 36 additions and 86 deletions

View File

@ -16,7 +16,6 @@ import { LabelDto } from '../dto/label.dto';
import { import {
Label, Label,
LABEL_POPULATE, LABEL_POPULATE,
LabelDocument,
LabelFull, LabelFull,
LabelPopulate, LabelPopulate,
} from '../schemas/label.schema'; } from '../schemas/label.schema';
@ -31,31 +30,4 @@ export class LabelRepository extends BaseRepository<
constructor(@InjectModel(Label.name) readonly model: Model<Label>) { constructor(@InjectModel(Label.name) readonly model: Model<Label>) {
super(model, Label, LABEL_POPULATE, LabelFull); 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,
},
},
},
);
},
);
}
} }

View File

@ -16,7 +16,6 @@ import { NlpEntityDto } from '../dto/nlp-entity.dto';
import { import {
NLP_ENTITY_POPULATE, NLP_ENTITY_POPULATE,
NlpEntity, NlpEntity,
NlpEntityDocument,
NlpEntityFull, NlpEntityFull,
NlpEntityPopulate, NlpEntityPopulate,
} from '../schemas/nlp-entity.schema'; } from '../schemas/nlp-entity.schema';
@ -32,20 +31,6 @@ export class NlpEntityRepository extends BaseRepository<
super(model, NlpEntity, NLP_ENTITY_POPULATE, NlpEntityFull); 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. * Post-update hook that triggers after an NLP entity is updated.
* Emits an event to notify other parts of the system about the update. * Emits an event to notify other parts of the system about the update.

View File

@ -27,7 +27,6 @@ import { NlpValueDto } from '../dto/nlp-value.dto';
import { import {
NLP_VALUE_POPULATE, NLP_VALUE_POPULATE,
NlpValue, NlpValue,
NlpValueDocument,
NlpValueFull, NlpValueFull,
NlpValueFullWithCount, NlpValueFullWithCount,
NlpValuePopulate, NlpValuePopulate,
@ -46,18 +45,6 @@ export class NlpValueRepository extends BaseRepository<
super(model, NlpValue, NLP_VALUE_POPULATE, NlpValueFull); 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. * Emits an event after an NLP value is updated, bypassing built-in values.
* *

View File

@ -69,21 +69,25 @@ export class NlpService {
* *
* @param entity - The NLP entity to be created. * @param entity - The NLP entity to be created.
*/ */
@OnEvent('hook:nlpEntity:create') @OnEvent('hook:nlpEntity:postCreate')
async handleEntityCreate(entity: NlpEntityDocument) { async handleEntityPostCreate(created: NlpEntityDocument) {
// Synchonize new entity with NLP if (!created.builtin) {
try { // Synchonize new entity with NLP
const helper = await this.helperService.getDefaultHelper(HelperType.NLU); try {
const foreignId = await helper.addEntity(entity); const helper = await this.helperService.getDefaultHelper(
this.logger.debug('New entity successfully synced!', foreignId); HelperType.NLU,
await this.nlpEntityService.updateOne( );
{ _id: entity._id }, const foreignId = await helper.addEntity(created);
{ this.logger.debug('New entity successfully synced!', foreignId);
foreign_id: foreignId, await this.nlpEntityService.updateOne(
}, { _id: created._id },
); {
} catch (err) { foreign_id: foreignId,
this.logger.error('Unable to sync a new entity', err); },
);
} catch (err) {
this.logger.error('Unable to sync a new entity', err);
}
} }
} }
@ -145,21 +149,23 @@ export class NlpService {
* *
* @param value - The NLP value to be created. * @param value - The NLP value to be created.
*/ */
@OnEvent('hook:nlpValue:create') @OnEvent('hook:nlpValue:postCreate')
async handleValueCreate(value: NlpValueDocument) { async handleValuePostCreate(created: NlpValueDocument) {
// Synchonize new value with NLP provider if (!created.builtin) {
try { // Synchonize new value with NLP provider
const helper = await this.helperService.getDefaultNluHelper(); try {
const foreignId = await helper.addValue(value); const helper = await this.helperService.getDefaultNluHelper();
this.logger.debug('New value successfully synced!', foreignId); const foreignId = await helper.addValue(created);
await this.nlpValueService.updateOne( this.logger.debug('New value successfully synced!', foreignId);
{ _id: value._id }, await this.nlpValueService.updateOne(
{ { _id: created._id },
foreign_id: foreignId, {
}, foreign_id: foreignId,
); },
} catch (err) { );
this.logger.error('Unable to sync a new value', err); } catch (err) {
this.logger.error('Unable to sync a new value', err);
}
} }
} }