Skip to content

Commit

Permalink
fix(api): create in-app integrations when new org is created (#5783)
Browse files Browse the repository at this point in the history
* fix: create in-app integrations when new org is created

* fix: total integrations should be 6

* fix: failing tests

* fix: failing tests
  • Loading branch information
jainpawan21 authored Jun 28, 2024
1 parent 405d971 commit f0e28c7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { areNovuEmailCredentialsSet, areNovuSmsCredentialsSet } from '@novu/appl
import { CreateNovuIntegrationsCommand } from './create-novu-integrations.command';
import { CreateIntegration } from '../create-integration/create-integration.usecase';
import { CreateIntegrationCommand } from '../create-integration/create-integration.command';
import { ChannelTypeEnum, EmailProviderIdEnum, SmsProviderIdEnum } from '@novu/shared';
import { ChannelTypeEnum, EmailProviderIdEnum, InAppProviderIdEnum, 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';

Expand Down Expand Up @@ -89,8 +89,32 @@ export class CreateNovuIntegrations {
}
}

private async createInAppIntegration(command: CreateNovuIntegrationsCommand) {
const inAppIntegrationCount = await this.integrationRepository.count({
providerId: InAppProviderIdEnum.Novu,
channel: ChannelTypeEnum.IN_APP,
_organizationId: command.organizationId,
_environmentId: command.environmentId,
});
if (inAppIntegrationCount === 0) {
await this.createIntegration.execute(
CreateIntegrationCommand.create({
providerId: InAppProviderIdEnum.Novu,
channel: ChannelTypeEnum.IN_APP,
name: 'Novu In-App',
active: true,
check: false,
userId: command.userId,
environmentId: command.environmentId,
organizationId: command.organizationId,
})
);
}
}

async execute(command: CreateNovuIntegrationsCommand): Promise<void> {
await this.createEmailIntegration(command);
await this.createSmsIntegration(command);
await this.createInAppIntegration(command);
}
}
19 changes: 16 additions & 3 deletions apps/api/src/app/organization/e2e/create-organization.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ApiServiceLevelEnum,
EmailProviderIdEnum,
ICreateOrganizationDto,
InAppProviderIdEnum,
JobTitleEnum,
MemberRoleEnum,
SmsProviderIdEnum,
Expand Down Expand Up @@ -120,6 +121,9 @@ describe('Create Organization - /organizations (POST)', async () => {
const novuSmsIntegration = integrations.filter(
(i) => i.active && i.name === 'Novu SMS' && i.providerId === SmsProviderIdEnum.Novu
);
const novuInAppIntegration = integrations.filter(
(i) => i.active && i.name === 'Novu In-App' && i.providerId === InAppProviderIdEnum.Novu
);
const novuEmailIntegrationProduction = novuEmailIntegration.filter(
(el) => el._environmentId === productionEnv?._id
);
Expand All @@ -130,15 +134,24 @@ describe('Create Organization - /organizations (POST)', async () => {
const novuSmsIntegrationDevelopment = novuSmsIntegration.filter(
(el) => el._environmentId === developmentEnv?._id
);
const novuInAppIntegrationProduction = novuInAppIntegration.filter(
(el) => el._environmentId === productionEnv?._id
);
const novuInAppIntegrationDevelopment = novuInAppIntegration.filter(
(el) => el._environmentId === developmentEnv?._id
);

expect(integrations.length).to.eq(4);
expect(integrations.length).to.eq(6);
expect(novuEmailIntegration?.length).to.eq(2);
expect(novuSmsIntegration?.length).to.eq(2);
expect(novuInAppIntegration?.length).to.eq(2);

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

expect(novuEmailIntegrationProduction[0].primary).to.eq(true);
expect(novuSmsIntegrationProduction[0].primary).to.eq(true);
Expand All @@ -162,7 +175,7 @@ describe('Create Organization - /organizations (POST)', async () => {
(i) => i.active && i.name === 'Novu SMS' && i.providerId === SmsProviderIdEnum.Novu
);

expect(integrations.length).to.eq(2);
expect(integrations.length).to.eq(4);
expect(novuSmsIntegration?.length).to.eq(2);
expect(novuSmsIntegration.filter((el) => el._environmentId === productionEnv?._id).length).to.eq(1);
expect(novuSmsIntegration.filter((el) => el._environmentId === developmentEnv?._id).length).to.eq(1);
Expand All @@ -185,7 +198,7 @@ describe('Create Organization - /organizations (POST)', async () => {
(i) => i.active && i.name === 'Novu Email' && i.providerId === EmailProviderIdEnum.Novu
);

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

0 comments on commit f0e28c7

Please sign in to comment.