mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
Merge pull request #155 from Hexastack/150-issue-optimize-api-emitted-events
refactor(api): emit events logic
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
import { NotFoundException } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { Test } from '@nestjs/testing';
|
||||
|
||||
@@ -44,7 +45,12 @@ describe('ContextVarController', () => {
|
||||
rootMongooseTestModule(installContextVarFixtures),
|
||||
MongooseModule.forFeature([ContextVarModel]),
|
||||
],
|
||||
providers: [LoggerService, ContextVarService, ContextVarRepository],
|
||||
providers: [
|
||||
LoggerService,
|
||||
ContextVarService,
|
||||
ContextVarRepository,
|
||||
EventEmitter2,
|
||||
],
|
||||
}).compile();
|
||||
contextVarController =
|
||||
module.get<ContextVarController>(ContextVarController);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||
*/
|
||||
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { MongooseModule, getModelToken } from '@nestjs/mongoose';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { Model } from 'mongoose';
|
||||
@@ -38,7 +39,7 @@ describe('BlockRepository', () => {
|
||||
rootMongooseTestModule(installBlockFixtures),
|
||||
MongooseModule.forFeature([BlockModel, CategoryModel, LabelModel]),
|
||||
],
|
||||
providers: [BlockRepository, CategoryRepository],
|
||||
providers: [BlockRepository, CategoryRepository, EventEmitter2],
|
||||
}).compile();
|
||||
blockRepository = module.get<BlockRepository>(BlockRepository);
|
||||
categoryRepository = module.get<CategoryRepository>(CategoryRepository);
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
import { Injectable, Optional } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import {
|
||||
Document,
|
||||
@@ -36,10 +37,11 @@ export class BlockRepository extends BaseRepository<
|
||||
BlockFull
|
||||
> {
|
||||
constructor(
|
||||
readonly eventEmitter: EventEmitter2,
|
||||
@InjectModel(Block.name) readonly model: Model<Block>,
|
||||
@Optional() private readonly logger?: LoggerService,
|
||||
) {
|
||||
super(model, Block, BLOCK_POPULATE, BlockFull);
|
||||
super(eventEmitter, model, Block, BLOCK_POPULATE, BlockFull);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
import { ForbiddenException, Injectable, Optional } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Document, Model, Query, TFilterQuery } from 'mongoose';
|
||||
|
||||
@@ -20,10 +21,11 @@ export class CategoryRepository extends BaseRepository<Category> {
|
||||
private readonly blockService: BlockService;
|
||||
|
||||
constructor(
|
||||
readonly eventEmitter: EventEmitter2,
|
||||
@InjectModel(Category.name) readonly model: Model<Category>,
|
||||
@Optional() blockService?: BlockService,
|
||||
) {
|
||||
super(model, Category);
|
||||
super(eventEmitter, model, Category);
|
||||
this.blockService = blockService;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model } from 'mongoose';
|
||||
|
||||
@@ -16,7 +17,10 @@ import { ContextVar } from '../schemas/context-var.schema';
|
||||
|
||||
@Injectable()
|
||||
export class ContextVarRepository extends BaseRepository<ContextVar> {
|
||||
constructor(@InjectModel(ContextVar.name) readonly model: Model<ContextVar>) {
|
||||
super(model, ContextVar);
|
||||
constructor(
|
||||
readonly eventEmitter: EventEmitter2,
|
||||
@InjectModel(ContextVar.name) readonly model: Model<ContextVar>,
|
||||
) {
|
||||
super(eventEmitter, model, ContextVar);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ import { BaseRepository } from '@/utils/generics/base-repository';
|
||||
import {
|
||||
Conversation,
|
||||
CONVERSATION_POPULATE,
|
||||
ConversationDocument,
|
||||
ConversationFull,
|
||||
ConversationPopulate,
|
||||
} from '../schemas/conversation.schema';
|
||||
@@ -28,20 +27,16 @@ export class ConversationRepository extends BaseRepository<
|
||||
ConversationFull
|
||||
> {
|
||||
constructor(
|
||||
readonly eventEmitter: EventEmitter2,
|
||||
@InjectModel(Conversation.name) readonly model: Model<Conversation>,
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
) {
|
||||
super(model, Conversation, CONVERSATION_POPULATE, ConversationFull);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after a new conversation is created. This method emits the event
|
||||
* with the newly created conversation document.
|
||||
*
|
||||
* @param created - The newly created conversation document.
|
||||
*/
|
||||
async postCreate(created: ConversationDocument): Promise<void> {
|
||||
this.eventEmitter.emit('hook:chatbot:conversation:start', created);
|
||||
super(
|
||||
eventEmitter,
|
||||
model,
|
||||
Conversation,
|
||||
CONVERSATION_POPULATE,
|
||||
ConversationFull,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -28,10 +28,10 @@ export class LabelRepository extends BaseRepository<
|
||||
LabelFull
|
||||
> {
|
||||
constructor(
|
||||
readonly eventEmitter: EventEmitter2,
|
||||
@InjectModel(Label.name) readonly model: Model<Label>,
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
) {
|
||||
super(model, Label, LABEL_POPULATE, LabelFull);
|
||||
super(eventEmitter, model, Label, LABEL_POPULATE, LabelFull);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
import { Injectable, Optional } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model } from 'mongoose';
|
||||
|
||||
@@ -39,12 +40,14 @@ export class MessageRepository extends BaseRepository<
|
||||
private readonly languageService: LanguageService;
|
||||
|
||||
constructor(
|
||||
readonly eventEmitter: EventEmitter2,
|
||||
@InjectModel(Message.name) readonly model: Model<AnyMessage>,
|
||||
@Optional() nlpSampleService?: NlpSampleService,
|
||||
@Optional() logger?: LoggerService,
|
||||
@Optional() languageService?: LanguageService,
|
||||
) {
|
||||
super(
|
||||
eventEmitter,
|
||||
model,
|
||||
Message as new () => AnyMessage,
|
||||
MESSAGE_POPULATE,
|
||||
|
||||
@@ -36,10 +36,10 @@ export class SubscriberRepository extends BaseRepository<
|
||||
SubscriberFull
|
||||
> {
|
||||
constructor(
|
||||
readonly eventEmitter: EventEmitter2,
|
||||
@InjectModel(Subscriber.name) readonly model: Model<Subscriber>,
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
) {
|
||||
super(model, Subscriber, SUBSCRIBER_POPULATE, SubscriberFull);
|
||||
super(eventEmitter, model, Subscriber, SUBSCRIBER_POPULATE, SubscriberFull);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,7 +54,6 @@ export class SubscriberRepository extends BaseRepository<
|
||||
'New users',
|
||||
created,
|
||||
);
|
||||
this.eventEmitter.emit('hook:subscriber:create', created);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,12 +79,6 @@ export class SubscriberRepository extends BaseRepository<
|
||||
): Promise<void> {
|
||||
const subscriberUpdates: SubscriberUpdateDto = updates?.['$set'];
|
||||
|
||||
this.eventEmitter.emit(
|
||||
'hook:subscriber:update:before',
|
||||
criteria,
|
||||
subscriberUpdates,
|
||||
);
|
||||
|
||||
const oldSubscriber = await this.findOne(criteria);
|
||||
|
||||
if (subscriberUpdates.assignedTo !== oldSubscriber?.assignedTo) {
|
||||
@@ -105,26 +98,6 @@ export class SubscriberRepository extends BaseRepository<
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits an event after successfully updating a subscriber.
|
||||
* Triggers the event with the updated subscriber data.
|
||||
*
|
||||
* @param _query - The Mongoose query object for finding and updating a subscriber.
|
||||
* @param updated - The updated subscriber entity.
|
||||
*/
|
||||
async postUpdate(
|
||||
_query: Query<
|
||||
Document<Subscriber, any, any>,
|
||||
Document<Subscriber, any, any>,
|
||||
unknown,
|
||||
Subscriber,
|
||||
'findOneAndUpdate'
|
||||
>,
|
||||
updated: Subscriber,
|
||||
) {
|
||||
this.eventEmitter.emit('hook:subscriber:update:after', updated);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a query to find a subscriber by their foreign ID.
|
||||
*
|
||||
|
||||
@@ -287,7 +287,7 @@ export class ChatService {
|
||||
*
|
||||
* @param subscriber - The end user (subscriber)
|
||||
*/
|
||||
@OnEvent('hook:subscriber:create')
|
||||
@OnEvent('hook:subscriber:postCreate')
|
||||
onSubscriberCreate(subscriber: Subscriber) {
|
||||
this.websocketGateway.broadcastSubscriberNew(subscriber);
|
||||
}
|
||||
@@ -297,7 +297,7 @@ export class ChatService {
|
||||
*
|
||||
* @param subscriber - The end user (subscriber)
|
||||
*/
|
||||
@OnEvent('hook:subscriber:update:after')
|
||||
@OnEvent('hook:subscriber:postUpdate')
|
||||
onSubscriberUpdate(subscriber: Subscriber) {
|
||||
this.websocketGateway.broadcastSubscriberUpdate(subscriber);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user