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: set novu providers as primary for new orgs #5299

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -6,10 +6,16 @@ import { CreateNovuIntegrationsCommand } from './create-novu-integrations.comman
import { CreateIntegration } from '../create-integration/create-integration.usecase';
import { CreateIntegrationCommand } from '../create-integration/create-integration.command';
import { ChannelTypeEnum, EmailProviderIdEnum, SmsProviderIdEnum } from '@novu/shared';
import { SetIntegrationAsPrimary } from '../set-integration-as-primary/set-integration-as-primary.usecase';
import { SetIntegrationAsPrimaryCommand } from '../set-integration-as-primary/set-integration-as-primary.command';

@Injectable()
export class CreateNovuIntegrations {
constructor(private createIntegration: CreateIntegration, private integrationRepository: IntegrationRepository) {}
constructor(
private createIntegration: CreateIntegration,
private integrationRepository: IntegrationRepository,
private setIntegrationAsPrimary: SetIntegrationAsPrimary
) {}

private async createEmailIntegration(command: CreateNovuIntegrationsCommand) {
if (!areNovuEmailCredentialsSet()) {
Expand All @@ -24,7 +30,7 @@ export class CreateNovuIntegrations {
});

if (emailIntegrationCount === 0) {
await this.createIntegration.execute(
const novuEmailIntegration = await this.createIntegration.execute(
CreateIntegrationCommand.create({
providerId: EmailProviderIdEnum.Novu,
channel: ChannelTypeEnum.EMAIL,
Expand All @@ -36,6 +42,14 @@ export class CreateNovuIntegrations {
organizationId: command.organizationId,
})
);
await this.setIntegrationAsPrimary.execute(
SetIntegrationAsPrimaryCommand.create({
organizationId: command.organizationId,
environmentId: command.environmentId,
integrationId: novuEmailIntegration._id,
userId: command.userId,
})
);
}
}

Expand All @@ -52,7 +66,7 @@ export class CreateNovuIntegrations {
});

if (smsIntegrationCount === 0) {
await this.createIntegration.execute(
const novuSmsIntegration = await this.createIntegration.execute(
CreateIntegrationCommand.create({
providerId: SmsProviderIdEnum.Novu,
channel: ChannelTypeEnum.SMS,
Expand All @@ -64,6 +78,14 @@ export class CreateNovuIntegrations {
organizationId: command.organizationId,
})
);
await this.setIntegrationAsPrimary.execute(
SetIntegrationAsPrimaryCommand.create({
organizationId: command.organizationId,
environmentId: command.environmentId,
integrationId: novuSmsIntegration._id,
userId: command.userId,
})
);
}
}

Expand Down
26 changes: 21 additions & 5 deletions apps/api/src/app/organization/e2e/create-organization.e2e.ts
Expand Up @@ -104,7 +104,7 @@ describe('Create Organization - /organizations (POST)', async () => {
expect(user?.jobTitle).to.eq(testOrganization.jobTitle);
});

it('should create organization with built in Novu integrations', async () => {
it('should create organization with built in Novu integrations and set them as primary', async () => {
const testOrganization: ICreateOrganizationDto = {
name: 'Org Name',
};
Expand All @@ -120,14 +120,30 @@ describe('Create Organization - /organizations (POST)', async () => {
const novuSmsIntegration = integrations.filter(
(i) => i.active && i.name === 'Novu SMS' && i.providerId === SmsProviderIdEnum.Novu
);
const novuEmailIntegrationProduction = novuEmailIntegration.filter(
(el) => el._environmentId === productionEnv?._id
);
const novuEmailIntegrationDevelopment = novuEmailIntegration.filter(
(el) => el._environmentId === developmentEnv?._id
);
const novuSmsIntegrationProduction = novuSmsIntegration.filter((el) => el._environmentId === productionEnv?._id);
const novuSmsIntegrationDevelopment = novuSmsIntegration.filter(
(el) => el._environmentId === developmentEnv?._id
);

expect(integrations.length).to.eq(4);
expect(novuEmailIntegration?.length).to.eq(2);
expect(novuSmsIntegration?.length).to.eq(2);
expect(novuEmailIntegration.filter((el) => el._environmentId === productionEnv?._id).length).to.eq(1);
expect(novuSmsIntegration.filter((el) => el._environmentId === productionEnv?._id).length).to.eq(1);
expect(novuEmailIntegration.filter((el) => el._environmentId === developmentEnv?._id).length).to.eq(1);
expect(novuSmsIntegration.filter((el) => el._environmentId === developmentEnv?._id).length).to.eq(1);

expect(novuEmailIntegrationProduction.length).to.eq(1);
expect(novuSmsIntegrationProduction.length).to.eq(1);
expect(novuEmailIntegrationDevelopment.length).to.eq(1);
expect(novuSmsIntegrationDevelopment.length).to.eq(1);

expect(novuEmailIntegrationProduction[0].primary).to.eq(true);
expect(novuSmsIntegrationProduction[0].primary).to.eq(true);
expect(novuEmailIntegrationDevelopment[0].primary).to.eq(true);
expect(novuSmsIntegrationDevelopment[0].primary).to.eq(true);
});

it('when Novu Email credentials are not set it should not create Novu Email integration', async () => {
Expand Down