Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nx circular dependencies #4958

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
"slugify": "^1.4.6",
"swagger-ui-express": "^4.4.0",
"twilio": "^4.14.1",
"uuid": "^8.3.2"
"uuid": "^8.3.2",
"i18next": "^23.7.6"
},
"devDependencies": {
"@faker-js/faker": "^6.0.0",
Expand Down Expand Up @@ -116,4 +117,4 @@
"eslint"
]
}
}
}
7 changes: 4 additions & 3 deletions apps/api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ const enterpriseImports = (): Array<Type | DynamicModule | Promise<DynamicModule
if (require('@novu/ee-auth')?.EEAuthModule) {
modules.push(require('@novu/ee-auth')?.EEAuthModule);
}
if (require('@novu/ee-translation')?.EnterpriseTranslationModule) {
modules.push(require('@novu/ee-translation')?.EnterpriseTranslationModule);
}
}
} catch (e) {
Logger.error(e, `Unexpected error while importing enterprise modules`, 'EnterpriseImport');
}

if (require('@novu/ee-translation')?.EnterpriseTranslationModule) {
modules.push(require('@novu/ee-translation')?.EnterpriseTranslationModule);
}

return modules;
};

Expand Down
37 changes: 33 additions & 4 deletions apps/api/src/app/content-templates/content-templates.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Body, Controller, Post, UseGuards } from '@nestjs/common';
import { Body, Controller, Logger, Post, UseGuards } from '@nestjs/common';
import { ApiExcludeController } from '@nestjs/swagger';
import { IEmailBlock, IJwtPayload, IMessageCTA, MessageTemplateContentType } from '@novu/shared';
import {
ApiException,
CompileEmailTemplate,
CompileEmailTemplateCommand,
CompileInAppTemplate,
CompileInAppTemplateCommand,
JwtAuthGuard,
} from '@novu/application-generic';
import * as i18next from 'i18next';
import { ModuleRef } from '@nestjs/core';

import { UserSession } from '../shared/framework/user.decorator';

Expand All @@ -17,7 +20,8 @@ import { UserSession } from '../shared/framework/user.decorator';
export class ContentTemplatesController {
constructor(
private compileEmailTemplateUsecase: CompileEmailTemplate,
private compileInAppTemplate: CompileInAppTemplate
private compileInAppTemplate: CompileInAppTemplate,
private moduleRef: ModuleRef
) {}

@Post('/preview/email')
Expand All @@ -39,7 +43,8 @@ export class ContentTemplatesController {
payload,
subject,
layoutId,
})
}),
this.initiateTranslations.bind(this)
);
}

Expand All @@ -58,7 +63,31 @@ export class ContentTemplatesController {
content,
payload,
cta,
})
}),
this.initiateTranslations.bind(this)
);
}

protected async initiateTranslations(environmentId: string, organizationId: string, locale: string | undefined) {
try {
if (process.env.NOVU_ENTERPRISE === 'true' || process.env.CI_EE_TEST === 'true') {
if (!require('@novu/ee-translation')?.TranslationsService) {
throw new ApiException('Translation module is not loaded');
}
const service = this.moduleRef.get(require('@novu/ee-translation')?.TranslationsService, { strict: false });
const { namespaces, resources } = await service.getTranslationsList(environmentId, organizationId);

await i18next.init({
resources,
ns: namespaces,
defaultNS: false,
nsSeparator: '.',
lng: locale || 'en',
compatibilityJSON: 'v2',
});
}
} catch (e) {
Logger.error(e, `Unexpected error while importing enterprise modules`, 'TranslationsService');
}
}
}
2 changes: 1 addition & 1 deletion enterprise
5 changes: 2 additions & 3 deletions packages/application-generic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"watch:build": "tsc -p tsconfig.json -w",
"watch:test": "jest src --watch",
"reset-hard": "git clean -dfx && git reset --hard && pnpm install",
"prepare-release": "run-s reset-hard test",
"link:submodules": "pnpm link ../../enterprise/packages/translation"
"prepare-release": "run-s reset-hard test"
},
"publishConfig": {
"access": "public"
Expand Down Expand Up @@ -187,4 +186,4 @@
"**/*.spec.js"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,24 @@ export class CompileEmailTemplate extends CompileTemplateBase {
super(organizationRepository, moduleRef);
}

public async execute(command: CompileEmailTemplateCommand) {
public async execute(
command: CompileEmailTemplateCommand,
initiateTranslations?: (
environmentId: string,
organizationId,
locale: string
) => Promise<void>
) {
const verifyPayloadService = new VerifyPayloadService();
const organization = await this.getOrganization(command.organizationId);

await this.initiateTranslations(
command.environmentId,
command.organizationId,
command.payload.subscriber?.locale || organization.defaultLocale
);
if (initiateTranslations) {
await initiateTranslations(
command.environmentId,
command.organizationId,
command.payload.subscriber?.locale || organization.defaultLocale
);
}

const isEditorMode = command.contentType === 'editor';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@ export class CompileInAppTemplate extends CompileTemplateBase {
super(organizationRepository, moduleRef);
}

public async execute(command: CompileInAppTemplateCommand) {
public async execute(
command: CompileInAppTemplateCommand,
initiateTranslations?: (
environmentId: string,
organizationId,
locale: string
) => Promise<void>
) {
const organization = await this.getOrganization(command.organizationId);

await this.initiateTranslations(
command.environmentId,
command.organizationId,
command.payload.subscriber?.locale || organization.defaultLocale
);
if (initiateTranslations) {
await initiateTranslations(
command.environmentId,
command.organizationId,
command.payload.subscriber?.locale || organization.defaultLocale
);
}

const payload = command.payload || {};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { OrganizationEntity, OrganizationRepository } from '@novu/dal';
import { ModuleRef } from '@nestjs/core';
import { Logger, NotFoundException } from '@nestjs/common';
import { ApiException } from '../../utils/exceptions';
import * as i18next from 'i18next';
import { NotFoundException } from '@nestjs/common';

export abstract class CompileTemplateBase {
protected constructor(
Expand All @@ -24,44 +22,4 @@ export abstract class CompileTemplateBase {

return organization;
}

protected async initiateTranslations(
environmentId: string,
organizationId: string,
locale: string | undefined
) {
try {
if (
process.env.NOVU_ENTERPRISE === 'true' ||
process.env.CI_EE_TEST === 'true'
) {
if (!require('@novu/ee-translation')?.TranslationsService) {
throw new ApiException('Translation module is not loaded');
}
const service = this.moduleRef.get(
require('@novu/ee-translation')?.TranslationsService,
{ strict: false }
);
const { namespaces, resources } = await service.getTranslationsList(
environmentId,
organizationId
);

await i18next.init({
resources,
ns: namespaces,
defaultNS: false,
nsSeparator: '.',
lng: locale || 'en',
compatibilityJSON: 'v2',
});
}
} catch (e) {
Logger.error(
e,
`Unexpected error while importing enterprise modules`,
'TranslationsService'
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Sentry from '@sentry/node';
import { OrganizationRepository, IntegrationEntity } from '@novu/dal';
import { ChannelTypeEnum, EmailProviderIdEnum } from '@novu/shared';
import { IEmailOptions } from '@novu/stateless';

import { ModuleRef } from '@nestjs/core';
import { AnalyticsService } from '../../services/analytics.service';
import { InstrumentUsecase } from '../../instrumentation';
import { MailFactory } from '../../factories/mail/mail.factory';
Expand All @@ -26,7 +26,8 @@ export class SendTestEmail {
private organizationRepository: OrganizationRepository,
private selectIntegration: SelectIntegration,
private analyticsService: AnalyticsService,
protected getNovuProviderCredentials: GetNovuProviderCredentials
protected getNovuProviderCredentials: GetNovuProviderCredentials,
protected moduleRef: ModuleRef
) {}

@InstrumentUsecase()
Expand Down
9 changes: 6 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.