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: add migration script to add novu providers to database #3886

Merged
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import '../../src/config';
import {
OrganizationRepository,
EnvironmentRepository,
IntegrationRepository,
ChannelTypeEnum,
EnvironmentEntity,
} from '@novu/dal';
import { EmailProviderIdEnum, SmsProviderIdEnum } from '@novu/shared';
import { NestFactory } from '@nestjs/core';
import { AppModule } from '../../src/app.module';

const organizationRepository = new OrganizationRepository();
const environmentRepository = new EnvironmentRepository();
const integrationRepository = new IntegrationRepository();

const createNovuIntegration = async (
environment: EnvironmentEntity,
channel: ChannelTypeEnum.EMAIL | ChannelTypeEnum.SMS
) => {
const providerId = channel === ChannelTypeEnum.SMS ? SmsProviderIdEnum.Novu : EmailProviderIdEnum.Novu;

const count = await integrationRepository.count({
_environmentId: environment._id,
_organizationId: environment._organizationId,
providerId,
channel,
});

if (count > 0) {
return;
}

const countChannelIntegrations = await integrationRepository.count({
_environmentId: environment._id,
_organizationId: environment._organizationId,
channel,
});
Comment on lines +34 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we want to skip the creation if the user already has integration on the same channel?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No we just like to keep it disabled so the user that are used to see them still see them as usual even with the new list design


const response = await integrationRepository.create({
_environmentId: environment._id,
_organizationId: environment._organizationId,
providerId,
channel,
active: countChannelIntegrations === 0,
});

console.log('Created Integration' + response._id);
};

export async function createNovuIntegrations() {
// Init the mongodb connection
const app = await NestFactory.create(AppModule, {
logger: false,
});

// eslint-disable-next-line no-console
console.log('start migration - novu integrations');

// eslint-disable-next-line no-console
console.log('get organizations and its environments');

const organizations = await organizationRepository.find({});
const totalOrganizations = organizations.length;
let currentOrganization = 0;
for (const organization of organizations) {
currentOrganization += 1;
console.log(`organization ${currentOrganization} of ${totalOrganizations}`);

const environments = await environmentRepository.findOrganizationEnvironments(organization._id);
for (const environment of environments) {
await createNovuIntegration(environment, ChannelTypeEnum.SMS);
await createNovuIntegration(environment, ChannelTypeEnum.EMAIL);

console.log('Prococessed environment' + environment._id);
}

console.log('Prococessed organization' + organization._id);
}

// eslint-disable-next-line no-console
console.log('end migration');
}

createNovuIntegrations();