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

View File

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

View File

@ -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.
*

View File

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