From 1f9dc065bcaabc915b099400caacc5b843c4acd9 Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Thu, 2 Mar 2023 15:19:03 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20`Marketplace`:=20Collect=20`Shopper?= =?UTF-8?q?`s=20Delivery=20Address=20before=20checkout?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - https://github.com/zinc-collective/convene/issues/1136 While collecting the address via Stripe was reasonable at the time, if we want to support dynamic pricing, we'll need the address ahead of time. This will also (theoretically) allow us to let people know when they are outside of the `Distributor` delivery area. --- app/furniture/marketplace/cart.rb | 6 +- app/furniture/marketplace/cart_policy.rb | 24 +++++++ .../marketplace/carts/_cart.html.erb | 66 ++++++++++++------- .../marketplace/carts/_footer.html.erb | 5 +- .../marketplace/carts/_total.html.erb | 12 ++-- app/furniture/marketplace/carts_controller.rb | 24 +++++++ app/furniture/marketplace/checkout.rb | 3 - spec/furniture/marketplace/cart_spec.rb | 8 ++- .../carts_controller_request_spec.rb | 21 ++++++ 9 files changed, 134 insertions(+), 35 deletions(-) create mode 100644 app/furniture/marketplace/cart_policy.rb create mode 100644 app/furniture/marketplace/carts_controller.rb create mode 100644 spec/furniture/marketplace/carts_controller_request_spec.rb diff --git a/app/furniture/marketplace/cart.rb b/app/furniture/marketplace/cart.rb index 2f974594a..10178fb3e 100644 --- a/app/furniture/marketplace/cart.rb +++ b/app/furniture/marketplace/cart.rb @@ -26,7 +26,11 @@ def product_total end end - delegate :delivery_fee, to: :marketplace + def delivery_fee + return marketplace.delivery_fee if delivery_address.present? + + 0 + end def price_total product_total + delivery_fee diff --git a/app/furniture/marketplace/cart_policy.rb b/app/furniture/marketplace/cart_policy.rb new file mode 100644 index 000000000..fa400707c --- /dev/null +++ b/app/furniture/marketplace/cart_policy.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +class Marketplace + class CartPolicy < ApplicationPolicy + alias_method :cart, :object + def permitted_attributes(_params = nil) + %i[delivery_address] + end + + def create? + true + end + + def update? + cart.shopper.person == current_person + end + + class Scope < ApplicationScope + def resolve + scope.all + end + end + end +end diff --git a/app/furniture/marketplace/carts/_cart.html.erb b/app/furniture/marketplace/carts/_cart.html.erb index 7a216ae34..5134a6c31 100644 --- a/app/furniture/marketplace/carts/_cart.html.erb +++ b/app/furniture/marketplace/carts/_cart.html.erb @@ -1,24 +1,44 @@ -
- - - - - - - - - - - <%- cart.marketplace.products.each do |product| %> - <%= render cart.cart_products.find_or_initialize_by(product: product) %> - <%- end %> - - <%= render "marketplace/carts/footer", cart: cart %> -
- <%= Marketplace::Product.human_attribute_name(:name) %> -  
+
+ + <%- if cart.delivery_address.blank? %> + <%= form_with model: cart.location do |cart_form| %> + <%= render "text_field", attribute: :delivery_address, form: cart_form %> + + <%= cart_form.submit %> + <%- end %> + <%- else %> +
+

+ Delivering to <%= cart.delivery_address %> + +

+ <%= button_to "Change Address", cart.location, params: { cart: { delivery_address: nil } }%> +
+ + <%- end %> + +
+ + + + + + + + + + + <%- cart.marketplace.products.each do |product| %> + <%= render cart.cart_products.find_or_initialize_by(product: product) %> + <%- end %> + + <%= render "marketplace/carts/footer", cart: cart %> +
+ <%= Marketplace::Product.human_attribute_name(:name) %> +  
+
diff --git a/app/furniture/marketplace/carts/_footer.html.erb b/app/furniture/marketplace/carts/_footer.html.erb index 29873d7e2..fa4228cf4 100644 --- a/app/furniture/marketplace/carts/_footer.html.erb +++ b/app/furniture/marketplace/carts/_footer.html.erb @@ -8,8 +8,9 @@ <%= render "marketplace/carts/total", cart: cart %> - <%= button_to("Checkout", cart.location(child: :checkout), data: { turbo: false }) %> + <%- if cart.delivery_address.present? %> + <%= button_to("Checkout", cart.location(child: :checkout), data: { turbo: false }) %> + <%- end %> - diff --git a/app/furniture/marketplace/carts/_total.html.erb b/app/furniture/marketplace/carts/_total.html.erb index a9a12bc18..a45ecdbbb 100644 --- a/app/furniture/marketplace/carts/_total.html.erb +++ b/app/furniture/marketplace/carts/_total.html.erb @@ -1,10 +1,12 @@ - - + + Products: <%= humanized_money_with_symbol(cart.product_total) %> - Delivery: - <%= humanized_money_with_symbol(cart.delivery_fee) %> - + <%- if cart.delivery_address.present? %> + Delivery: + <%= humanized_money_with_symbol(cart.delivery_fee) %> + <%- end %> + Total: <%= humanized_money_with_symbol(cart.price_total) %> diff --git a/app/furniture/marketplace/carts_controller.rb b/app/furniture/marketplace/carts_controller.rb new file mode 100644 index 000000000..ade14108a --- /dev/null +++ b/app/furniture/marketplace/carts_controller.rb @@ -0,0 +1,24 @@ +class Marketplace + class CartsController < Controller + def update + authorize(cart) + cart.update(cart_params) + respond_to do |format| + format.html do + redirect_to cart.room.location + end + format.turbo_stream do + render turbo_stream: [turbo_stream.replace(dom_id(cart), cart)] + end + end + end + + def cart + policy_scope(marketplace.carts).find(params[:id]) + end + + def cart_params + policy(Cart).permit(params.require(:cart)) + end + end +end diff --git a/app/furniture/marketplace/checkout.rb b/app/furniture/marketplace/checkout.rb index 8a59e435c..7eae2a884 100644 --- a/app/furniture/marketplace/checkout.rb +++ b/app/furniture/marketplace/checkout.rb @@ -16,9 +16,6 @@ def create_stripe_session(success_url:, cancel_url:) mode: "payment", success_url: success_url, cancel_url: cancel_url, - shipping_address_collection: { - allowed_countries: ["US"], - }, payment_intent_data: { transfer_group: cart.id } diff --git a/spec/furniture/marketplace/cart_spec.rb b/spec/furniture/marketplace/cart_spec.rb index e2021660a..e9572634e 100644 --- a/spec/furniture/marketplace/cart_spec.rb +++ b/spec/furniture/marketplace/cart_spec.rb @@ -22,7 +22,13 @@ cart.cart_products.create!(product: product_b, quantity: 2) end - it { is_expected.to eql(product_a.price + product_b.price + product_b.price + marketplace.delivery_fee) } + it { is_expected.to eql(product_a.price + product_b.price * 2) } + + 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 + product_b.price + marketplace.delivery_fee) } + end end describe "#product_total" do diff --git a/spec/furniture/marketplace/carts_controller_request_spec.rb b/spec/furniture/marketplace/carts_controller_request_spec.rb new file mode 100644 index 000000000..bcf6f486f --- /dev/null +++ b/spec/furniture/marketplace/carts_controller_request_spec.rb @@ -0,0 +1,21 @@ +require "rails_helper" + +RSpec.describe Marketplace::CartsController, type: :request do + let(:marketplace) { create(:marketplace) } + let(:space) { marketplace.space } + let(:room) { marketplace.room } + let(:person) { create(:person) } + + let(:shopper) { create(:marketplace_shopper, person: person) } + + describe "#update" do + it "changes the delivery address" do + cart = create(:marketplace_cart, marketplace: marketplace, shopper: shopper) + sign_in(space, person) + + put polymorphic_path(cart.location), params: {cart: {delivery_address: "123 N West St"}} + + expect(response).to redirect_to(room.location) + end + end +end