fix(api): baseRepository constructor

This commit is contained in:
yassinedorbozgithub 2024-10-05 12:15:50 +01:00
parent 90256a350b
commit be1787ec48
27 changed files with 105 additions and 55 deletions

View File

@ -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,8 +17,11 @@ import { BotStats, BotStatsType } from '../schemas/bot-stats.schema';
@Injectable()
export class BotStatsRepository extends BaseRepository<BotStats> {
constructor(@InjectModel(BotStats.name) readonly model: Model<BotStats>) {
super(model, BotStats);
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(BotStats.name) readonly model: Model<BotStats>,
) {
super(eventEmitter, model, BotStats);
}
/**

View File

@ -18,10 +18,9 @@ import { Attachment } from '../schemas/attachment.schema';
@Injectable()
export class AttachmentRepository extends BaseRepository<Attachment, never> {
constructor(
@InjectModel(Attachment.name) readonly model: Model<Attachment>,
readonly eventEmitter: EventEmitter2,
@InjectModel(Attachment.name) readonly model: Model<Attachment>,
) {
super(model, Attachment);
super.setEventEmitter(eventEmitter);
super(eventEmitter, model, Attachment);
}
}

View File

@ -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);
}
/**

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -27,11 +27,16 @@ export class ConversationRepository extends BaseRepository<
ConversationFull
> {
constructor(
@InjectModel(Conversation.name) readonly model: Model<Conversation>,
readonly eventEmitter: EventEmitter2,
@InjectModel(Conversation.name) readonly model: Model<Conversation>,
) {
super(model, Conversation, CONVERSATION_POPULATE, ConversationFull);
super.setEventEmitter(eventEmitter);
super(
eventEmitter,
model,
Conversation,
CONVERSATION_POPULATE,
ConversationFull,
);
}
/**

View File

@ -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);
}
/**

View File

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

View File

@ -24,6 +24,7 @@ import { SubscriberUpdateDto } from '../dto/subscriber.dto';
import {
Subscriber,
SUBSCRIBER_POPULATE,
SubscriberDocument,
SubscriberFull,
SubscriberPopulate,
} from '../schemas/subscriber.schema';
@ -35,11 +36,24 @@ export class SubscriberRepository extends BaseRepository<
SubscriberFull
> {
constructor(
@InjectModel(Subscriber.name) readonly model: Model<Subscriber>,
readonly eventEmitter: EventEmitter2,
@InjectModel(Subscriber.name) readonly model: Model<Subscriber>,
) {
super(model, Subscriber, SUBSCRIBER_POPULATE, SubscriberFull);
super.setEventEmitter(eventEmitter);
super(eventEmitter, model, Subscriber, SUBSCRIBER_POPULATE, SubscriberFull);
}
/**
* Emits events related to the creation of a new subscriber.
*
* @param created - The newly created subscriber document.
*/
async postCreate(created: SubscriberDocument): Promise<void> {
this.eventEmitter.emit(
'hook:stats:entry',
'new_users',
'New users',
created,
);
}
/**

View File

@ -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';
@ -19,11 +20,12 @@ import { Content } from '../schemas/content.schema';
@Injectable()
export class ContentTypeRepository extends BaseRepository<ContentType> {
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(model, ContentType);
super(eventEmitter, model, ContentType);
}
/**

View File

@ -7,6 +7,7 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import {
Document,
@ -33,8 +34,11 @@ export class ContentRepository extends BaseRepository<
ContentPopulate,
ContentFull
> {
constructor(@InjectModel(Content.name) readonly model: Model<Content>) {
super(model, Content, CONTENT_POPULATE, ContentFull);
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Content.name) readonly model: Model<Content>,
) {
super(eventEmitter, model, Content, CONTENT_POPULATE, ContentFull);
}
/**

View File

@ -29,11 +29,10 @@ export class MenuRepository extends BaseRepository<
MenuFull
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Menu.name) readonly model: Model<Menu>,
private readonly eventEmitter: EventEmitter2,
) {
super(model, Menu, MENU_POPULATE, MenuFull);
super.setEventEmitter(eventEmitter);
super(eventEmitter, model, Menu, MENU_POPULATE, MenuFull);
}
/**

View File

@ -18,10 +18,10 @@ import { Language } from '../schemas/language.schema';
@Injectable()
export class LanguageRepository extends BaseRepository<Language> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Language.name) readonly model: Model<Language>,
private readonly eventEmitter: EventEmitter2,
) {
super(model, Language);
super(eventEmitter, model, Language);
}
/**

View File

@ -18,10 +18,9 @@ import { Translation } from '../../i18n/schemas/translation.schema';
@Injectable()
export class TranslationRepository extends BaseRepository<Translation> {
constructor(
@InjectModel(Translation.name) readonly model: Model<Translation>,
readonly eventEmitter: EventEmitter2,
@InjectModel(Translation.name) readonly model: Model<Translation>,
) {
super(model, Translation);
super.setEventEmitter(eventEmitter);
super(eventEmitter, model, Translation);
}
}

View File

@ -29,12 +29,12 @@ export class NlpEntityRepository extends BaseRepository<
NlpEntityFull
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(NlpEntity.name) readonly model: Model<NlpEntity>,
private readonly nlpValueRepository: NlpValueRepository,
private readonly nlpSampleEntityRepository: NlpSampleEntityRepository,
private readonly eventEmitter: EventEmitter2,
) {
super(model, NlpEntity, NLP_ENTITY_POPULATE, NlpEntityFull);
super(eventEmitter, model, NlpEntity, NLP_ENTITY_POPULATE, NlpEntityFull);
}
/**

View File

@ -7,6 +7,7 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@ -26,9 +27,11 @@ 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

@ -7,6 +7,7 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Document, Model, Query, TFilterQuery } from 'mongoose';
@ -27,10 +28,11 @@ export class NlpSampleRepository extends BaseRepository<
NlpSampleFull
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(NlpSample.name) readonly model: Model<NlpSample>,
private readonly nlpSampleEntityRepository: NlpSampleEntityRepository,
) {
super(model, NlpSample, NLP_SAMPLE_POPULATE, NlpSampleFull);
super(eventEmitter, model, NlpSample, NLP_SAMPLE_POPULATE, NlpSampleFull);
}
/**

View File

@ -29,11 +29,11 @@ export class NlpValueRepository extends BaseRepository<
NlpValueFull
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(NlpValue.name) readonly model: Model<NlpValue>,
private readonly nlpSampleEntityRepository: NlpSampleEntityRepository,
private readonly eventEmitter: EventEmitter2,
) {
super(model, NlpValue, NLP_VALUE_POPULATE, NlpValueFull);
super(eventEmitter, model, NlpValue, NLP_VALUE_POPULATE, NlpValueFull);
}
/**

View File

@ -19,11 +19,11 @@ import { Setting } from '../schemas/setting.schema';
@Injectable()
export class SettingRepository extends BaseRepository<Setting> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Setting.name) readonly model: Model<Setting>,
private readonly eventEmitter: EventEmitter2,
private readonly i18n: I18nService,
) {
super(model, Setting);
super(eventEmitter, model, Setting);
}
/**

View File

@ -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 { InjectModel } from '@nestjs/mongoose';
import { Model, TFilterQuery } from 'mongoose';
@ -25,8 +26,11 @@ export class InvitationRepository extends BaseRepository<
InvitationPopulate,
InvitationFull
> {
constructor(@InjectModel(Invitation.name) readonly model: Model<Invitation>) {
super(model, Invitation, INVITATION_POPULATE, InvitationFull);
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Invitation.name) readonly model: Model<Invitation>,
) {
super(eventEmitter, model, Invitation, INVITATION_POPULATE, InvitationFull);
}
/**

View File

@ -7,6 +7,7 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model as MongooseModel } from 'mongoose';
@ -27,11 +28,12 @@ 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(model, Model, MODEL_POPULATE, ModelFull);
super(eventEmitter, model, Model, MODEL_POPULATE, ModelFull);
}
/**

View File

@ -27,11 +27,10 @@ export class PermissionRepository extends BaseRepository<
PermissionFull
> {
constructor(
@InjectModel(Permission.name) readonly model: Model<Permission>,
readonly eventEmitter: EventEmitter2,
@InjectModel(Permission.name) readonly model: Model<Permission>,
) {
super(model, Permission, PERMISSION_POPULATE, PermissionFull);
super.setEventEmitter(eventEmitter);
super(eventEmitter, model, Permission, PERMISSION_POPULATE, PermissionFull);
}
/**

View File

@ -29,13 +29,12 @@ export class RoleRepository extends BaseRepository<
RoleFull
> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Role.name) readonly model: Model<Role>,
@InjectModel(Permission.name)
private readonly permissionModel: Model<Permission>,
readonly eventEmitter: EventEmitter2,
) {
super(model, Role, ROLE_POPULATE, RoleFull);
super.setEventEmitter(eventEmitter);
super(eventEmitter, model, Role, ROLE_POPULATE, RoleFull);
}
/**

View File

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

View File

@ -49,9 +49,8 @@ export abstract class BaseRepository<
private readonly leanOpts = { virtuals: true, defaults: true, getters: true };
private emitter: EventEmitter2;
constructor(
private readonly emitter: EventEmitter2,
readonly model: Model<T>,
private readonly cls: new () => T,
protected readonly populate: P[] = [],
@ -68,10 +67,6 @@ export abstract class BaseRepository<
return `hook:${this.cls.name.toLocaleLowerCase()}:${suffix.toLocaleLowerCase()}`;
}
setEventEmitter(eventEmitter: EventEmitter2) {
this.emitter = eventEmitter;
}
private registerLifeCycleHooks() {
const repository = this;
const hooks = LifecycleHookManager.getHooks(this.cls.name);

View File

@ -7,6 +7,7 @@
*/
import { Module } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { MongooseModule } from '@nestjs/mongoose';
import { installDummyFixtures } from '@/utils/test/fixtures/dummy';
@ -21,6 +22,6 @@ import { DummyService } from './services/dummy.service';
rootMongooseTestModule(installDummyFixtures),
MongooseModule.forFeature([DummyModel]),
],
providers: [DummyRepository, DummyService],
providers: [DummyRepository, DummyService, EventEmitter2],
})
export class DummyModule {}

View File

@ -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 { Dummy } from '../schemas/dummy.schema';
@Injectable()
export class DummyRepository extends BaseRepository<Dummy> {
constructor(@InjectModel(Dummy.name) readonly model: Model<Dummy>) {
super(model, Dummy);
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Dummy.name) readonly model: Model<Dummy>,
) {
super(eventEmitter, model, Dummy);
}
}