feat: centrelize eventEmitter

This commit is contained in:
yassinedorbozgithub 2025-03-23 16:37:40 +01:00
parent ac770154f5
commit 80f7fdf8f5
36 changed files with 105 additions and 202 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@ -17,11 +16,8 @@ import { BotStats, BotStatsType } from '../schemas/bot-stats.schema';
@Injectable()
export class BotStatsRepository extends BaseRepository<BotStats> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(BotStats.name) readonly model: Model<BotStats>,
) {
super(eventEmitter, model, BotStats);
constructor(@InjectModel(BotStats.name) readonly model: Model<BotStats>) {
super(model, BotStats);
}
/**

View File

@ -7,7 +7,7 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';
import { OnEvent } from '@nestjs/event-emitter';
import { Subscriber } from '@/chat/schemas/subscriber.schema';
import { config } from '@/config';
@ -21,7 +21,6 @@ import { BotStats, BotStatsType } from '../schemas/bot-stats.schema';
export class BotStatsService extends BaseService<BotStats> {
constructor(
readonly repository: BotStatsRepository,
private readonly eventEmitter: EventEmitter2,
private readonly logger: LoggerService,
) {
super(repository);

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@ -17,10 +16,7 @@ import { Attachment } from '../schemas/attachment.schema';
@Injectable()
export class AttachmentRepository extends BaseRepository<Attachment, never> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Attachment.name) readonly model: Model<Attachment>,
) {
super(eventEmitter, model, Attachment);
constructor(@InjectModel(Attachment.name) readonly model: Model<Attachment>) {
super(model, Attachment);
}
}

View File

@ -18,7 +18,6 @@ import {
Req,
UseInterceptors,
} from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { CsrfCheck } from '@tekuconcept/nestjs-csrf';
import { Request } from 'express'; // Import the Express request and response types
@ -66,7 +65,6 @@ export class MessageController extends BaseController<
private readonly subscriberService: SubscriberService,
private readonly channelService: ChannelService,
private readonly logger: LoggerService,
private readonly eventEmitter: EventEmitter2,
) {
super(messageService);
}

View File

@ -7,7 +7,6 @@
*/
import { Injectable, Optional } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import {
Document,
@ -38,11 +37,10 @@ export class BlockRepository extends BaseRepository<
BlockDto
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Block.name) readonly model: Model<Block>,
@Optional() private readonly logger?: LoggerService,
) {
super(eventEmitter, model, Block, BLOCK_POPULATE, BlockFull);
super(model, Block, BLOCK_POPULATE, BlockFull);
}
/**

View File

@ -7,7 +7,6 @@
*/
import { ForbiddenException, Injectable, Optional } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Document, Model, Query } from 'mongoose';
@ -28,11 +27,10 @@ export class CategoryRepository extends BaseRepository<
private readonly blockService: BlockService;
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Category.name) readonly model: Model<Category>,
@Optional() blockService?: BlockService,
) {
super(eventEmitter, model, Category);
super(model, Category);
this.blockService = blockService!;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -12,7 +12,6 @@ import {
NotFoundException,
Optional,
} from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Document, Model, Query } from 'mongoose';
@ -33,11 +32,10 @@ export class ContextVarRepository extends BaseRepository<
private readonly blockService: BlockService;
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(ContextVar.name) readonly model: Model<ContextVar>,
@Optional() blockService?: BlockService,
) {
super(eventEmitter, model, ContextVar);
super(model, ContextVar);
if (blockService) this.blockService = blockService;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@ -29,16 +28,9 @@ export class ConversationRepository extends BaseRepository<
ConversationDto
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Conversation.name) readonly model: Model<Conversation>,
) {
super(
eventEmitter,
model,
Conversation,
CONVERSATION_POPULATE,
ConversationFull,
);
super(model, Conversation, CONVERSATION_POPULATE, ConversationFull);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Document, Model, Query } from 'mongoose';
@ -30,11 +29,8 @@ export class LabelRepository extends BaseRepository<
LabelFull,
LabelDto
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Label.name) readonly model: Model<Label>,
) {
super(eventEmitter, model, Label, LABEL_POPULATE, LabelFull);
constructor(@InjectModel(Label.name) readonly model: Model<Label>) {
super(model, Label, LABEL_POPULATE, LabelFull);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@ -28,12 +27,8 @@ export class MessageRepository extends BaseRepository<
MessagePopulate,
MessageFull
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Message.name) readonly model: Model<AnyMessage>,
) {
constructor(@InjectModel(Message.name) readonly model: Model<AnyMessage>) {
super(
eventEmitter,
model,
Message as new () => AnyMessage,
MESSAGE_POPULATE,

View File

@ -6,7 +6,6 @@
* 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 { Model } from 'mongoose';
@ -51,7 +50,6 @@ describe('SubscriberRepository', () => {
let allSubscribers: Subscriber[];
let allAttachments: Attachment[];
let subscribersWithPopulatedFields: SubscriberFull[];
let eventEmitter: EventEmitter2;
beforeAll(async () => {
const { getMocks } = await buildTestingMocks({
@ -78,14 +76,12 @@ describe('SubscriberRepository', () => {
userRepository,
attachmentRepository,
subscriberModel,
eventEmitter,
] = await getMocks([
SubscriberRepository,
LabelRepository,
UserRepository,
AttachmentRepository,
getModelToken(Subscriber.name),
EventEmitter2,
]);
allLabels = await labelRepository.findAll();
@ -174,16 +170,16 @@ describe('SubscriberRepository', () => {
jest
.spyOn(subscriberRepository, 'findOne')
.mockResolvedValue(oldSubscriber);
jest.spyOn(eventEmitter, 'emit');
jest.spyOn(subscriberRepository.eventEmitter, 'emit');
await subscriberRepository.updateOne(oldSubscriber.id, updates);
expect(eventEmitter.emit).toHaveBeenCalledWith(
expect(subscriberRepository.eventEmitter.emit).toHaveBeenCalledWith(
'hook:subscriber:assign',
expect.anything(),
expect.anything(),
);
expect(eventEmitter.emit).toHaveBeenCalledWith(
expect(subscriberRepository.eventEmitter.emit).toHaveBeenCalledWith(
'hook:analytics:passation',
expect.anything(),
true, // Because assignedTo has changed
@ -201,16 +197,16 @@ describe('SubscriberRepository', () => {
jest
.spyOn(subscriberRepository, 'findOne')
.mockResolvedValue(oldSubscriber);
jest.spyOn(eventEmitter, 'emit');
jest.spyOn(subscriberRepository.eventEmitter, 'emit');
await subscriberRepository.updateOne(oldSubscriber.id, updates);
expect(eventEmitter.emit).not.toHaveBeenCalledWith(
expect(subscriberRepository.eventEmitter.emit).not.toHaveBeenCalledWith(
'hook:subscriber:assign',
expect.anything(),
expect.anything(),
);
expect(eventEmitter.emit).not.toHaveBeenCalledWith(
expect(subscriberRepository.eventEmitter.emit).not.toHaveBeenCalledWith(
'hook:analytics:passation',
expect.anything(),
expect.anything(),

View File

@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import {
Document,
@ -36,11 +35,8 @@ export class SubscriberRepository extends BaseRepository<
SubscriberFull,
SubscriberDto
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Subscriber.name) readonly model: Model<Subscriber>,
) {
super(eventEmitter, model, Subscriber, SUBSCRIBER_POPULATE, SubscriberFull);
constructor(@InjectModel(Subscriber.name) readonly model: Model<Subscriber>) {
super(model, Subscriber, SUBSCRIBER_POPULATE, SubscriberFull);
}
/**

View File

@ -10,7 +10,6 @@ import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import Handlebars from 'handlebars';
import { AttachmentService } from '@/attachment/services/attachment.service';
import EventWrapper from '@/channel/lib/EventWrapper';
import { ChannelName } from '@/channel/types';
import { ContentService } from '@/cms/services/content.service';
@ -51,7 +50,6 @@ export class BlockService extends BaseService<
constructor(
readonly repository: BlockRepository,
private readonly contentService: ContentService,
private readonly attachmentService: AttachmentService,
private readonly settingService: SettingService,
private readonly pluginService: PluginService,
private readonly logger: LoggerService,

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { ForbiddenException, Injectable, Optional } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Document, Model, Query } from 'mongoose';
@ -27,12 +26,11 @@ export class ContentTypeRepository extends BaseRepository<
ContentTypeDto
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(ContentType.name) readonly model: Model<ContentType>,
@InjectModel(Content.name) private readonly contentModel: Model<Content>,
@Optional() private readonly blockService?: BlockService,
) {
super(eventEmitter, model, ContentType);
super(model, ContentType);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import {
Document,
@ -36,11 +35,8 @@ export class ContentRepository extends BaseRepository<
ContentFull,
ContentDto
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Content.name) readonly model: Model<Content>,
) {
super(eventEmitter, model, Content, CONTENT_POPULATE, ContentFull);
constructor(@InjectModel(Content.name) readonly model: Model<Content>) {
super(model, Content, CONTENT_POPULATE, ContentFull);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@ -30,11 +29,8 @@ export class MenuRepository extends BaseRepository<
MenuFull,
MenuDto
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Menu.name) readonly model: Model<Menu>,
) {
super(eventEmitter, model, Menu, MENU_POPULATE, MenuFull);
constructor(@InjectModel(Menu.name) readonly model: Model<Menu>) {
super(model, Menu, MENU_POPULATE, MenuFull);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -24,7 +24,6 @@ import { CsrfCheck } from '@tekuconcept/nestjs-csrf';
import { CsrfInterceptor } from '@/interceptors/csrf.interceptor';
import { LoggerService } from '@/logger/logger.service';
import { SettingService } from '@/setting/services/setting.service';
import { BaseController } from '@/utils/generics/base-controller';
import { DeleteResult } from '@/utils/generics/base-repository';
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
@ -43,7 +42,6 @@ export class TranslationController extends BaseController<Translation> {
constructor(
private readonly languageService: LanguageService,
private readonly translationService: TranslationService,
private readonly settingService: SettingService,
private readonly logger: LoggerService,
) {
super(translationService);

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Document, Model, Query } from 'mongoose';
@ -24,11 +23,8 @@ export class LanguageRepository extends BaseRepository<
never,
LanguageDto
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Language.name) readonly model: Model<Language>,
) {
super(eventEmitter, model, Language);
constructor(@InjectModel(Language.name) readonly model: Model<Language>) {
super(model, Language);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@ -18,9 +17,8 @@ import { Translation } from '../../i18n/schemas/translation.schema';
@Injectable()
export class TranslationRepository extends BaseRepository<Translation> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Translation.name) readonly model: Model<Translation>,
) {
super(eventEmitter, model, Translation);
super(model, Translation);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Document, Model, Query } from 'mongoose';
@ -34,12 +33,11 @@ export class NlpEntityRepository extends BaseRepository<
NlpEntityDto
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(NlpEntity.name) readonly model: Model<NlpEntity>,
private readonly nlpValueRepository: NlpValueRepository,
private readonly nlpSampleEntityRepository: NlpSampleEntityRepository,
) {
super(eventEmitter, model, NlpEntity, NLP_ENTITY_POPULATE, NlpEntityFull);
super(model, NlpEntity, NLP_ENTITY_POPULATE, NlpEntityFull);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@ -27,11 +26,9 @@ export class NlpSampleEntityRepository extends BaseRepository<
NlpSampleEntityFull
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(NlpSampleEntity.name) readonly model: Model<NlpSampleEntity>,
) {
super(
eventEmitter,
model,
NlpSampleEntity,
NLP_SAMPLE_ENTITY_POPULATE,

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Document, Model, Query } from 'mongoose';
@ -32,11 +31,10 @@ export class NlpSampleRepository extends BaseRepository<
TNlpSampleDto
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(NlpSample.name) readonly model: Model<NlpSample>,
private readonly nlpSampleEntityRepository: NlpSampleEntityRepository,
) {
super(eventEmitter, model, NlpSample, NLP_SAMPLE_POPULATE, NlpSampleFull);
super(model, NlpSample, NLP_SAMPLE_POPULATE, NlpSampleFull);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Document, Model, Query } from 'mongoose';
@ -33,11 +32,10 @@ export class NlpValueRepository extends BaseRepository<
NlpValueDto
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(NlpValue.name) readonly model: Model<NlpValue>,
private readonly nlpSampleEntityRepository: NlpSampleEntityRepository,
) {
super(eventEmitter, model, NlpValue, NLP_VALUE_POPULATE, NlpValueFull);
super(model, NlpValue, NLP_VALUE_POPULATE, NlpValueFull);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -18,7 +18,7 @@ import {
import { CsrfCheck } from '@tekuconcept/nestjs-csrf';
import { CsrfInterceptor } from '@/interceptors/csrf.interceptor';
import { LoggerService } from '@/logger/logger.service';
import { BaseController } from '@/utils/generics/base-controller';
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
import { PageQueryPipe } from '@/utils/pagination/pagination-query.pipe';
import { SearchFilterPipe } from '@/utils/pipes/search-filter.pipe';
@ -29,11 +29,10 @@ import { SettingService } from '../services/setting.service';
@UseInterceptors(CsrfInterceptor)
@Controller('setting')
export class SettingController {
constructor(
private readonly settingService: SettingService,
private readonly logger: LoggerService,
) {}
export class SettingController extends BaseController<Setting> {
constructor(private readonly settingService: SettingService) {
super(settingService);
}
/**
* Finds settings that match the provided filters and sorting options.

View File

@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@ -17,10 +16,7 @@ import { Metadata } from '../schemas/metadata.schema';
@Injectable()
export class MetadataRepository extends BaseRepository<Metadata> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Metadata.name) readonly model: Model<Metadata>,
) {
super(eventEmitter, model, Metadata);
constructor(@InjectModel(Metadata.name) readonly model: Model<Metadata>) {
super(model, Metadata);
}
}

View File

@ -6,7 +6,6 @@
* 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 { getModelToken, MongooseModule } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@ -25,7 +24,6 @@ import { SettingRepository } from './setting.repository';
describe('SettingRepository', () => {
let settingRepository: SettingRepository;
let settingModel: Model<Setting>;
let eventEmitter: EventEmitter2;
beforeAll(async () => {
const { getMocks } = await buildTestingMocks({
@ -35,10 +33,9 @@ describe('SettingRepository', () => {
],
providers: [SettingRepository],
});
[settingRepository, settingModel, eventEmitter] = await getMocks([
[settingRepository, settingModel] = await getMocks([
SettingRepository,
getModelToken(Setting.name),
EventEmitter2,
]);
});
@ -102,11 +99,11 @@ describe('SettingRepository', () => {
label: 'theme',
});
jest.spyOn(eventEmitter, 'emit');
jest.spyOn(settingRepository.eventEmitter, 'emit');
await settingRepository.postUpdate({} as any, mockSetting);
expect(eventEmitter.emit).toHaveBeenCalledWith(
expect(settingRepository.eventEmitter.emit).toHaveBeenCalledWith(
'hook:general:theme',
mockSetting,
);

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,10 +7,7 @@
*/
import { Injectable } from '@nestjs/common';
import {
EventEmitter2,
IHookSettingsGroupLabelOperationMap,
} from '@nestjs/event-emitter';
import { IHookSettingsGroupLabelOperationMap } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import {
Document,
@ -29,11 +26,8 @@ import { SettingType } from '../schemas/types';
@Injectable()
export class SettingRepository extends BaseRepository<Setting> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Setting.name) readonly model: Model<Setting>,
) {
super(eventEmitter, model, Setting);
constructor(@InjectModel(Setting.name) readonly model: Model<Setting>) {
super(model, Setting);
}
async preCreateValidate(

View File

@ -1,12 +1,11 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 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 { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@ -27,11 +26,8 @@ export class InvitationRepository extends BaseRepository<
InvitationPopulate,
InvitationFull
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Invitation.name) readonly model: Model<Invitation>,
) {
super(eventEmitter, model, Invitation, INVITATION_POPULATE, InvitationFull);
constructor(@InjectModel(Invitation.name) readonly model: Model<Invitation>) {
super(model, Invitation, INVITATION_POPULATE, InvitationFull);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model as MongooseModel } from 'mongoose';
@ -28,12 +27,11 @@ export class ModelRepository extends BaseRepository<
ModelFull
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Model.name) readonly model: MongooseModel<Model>,
@InjectModel(Permission.name)
private readonly permissionModel: MongooseModel<Permission>,
) {
super(eventEmitter, model, Model, MODEL_POPULATE, ModelFull);
super(model, Model, MODEL_POPULATE, ModelFull);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@ -28,10 +27,7 @@ export class PermissionRepository extends BaseRepository<
PermissionFull,
PermissionDto
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Permission.name) readonly model: Model<Permission>,
) {
super(eventEmitter, model, Permission, PERMISSION_POPULATE, PermissionFull);
constructor(@InjectModel(Permission.name) readonly model: Model<Permission>) {
super(model, Permission, PERMISSION_POPULATE, PermissionFull);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@ -31,14 +30,13 @@ export class RoleRepository extends BaseRepository<
RoleDto
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Role.name) readonly model: Model<Role>,
@InjectModel(Permission.name)
private readonly permissionModel: Model<Permission>,
@InjectModel(Invitation.name)
private readonly invitationModel: Model<Invitation>,
) {
super(eventEmitter, model, Role, ROLE_POPULATE, RoleFull);
super(model, Role, ROLE_POPULATE, RoleFull);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import {
Document,
@ -37,11 +36,8 @@ export class UserRepository extends BaseRepository<
UserFull,
UserDto
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(User.name) readonly model: Model<User>,
) {
super(eventEmitter, model, User, USER_POPULATE, UserFull);
constructor(@InjectModel(User.name) readonly model: Model<User>) {
super(model, User, USER_POPULATE, UserFull);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -23,7 +23,11 @@ export abstract class BaseController<
TFull extends Omit<T, P> = never,
Dto extends DtoConfig = object,
> {
constructor(protected readonly service: BaseService<T, P, TFull, Dto>) {}
eventEmitter: typeof this.service.eventEmitter;
constructor(protected readonly service: BaseService<T, P, TFull, Dto>) {
this.eventEmitter = service.eventEmitter;
}
/**
* Checks if the given populate fields are allowed based on the allowed fields list.

View File

@ -79,13 +79,15 @@ export abstract class BaseRepository<
private readonly leanOpts = { virtuals: true, defaults: true, getters: true };
eventEmitter: EventEmitter2;
constructor(
private readonly emitter: EventEmitter2,
readonly model: Model<T>,
private readonly cls: new () => T,
protected readonly populate: P[] = [],
protected readonly clsPopulate?: new () => TFull,
) {
this.eventEmitter = new EventEmitter2();
this.registerLifeCycleHooks();
}
@ -112,7 +114,7 @@ export abstract class BaseRepository<
hooks.validate.pre.execute(async function () {
const doc = this as HydratedDocument<T>;
await repository.preCreateValidate(doc);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.preCreateValidate),
doc,
);
@ -120,7 +122,7 @@ export abstract class BaseRepository<
hooks.validate.post.execute(async function (created: HydratedDocument<T>) {
await repository.postCreateValidate(created);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.postCreateValidate),
created,
);
@ -129,7 +131,7 @@ export abstract class BaseRepository<
hooks.save.pre.execute(async function () {
const doc = this as HydratedDocument<T>;
await repository.preCreate(doc);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.preCreate),
doc,
);
@ -137,7 +139,7 @@ export abstract class BaseRepository<
hooks.save.post.execute(async function (created: HydratedDocument<T>) {
await repository.postCreate(created);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.postCreate),
created,
);
@ -147,7 +149,7 @@ export abstract class BaseRepository<
const query = this as Query<DeleteResult, D, unknown, T, 'deleteOne'>;
const criteria = query.getQuery();
await repository.preDelete(query, criteria);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.preDelete),
query,
criteria,
@ -157,7 +159,7 @@ export abstract class BaseRepository<
hooks?.deleteOne.post.execute(async function (result: DeleteResult) {
const query = this as Query<DeleteResult, D, unknown, T, 'deleteOne'>;
await repository.postDelete(query, result);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.postDelete),
query,
result,
@ -171,7 +173,7 @@ export abstract class BaseRepository<
});
hooks.deleteMany.post.execute(async function (result: DeleteResult) {
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.postDelete),
result,
);
@ -187,7 +189,7 @@ export abstract class BaseRepository<
throw new Error('Unable to run findOneAndUpdate pre hook');
}
await repository.preUpdate(query, criteria, updates);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.preUpdate),
criteria,
updates?.['$set'],
@ -202,7 +204,7 @@ export abstract class BaseRepository<
throw new Error('Unable to execute updateMany() pre-hook');
}
await repository.preUpdateMany(query, criteria, updates);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.preUpdateMany),
criteria,
updates?.['$set'],
@ -212,7 +214,7 @@ export abstract class BaseRepository<
hooks.updateMany.post.execute(async function (updated: any) {
const query = this as Query<D, D, unknown, T, 'updateMany'>;
await repository.postUpdateMany(query, updated);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.postUpdateMany),
updated,
);
@ -227,7 +229,7 @@ export abstract class BaseRepository<
query,
plainToClass(repository.cls, updated, repository.transformOpts),
);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.postUpdate),
updated,
);
@ -503,14 +505,14 @@ export abstract class BaseRepository<
}
await this.preUpdateValidate(filterCriteria, queryUpdates);
await this.emitter.emitAsync(
await this.eventEmitter.emitAsync(
this.getEventName(EHook.preUpdateValidate),
filterCriteria,
queryUpdates,
);
await this.postUpdateValidate(filterCriteria, queryUpdates);
await this.emitter.emitAsync(
await this.eventEmitter.emitAsync(
this.getEventName(EHook.postUpdateValidate),
filterCriteria,
queryUpdates,

View File

@ -26,9 +26,11 @@ export abstract class BaseService<
Dto extends DtoConfig = object,
U extends Omit<T, keyof BaseSchema> = Omit<T, keyof BaseSchema>,
> {
constructor(
protected readonly repository: BaseRepository<T, P, TFull, Dto>,
) {}
eventEmitter: typeof this.repository.eventEmitter;
constructor(protected readonly repository: BaseRepository<T, P, TFull, Dto>) {
this.eventEmitter = repository.eventEmitter;
}
getRepository() {
return this.repository;

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@ -17,10 +16,7 @@ import { Dummy } from '../schemas/dummy.schema';
@Injectable()
export class DummyRepository extends BaseRepository<Dummy> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Dummy.name) readonly model: Model<Dummy>,
) {
super(eventEmitter, model, Dummy);
constructor(@InjectModel(Dummy.name) readonly model: Model<Dummy>) {
super(model, Dummy);
}
}