[go: up one dir, main page]

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

refactor(api): Use UpdatePreference use-case for all Subscriber Preference updates #6889

Merged

Conversation

rifont
Copy link
Contributor
@rifont rifont commented Nov 7, 2024

What changed? Why was the change needed?

  • Use UpdatePreference use-case for all Subscriber Preference updates
    • The use-cases previously used by Subscriber and Widget API were not updating V2 Preferences. This resulted in cases of Subscriber Preference reads to incorrectly return default preferences
  • Skip all tests that asserted on the top-level enabled flag
    • The enabled flag was introduced at a time before critical workflows were available. Setting the enabled flag to false on a preference set previously had the effect of preventing that Subscriber's preference settings from being read during notification delivery preference resolution. The Workflow critical flag handles this use-case in a more read-optimal way by requiring only a single Workflow resource to be mutated, rather than all Subscriber resources. I have left these tests in the codebase for now, we can debate further on complete deletion of the enabled flag as part of NV-4597
  • Add instrumentation for the UpdatePreference use-case

Screenshots

Expand for optional sections

Related enterprise PR

Special notes for your reviewer

Copy link
linear bot commented Nov 7, 2024

Copy link
netlify bot commented Nov 7, 2024

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

Name Link
🔨 Latest commit b96fc0f
🔍 Latest deploy log https://app.netlify.com/sites/novu-stg-vite-dashboard-poc/deploys/67331d3e1110680008335a75
😎 Deploy Preview https://deploy-preview-6889--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.

@rifont rifont changed the title refactor(subscribers): update preference handling logic refactor(api): Use UpdatePreference use-case for all Subscriber Preference updates Nov 7, 2024
@rifont rifont marked this pull request as ready for review November 8, 2024 14:47
const response = await updateGlobalPreferences({}, session);

expect(response.data.data.preference.channels).to.eql({});
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test case asserts that empty objects can be supplied when updating global preferences in relation to this UpsertPreferences change

Copy link
Contributor Author
@rifont rifont Nov 8, 2024

Choose a reason for hiding this comment

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

As you will see further down, it's not ideal that we have some mapping logic in the controller level. I decided that's a suitable approach for now as we'll be performing a Subscriber API overhaul soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A bunch of deletions for the old use-cases that only updated V1 Preferences 🧹

@@ -67,6 +66,7 @@ export class UpdatePreferences {
return await this.findPreference(command, subscriber);
}

@Instrument()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding some more instrumentation to the update preference flow.

Copy link
Contributor Author
@rifont rifont Nov 8, 2024

Choose a reason for hiding this comment

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

In a follow-up PR (to keep this diff clean), I will rename this usecase from UpdatePreferences to UpsertSubscriberPreferences as it functions as such now.

} else {
await this.updateUserPreference(command, subscriber);
}
await this.updateSubscriberPreference(command, subscriber);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As said above, we treat this as an upsert now. GetSubscriberPreferences always returns a valid preference set now (falling back to default when nothing has been persisted), so there's no need to perform an extra lookup now before persisting the data.

Copy link
Contributor

Choose a reason for hiding this comment

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

Much cleaner!

} else {
await this.updateUserPreference(command, subscriber);
}
await this.updateSubscriberPreference(command, subscriber);
Copy link
Contributor

Choose a reason for hiding this comment

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

Much cleaner!

});
): Promise<Omit<UpdateSubscriberPreferenceResponseDto, 'template'>> {
const channels = body.preferences?.reduce((acc, curr) => {
acc[curr.type] = curr.enabled;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this enabled flag the same as the one we talked about in skipped tests?

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, this is the channel-level flag. There are 2 enabled flags:

  • enabled at top level of V1 Preferences, which controls whether to use the subscriber preference during preference merging
  • enabled flag for each channel, which controls delivery for the channel

@rifont rifont merged commit 55adc38 into next Nov 12, 2024
32 checks passed
@rifont rifont deleted the nv-4595-common-preference-use-cases-for-subscriber-api-inbox-api branch November 12, 2024 17:35
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.

2 participants