feat(api): rename events

This commit is contained in:
yassinedorbozgithub 2024-10-07 15:39:41 +01:00
parent 3c8deb854e
commit 14df503041
6 changed files with 18 additions and 45 deletions

View File

@ -835,7 +835,7 @@ export default class OfflineHandler extends ChannelHandler {
const type = event.getEventType(); const type = event.getEventType();
if (type) { if (type) {
this.eventEmitter.emit('hook:chatbot:' + type, event); this.eventEmitter.emit(`hook:chatbot:${type}`, event);
} else { } else {
this.logger.error( this.logger.error(
'Offline Channel Handler : Webhook received unknown event ', 'Offline Channel Handler : Webhook received unknown event ',

View File

@ -50,7 +50,7 @@ export class NlpEntityRepository extends BaseRepository<
): Promise<void> { ): Promise<void> {
if (!_created.builtin) { if (!_created.builtin) {
// Bypass builtin entities (probably fixtures) // Bypass builtin entities (probably fixtures)
this.eventEmitter.emit('hook:nlp:entity:create', _created); this.eventEmitter.emit('hook:nlpEntity:create', _created);
} }
} }
@ -74,7 +74,7 @@ export class NlpEntityRepository extends BaseRepository<
): Promise<void> { ): Promise<void> {
if (!updated?.builtin) { if (!updated?.builtin) {
// Bypass builtin entities (probably fixtures) // Bypass builtin entities (probably fixtures)
this.eventEmitter.emit('hook:nlp:entity:update', updated); this.eventEmitter.emit('hook:nlpEntity:update', updated);
} }
} }
@ -107,7 +107,7 @@ export class NlpEntityRepository extends BaseRepository<
entities entities
.filter((e) => !e.builtin) .filter((e) => !e.builtin)
.map((e) => { .map((e) => {
this.eventEmitter.emit('hook:nlp:entity:delete', e); this.eventEmitter.emit('hook:nlpEntity:delete', e);
}); });
} else { } else {
throw new Error('Attempted to delete NLP entity using unknown criteria'); throw new Error('Attempted to delete NLP entity using unknown criteria');

View File

@ -17,7 +17,6 @@ import { NlpSampleEntityRepository } from './nlp-sample-entity.repository';
import { import {
NLP_VALUE_POPULATE, NLP_VALUE_POPULATE,
NlpValue, NlpValue,
NlpValueDocument,
NlpValueFull, NlpValueFull,
NlpValuePopulate, NlpValuePopulate,
} from '../schemas/nlp-value.schema'; } from '../schemas/nlp-value.schema';
@ -41,10 +40,10 @@ export class NlpValueRepository extends BaseRepository<
* *
* @param created - The newly created NLP value document. * @param created - The newly created NLP value document.
*/ */
async postCreate(created: NlpValueDocument): Promise<void> { async postCreate(created: NlpValue): Promise<void> {
if (!created.builtin) { if (!created.builtin) {
// Bypass builtin entities (probably fixtures) // Bypass builtin entities (probably fixtures)
this.eventEmitter.emit('hook:nlp:value:create', created); this.eventEmitter.emit('hook:nlpValue:create', created);
} }
} }
@ -66,7 +65,7 @@ export class NlpValueRepository extends BaseRepository<
): Promise<void> { ): Promise<void> {
if (!updated?.builtin) { if (!updated?.builtin) {
// Bypass builtin entities (probably fixtures) // Bypass builtin entities (probably fixtures)
this.eventEmitter.emit('hook:nlp:value:update', updated); this.eventEmitter.emit('hook:nlpValue:update', updated);
} }
} }
@ -96,7 +95,7 @@ export class NlpValueRepository extends BaseRepository<
entities entities
.filter((e) => !e.builtin) .filter((e) => !e.builtin)
.map((e) => { .map((e) => {
this.eventEmitter.emit('hook:nlp:value:delete', e); this.eventEmitter.emit('hook:nlpValue:delete', e);
}); });
} else if (criteria.entity) { } else if (criteria.entity) {
// Do nothing : cascading deletes coming from Nlp Sample Entity // Do nothing : cascading deletes coming from Nlp Sample Entity

View File

@ -100,7 +100,7 @@ export class NlpService {
/** /**
* Handles the event triggered when NLP settings are updated. Re-initializes the NLP service. * Handles the event triggered when NLP settings are updated. Re-initializes the NLP service.
*/ */
@OnEvent('hook:settings:nlp_settings:*') @OnEvent('hook:nlp:settings')
async handleSettingsUpdate() { async handleSettingsUpdate() {
this.initNLP(); this.initNLP();
} }
@ -111,7 +111,7 @@ export class NlpService {
* @param entity - The NLP entity to be created. * @param entity - The NLP entity to be created.
* @returns The updated entity after synchronization. * @returns The updated entity after synchronization.
*/ */
@OnEvent('hook:nlp:entity:create') @OnEvent('hook:nlpEntity:create')
async handleEntityCreate(entity: NlpEntity) { async handleEntityCreate(entity: NlpEntity) {
// Synchonize new entity with NLP // Synchonize new entity with NLP
try { try {
@ -131,7 +131,7 @@ export class NlpService {
* *
* @param entity - The NLP entity to be updated. * @param entity - The NLP entity to be updated.
*/ */
@OnEvent('hook:nlp:entity:update') @OnEvent('hook:nlpEntity:update')
async handleEntityUpdate(entity: NlpEntity) { async handleEntityUpdate(entity: NlpEntity) {
// Synchonize new entity with NLP provider // Synchonize new entity with NLP provider
try { try {
@ -147,7 +147,7 @@ export class NlpService {
* *
* @param entity - The NLP entity to be deleted. * @param entity - The NLP entity to be deleted.
*/ */
@OnEvent('hook:nlp:entity:delete') @OnEvent('hook:nlpEntity:delete')
async handleEntityDelete(entity: NlpEntity) { async handleEntityDelete(entity: NlpEntity) {
// Synchonize new entity with NLP provider // Synchonize new entity with NLP provider
try { try {
@ -165,7 +165,7 @@ export class NlpService {
* *
* @returns The updated value after synchronization. * @returns The updated value after synchronization.
*/ */
@OnEvent('hook:nlp:value:create') @OnEvent('hook:nlpValue:create')
async handleValueCreate(value: NlpValue) { async handleValueCreate(value: NlpValue) {
// Synchonize new value with NLP provider // Synchonize new value with NLP provider
try { try {
@ -185,7 +185,7 @@ export class NlpService {
* *
* @param value - The NLP value to be updated. * @param value - The NLP value to be updated.
*/ */
@OnEvent('hook:nlp:value:update') @OnEvent('hook:nlpValue:update')
async handleValueUpdate(value: NlpValue) { async handleValueUpdate(value: NlpValue) {
// Synchonize new value with NLP provider // Synchonize new value with NLP provider
try { try {
@ -201,7 +201,7 @@ export class NlpService {
* *
* @param value - The NLP value to be deleted. * @param value - The NLP value to be deleted.
*/ */
@OnEvent('hook:nlp:value:delete') @OnEvent('hook:nlpValue:delete')
async handleValueDelete(value: NlpValue) { async handleValueDelete(value: NlpValue) {
// Synchonize new value with NLP provider // Synchonize new value with NLP provider
try { try {

View File

@ -9,7 +9,7 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter'; import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose'; import { InjectModel } from '@nestjs/mongoose';
import { Document, Model, Query, Types } from 'mongoose'; import { Document, Model, Types } from 'mongoose';
import { I18nService } from '@/i18n/services/i18n.service'; import { I18nService } from '@/i18n/services/i18n.service';
import { BaseRepository } from '@/utils/generics/base-repository'; import { BaseRepository } from '@/utils/generics/base-repository';
@ -58,30 +58,4 @@ export class SettingRepository extends BaseRepository<Setting> {
throw new Error('Setting Model : Value must be a boolean!'); throw new Error('Setting Model : Value must be a boolean!');
} }
} }
/**
* Emits an event after a `Setting` has been updated.
*
* This method is used to synchronize global settings by emitting an event
* based on the `group` and `label` of the `Setting`.
*
* @param _query The Mongoose query object used to find and update the document.
* @param setting The updated `Setting` object.
*/
async postUpdate(
_query: Query<
Document<Setting, any, any>,
Document<Setting, any, any>,
unknown,
Setting,
'findOneAndUpdate'
>,
setting: Setting,
) {
// Sync global settings var
this.eventEmitter.emit(
'hook:settings:' + setting.group + ':' + setting.label,
setting,
);
}
} }

View File

@ -107,10 +107,10 @@ export class SettingService extends BaseService<Setting> {
} }
/** /**
* Event handler for setting updates. Listens to 'hook:settings:*:*' events * Event handler for setting updates. Listens to 'hook:setting:*' events
* and invalidates the cache for settings when triggered. * and invalidates the cache for settings when triggered.
*/ */
@OnEvent('hook:settings:*:*') @OnEvent('hook:setting:*')
async handleSettingUpdateEvent() { async handleSettingUpdateEvent() {
this.cacheManager.del(SETTING_CACHE_KEY); this.cacheManager.del(SETTING_CACHE_KEY);
} }