From a4033991c1715ccddb131360a9460c54d385ef4e Mon Sep 17 00:00:00 2001 From: yassinedorbozgithub Date: Thu, 19 Jun 2025 08:43:16 +0100 Subject: [PATCH] fix(api): make optional DI required --- .../attachment/services/attachment.service.ts | 4 +- .../chat/repositories/category.repository.ts | 6 +-- .../repositories/context-var.repository.ts | 8 +-- api/src/chat/services/message.service.ts | 11 +--- api/src/chat/services/subscriber.service.ts | 11 +--- .../repositories/content-type.repository.ts | 6 +-- api/src/user/services/invitation.service.ts | 50 +++++++++---------- .../user/services/passwordReset.service.ts | 50 +++++++++---------- .../user/services/validate-account.service.ts | 50 +++++++++---------- 9 files changed, 83 insertions(+), 113 deletions(-) diff --git a/api/src/attachment/services/attachment.service.ts b/api/src/attachment/services/attachment.service.ts index fc7cf231..10401d79 100644 --- a/api/src/attachment/services/attachment.service.ts +++ b/api/src/attachment/services/attachment.service.ts @@ -8,7 +8,7 @@ import { Readable, Stream } from 'stream'; -import { Injectable, Optional, StreamableFile } from '@nestjs/common'; +import { Injectable, StreamableFile } from '@nestjs/common'; import { HelperService } from '@/helper/helper.service'; import { HelperType } from '@/helper/types'; @@ -22,7 +22,7 @@ import { Attachment } from '../schemas/attachment.schema'; export class AttachmentService extends BaseService { constructor( readonly repository: AttachmentRepository, - @Optional() private readonly helperService: HelperService, + private readonly helperService: HelperService, ) { super(repository); } diff --git a/api/src/chat/repositories/category.repository.ts b/api/src/chat/repositories/category.repository.ts index dc628d21..392341bc 100644 --- a/api/src/chat/repositories/category.repository.ts +++ b/api/src/chat/repositories/category.repository.ts @@ -6,7 +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 { ForbiddenException, Injectable, Optional } from '@nestjs/common'; +import { ForbiddenException, Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Document, Model, Query } from 'mongoose'; @@ -26,7 +26,7 @@ export class CategoryRepository extends BaseRepository< > { constructor( @InjectModel(Category.name) readonly model: Model, - @Optional() private readonly blockService?: BlockService, + private readonly blockService: BlockService, ) { super(model, Category); } @@ -49,7 +49,7 @@ export class CategoryRepository extends BaseRepository< criteria: TFilterQuery, ) { if (criteria._id) { - const block = await this.blockService?.findOneAndPopulate({ + const block = await this.blockService.findOneAndPopulate({ category: criteria._id, }); diff --git a/api/src/chat/repositories/context-var.repository.ts b/api/src/chat/repositories/context-var.repository.ts index fe3f7e35..f96250fd 100644 --- a/api/src/chat/repositories/context-var.repository.ts +++ b/api/src/chat/repositories/context-var.repository.ts @@ -10,7 +10,6 @@ import { ForbiddenException, Injectable, NotFoundException, - Optional, } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Document, Model, Query } from 'mongoose'; @@ -29,14 +28,11 @@ export class ContextVarRepository extends BaseRepository< never, ContextVarDto > { - private readonly blockService: BlockService; - constructor( @InjectModel(ContextVar.name) readonly model: Model, - @Optional() blockService?: BlockService, + private readonly blockService: BlockService, ) { super(model, ContextVar); - if (blockService) this.blockService = blockService; } /** @@ -65,7 +61,7 @@ export class ContextVarRepository extends BaseRepository< throw new NotFoundException(`Context var with ID ${id} not found.`); } - const associatedBlocks = await this.blockService?.find({ + const associatedBlocks = await this.blockService.find({ capture_vars: { $elemMatch: { context_var: contextVar.name } }, }); diff --git a/api/src/chat/services/message.service.ts b/api/src/chat/services/message.service.ts index 5940ab94..3e6be042 100644 --- a/api/src/chat/services/message.service.ts +++ b/api/src/chat/services/message.service.ts @@ -6,11 +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 { - Injectable, - InternalServerErrorException, - Optional, -} from '@nestjs/common'; +import { Injectable, InternalServerErrorException } from '@nestjs/common'; import { BaseService } from '@/utils/generics/base-service'; import { @@ -36,14 +32,11 @@ export class MessageService extends BaseService< MessagePopulate, MessageFull > { - private readonly gateway: WebsocketGateway; - constructor( private readonly messageRepository: MessageRepository, - @Optional() gateway?: WebsocketGateway, + private readonly gateway: WebsocketGateway, ) { super(messageRepository); - if (gateway) this.gateway = gateway; } /** diff --git a/api/src/chat/services/subscriber.service.ts b/api/src/chat/services/subscriber.service.ts index 9d8457b0..686f7b6e 100644 --- a/api/src/chat/services/subscriber.service.ts +++ b/api/src/chat/services/subscriber.service.ts @@ -6,11 +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 { - Injectable, - InternalServerErrorException, - Optional, -} from '@nestjs/common'; +import { Injectable, InternalServerErrorException } from '@nestjs/common'; import { OnEvent } from '@nestjs/event-emitter'; import mime from 'mime'; import { v4 as uuidv4 } from 'uuid'; @@ -53,15 +49,12 @@ export class SubscriberService extends BaseService< SubscriberFull, SubscriberDto > { - private readonly gateway: WebsocketGateway; - constructor( readonly repository: SubscriberRepository, protected readonly attachmentService: AttachmentService, - @Optional() gateway?: WebsocketGateway, + protected readonly gateway: WebsocketGateway, ) { super(repository); - if (gateway) this.gateway = gateway; } /** diff --git a/api/src/cms/repositories/content-type.repository.ts b/api/src/cms/repositories/content-type.repository.ts index a0c2d565..67390efe 100644 --- a/api/src/cms/repositories/content-type.repository.ts +++ b/api/src/cms/repositories/content-type.repository.ts @@ -6,7 +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 { ForbiddenException, Injectable, Optional } from '@nestjs/common'; +import { ForbiddenException, Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Document, Model, Query } from 'mongoose'; @@ -28,7 +28,7 @@ export class ContentTypeRepository extends BaseRepository< constructor( @InjectModel(ContentType.name) readonly model: Model, @InjectModel(Content.name) private readonly contentModel: Model, - @Optional() private readonly blockService?: BlockService, + private readonly blockService: BlockService, ) { super(model, ContentType); } @@ -53,7 +53,7 @@ export class ContentTypeRepository extends BaseRepository< criteria: TFilterQuery, ) { if (criteria._id) { - const associatedBlock = await this.blockService?.findOne({ + const associatedBlock = await this.blockService.findOne({ 'options.content.entity': criteria._id, }); if (associatedBlock) { diff --git a/api/src/user/services/invitation.service.ts b/api/src/user/services/invitation.service.ts index b510038d..3b9f258c 100644 --- a/api/src/user/services/invitation.service.ts +++ b/api/src/user/services/invitation.service.ts @@ -12,7 +12,6 @@ import { Inject, Injectable, InternalServerErrorException, - Optional, } from '@nestjs/common'; import { JwtService, JwtSignOptions } from '@nestjs/jwt'; @@ -41,7 +40,7 @@ export class InvitationService extends BaseService< @Inject(JwtService) private readonly jwtService: JwtService, protected readonly i18n: I18nService, public readonly languageService: LanguageService, - @Optional() private readonly mailerService?: MailerService, + private readonly mailerService: MailerService, ) { super(repository); } @@ -61,31 +60,28 @@ export class InvitationService extends BaseService< */ async create(dto: InvitationCreateDto): Promise { const jwt = await this.sign({ ...dto }); - if (this.mailerService) { - try { - const defaultLanguage = await this.languageService.getDefaultLanguage(); - await this.mailerService.sendMail({ - to: dto.email, - template: 'invitation.mjml', - context: { - appName: config.parameters.appName, - appUrl: config.uiBaseUrl, - token: jwt, - // TODO: Which language should we use? - t: (key: string) => - this.i18n.t(key, { lang: defaultLanguage.code }), - }, - subject: this.i18n.t('invitation_subject'), - }); - } catch (e) { - this.logger.error( - 'Could not send email', - e.message, - e.stack, - 'InvitationService', - ); - throw new InternalServerErrorException('Could not send email'); - } + try { + const defaultLanguage = await this.languageService.getDefaultLanguage(); + await this.mailerService.sendMail({ + to: dto.email, + template: 'invitation.mjml', + context: { + appName: config.parameters.appName, + appUrl: config.uiBaseUrl, + token: jwt, + // TODO: Which language should we use? + t: (key: string) => this.i18n.t(key, { lang: defaultLanguage.code }), + }, + subject: this.i18n.t('invitation_subject'), + }); + } catch (e) { + this.logger.error( + 'Could not send email', + e.message, + e.stack, + 'InvitationService', + ); + throw new InternalServerErrorException('Could not send email'); } const newInvitation = await super.create({ ...dto, token: jwt }); return { ...newInvitation, token: jwt }; diff --git a/api/src/user/services/passwordReset.service.ts b/api/src/user/services/passwordReset.service.ts index 6081d485..8a7db8fa 100644 --- a/api/src/user/services/passwordReset.service.ts +++ b/api/src/user/services/passwordReset.service.ts @@ -14,7 +14,6 @@ import { Injectable, InternalServerErrorException, NotFoundException, - Optional, UnauthorizedException, } from '@nestjs/common'; import { JwtService, JwtSignOptions } from '@nestjs/jwt'; @@ -37,7 +36,7 @@ export class PasswordResetService { private readonly userService: UserService, public readonly i18n: I18nService, public readonly languageService: LanguageService, - @Optional() private readonly mailerService?: MailerService, + private readonly mailerService: MailerService, ) {} public readonly jwtSignOptions: JwtSignOptions = { @@ -60,31 +59,28 @@ export class PasswordResetService { } const jwt = await this.sign({ ...dto }); - if (this.mailerService) { - try { - const defaultLanguage = await this.languageService.getDefaultLanguage(); - await this.mailerService.sendMail({ - to: dto.email, - template: 'password_reset.mjml', - context: { - appName: config.parameters.appName, - appUrl: config.uiBaseUrl, - token: jwt, - first_name: user.first_name, - t: (key: string) => - this.i18n.t(key, { lang: defaultLanguage.code }), - }, - subject: this.i18n.t('password_reset_subject'), - }); - } catch (e) { - this.logger.error( - 'Could not send email', - e.message, - e.stack, - 'InvitationService', - ); - throw new InternalServerErrorException('Could not send email'); - } + try { + const defaultLanguage = await this.languageService.getDefaultLanguage(); + await this.mailerService.sendMail({ + to: dto.email, + template: 'password_reset.mjml', + context: { + appName: config.parameters.appName, + appUrl: config.uiBaseUrl, + token: jwt, + first_name: user.first_name, + t: (key: string) => this.i18n.t(key, { lang: defaultLanguage.code }), + }, + subject: this.i18n.t('password_reset_subject'), + }); + } catch (e) { + this.logger.error( + 'Could not send email', + e.message, + e.stack, + 'InvitationService', + ); + throw new InternalServerErrorException('Could not send email'); } // TODO: hash the token before saving it diff --git a/api/src/user/services/validate-account.service.ts b/api/src/user/services/validate-account.service.ts index f8fd354c..b0048182 100644 --- a/api/src/user/services/validate-account.service.ts +++ b/api/src/user/services/validate-account.service.ts @@ -12,7 +12,6 @@ import { Inject, Injectable, InternalServerErrorException, - Optional, UnauthorizedException, } from '@nestjs/common'; import { JwtService, JwtSignOptions } from '@nestjs/jwt'; @@ -40,7 +39,7 @@ export class ValidateAccountService { private logger: LoggerService, private readonly i18n: I18nService, private readonly languageService: LanguageService, - @Optional() private readonly mailerService?: MailerService, + private readonly mailerService: MailerService, ) {} /** @@ -77,31 +76,28 @@ export class ValidateAccountService { ) { const confirmationToken = await this.sign({ email: dto.email }); - if (this.mailerService) { - try { - const defaultLanguage = await this.languageService.getDefaultLanguage(); - await this.mailerService.sendMail({ - to: dto.email, - template: 'account_confirmation.mjml', - context: { - appName: config.parameters.appName, - appUrl: config.uiBaseUrl, - token: confirmationToken, - first_name: dto.first_name, - t: (key: string) => - this.i18n.t(key, { lang: defaultLanguage.code }), - }, - subject: this.i18n.t('account_confirmation_subject'), - }); - } catch (e) { - this.logger.error( - 'Could not send email', - e.message, - e.stack, - 'ValidateAccount', - ); - throw new InternalServerErrorException('Could not send email'); - } + try { + const defaultLanguage = await this.languageService.getDefaultLanguage(); + await this.mailerService.sendMail({ + to: dto.email, + template: 'account_confirmation.mjml', + context: { + appName: config.parameters.appName, + appUrl: config.uiBaseUrl, + token: confirmationToken, + first_name: dto.first_name, + t: (key: string) => this.i18n.t(key, { lang: defaultLanguage.code }), + }, + subject: this.i18n.t('account_confirmation_subject'), + }); + } catch (e) { + this.logger.error( + 'Could not send email', + e.message, + e.stack, + 'ValidateAccount', + ); + throw new InternalServerErrorException('Could not send email'); } }