Skip to content

Commit

Permalink
🧹🥗🥔 Marketplace: Use DeliveryArea#price to calculate `Cart#price_…
Browse files Browse the repository at this point in the history
…total` (#1323)

* 🥔 `Marketplace`: Use `DeliveryArea#price` to calculate `Cart#price_total`

- #1136

Now the pricing reflects the delivery area when the Marketplace and Cart
have them! Fancy!

I feel like this is a reasonable amount of test coverage for this
change, but if we want to add `request` or `component` specs or
something I could be persuaded...

* 🧹`Marketplace`: Missed some attributes for `Delivery`
  • Loading branch information
zspencer authored Apr 9, 2023
1 parent 7d783ea commit 7b8b5f7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 22 deletions.
7 changes: 1 addition & 6 deletions app/furniture/marketplace/cart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ def product_total
def delivery
@delivery ||= becomes(Delivery)
end

def delivery_fee
return marketplace.delivery_fee if delivery_address.present?

0
end
delegate :fee, to: :delivery, prefix: true

def tax_total
cart_products.sum(0, &:tax_amount)
Expand Down
9 changes: 1 addition & 8 deletions app/furniture/marketplace/cart/delivery.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
class Marketplace
class Cart
class Delivery < Cart
class Delivery < ::Marketplace::Delivery
extend StripsNamespaceFromModelName
location(routed_as: :resource, parent: :cart)
attribute :delivery_window, ::Marketplace::Delivery::WindowType.new
def window
delivery_window
end

belongs_to :delivery_area
validates :delivery_area_id, presence: true # rubocop:disable Rails/RedundantPresenceValidationOnBelongsTo

validates :contact_email, presence: true
validates :contact_phone_number, presence: true
Expand Down
11 changes: 11 additions & 0 deletions app/furniture/marketplace/delivery.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
class Marketplace
class Delivery < Record
self.table_name = "marketplace_orders"

belongs_to :marketplace
belongs_to :shopper
belongs_to :delivery_area
validates :delivery_area_id, presence: true # rubocop:disable Rails/RedundantPresenceValidationOnBelongsTo

has_encrypted :delivery_address
attribute :delivery_window, WindowType.new
has_encrypted :contact_phone_number
has_encrypted :contact_email

def window
delivery_window
end

def fee
delivery_area&.price.presence || marketplace&.delivery_fee.presence
end
end
end
5 changes: 5 additions & 0 deletions spec/factories/furniture/marketplace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@
price { Faker::Commerce.price }
end

factory :marketplace_delivery, class: "Marketplace::Delivery" do
marketplace
shopper { association(:marketplace_shopper) }
end

factory :marketplace_cart_delivery, class: "Marketplace::Cart::Delivery" do
marketplace { association(:marketplace, :with_delivery_areas) }
delivery_address { Faker::Address.full_address }
Expand Down
8 changes: 1 addition & 7 deletions spec/furniture/marketplace/cart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@
cart.cart_products.create!(product: product_b, quantity: 2)
end

it { is_expected.to eql(product_a.price + product_b.price * 2 + (product_b.price * 2 * 0.05)) }

context "when the #delivery_address is present" do
let(:cart) { create(:marketplace_cart, delivery_address: "123", marketplace: marketplace) }

it { is_expected.to eql(product_a.price + product_b.price * 2 + marketplace.delivery_fee + (product_b.price * 2 * 0.05)) }
end
it { is_expected.to eql(product_a.price + product_b.price * 2 + cart.delivery_fee + (product_b.price * 2 * 0.05)) }
end

describe "#product_total" do
Expand Down
27 changes: 26 additions & 1 deletion spec/furniture/marketplace/delivery_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require "rails_helper"

RSpec.describe Marketplace::Delivery, type: :model do
subject(:delivery) { described_class.new }
subject(:delivery) { build(:marketplace_delivery, marketplace: marketplace, delivery_area: delivery_area) }

let(:marketplace) { build(:marketplace) }
let(:delivery_area) { nil }

it { is_expected.to belong_to(:marketplace) }
it { is_expected.to belong_to(:shopper) }
Expand All @@ -12,6 +15,28 @@
it { is_expected.to be_a(Marketplace::Delivery::Window) }
end

describe "#fee" do
subject(:fee) { delivery.fee }

context "when there is not a marketplace delivery fee or a delivery area delivery fee" do
it { is_expected.to eq(0) }
end

context "when the marketplace has a delivery fee and the delivery area has a delivery fee" do
let(:marketplace) { build(:marketplace, delivery_fee_cents: 25_00) }
let(:delivery_area) { build(:marketplace_delivery_area, price_cents: 50_00, marketplace: marketplace) }

it { is_expected.to eq(delivery_area.price) }
end

context "when the marketplace has a delivery fee but the delivery area does not" do
let(:marketplace) { build(:marketplace, delivery_fee_cents: 25_00) }
let(:delivery_area) { build(:marketplace_delivery_area, price_cents: nil, marketplace: marketplace) }

it { is_expected.to eq(marketplace.delivery_fee) }
end
end

describe "#window" do
subject(:window) { delivery.window }

Expand Down

0 comments on commit 7b8b5f7

Please sign in to comment.