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

feat(api): set integration as primary and priority system #3873

Merged
Show file tree
Hide file tree
Changes from 6 commits
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
265 changes: 264 additions & 1 deletion apps/api/src/app/integrations/e2e/create-integration.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { IntegrationRepository, EnvironmentRepository } from '@novu/dal';
import { UserSession } from '@novu/testing';
import { ChannelTypeEnum, EmailProviderIdEnum, SmsProviderIdEnum } from '@novu/shared';
import {
ChannelTypeEnum,
ChatProviderIdEnum,
EmailProviderIdEnum,
InAppProviderIdEnum,
PushProviderIdEnum,
SmsProviderIdEnum,
} from '@novu/shared';
import { expect } from 'chai';

const ORIGINAL_IS_MULTI_PROVIDER_CONFIGURATION_ENABLED = process.env.IS_MULTI_PROVIDER_CONFIGURATION_ENABLED;
Expand Down Expand Up @@ -187,6 +194,262 @@ describe('Create Integration - /integration (POST)', function () {
expect(data.credentials?.tlsOptions).to.eql(payload.credentials.tlsOptions);
expect(data.active).to.equal(true);
});

it('should not calculate primary and priority fields when is not active', async function () {
const payload = {
providerId: EmailProviderIdEnum.SendGrid,
channel: ChannelTypeEnum.EMAIL,
active: false,
check: false,
};

const {
body: { data },
} = await session.testAgent.post('/v1/integrations').send(payload);

expect(data.priority).to.equal(0);
expect(data.primary).to.equal(false);
expect(data.active).to.equal(false);
});

it('should not calculate primary and priority fields for in-app channel', async function () {
const payload = {
providerId: InAppProviderIdEnum.Novu,
channel: ChannelTypeEnum.IN_APP,
active: true,
check: false,
};

const {
body: { data },
} = await session.testAgent.post('/v1/integrations').send(payload);

expect(data.priority).to.equal(0);
expect(data.primary).to.equal(false);
expect(data.active).to.equal(true);
});

it('should not calculate primary and priority fields for push channel', async function () {
const payload = {
providerId: PushProviderIdEnum.FCM,
channel: ChannelTypeEnum.PUSH,
active: true,
check: false,
};

const {
body: { data },
} = await session.testAgent.post('/v1/integrations').send(payload);

expect(data.priority).to.equal(0);
expect(data.primary).to.equal(false);
expect(data.active).to.equal(true);
});

it('should not calculate primary and priority fields for chat channel', async function () {
const payload = {
providerId: ChatProviderIdEnum.Slack,
channel: ChannelTypeEnum.CHAT,
active: true,
check: false,
};

const {
body: { data },
} = await session.testAgent.post('/v1/integrations').send(payload);

expect(data.priority).to.equal(0);
expect(data.primary).to.equal(false);
expect(data.active).to.equal(true);
});

it('should set the integration as primary when its active and there are no other active integrations', async function () {
await integrationRepository.deleteMany({
_organizationId: session.organization._id,
_environmentId: session.environment._id,
});

const payload = {
providerId: EmailProviderIdEnum.SendGrid,
channel: ChannelTypeEnum.EMAIL,
active: true,
check: false,
};

const {
body: { data },
} = await session.testAgent.post('/v1/integrations').send(payload);

expect(data.priority).to.equal(1);
expect(data.primary).to.equal(true);
expect(data.active).to.equal(true);
});

it(
'should set the integration as primary when its active ' +
'and there are no other active integrations excluding Novu',
async function () {
await integrationRepository.deleteMany({
_organizationId: session.organization._id,
_environmentId: session.environment._id,
});

const novuEmail = await integrationRepository.create({
name: 'novuEmail',
identifier: 'novuEmail',
providerId: EmailProviderIdEnum.Novu,
channel: ChannelTypeEnum.EMAIL,
active: true,
primary: false,
priority: 1,
_organizationId: session.organization._id,
_environmentId: session.environment._id,
});

const payload = {
providerId: EmailProviderIdEnum.SendGrid,
channel: ChannelTypeEnum.EMAIL,
active: true,
check: false,
};

const {
body: { data },
} = await session.testAgent.post('/v1/integrations').send(payload);

expect(data.priority).to.equal(2);
expect(data.primary).to.equal(true);
expect(data.active).to.equal(true);

const [first, second] = await await integrationRepository.find(
{
_organizationId: session.organization._id,
_environmentId: session.environment._id,
channel: ChannelTypeEnum.EMAIL,
},
undefined,
{ sort: { priority: -1 } }
);

expect(first._id).to.equal(data._id);
expect(first.primary).to.equal(true);
expect(first.active).to.equal(true);
expect(first.priority).to.equal(2);

expect(second._id).to.equal(novuEmail._id);
expect(second.primary).to.equal(false);
expect(second.active).to.equal(true);
expect(second.priority).to.equal(1);
}
);

it('should not set the integration as primary when there is primary integration', async function () {
await integrationRepository.deleteMany({
_organizationId: session.organization._id,
_environmentId: session.environment._id,
});

const primaryIntegration = await integrationRepository.create({
name: 'primaryIntegration',
identifier: 'primaryIntegration',
providerId: EmailProviderIdEnum.SendGrid,
channel: ChannelTypeEnum.EMAIL,
active: true,
primary: true,
priority: 1,
_organizationId: session.organization._id,
_environmentId: session.environment._id,
});

const payload = {
providerId: EmailProviderIdEnum.SendGrid,
channel: ChannelTypeEnum.EMAIL,
active: true,
check: false,
};

const {
body: { data },
} = await session.testAgent.post('/v1/integrations').send(payload);

expect(data.priority).to.equal(1);
expect(data.primary).to.equal(false);
expect(data.active).to.equal(true);

const [first, second] = await await integrationRepository.find(
{
_organizationId: session.organization._id,
_environmentId: session.environment._id,
channel: ChannelTypeEnum.EMAIL,
},
undefined,
{ sort: { priority: -1 } }
);

expect(first._id).to.equal(primaryIntegration._id);
expect(first.primary).to.equal(true);
expect(first.active).to.equal(true);
expect(first.priority).to.equal(2);

expect(second._id).to.equal(data._id);
expect(second.primary).to.equal(false);
expect(second.active).to.equal(true);
expect(second.priority).to.equal(1);
});

it('should calculate the highest priority but not set primary if there is another active integration', async function () {
await integrationRepository.deleteMany({
_organizationId: session.organization._id,
_environmentId: session.environment._id,
});

const activeIntegration = await integrationRepository.create({
name: 'activeIntegration',
identifier: 'activeIntegration',
providerId: EmailProviderIdEnum.SendGrid,
channel: ChannelTypeEnum.EMAIL,
active: true,
primary: false,
priority: 1,
_organizationId: session.organization._id,
_environmentId: session.environment._id,
});

const payload = {
providerId: EmailProviderIdEnum.SendGrid,
channel: ChannelTypeEnum.EMAIL,
active: true,
check: false,
};

const {
body: { data },
} = await session.testAgent.post('/v1/integrations').send(payload);

expect(data.priority).to.equal(2);
expect(data.primary).to.equal(false);
expect(data.active).to.equal(true);

const [first, second] = await await integrationRepository.find(
{
_organizationId: session.organization._id,
_environmentId: session.environment._id,
channel: ChannelTypeEnum.EMAIL,
},
undefined,
{ sort: { priority: -1 } }
);

expect(first._id).to.equal(data._id);
expect(first.primary).to.equal(false);
expect(first.active).to.equal(true);
expect(first.priority).to.equal(2);

expect(second._id).to.equal(activeIntegration._id);
expect(second.primary).to.equal(false);
expect(second.active).to.equal(true);
expect(second.priority).to.equal(1);
});
});

async function insertIntegrationTwice(
Expand Down
11 changes: 4 additions & 7 deletions apps/api/src/app/integrations/e2e/get-active-integration.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Get Active Integrations [IS_MULTI_PROVIDER_CONFIGURATION_ENABLED=true]
expect(chatIntegration.length).to.equal(4);

const selectedInAppIntegrations = filterEnvIntegrations(inAppIntegration, session.environment._id);
expect(selectedInAppIntegrations.length).to.equal(1);
expect(selectedInAppIntegrations.length).to.equal(0);

const selectedEmailIntegrations = filterEnvIntegrations(emailIntegration, session.environment._id);
expect(selectedEmailIntegrations.length).to.equal(1);
Expand All @@ -59,13 +59,10 @@ describe('Get Active Integrations [IS_MULTI_PROVIDER_CONFIGURATION_ENABLED=true]
expect(selectedSmsIntegrations.length).to.equal(1);

const selectedPushIntegrations = filterEnvIntegrations(pushIntegration, session.environment._id);
expect(selectedPushIntegrations.length).to.equal(1);
expect(selectedPushIntegrations.length).to.equal(0);

const selected = chatIntegration.filter((integration) => integration.selected);
const notSelected = chatIntegration.filter((integration) => !integration.selected);

expect(selected.length).to.equal(2);
expect(notSelected.length).to.equal(2);
const selectedChatIntegrations = filterEnvIntegrations(chatIntegration, session.environment._id);
expect(selectedChatIntegrations.length).to.equal(0);

for (const integration of activeIntegrations) {
expect(integration.active).to.equal(true);
Expand Down
Loading