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: proper error handling for missing subscriber on preference update #2754

Merged
merged 4 commits into from
Feb 15, 2023

Conversation

scopsy
Copy link
Contributor

@scopsy scopsy commented Feb 13, 2023

What change does this PR introduce?

  • Refactor usage of subscriberId and internal subscriber id on fetch preference
  • Throw exception when subscriber not found
  • Added missing change

Why was this change needed?

NV-1459

Other information (Screenshots)

@linear
Copy link

linear bot commented Feb 13, 2023

NV-1459 Add subscriber validation on /subscribers/:subscriberid/preferences

At the moment if the user is passing an incorrect subscriberid the server will return 500.

we need to add validation + throwing API error with more informative message

const buildCommand = GetSubscriberTemplatePreferenceCommand.create({
organizationId: job._organizationId,
subscriberId: job._subscriberId,
subscriberId: subscriber.subscriberId,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

to make sure we only pass here the public subscriberId to avoid confusion

Copy link
Contributor

Choose a reason for hiding this comment

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

This flow is too confusing! that's why I refactored it on the #2489 😀

@@ -25,11 +26,16 @@ export class GetSubscriberTemplatePreference {

async execute(command: GetSubscriberTemplatePreferenceCommand): Promise<ISubscriberPreferenceResponse> {
const activeChannels = await this.queryActiveChannels(command);
const subscriber = await this.subscriberRepository.findBySubscriberId(command.environmentId, command.subscriberId);
const 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.

To avoid a query when not needed

@@ -174,11 +175,15 @@ export class SendMessage {
environmentId: job._environmentId,
});

const subscriber = await this.subscriberRepository.findById(job._subscriberId);
Copy link
Contributor

Choose a reason for hiding this comment

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

In this PR #2489 we added user's subscriberId to the job entity. I will add a not note on this PR to get the subscriber from the cache first.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Awesome so we can fix it there later 🎉

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah i left a note there so we wont forget 😅

@@ -174,11 +175,15 @@ export class SendMessage {
environmentId: job._environmentId,
});

const subscriber = await this.subscriberRepository.findById(job._subscriberId);
if (!subscriber) throw new ApiException('Subscriber not found with id ' + job._subscriberId);
Copy link
Contributor

Choose a reason for hiding this comment

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

Btw I am not sure we need to add the exception here because if the flow got till here then we are after ProcessSubscriber usecase, in that case, the execute function already fetched/created a subscriber and passed it.

the possible exception we can get is when the user tries to update an existing subscriber through
/subscribers/:subscriberid/preferences
on the GetPreferences usecase flow.
wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

never mind i can see that you added a check on the get-subscriber-template-preference.usecase.ts. :)


const subscriberPreference = await this.subscriberPreferenceRepository.findOne({
_environmentId: command.environmentId,
_subscriberId: subscriber !== null ? subscriber._id : command.subscriberId,
_subscriberId: subscriber._id,
Copy link
Contributor

Choose a reason for hiding this comment

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

🤩

Copy link
Contributor

@djabarovgeorge djabarovgeorge left a comment

Choose a reason for hiding this comment

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

Looks great to me :)

scopsy and others added 3 commits February 14, 2023 11:28
…preference/get-subscriber-template-preference.usecase.ts

Co-authored-by: George Djabarov <39195835+djabarovgeorge@users.noreply.github.com>
@scopsy scopsy added this pull request to the merge queue Feb 15, 2023
Merged via the queue into next with commit 5ded62c Feb 15, 2023
@scopsy scopsy deleted the nv-1459-add-subscriber-validation-on branch February 15, 2023 09:45
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.

None yet

2 participants