From ac27fdfc57fd48676ad0842cfba16bae8a60b1b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20S=C3=B6derberg?= Date: Mon, 31 Jul 2023 13:30:49 +0200 Subject: [PATCH] feat: add migration script to add novu providers to database --- .../novu-integrations.migration.ts | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 apps/api/migrations/novu-integrations/novu-integrations.migration.ts diff --git a/apps/api/migrations/novu-integrations/novu-integrations.migration.ts b/apps/api/migrations/novu-integrations/novu-integrations.migration.ts new file mode 100644 index 00000000000..9ad71819ab1 --- /dev/null +++ b/apps/api/migrations/novu-integrations/novu-integrations.migration.ts @@ -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, + }); + + 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();