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: Refactor to turn Checkout into a Command #1062

Merged
merged 1 commit into from
Jan 22, 2023

Conversation

zspencer
Copy link
Member

#831

This "Demotes" Checkout from an Active Record and makes it serve more as a Command object than a Domain Model. It's still ActiveModel'y so it can be rendered using render checkout or routed to using redirect_to checkout.location, but otherwise it's not holding any data or what not.

This continues to allow us to leverage Rails' view-and-path-lookup ergonomics when rendering or redirecting to a Marketplace::Cart or a Marketplace::Order; but also adds a touch of duplication now that the domain models don't share a common ancestor.

@zspencer zspencer requested review from KellyAH, anaulin and a team and removed request for KellyAH January 21, 2023 01:35
Copy link
Member Author

@zspencer zspencer left a comment

Choose a reason for hiding this comment

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

OK, I think this gets us closer to a more reasonable design; without the weird cart/checkout/order trifecta.

I didn't inherit from Cart for Order and OrderedProduct from CartProduct (or vice versa ) but instead have them sharing the same database table...

I could imagine a world where the Cart class inherits from Order and adds the default scope; which would probably allow us to get rid of the CartProduct table and class definition as well; but I ran out of steam before getting htere.

self.location_parent = :marketplace

default_scope { where(status: :pre_checkout) }
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 ensures whenever we try and find a Cart it doesn't include anything that has been checked out.

has_many :cart_products, dependent: :destroy, inverse_of: :cart
has_many :products, through: :cart_products, inverse_of: :carts
has_one :checkout, inverse_of: :cart
has_one :order, inverse_of: :cart
Copy link
Member Author

Choose a reason for hiding this comment

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

Now we don't need these anymore, since the Checkout table is dead! DEAD!

}

def self.model_name
@_model_name ||= ActiveModel::Name.new(self, ::Marketplace)
end
Copy link
Member Author

Choose a reason for hiding this comment

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

I pulled this up into Marketplace::Record so all the Marketplace domain-objects don't need to define their own model-name.

}
}, {
api_key: marketplace.stripe_api_key
})
end

def complete(stripe_session_id:)
update!(status: :paid, stripe_session_id: stripe_session_id)
cart.update!(status: :checked_out)
cart.update!(status: :paid, stripe_session_id: stripe_session_id)
Copy link
Member Author

Choose a reason for hiding this comment

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

No more double update!

@@ -14,6 +13,9 @@ class Product < ApplicationRecord
has_many :cart_products, inverse_of: :product, dependent: :destroy
has_many :carts, through: :cart_products, inverse_of: :products

has_many :ordered_products, inverse_of: :product, dependent: :destroy
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 makes me think that maybe we don't want to be able to destroy Products that have been Ordered!

Copy link
Contributor

@KellyAH KellyAH Jan 22, 2023

Choose a reason for hiding this comment

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

  1. so we'll always have to keep a record of any products in the database if anyone has ordered them even if the product is removed from the Marketplace/is no longer for sale?
  2. Also have we decided on if products will be soft or hard deleted?

Guess that's okay. Shouldn't take up too much database space right?

Copy link
Member Author

@zspencer zspencer Jan 22, 2023

Choose a reason for hiding this comment

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

Yea, it would be scary if a Product getting destroyed the Orders OrderedProducts because then how would Vendors or Shoppers be able to see what was bought when?

Copy link
Member Author

Choose a reason for hiding this comment

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

(Note: our hope is that Database costs are covered on a per-space basis; but I'm not entirely sure how we would go about doing that yet :D)

@zspencer zspencer marked this pull request as draft January 21, 2023 02:28
@zspencer
Copy link
Member Author

Wooops, the tests aren't passing jus a sec.

#831

This "Demotes" `Checkout` from an Active Record and makes it serve more
as a Command object than a Domain Model. It's still ActiveModel'y so it
can be rendered using `render checkout` or routed to using `redirect_to
checkout.location`, but otherwise it's not holding any data or what not.
@zspencer zspencer force-pushed the marketplace/eliminate-checkout-table branch from 397f8ef to feba391 Compare January 21, 2023 02:39
@zspencer zspencer marked this pull request as ready for review January 21, 2023 02:48
has_many :products, through: :ordered_products, inverse_of: :orders

enum status: {
paid: "paid"
Copy link
Contributor

@KellyAH KellyAH Jan 22, 2023

Choose a reason for hiding this comment

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

note: I can imagine more order statuses will need to be added once customers start using the feature. refunded, canceled, delivered and maybe some of the ones listed here.

But yeah let's Keep It Simple for now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Concur!

@zspencer zspencer merged commit c39c1f6 into main Jan 22, 2023
@zspencer zspencer deleted the marketplace/eliminate-checkout-table branch January 22, 2023 17:40
@zspencer zspencer added this to the 1.0 - Andromeda milestone May 7, 2023
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.

3 participants