Skip to content

Commit

Permalink
refactor(api,application-generic,ws,worker,webhook): resolve circular…
Browse files Browse the repository at this point in the history
… dependencies; import refactor (#6268)
  • Loading branch information
ChmaraX authored Aug 8, 2024
1 parent af4e468 commit 4a363ce
Show file tree
Hide file tree
Showing 19 changed files with 214 additions and 258 deletions.
2 changes: 1 addition & 1 deletion .source
6 changes: 0 additions & 6 deletions apps/api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ const enterpriseImports = (): Array<Type | DynamicModule | Promise<DynamicModule
}
}

if (isClerkEnabled()) {
if (require('@novu/ee-auth')?.EEAuthModule) {
modules.push(require('@novu/ee-auth')?.EEAuthModule);
}
}

return modules;
};

Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/app/auth/community.auth.module.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { EnvironmentsModule } from '../environments/environments.module';
import { JwtSubscriberStrategy } from './services/passport/subscriber-jwt.strategy';
import { RootEnvironmentGuard } from './framework/root-environment-guard.service';
import { ApiKeyStrategy } from './services/passport/apikey.strategy';
import { injectRepositories } from '@novu/application-generic';
import { injectCommunityAuthProviders } from '@novu/application-generic';

const AUTH_STRATEGIES: Provider[] = [JwtStrategy, ApiKeyStrategy, JwtSubscriberStrategy];

Expand Down Expand Up @@ -47,7 +47,7 @@ export function getCommunityAuthModuleConfig(): ModuleMetadata {
providers: [
...USE_CASES,
...AUTH_STRATEGIES,
...injectRepositories({ repositoriesOnly: false }),
...injectCommunityAuthProviders({ repositoriesOnly: false }),
AuthService,
RolesGuard,
RootEnvironmentGuard,
Expand Down
16 changes: 8 additions & 8 deletions apps/api/src/app/auth/ee.auth.module.config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {
AuthService,
SwitchEnvironment,
SwitchOrganization,
injectRepositories,
PlatformException,
} from '@novu/application-generic';
import { AuthService, SwitchEnvironment, SwitchOrganization, PlatformException } from '@novu/application-generic';
import { RolesGuard } from './framework/roles.guard';
import { RootEnvironmentGuard } from './framework/root-environment-guard.service';
import { MiddlewareConsumer, ModuleMetadata } from '@nestjs/common';
import { ApiKeyStrategy } from './services/passport/apikey.strategy';
import { JwtSubscriberStrategy } from './services/passport/subscriber-jwt.strategy';
import { OrganizationModule } from '../organization/organization.module';

function getEEAuthProviders() {
const eeAuthPackage = require('@novu/ee-auth');

return eeAuthPackage.injectEEAuthProviders();
}

export function getEEModuleConfig(): ModuleMetadata {
const eeAuthPackage = require('@novu/ee-auth');
const eeAuthModule = eeAuthPackage?.eEAuthModule;
Expand All @@ -25,7 +25,7 @@ export function getEEModuleConfig(): ModuleMetadata {
controllers: [...eeAuthModule.controllers],
providers: [
...eeAuthModule.providers,
...injectRepositories(),
...getEEAuthProviders(),
// reused services
ApiKeyStrategy,
JwtSubscriberStrategy,
Expand Down
16 changes: 13 additions & 3 deletions apps/api/src/app/shared/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,22 @@ import {
storageService,
ExecutionLogRoute,
CreateExecutionDetails,
injectRepositories,
injectCommunityAuthProviders,
ExecuteBridgeRequest,
} from '@novu/application-generic';

import packageJson from '../../../package.json';
import { JobTopicNameEnum } from '@novu/shared';
import { JobTopicNameEnum, isClerkEnabled } from '@novu/shared';

function getDynamicAuthProviders() {
if (isClerkEnabled()) {
const eeAuthPackage = require('@novu/ee-auth');

return eeAuthPackage.injectEEAuthProviders();
} else {
return injectCommunityAuthProviders();
}
}

const DAL_MODELS = [
UserRepository,
Expand All @@ -72,7 +82,7 @@ const DAL_MODELS = [
TenantRepository,
WorkflowOverrideRepository,
ControlVariablesRepository,
...injectRepositories(),
...getDynamicAuthProviders(),
];

const dalService = {
Expand Down
3 changes: 3 additions & 0 deletions apps/webhook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
"tsconfig-paths": "~4.1.0",
"typescript": "4.9.5"
},
"optionalDependencies": {
"@novu/ee-auth": "workspace:*"
},
"workspaces": {
"nohoist": [
"@nestjs/platform-socket.io",
Expand Down
15 changes: 13 additions & 2 deletions apps/webhook/src/shared/shared.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Module } from '@nestjs/common';
import { AnalyticsService, injectRepositories } from '@novu/application-generic';
import { AnalyticsService, injectCommunityAuthProviders } from '@novu/application-generic';
import {
DalService,
UserRepository,
Expand All @@ -14,6 +14,17 @@ import {
IntegrationRepository,
JobRepository,
} from '@novu/dal';
import { isClerkEnabled } from '@novu/shared';

function getDynamicAuthProviders() {
if (isClerkEnabled()) {
const eeAuthPackage = require('@novu/ee-auth');

return eeAuthPackage.injectEEAuthProviders();
} else {
return injectCommunityAuthProviders();
}
}

const DAL_MODELS = [
UserRepository,
Expand All @@ -27,7 +38,7 @@ const DAL_MODELS = [
MemberRepository,
IntegrationRepository,
JobRepository,
...injectRepositories(),
...getDynamicAuthProviders(),
];

const dalService = new DalService();
Expand Down
16 changes: 13 additions & 3 deletions apps/worker/src/app/shared/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,26 @@ import {
UpdateSubscriber,
UpdateSubscriberChannel,
UpdateTenant,
injectRepositories,
injectCommunityAuthProviders,
ExecuteBridgeRequest,
} from '@novu/application-generic';

import packageJson from '../../../package.json';
import { CreateLog } from './logs';
import { JobTopicNameEnum } from '@novu/shared';
import { JobTopicNameEnum, isClerkEnabled } from '@novu/shared';
import { ActiveJobsMetricService } from '../workflow/services';
import { UNIQUE_WORKER_DEPENDENCIES, workersToProcess } from '../../config/worker-init.config';

function getDynamicAuthProviders() {
if (isClerkEnabled()) {
const eeAuthPackage = require('@novu/ee-auth');

return eeAuthPackage.injectEEAuthProviders();
} else {
return injectCommunityAuthProviders();
}
}

const DAL_MODELS = [
UserRepository,
OrganizationRepository,
Expand All @@ -86,7 +96,7 @@ const DAL_MODELS = [
TenantRepository,
WorkflowOverrideRepository,
ControlVariablesRepository,
...injectRepositories(),
...getDynamicAuthProviders(),
];

const dalService = {
Expand Down
3 changes: 3 additions & 0 deletions apps/ws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
"tsconfig-paths": "~4.1.0",
"typescript": "4.9.5"
},
"optionalDependencies": {
"@novu/ee-auth": "workspace:*"
},
"workspaces": {
"nohoist": [
"@nestjs/platform-socket.io",
Expand Down
16 changes: 13 additions & 3 deletions apps/ws/src/shared/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,21 @@ import {
DalServiceHealthIndicator,
WebSocketsInMemoryProviderService,
QueuesModule,
injectRepositories,
injectCommunityAuthProviders,
} from '@novu/application-generic';

import { SubscriberOnlineService } from './subscriber-online';
import { JobTopicNameEnum } from '@novu/shared';
import { JobTopicNameEnum, isClerkEnabled } from '@novu/shared';

function getDynamicAuthProviders() {
if (isClerkEnabled()) {
const eeAuthPackage = require('@novu/ee-auth');

return eeAuthPackage.injectEEAuthProviders();
} else {
return injectCommunityAuthProviders();
}
}

const DAL_MODELS = [
UserRepository,
Expand All @@ -31,7 +41,7 @@ const DAL_MODELS = [
NotificationRepository,
MessageRepository,
MemberRepository,
...injectRepositories(),
...getDynamicAuthProviders(),
];

const dalService = {
Expand Down
1 change: 1 addition & 0 deletions enterprise/packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@clerk/clerk-sdk-node": "^5.0.19",
"@novu/dal": "workspace:*",
"@novu/shared": "workspace:*",
"@novu/application-generic": "workspace:*",
"mongoose": "^7.5.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
Expand Down
3 changes: 3 additions & 0 deletions enterprise/packages/billing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@
"@nestjs/swagger": "^7.1.8",
"@nestjs/throttler": "^5.0.1",
"@novu/dal": "workspace:*"
},
"optionalDependencies": {
"@novu/ee-auth": "workspace:*"
}
}
3 changes: 3 additions & 0 deletions enterprise/packages/translation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@
"@nestjs/swagger": "^7.1.8",
"@nestjs/platform-express": "^10.2.2",
"@novu/dal": "workspace:*"
},
"optionalDependencies": {
"@novu/ee-auth": "workspace:*"
}
}
1 change: 0 additions & 1 deletion libs/application-generic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
"slugify": "^1.4.6"
},
"optionalDependencies": {
"@novu/ee-auth": "workspace:*",
"@novu/ee-shared-services": "workspace:*",
"@taskforcesh/bullmq-pro": "5.1.14"
},
Expand Down
2 changes: 1 addition & 1 deletion libs/application-generic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export * from './health/index';
export * from './instrumentation/index';
export * from './logging/index';
export * from './modules';
export * from './utils/inject-repositories';
export * from './utils/inject-auth-providers';
export * from './profiling';
export * from './resilience';
export * from './services';
Expand Down
108 changes: 108 additions & 0 deletions libs/application-generic/src/utils/inject-auth-providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { Reflector } from '@nestjs/core';
import { JwtService } from '@nestjs/jwt';
import {
CommunityUserRepository,
CommunityMemberRepository,
CommunityOrganizationRepository,
EnvironmentRepository,
SubscriberRepository,
UserRepository,
MemberRepository,
OrganizationRepository,
} from '@novu/dal';
import {
AnalyticsService,
CommunityAuthService,
CommunityUserAuthGuard,
} from '../services';
import { CreateUser, SwitchOrganization } from '../usecases';

/**
* Injects community auth providers, or providers handling user management (services, repositories, guards ...) into the application.
* This function is closely related to its enterprise counterpart:
*
* @see @novu/ee-auth -> injectEEAuthProviders()
*
*/
export function injectCommunityAuthProviders(
{ repositoriesOnly }: { repositoriesOnly?: boolean } = {
repositoriesOnly: true,
}
) {
const userRepositoryProvider = {
provide: 'USER_REPOSITORY',
useClass: CommunityUserRepository,
};

const memberRepositoryProvider = {
provide: 'MEMBER_REPOSITORY',
useClass: CommunityMemberRepository,
};

const organizationRepositoryProvider = {
provide: 'ORGANIZATION_REPOSITORY',
useClass: CommunityOrganizationRepository,
};

const authServiceProvider = {
provide: 'AUTH_SERVICE',
useFactory: (
userRepository: UserRepository,
subscriberRepository: SubscriberRepository,
createUserUsecase: CreateUser,
jwtService: JwtService,
analyticsService: AnalyticsService,
organizationRepository: OrganizationRepository,
environmentRepository: EnvironmentRepository,
memberRepository: MemberRepository,
switchOrganizationUsecase: SwitchOrganization
) => {
return new CommunityAuthService(
userRepository,
subscriberRepository,
createUserUsecase,
jwtService,
analyticsService,
organizationRepository,
environmentRepository,
memberRepository,
switchOrganizationUsecase
);
},
inject: [
UserRepository,
SubscriberRepository,
CreateUser,
JwtService,
AnalyticsService,
OrganizationRepository,
EnvironmentRepository,
MemberRepository,
SwitchOrganization,
],
};

const userAuthGuardProvider = {
provide: 'USER_AUTH_GUARD',
useFactory: (reflector: Reflector) => {
return new CommunityUserAuthGuard(reflector);
},
inject: [Reflector],
};

if (repositoriesOnly) {
return [
userRepositoryProvider,
memberRepositoryProvider,
organizationRepositoryProvider,
];
}

return [
userRepositoryProvider,
memberRepositoryProvider,
organizationRepositoryProvider,
authServiceProvider,
userAuthGuardProvider,
];
}
Loading

0 comments on commit 4a363ce

Please sign in to comment.