Skip to content

Commit

Permalink
Merge pull request #3873 from novuhq/nv-2607-api-update-integrations-…
Browse files Browse the repository at this point in the history
…logic-for-primary-and-private-fields

feat(api): set integration as primary and priority system
  • Loading branch information
LetItRock authored Aug 8, 2023
2 parents d5303b3 + 57a7839 commit 70eb4c7
Show file tree
Hide file tree
Showing 23 changed files with 2,199 additions and 185 deletions.
10 changes: 7 additions & 3 deletions apps/api/src/app/events/e2e/trigger-event.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,8 @@ describe(`Trigger event - ${eventTriggerPath} (POST)`, function () {
{
$set: {
active: true,
primary: true,
priority: 1,
},
}
);
Expand Down Expand Up @@ -851,8 +853,6 @@ describe(`Trigger event - ${eventTriggerPath} (POST)`, function () {
const newSubscriberIdInAppNotification = SubscriberRepository.createObjectId();
const channelType = ChannelTypeEnum.EMAIL;

template = await createTemplate(session, channelType);

template = await session.createTemplate({
steps: [
{
Expand Down Expand Up @@ -894,7 +894,11 @@ describe(`Trigger event - ${eventTriggerPath} (POST)`, function () {
check: false,
};

await session.testAgent.post('/v1/integrations').send(payload);
const {
body: { data },
} = await session.testAgent.post('/v1/integrations').send(payload);
await session.testAgent.post(`/v1/integrations/${data._id}/set-primary`).send({});

await sendTrigger(session, template, newSubscriberIdInAppNotification, {
nested: {
subject: 'a subject nested',
Expand Down

This file was deleted.

270 changes: 269 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,267 @@ 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 () {
await integrationRepository.deleteMany({
_organizationId: session.organization._id,
_environmentId: session.environment._id,
});

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(false);
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
Loading

0 comments on commit 70eb4c7

Please sign in to comment.