-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
davidsoderberg
merged 1 commit into
nv-2503-store-novu-providers-email-sms-in-the-database
from
nv-2559-create-migration-script-to-create-dev-and-prod-providers-for
Aug 2, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
apps/api/migrations/novu-integrations/novu-integrations.migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
|
||
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(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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