Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Use `Dependencies.completed_order_finder` in Storefront order controllers for fetching complete orders

Damian Legawieca60a937831fd
diff --git a/core/app/finders/spree/orders/find_complete.rb b/core/app/finders/spree/orders/find_complete.rb
index e4347efec4..6ada10d139 100644
--- a/core/app/finders/spree/orders/find_complete.rb
+++ b/core/app/finders/spree/orders/find_complete.rb
@@ -3,13 +3,14 @@ module Spree
class FindComplete
include Spree::Orders::FinderHelper
- attr_reader :user, :number, :token, :store
+ attr_reader :user, :number, :token, :store, :email
- def initialize(user: nil, number: nil, token: nil, store: nil)
+ def initialize(user: nil, number: nil, token: nil, store: nil, email: nil)
@user = user
@number = number
@token = token
@store = store
+ @email = email
end
def execute
@@ -17,6 +18,7 @@ module Spree
orders = by_number(orders)
orders = by_token(orders)
orders = by_store(orders)
+ orders = by_email(orders)
orders
end
@@ -43,6 +45,10 @@ module Spree
store.present?
end
+ def email?
+ email.present?
+ end
+
def by_user(orders)
return orders unless user?
@@ -66,6 +72,12 @@ module Spree
orders.where(store: store)
end
+
+ def by_email(orders)
+ return orders unless email?
+
+ orders.where(email: email.strip.downcase)
+ end
end
end
end
diff --git a/storefront/app/controllers/spree/account/orders_controller.rb b/storefront/app/controllers/spree/account/orders_controller.rb
index 49aca89572..04e07e4d6e 100644
--- a/storefront/app/controllers/spree/account/orders_controller.rb
+++ b/storefront/app/controllers/spree/account/orders_controller.rb
@@ -14,14 +14,18 @@ module Spree
def accurate_title
if action_name == 'show'
- Spree.t(:order_details_with_number, number: @order.number)
+ "#{Spree.t(:order)} ##{@order.number}"
else
Spree.t(:my_orders)
end
end
def orders_scope
- try_spree_current_user.completed_orders.for_store(current_store).order(completed_at: :desc)
+ order_finder.new(user: try_spree_current_user, store: current_store).execute
+ end
+
+ def order_finder
+ Spree::Dependencies.completed_order_finder.constantize
end
end
end
diff --git a/storefront/app/controllers/spree/order_status_controller.rb b/storefront/app/controllers/spree/order_status_controller.rb
index 106c1caf91..cb2bb28397 100644
--- a/storefront/app/controllers/spree/order_status_controller.rb
+++ b/storefront/app/controllers/spree/order_status_controller.rb
@@ -7,13 +7,15 @@ module Spree
# validate email/order number and redirect to order page
# POST /order_status
def create
- @order = current_store.orders.complete.without_vendor.find_by(number: params[:number], email: params[:email])
+ raise ActiveRecord::RecordNotFound if params[:number].blank?
+
+ @order = order_finder.new(number: params[:number], email: params[:email], store: current_store).execute.first
if @order
- redirect_to spree.order_path(@order, token: @order.token)
+ redirect_to spree.order_path(@order, token: @order.token), status: :see_other
else
flash[:error] = Spree.t(:order_not_found)
- render :new
+ render :new, status: :unprocessable_entity
end
end
@@ -22,5 +24,9 @@ module Spree
def accurate_title
Spree.t(:order_status)
end
+
+ def order_finder
+ Spree::Dependencies.completed_order_finder.constantize
+ end
end
end
diff --git a/storefront/app/controllers/spree/orders_controller.rb b/storefront/app/controllers/spree/orders_controller.rb
index 2414b1f3e4..61ab25ffe8 100644
--- a/storefront/app/controllers/spree/orders_controller.rb
+++ b/storefront/app/controllers/spree/orders_controller.rb
@@ -10,9 +10,13 @@ module Spree
before_action :assign_order_with_lock, only: :update
- # GET /orders/:id/:token
+ # GET /orders/:id
def show
- @order = complete_order_scope.find_by!(number: params[:id], token: params[:token])
+ @order = complete_order_finder.new(number: params[:id], token: params[:token], store: current_store).execute.first
+
+ raise ActiveRecord::RecordNotFound if @order.blank?
+
+ @shipments = @order.shipments
end
# PUT /cart
@@ -78,8 +82,8 @@ module Spree
Spree::Dependencies.cart_update_service.constantize
end
- def complete_order_scope
- current_store.orders.complete
+ def complete_order_finder
+ Spree::Dependencies.completed_order_finder.constantize
end
def remove_out_of_stock_items
diff --git a/storefront/app/views/themes/default/spree/account/orders/show.html.erb b/storefront/app/views/themes/default/spree/account/orders/show.html.erb
index abd9c4cf01..3a2b3561b6 100644
--- a/storefront/app/views/themes/default/spree/account/orders/show.html.erb
+++ b/storefront/app/views/themes/default/spree/account/orders/show.html.erb
@@ -17,6 +17,6 @@
<h1 class="text-xl uppercase font-medium my-4 ">
<%= accurate_title %>
</h1>
- <%= render 'spree/shared/order_details', order: @order %>
+ <%= render 'spree/shared/order_details', order: @order, shipments: @shipments %>
</div>
</div>
diff --git a/storefront/app/views/themes/default/spree/orders/show.html.erb b/storefront/app/views/themes/default/spree/orders/show.html.erb
index 79afa7d7d4..48a8c459e3 100644
--- a/storefront/app/views/themes/default/spree/orders/show.html.erb
+++ b/storefront/app/views/themes/default/spree/orders/show.html.erb
@@ -2,5 +2,5 @@
<h1 class="text-2xl uppercase font-medium tracking-tight py-5 text-center lg:text-left mt-5">
<%= accurate_title %>
</h1>
- <%= render 'spree/shared/order_details', order: @order %>
+ <%= render 'spree/shared/order_details', order: @order, shipments: @shipments %>
</div>
diff --git a/storefront/app/views/themes/default/spree/shared/_order_details.html.erb b/storefront/app/views/themes/default/spree/shared/_order_details.html.erb
index 1ef6a9229a..ae8c38aa29 100644
--- a/storefront/app/views/themes/default/spree/shared/_order_details.html.erb
+++ b/storefront/app/views/themes/default/spree/shared/_order_details.html.erb
@@ -1,10 +1,12 @@
+<% shipments ||= order.shipments %>
+
<% if order.order_refunded? || order.partially_refunded? %>
<div class="alert-warning mb-5 py-2 px-3">
<% if order.gift_card %>
- <% key = "refund_action_required_message.order_#{@shipments.shipped_but_canceled.any? ? 'refunded' : 'canceled'}.#{order.partially_refunded? ? 'partially' : 'fully'}" %>
+ <% key = "refund_action_required_message.order_#{shipments.shipped_but_canceled.any? ? 'refunded' : 'canceled'}.#{order.partially_refunded? ? 'partially' : 'fully'}" %>
<%= Spree.t(key, customer_support_email: order.store.customer_support_email ) %>
<% else %>
- <% key = "refund_action_not_required_message.order_#{@shipments.shipped_but_canceled.any? ? 'refunded' : 'canceled'}.#{order.partially_refunded? ? 'partially' : 'fully'}" %>
+ <% key = "refund_action_not_required_message.order_#{shipments.shipped_but_canceled.any? ? 'refunded' : 'canceled'}.#{order.partially_refunded? ? 'partially' : 'fully'}" %>
<%= Spree.t(key) %>
<% end %>
</div>
@@ -14,10 +16,10 @@
<%= Spree.t(:completed_at) %> <%= local_time(order.completed_at) %>
</p>
-<%== color_options_style_for_line_items(@shipments.flat_map(&:line_items)) %>
+<%== color_options_style_for_line_items(shipments.flat_map(&:line_items)) %>
<ul>
- <%= render partial: 'spree/shared/order_shipment', collection: @shipments, as: :shipment, cached: spree_base_cache_scope %>
+ <%= render partial: 'spree/shared/order_shipment', collection: shipments, as: :shipment, cached: spree_base_cache_scope %>
</ul>
<div class="mt-6 bg-accent mb-24">
@@ -28,7 +30,7 @@
<%= Spree.t(:billing_address) %>
</div>
<div class="!leading-[1.375rem] text-neutral-800">
- <%= render 'spree/shared/address', address: @order.billing_address %>
+ <%= render 'spree/shared/address', address: order.billing_address %>
</div>
</div>
<!-- payment into -->
@@ -37,7 +39,7 @@
<%= Spree.t(:payment_information) %>
</div>
<div class="!leading-[1.375rem] text-neutral-800">
- <%= render collection: @order.payments.valid, partial: 'spree/shared/payment', cached: spree_base_cache_scope %>
+ <%= render collection: order.payments.valid, partial: 'spree/shared/payment', cached: spree_base_cache_scope %>
</div>
</div>
</div>
diff --git a/storefront/spec/controllers/spree/order_status_controller_spec.rb b/storefront/spec/controllers/spree/order_status_controller_spec.rb
new file mode 100644
index 0000000000..bc890bc782
--- /dev/null
+++ b/storefront/spec/controllers/spree/order_status_controller_spec.rb
@@ -0,0 +1,66 @@
+require 'spec_helper'
+
+RSpec.describe Spree::OrderStatusController, type: :controller do
+ let(:store) { Spree::Store.default }
+ let(:order) { create(:completed_order_with_totals, store: store) }
+
+ before do
+ allow(controller).to receive(:current_store).and_return(store)
+ end
+
+ describe 'GET #new' do
+ before { get :new }
+
+ it 'returns http success' do
+ expect(response).to have_http_status(:success)
+ end
+
+ it 'renders the new template' do
+ expect(response).to render_template(:new)
+ end
+ end
+
+ describe 'POST #create' do
+ before { order }
+
+ context 'with valid order number and email' do
+ before do
+ post :create, params: { number: order.number, email: order.email }
+ end
+
+ it 'redirects to order path' do
+ expect(response).to redirect_to(spree.order_path(order, token: order.token))
+ end
+
+ it 'returns redirect status' do
+ expect(response).to have_http_status(:see_other)
+ end
+ end
+
+ context 'with invalid order number' do
+ before do
+ post :create, params: { number: 'invalid', email: order.email }
+ end
+
+ it 'sets flash error message' do
+ expect(flash[:error]).to eq Spree.t(:order_not_found)
+ end
+
+ it 'renders new template' do
+ expect(response).to render_template(:new)
+ end
+
+ it 'returns unprocessable entity status' do
+ expect(response).to have_http_status(:unprocessable_entity)
+ end
+ end
+
+ context 'with blank order number' do
+ it 'raises ActiveRecord::RecordNotFound' do
+ expect {
+ post :create, params: { number: '', email: order.email }
+ }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+ end
+end
diff --git a/storefront/spec/controllers/spree/orders_controller_spec.rb b/storefront/spec/controllers/spree/orders_controller_spec.rb
index 87a4889b2a..9eeb6c598a 100644
--- a/storefront/spec/controllers/spree/orders_controller_spec.rb
+++ b/storefront/spec/controllers/spree/orders_controller_spec.rb
@@ -96,4 +96,30 @@ describe Spree::OrdersController, type: :controller do
expect(order.reload.line_items.count).to eq 0
end
end
+
+ describe '#show' do
+ let(:order) { create(:completed_order_with_totals, store: store) }
+
+ before do
+ allow(controller).to receive(:current_store).and_return(store)
+ end
+
+ it 'renders the show template' do
+ get :show, params: { id: order.number, token: order.token }
+ expect(response).to render_template(:show)
+ end
+
+ it 'assigns @order' do
+ get :show, params: { id: order.number, token: order.token }
+ expect(assigns(:order)).to eq(order)
+ end
+
+ context 'when order is not found' do
+ it 'raises ActiveRecord::RecordNotFound' do
+ expect {
+ get :show, params: { id: 'invalid', token: order.token }
+ }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+ end
end