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

🐞🔨 Marketplace: DeliveryAreas cannot be Removed when in use #2015

Merged

Conversation

zspencer
Copy link
Member

This:

  • Removes the affordance to destroy DeliveryAreas with an Order
  • Un-sets Cart#delivery_area on DeliveryAreas#destroy

@zspencer zspencer requested review from a team December 10, 2023 21:42
@zspencer zspencer marked this pull request as draft December 10, 2023 21:50
@zspencer zspencer marked this pull request as ready for review December 10, 2023 22:41
@zspencer zspencer added 🐞 bug Something isn't working 🔨 Squashed Bugs we think we fixed labels Dec 10, 2023
# rubocop:disable Rails/SkipsModelValidations
delivery_area.carts.update_all(delivery_area_id: nil)
# rubocop:enable Rails/SkipsModelValidations
delivery_area.destroy
Copy link
Member

Choose a reason for hiding this comment

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

Might be worth wrapping these two lines in a transaction.

Copy link
Member Author

Choose a reason for hiding this comment

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

ooo good point.

Copy link
Member

Choose a reason for hiding this comment

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

Random thought: could we accomplish this with something like this on the Delivery Area model?

  has_many :pre_checkout_carts, ...appropriate scopes go here..., dependent: :nullify

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe? I forgot about dependent: :nullify; but the test coverage should prove it out.

@@ -13,7 +13,7 @@ def create

when "checkout.session.completed"
payment_intent = Stripe::PaymentIntent.retrieve(event.data.object.payment_intent, {api_key: marketplace.stripe_api_key})
order = marketplace.orders.find_by(id: payment_intent.transfer_group)
order = Order.unscoped.find_by(id: payment_intent.transfer_group, marketplace:)
Copy link
Member

Choose a reason for hiding this comment

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

I don't love how this reads. It immediately makes me want to go and check why does this need to be unscoped and what does that mean. Does that mean that really this is a "cart_or_order"? Or should this be a "cart"? Or something else?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a consequence of that default scope that I added; I'm going to revert that.

@@ -1,6 +1,7 @@
class Marketplace
class Order < Record
self.table_name = "marketplace_orders"
default_scope { where.not(status: :pre_checkout) }
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need a default scope here? How certain are we that this won't introduce yet another bug somewhere where an edge case isn't tested? (I'm not certain at all, personally.)

I'd rather we explicitly applied named scopes as needed, instead of having a default scope.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea, this is a side effect of not having split the marketplace_carts and marketplace_orders tables out. I like putting it in a named scope!

OK, this should be a bit nicer. There's some other cleanup I want to do
in another PR to:

- Rip out the Turbo Streams (they don't seem useful)
- Make the button say "Archive" when there are Orders
@zspencer zspencer force-pushed the marketplace/delivery-areas/cannot-be-removed-when-in-use branch from 160f3ac to d600a0c Compare December 11, 2023 21:36
@zspencer zspencer requested review from anaulin and a team December 11, 2023 21:55
has_many :orders, inverse_of: :delivery_area
has_many :carts, inverse_of: :delivery_area
has_many :orders, -> { checked_out }, inverse_of: :delivery_area
has_many :carts, inverse_of: :delivery_area, dependent: :nullify
Copy link
Member

Choose a reason for hiding this comment

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

Ah, and this works because Marketplace::Carts has a default scope of :pre_checkout, right? Nice

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, which is part of why I kinda-like the default scopes; but also I now have a ticket for the refactor: #2020

Copy link
Member

@anaulin anaulin left a comment

Choose a reason for hiding this comment

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

Nice, ship it!

@zspencer zspencer merged commit 7d9862f into main Dec 11, 2023
4 checks passed
@zspencer zspencer deleted the marketplace/delivery-areas/cannot-be-removed-when-in-use branch December 11, 2023 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working 🔨 Squashed Bugs we think we fixed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants