From 7624e9486f65f00889518de91914857150379a01 Mon Sep 17 00:00:00 2001 From: Adam Chmara Date: Mon, 14 Oct 2024 15:52:22 +0200 Subject: [PATCH] refactor(api,worker): untagle modules and deps (#6673) --- .source | 2 +- .../app/rate-limiting/rate-limiting.module.ts | 3 +- .../get-api-rate-limit-maximum.spec.ts | 6 +-- .../get-api-rate-limit-maximum.usecase.ts | 4 +- apps/api/src/app/shared/shared.module.ts | 54 +++++++++++++------ apps/worker/package.json | 1 - apps/worker/src/app/shared/shared.module.ts | 19 +------ .../src/app/telemetry/telemetry.module.ts | 8 +-- .../usecases/userInfoService.usecase.ts | 8 +-- enterprise/packages/billing/package.json | 4 +- enterprise/packages/translation/package.json | 4 -- pnpm-lock.yaml | 17 ++---- 12 files changed, 59 insertions(+), 71 deletions(-) diff --git a/.source b/.source index 3be61785b93..604b3ead2dd 160000 --- a/.source +++ b/.source @@ -1 +1 @@ -Subproject commit 3be61785b93134067621bc6163c2c3d4930df41d +Subproject commit 604b3ead2dd9d2b9549920d8be7e8beba70d6a8b diff --git a/apps/api/src/app/rate-limiting/rate-limiting.module.ts b/apps/api/src/app/rate-limiting/rate-limiting.module.ts index 70a46a9f0aa..73919a1ae2a 100644 --- a/apps/api/src/app/rate-limiting/rate-limiting.module.ts +++ b/apps/api/src/app/rate-limiting/rate-limiting.module.ts @@ -1,5 +1,6 @@ import { Module } from '@nestjs/common'; import { ThrottlerModule } from '@nestjs/throttler'; +import { CommunityOrganizationRepository } from '@novu/dal'; import { USE_CASES } from './usecases'; import { SharedModule } from '../shared/shared.module'; import { ApiRateLimitInterceptor } from './guards'; @@ -15,7 +16,7 @@ import { ApiRateLimitInterceptor } from './guards'; }, ]), ], - providers: [...USE_CASES, ApiRateLimitInterceptor], + providers: [...USE_CASES, ApiRateLimitInterceptor, CommunityOrganizationRepository], exports: [...USE_CASES, ApiRateLimitInterceptor], }) export class RateLimitingModule {} diff --git a/apps/api/src/app/rate-limiting/usecases/get-api-rate-limit-maximum/get-api-rate-limit-maximum.spec.ts b/apps/api/src/app/rate-limiting/usecases/get-api-rate-limit-maximum/get-api-rate-limit-maximum.spec.ts index b72585f4d76..36c89a3fa26 100644 --- a/apps/api/src/app/rate-limiting/usecases/get-api-rate-limit-maximum/get-api-rate-limit-maximum.spec.ts +++ b/apps/api/src/app/rate-limiting/usecases/get-api-rate-limit-maximum/get-api-rate-limit-maximum.spec.ts @@ -1,4 +1,4 @@ -import { EnvironmentRepository, OrganizationRepository } from '@novu/dal'; +import { CommunityOrganizationRepository, EnvironmentRepository } from '@novu/dal'; import { UserSession } from '@novu/testing'; import { ApiRateLimitCategoryEnum, ApiServiceLevelEnum } from '@novu/shared'; import { expect } from 'chai'; @@ -27,7 +27,7 @@ const mockDefaultApiRateLimits = { describe('GetApiRateLimitMaximum', async () => { let useCase: GetApiRateLimitMaximum; let session: UserSession; - let organizationRepository: OrganizationRepository; + let organizationRepository: CommunityOrganizationRepository; let environmentRepository: EnvironmentRepository; let getDefaultApiRateLimits: GetApiRateLimitServiceMaximumConfig; @@ -48,7 +48,7 @@ describe('GetApiRateLimitMaximum', async () => { await session.initialize(); useCase = moduleRef.get(GetApiRateLimitMaximum); - organizationRepository = moduleRef.get(OrganizationRepository); + organizationRepository = moduleRef.get(CommunityOrganizationRepository); environmentRepository = moduleRef.get(EnvironmentRepository); getDefaultApiRateLimits = moduleRef.get(GetApiRateLimitServiceMaximumConfig); diff --git a/apps/api/src/app/rate-limiting/usecases/get-api-rate-limit-maximum/get-api-rate-limit-maximum.usecase.ts b/apps/api/src/app/rate-limiting/usecases/get-api-rate-limit-maximum/get-api-rate-limit-maximum.usecase.ts index 87a5c3e1f83..4d38a314896 100644 --- a/apps/api/src/app/rate-limiting/usecases/get-api-rate-limit-maximum/get-api-rate-limit-maximum.usecase.ts +++ b/apps/api/src/app/rate-limiting/usecases/get-api-rate-limit-maximum/get-api-rate-limit-maximum.usecase.ts @@ -1,5 +1,5 @@ import { Injectable, InternalServerErrorException, Logger } from '@nestjs/common'; -import { EnvironmentRepository, OrganizationRepository } from '@novu/dal'; +import { CommunityOrganizationRepository, EnvironmentRepository } from '@novu/dal'; import { buildMaximumApiRateLimitKey, CachedEntity, InstrumentUsecase } from '@novu/application-generic'; import { ApiRateLimitCategoryEnum, ApiServiceLevelEnum, IApiRateLimitMaximum } from '@novu/shared'; import { GetApiRateLimitMaximumCommand } from './get-api-rate-limit-maximum.command'; @@ -12,7 +12,7 @@ const LOG_CONTEXT = 'GetApiRateLimit'; export class GetApiRateLimitMaximum { constructor( private environmentRepository: EnvironmentRepository, - private organizationRepository: OrganizationRepository, + private organizationRepository: CommunityOrganizationRepository, private getDefaultApiRateLimits: GetApiRateLimitServiceMaximumConfig ) {} diff --git a/apps/api/src/app/shared/shared.module.ts b/apps/api/src/app/shared/shared.module.ts index 2ac8cfcfd55..0cf22a587c2 100644 --- a/apps/api/src/app/shared/shared.module.ts +++ b/apps/api/src/app/shared/shared.module.ts @@ -39,8 +39,8 @@ import { ExecuteBridgeRequest, ExecutionLogRoute, featureFlagsService, - getFeatureFlag, injectCommunityAuthProviders, + getFeatureFlag, InvalidateCacheService, LoggerModule, QueuesModule, @@ -48,6 +48,7 @@ import { } from '@novu/application-generic'; import { isClerkEnabled, JobTopicNameEnum } from '@novu/shared'; +import { JwtModule } from '@nestjs/jwt'; import packageJson from '../../../package.json'; function getDynamicAuthProviders() { @@ -85,7 +86,6 @@ const DAL_MODELS = [ WorkflowOverrideRepository, ControlValuesRepository, PreferencesRepository, - ...getDynamicAuthProviders(), ]; const dalService = { @@ -116,21 +116,43 @@ const PROVIDERS = [ getFeatureFlag, ]; +const IMPORTS = [ + QueuesModule.forRoot([ + JobTopicNameEnum.EXECUTION_LOG, + JobTopicNameEnum.WEB_SOCKETS, + JobTopicNameEnum.WORKFLOW, + JobTopicNameEnum.INBOUND_PARSE_MAIL, + ]), + LoggerModule.forRoot( + createNestLoggingModuleOptions({ + serviceName: packageJson.name, + version: packageJson.version, + }) + ), +]; + +if (process.env.NODE_ENV === 'test') { + /** + * This is here only because of the tests. These providers are available at AppModule level, + * but since in tests we are often importing just the SharedModule and not the entire AppModule + * we need to make sure these providers are available. + * + * TODO: modify tests to either import all services they need explicitly, or remove repositories from SharedModule, + * and then import SharedModule + repositories explicitly. + */ + PROVIDERS.push(...getDynamicAuthProviders()); + IMPORTS.push( + JwtModule.register({ + secret: `${process.env.JWT_SECRET}`, + signOptions: { + expiresIn: 360000, + }, + }) + ); +} + @Module({ - imports: [ - QueuesModule.forRoot([ - JobTopicNameEnum.EXECUTION_LOG, - JobTopicNameEnum.WEB_SOCKETS, - JobTopicNameEnum.WORKFLOW, - JobTopicNameEnum.INBOUND_PARSE_MAIL, - ]), - LoggerModule.forRoot( - createNestLoggingModuleOptions({ - serviceName: packageJson.name, - version: packageJson.version, - }) - ), - ], + imports: [...IMPORTS], providers: [...PROVIDERS], exports: [...PROVIDERS, LoggerModule, QueuesModule], }) diff --git a/apps/worker/package.json b/apps/worker/package.json index 5a03eb68326..ad0b1b9b3d6 100644 --- a/apps/worker/package.json +++ b/apps/worker/package.json @@ -89,7 +89,6 @@ "typescript": "5.6.2" }, "optionalDependencies": { - "@novu/ee-auth": "workspace:*", "@novu/ee-billing": "workspace:*", "@novu/ee-shared-services": "workspace:*", "@novu/ee-translation": "workspace:*" diff --git a/apps/worker/src/app/shared/shared.module.ts b/apps/worker/src/app/shared/shared.module.ts index 25f10151372..5ccc0dac16b 100644 --- a/apps/worker/src/app/shared/shared.module.ts +++ b/apps/worker/src/app/shared/shared.module.ts @@ -16,7 +16,6 @@ import { ExecuteBridgeRequest, featureFlagsService, GetTenant, - injectCommunityAuthProviders, InvalidateCacheService, LoggerModule, MetricsModule, @@ -43,35 +42,21 @@ import { NotificationGroupRepository, NotificationRepository, NotificationTemplateRepository, - OrganizationRepository, SubscriberPreferenceRepository, SubscriberRepository, TenantRepository, TopicRepository, TopicSubscribersRepository, - UserRepository, WorkflowOverrideRepository, } from '@novu/dal'; -import { isClerkEnabled, JobTopicNameEnum } from '@novu/shared'; +import { JobTopicNameEnum } from '@novu/shared'; import packageJson from '../../../package.json'; import { UNIQUE_WORKER_DEPENDENCIES } from '../../config/worker-init.config'; import { ActiveJobsMetricService } from '../workflow/services'; import { CreateLog } from './logs'; -function getDynamicAuthProviders() { - if (isClerkEnabled()) { - // eslint-disable-next-line global-require - const eeAuthPackage = require('@novu/ee-auth'); - - return eeAuthPackage.injectEEAuthProviders(); - } else { - return injectCommunityAuthProviders(); - } -} - const DAL_MODELS = [ - OrganizationRepository, EnvironmentRepository, ExecutionDetailsRepository, NotificationTemplateRepository, @@ -90,8 +75,6 @@ const DAL_MODELS = [ TenantRepository, WorkflowOverrideRepository, ControlValuesRepository, - UserRepository, - ...getDynamicAuthProviders(), ]; const dalService = { diff --git a/apps/worker/src/app/telemetry/telemetry.module.ts b/apps/worker/src/app/telemetry/telemetry.module.ts index 5df1f520dac..9d0e8e30d99 100644 --- a/apps/worker/src/app/telemetry/telemetry.module.ts +++ b/apps/worker/src/app/telemetry/telemetry.module.ts @@ -5,20 +5,20 @@ import { IntegrationRepository, NotificationRepository, NotificationTemplateRepository, - OrganizationRepository, SubscriberRepository, TopicRepository, - UserRepository, + CommunityUserRepository, + CommunityOrganizationRepository, } from '@novu/dal'; import { SharedModule } from '../shared/shared.module'; import { MachineInfoService } from './usecases/machineInfoService.usecase'; import { UserInfoService } from './usecases/userInfoService.usecase'; const REPOSITORIES = [ - UserRepository, + CommunityUserRepository, + CommunityOrganizationRepository, NotificationTemplateRepository, NotificationRepository, - OrganizationRepository, TopicRepository, SubscriberRepository, IntegrationRepository, diff --git a/apps/worker/src/app/telemetry/usecases/userInfoService.usecase.ts b/apps/worker/src/app/telemetry/usecases/userInfoService.usecase.ts index 017a8abddb1..27006603b00 100644 --- a/apps/worker/src/app/telemetry/usecases/userInfoService.usecase.ts +++ b/apps/worker/src/app/telemetry/usecases/userInfoService.usecase.ts @@ -2,13 +2,13 @@ import { HttpService } from '@nestjs/axios'; import { Injectable } from '@nestjs/common'; import { Cron, CronExpression } from '@nestjs/schedule'; import { + CommunityUserRepository, + CommunityOrganizationRepository, IntegrationRepository, NotificationRepository, NotificationTemplateRepository, - OrganizationRepository, SubscriberRepository, TopicRepository, - UserRepository, } from '@novu/dal'; import { loadOrCreateMachineId } from '../utils/machine.utils'; import { sendDataToNovuTrace } from '../utils/sendDataToNovuTrace.utils'; @@ -16,8 +16,8 @@ import { sendDataToNovuTrace } from '../utils/sendDataToNovuTrace.utils'; @Injectable() export class UserInfoService { constructor( - private readonly userRepository: UserRepository, - private readonly organizationRepository: OrganizationRepository, + private readonly userRepository: CommunityUserRepository, + private readonly organizationRepository: CommunityOrganizationRepository, private readonly notificationTemplateRepository: NotificationTemplateRepository, private readonly notificationRepository: NotificationRepository, private readonly topicRepository: TopicRepository, diff --git a/enterprise/packages/billing/package.json b/enterprise/packages/billing/package.json index 7dba83baa18..8afb829ec4e 100644 --- a/enterprise/packages/billing/package.json +++ b/enterprise/packages/billing/package.json @@ -16,6 +16,7 @@ "dependencies": { "@novu/application-generic": "workspace:*", "@novu/ee-dal": "workspace:*", + "@novu/ee-auth": "workspace:*", "@novu/shared": "workspace:*", "class-transformer": "0.5.1", "class-validator": "0.14.1", @@ -47,8 +48,5 @@ "@nestjs/swagger": "7.4.0", "@nestjs/throttler": "6.2.1", "@novu/dal": "workspace:*" - }, - "optionalDependencies": { - "@novu/ee-auth": "workspace:*" } } diff --git a/enterprise/packages/translation/package.json b/enterprise/packages/translation/package.json index d56426de531..2e9005eefa3 100644 --- a/enterprise/packages/translation/package.json +++ b/enterprise/packages/translation/package.json @@ -39,12 +39,8 @@ }, "peerDependencies": { "@nestjs/common": "10.4.1", - "@nestjs/jwt": "10.2.0", "@nestjs/swagger": "7.4.0", "@nestjs/platform-express": "10.4.1", "@novu/dal": "workspace:*" - }, - "optionalDependencies": { - "@novu/ee-auth": "workspace:*" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 597b9520bc9..b0599c8ad6b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1843,9 +1843,6 @@ importers: specifier: ^8.3.2 version: 8.3.2 optionalDependencies: - '@novu/ee-auth': - specifier: workspace:* - version: link:../../enterprise/packages/auth '@novu/ee-billing': specifier: workspace:* version: link:../../enterprise/packages/billing @@ -2206,6 +2203,9 @@ importers: '@novu/dal': specifier: workspace:* version: link:../../../libs/dal + '@novu/ee-auth': + specifier: workspace:* + version: link:../auth '@novu/ee-dal': specifier: workspace:* version: link:../dal @@ -2239,10 +2239,6 @@ importers: stripe-event-types: specifier: ^3.1.0 version: 3.1.0(stripe@11.18.0) - optionalDependencies: - '@novu/ee-auth': - specifier: workspace:* - version: link:../auth devDependencies: '@types/chai': specifier: ^4.2.11 @@ -2354,9 +2350,6 @@ importers: '@nestjs/common': specifier: 10.4.1 version: 10.4.1(class-transformer@0.5.1)(class-validator@0.14.1)(reflect-metadata@0.2.2)(rxjs@7.8.1) - '@nestjs/jwt': - specifier: 10.2.0 - version: 10.2.0(@nestjs/common@10.4.1(class-transformer@0.5.1)(class-validator@0.14.1)(reflect-metadata@0.2.2)(rxjs@7.8.1)) '@nestjs/platform-express': specifier: 10.4.1 version: 10.4.1(@nestjs/common@10.4.1(class-transformer@0.5.1)(class-validator@0.14.1)(reflect-metadata@0.2.2)(rxjs@7.8.1))(@nestjs/core@10.4.1) @@ -2393,10 +2386,6 @@ importers: slugify: specifier: ^1.4.6 version: 1.6.6 - optionalDependencies: - '@novu/ee-auth': - specifier: workspace:* - version: link:../auth devDependencies: '@types/chai': specifier: ^4.2.11