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

Simplify Coupon PromotionHandler #521

Merged
merged 6 commits into from
Oct 4, 2017

Conversation

jhawthorn
Copy link
Contributor

The controversial commit should be the last one where I remove some functionality. Hopefully I explained my reasoning well in the commit message.

@cbrunsdon
Copy link
Contributor

As discussed IRL hawth is going to throw some test cases about what didn't work before and how the new behaviour should be.

@jhawthorn
Copy link
Contributor Author

This removes a fair chunk of messaging we gave to clients that was (usually) right, doesn't it? Like the fact the coupon might be inelligible because a better one exists? I'm not sure I'm comfortable removing that.

Wait, how and when do coupons get applied? was the update_totals and persist_totals ever ran? It seems like this changes quite a bit of behaviour....

update_totals and persist_totals are still run they've been moved to the other method. This raises a good point: with the existing behaviour, it was possible that applying the coupon could put the order in an inconsistent state. If the first adjustment was marked ineligible (there was a better coupon) but was applied to a different adjustment in the order, the order would not have the correct totals until another order.update was called.

@jhawthorn
Copy link
Contributor Author

@cbrunsdon Added a test for the previously broken behaviour. It fails on versions before 02ed14f (because successful? is false, despite applying the coupon) and passes now.

@cbrunsdon
Copy link
Contributor

To be clear: there were no tests testing the :coupon_code_not_found and :coupon_code_better_exists logic that was previously being returned and we are changing that behaviour.

Yes, those happen under pretty dubious circumstances, but that is the existing behaviour.

Does anyone want to chip in on if they rely on those being returned or not? Maybe @alepore or @tvdeyen?

@peterberkenbosch
Copy link
Contributor

@jhawthorn do we need to revisit this one?

Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

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

This looks like a good change for me. Could we continue work on this (if anything is necessary) or just close this and create a TODO issue otherwise.

Use any? on an array of results instead of or-ing on each iteration.
Copy link
Member

@gmacdougall gmacdougall left a comment

Choose a reason for hiding this comment

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

I like this. It needs a rebase, but I think it should be good.

@@ -237,8 +237,7 @@ def blacklisted?(promotable)
when Spree::LineItem
!promotable.product.promotionable?
when Spree::Order
promotable.line_items.any? &&
promotable.line_items.joins(:product).where(spree_products: { promotionable: false }).any?
promotable.line_items.joins(:product).where(spree_products: {promotionable: false}).exists?

Choose a reason for hiding this comment

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

Space inside { missing.
Space inside } missing.

This was running one query to check if there were any line items and a
second to check if any were from unpromotable products. The first check
is redundant.
Rather than refering to it as order.coupon_code.downcase throughout.
This could later be refactored to it being passed into the class rather
than stored as as non-volatile on the order.
Removed determine_promotion_application_result. This code previously
looked through all of the adjustments on the order in an attempt to
determine if this promotion was in effect, or if a previously applied
promotion was taking precedence.

I've removed this logic for a few reasons:

* It could return false negatives. It only considered the first matching
  adjustment, so it would return an error even if that adjustment was
  ineligible even if a later one was in use.

* It doesn't make sense to return an error here because adjustments and
  an OrderPromotion have been created. Even if they are now ineligible
  they could become eligible later.

* This ties coupon application to actions which create adjustments.
  Other actions (like the defunct CreateLineItems) would either have to
  be added here.

* Our test suites did not cover either of these failure cases.
@gmacdougall gmacdougall merged commit ea56055 into solidusio:master Oct 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants