Skip to content

Commit

Permalink
Merge pull request #5299 from novuhq/nv-3578-novu-providers-not-set-a…
Browse files Browse the repository at this point in the history
…s-primary-for-new-org

fix: set novu providers as primary for new orgs
  • Loading branch information
ainouzgali committed Mar 13, 2024
2 parents 3493254 + 942afdd commit 906a41c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
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

0 comments on commit 906a41c

Please sign in to comment.