diff --git a/apps/api/src/app/integrations/usecases/create-novu-integrations/create-novu-integrations.usecase.ts b/apps/api/src/app/integrations/usecases/create-novu-integrations/create-novu-integrations.usecase.ts index d466c7b9716..6a37e2ab41e 100644 --- a/apps/api/src/app/integrations/usecases/create-novu-integrations/create-novu-integrations.usecase.ts +++ b/apps/api/src/app/integrations/usecases/create-novu-integrations/create-novu-integrations.usecase.ts @@ -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()) { @@ -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, @@ -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, + }) + ); } } @@ -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, @@ -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, + }) + ); } } diff --git a/apps/api/src/app/organization/e2e/create-organization.e2e.ts b/apps/api/src/app/organization/e2e/create-organization.e2e.ts index 02f2dbcec26..bc77ee59977 100644 --- a/apps/api/src/app/organization/e2e/create-organization.e2e.ts +++ b/apps/api/src/app/organization/e2e/create-organization.e2e.ts @@ -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', }; @@ -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 () => {