-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Auto Remove Deleted Subscribers from topic #5187
Merged
djabarovgeorge
merged 2 commits into
next
from
nv-3230-1-auto-remove-deleted-subscribers-from-topic
Feb 13, 2024
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
137 changes: 137 additions & 0 deletions
137
apps/api/migrations/topic-subscriber-normalize/topic-subscriber-normalize.migration.spec.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,137 @@ | ||
import axios from 'axios'; | ||
import { beforeEach } from 'mocha'; | ||
import { expect } from 'chai'; | ||
|
||
import { ExternalSubscriberId, TopicId, TopicKey, TopicName } from '@novu/shared'; | ||
import { SubscriberEntity, SubscriberRepository, TopicSubscribersRepository } from '@novu/dal'; | ||
import { UserSession, SubscribersService } from '@novu/testing'; | ||
|
||
import { topicSubscriberNormalize } from './topic-subscriber-normalize.migration'; | ||
|
||
const axiosInstance = axios.create(); | ||
const TOPIC_PATH = '/v1/topics'; | ||
|
||
describe('Remove all the stale topic subscriber relations', () => { | ||
let session: UserSession; | ||
let subscriberService: SubscribersService; | ||
const subscriberRepository = new SubscriberRepository(); | ||
const topicSubscribersRepository = new TopicSubscribersRepository(); | ||
|
||
beforeEach(async () => { | ||
session = new UserSession(); | ||
await session.initialize(); | ||
subscriberService = new SubscribersService(session.organization._id, session.environment._id); | ||
}); | ||
|
||
it('should remove topic subscriber relation record on removed subscribers', async () => { | ||
const subscriberId = '123'; | ||
const createdSubscriber = await subscriberService.createSubscriber({ subscriberId: subscriberId }); | ||
const firstTopicKey = `topic-key-1-trigger-event`; | ||
const firstTopicName = `topic-name-1-trigger-event`; | ||
const newTopic = await createTopic(session, firstTopicKey, firstTopicName); | ||
await addSubscribersToTopic(session, { _id: newTopic._id, key: newTopic.key }, [createdSubscriber]); | ||
|
||
// create subscriber and its relation to topic | ||
const subscriber = await subscriberRepository.findBySubscriberId(session.environment._id, subscriberId); | ||
const topicSubscriber = await topicSubscribersRepository.findOne({ | ||
_environmentId: session.environment._id, | ||
_organizationId: session.organization._id, | ||
externalSubscriberId: subscriberId, | ||
}); | ||
|
||
if (!subscriber) { | ||
expect(subscriber).to.be.ok; | ||
throw new Error('Subscriber not found'); | ||
} | ||
if (!topicSubscriber) { | ||
expect(topicSubscriber).to.be.ok; | ||
throw new Error('topicSubscriber not found'); | ||
} | ||
|
||
expect(subscriber.subscriberId).to.be.equal(subscriberId); | ||
expect(topicSubscriber.externalSubscriberId).to.be.equal(subscriberId); | ||
// END - create subscriber and its relation to topic | ||
|
||
await subscriberRepository.delete({ | ||
_environmentId: subscriber._environmentId, | ||
_organizationId: subscriber._organizationId, | ||
subscriberId: subscriber.subscriberId, | ||
}); | ||
|
||
const subscriberAfterDeletion = await subscriberRepository.findBySubscriberId( | ||
session.environment._id, | ||
subscriberId | ||
); | ||
const topicSubscriberAfterDeletion = await topicSubscribersRepository.findOne({ | ||
_environmentId: session.environment._id, | ||
_organizationId: session.organization._id, | ||
externalSubscriberId: subscriberId, | ||
}); | ||
|
||
expect(subscriberAfterDeletion).to.not.be.ok; | ||
expect(topicSubscriberAfterDeletion).to.be.ok; | ||
|
||
await topicSubscriberNormalize(); | ||
|
||
const topicSubscriberAfterMigration = await topicSubscribersRepository.findOne({ | ||
_environmentId: session.environment._id, | ||
_organizationId: session.organization._id, | ||
externalSubscriberId: subscriberId, | ||
}); | ||
|
||
expect(topicSubscriberAfterMigration).to.not.be.ok; | ||
}); | ||
}); | ||
|
||
const createTopic = async ( | ||
session: UserSession, | ||
key: TopicKey, | ||
name: TopicName | ||
): Promise<{ _id: TopicId; key: TopicKey }> => { | ||
const response = await axiosInstance.post( | ||
`${session.serverUrl}${TOPIC_PATH}`, | ||
{ | ||
key, | ||
name, | ||
}, | ||
{ | ||
headers: { | ||
authorization: `ApiKey ${session.apiKey}`, | ||
}, | ||
} | ||
); | ||
|
||
expect(response.status).to.eql(201); | ||
const body = response.data; | ||
expect(body.data._id).to.exist; | ||
expect(body.data.key).to.eql(key); | ||
|
||
return body.data; | ||
}; | ||
|
||
const addSubscribersToTopic = async ( | ||
session: UserSession, | ||
createdTopicDto: { _id: TopicId; key: TopicKey }, | ||
subscribers: SubscriberEntity[] | ||
) => { | ||
const subscriberIds: ExternalSubscriberId[] = subscribers.map( | ||
(subscriber: SubscriberEntity) => subscriber.subscriberId | ||
); | ||
|
||
const response = await axiosInstance.post( | ||
`${session.serverUrl}${TOPIC_PATH}/${createdTopicDto.key}/subscribers`, | ||
{ | ||
subscribers: subscriberIds, | ||
}, | ||
{ | ||
headers: { | ||
authorization: `ApiKey ${session.apiKey}`, | ||
}, | ||
} | ||
); | ||
|
||
expect(response.status).to.be.eq(200); | ||
expect(response.data.data).to.be.eql({ | ||
succeeded: subscriberIds, | ||
}); | ||
}; |
50 changes: 50 additions & 0 deletions
50
apps/api/migrations/topic-subscriber-normalize/topic-subscriber-normalize.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,50 @@ | ||
import '../../src/config'; | ||
|
||
import { NestFactory } from '@nestjs/core'; | ||
import { SubscriberRepository, TopicSubscribersRepository } from '@novu/dal'; | ||
|
||
import { AppModule } from '../../src/app.module'; | ||
|
||
/* | ||
* topic subscriber normalize - will remove deleted subscribers from topic subscribers | ||
*/ | ||
export async function topicSubscriberNormalize() { | ||
// eslint-disable-next-line no-console | ||
console.log('start migration - topic subscriber normalize - will remove deleted subscribers from topic subscribers'); | ||
|
||
const app = await NestFactory.create(AppModule, { | ||
logger: false, | ||
}); | ||
const topicSubscribersRepository = app.get(TopicSubscribersRepository); | ||
const subscriberRepository = app.get(SubscriberRepository); | ||
|
||
const cursor = await topicSubscribersRepository._model | ||
.find({} as any) | ||
.batchSize(1000) | ||
.cursor(); | ||
|
||
for await (const topicSubscriber of cursor) { | ||
const subscriber = await subscriberRepository.findBySubscriberId( | ||
topicSubscriber._environmentId.toString(), | ||
topicSubscriber.externalSubscriberId | ||
); | ||
|
||
if (!subscriber) { | ||
// eslint-disable-next-line no-console | ||
console.log( | ||
`remove relation topic subscriber ${topicSubscriber.externalSubscriberId} from topic ${topicSubscriber._topicId}` | ||
); | ||
|
||
await topicSubscribersRepository.delete({ | ||
_environmentId: topicSubscriber._environmentId.toString(), | ||
_organizationId: topicSubscriber._organizationId, | ||
externalSubscriberId: topicSubscriber.externalSubscriberId, | ||
}); | ||
} | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.log('end migration- topic subscriber normalize'); | ||
|
||
app.close(); | ||
} |
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
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
Oops, something went wrong.
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.
wow, that was easy peasy :D
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.
easy peasy but we postponed the async matter here, but imo we definitely should start the discussion in order to support possible long processing into an async service like an external queue.
REF https://linear.app/novu/issue/NV-3230/auto-remove-deleted-subscribers-from-topic#comment-13955c84