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

fix(api): Subscriber deletion side-effects #6872

Merged
merged 6 commits into from
Nov 8, 2024

Conversation

SokratisVidros
Copy link
Contributor

What changed? Why was the change needed?

Clear subscribers along with all their related data and their cached entries

)?.[0];

expect(deletedSubscriber.deleted).to.equal(true);
const subscriber = await subscriberRepository.findBySubscriberId(session.environment._id, '123');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simplify the E2E so that the findDeleted can be removed.

@@ -51,10 +47,9 @@ describe('Remove Subscriber', function () {
organizationId: session.organization._id,
})
);
throw new Error('Should not reach here');
expect(true, 'Should never reach this statement').to.be.false;
Copy link
Contributor Author

@SokratisVidros SokratisVidros Nov 6, 2024

Choose a reason for hiding this comment

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

A small refactoring without throwing an error will trigger the catch block again and will provide a better message for the test results.

}),
});
}),
this.invalidateCache.invalidateQuery({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Clear inbox entries from cache

_environmentId: subscriber._environmentId,
_organizationId: subscriber._organizationId,
subscriberId: subscriber.subscriberId,
await this.subscriberRepository.withTransaction(async () => {
Copy link
Contributor Author

@SokratisVidros SokratisVidros Nov 6, 2024

Choose a reason for hiding this comment

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

Delete all related records atomically. Postgres, I miss you!

@@ -157,55 +157,19 @@ export class SubscriberRepository extends BaseRepository<SubscriberDBModel, Subs
);
}

async delete(query: SubscriberDeleteQuery) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Clean ups for unused methods

@@ -28,13 +28,6 @@ export class PreferencesRepository extends BaseRepository<PreferencesDBModel, Pr
return this.mapEntity(item);
}

async delete(query: PreferencesQuery) {
const item = await this.findOne({ _id: query._id, _environmentId: query._environmentId });
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no need for an extra query when trying to delete a record. The number of affected rows returned by mongoose indicates if the record was found or not.

});
});

if (deletedSubscriberCount === 0) {
Copy link
Contributor Author

@SokratisVidros SokratisVidros Nov 6, 2024

Choose a reason for hiding this comment

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

Usually, I prefer always returning a 2xx even if the record is already deleted but since this is a breaking change for the API, I preserved the previous 404 error.

@SokratisVidros SokratisVidros force-pushed the subscriber_deletion_sideeffects branch from 1ace52f to c544a89 Compare November 6, 2024 15:18
@SokratisVidros SokratisVidros changed the title Subscriber deletion sideeffects fix(api): Subscriber deletion side-effects Nov 6, 2024
Copy link

netlify bot commented Nov 6, 2024

Deploy Preview for novu-stg-vite-dashboard-poc ready!

Name Link
🔨 Latest commit 4e1a6f1
🔍 Latest deploy log https://app.netlify.com/sites/novu-stg-vite-dashboard-poc/deploys/672dc87379a3d30008b74415
😎 Deploy Preview https://deploy-preview-6872--novu-stg-vite-dashboard-poc.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Collaborator

@rifont rifont left a comment

Choose a reason for hiding this comment

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

💯

…t-preferences.usecase.ts

Co-authored-by: Richard Fontein <32132657+rifont@users.noreply.github.com>
@SokratisVidros SokratisVidros merged commit 5a0efd7 into next Nov 8, 2024
9 of 10 checks passed
@SokratisVidros SokratisVidros deleted the subscriber_deletion_sideeffects branch November 8, 2024 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants