Skip to content

Commit

Permalink
Allow referencing of existing return items in create
Browse files Browse the repository at this point in the history
This change pulls the `before_action` from the backend customer returns
controller for parsing `return_items_attributes` in order to handle
creating a new customer return which references existing return items
from a return authorization. This change works around a limitation in
Rails when trying to update the association on existing nested resource
while creating the related record.

Co-authored-by: Mike Conlin <[email protected]>
  • Loading branch information
forkata and Mike Conlin committed Apr 16, 2021
1 parent 6bd1e40 commit c99c98b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
27 changes: 26 additions & 1 deletion api/app/controllers/spree/api/customer_returns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ module Spree
module Api
class CustomerReturnsController < Spree::Api::BaseController
before_action :load_order
before_action :build_customer_return, only: [:create]
around_action :lock_order, only: [:create, :update, :destroy, :cancel]

rescue_from Spree::Order::InsufficientStock, with: :insufficient_stock_error

def create
authorize! :create, CustomerReturn
@customer_return = CustomerReturn.create(customer_return_params)

if @customer_return.save
respond_with(@customer_return, status: 201, default_template: :show)
else
Expand Down Expand Up @@ -62,6 +63,30 @@ def load_order
def customer_return_params
params.require(:customer_return).permit(permitted_customer_return_attributes)
end

def build_customer_return
customer_return_attributes = customer_return_params
return_items_params = customer_return_attributes.
delete(:return_items_attributes)

if return_items_params.is_a? ActionController::Parameters
return_items_params = return_items_params.values
end

@customer_return = CustomerReturn.new(customer_return_attributes)

@customer_return.return_items = return_items_params.map do |item_params|
return_item = if item_params[:id]
Spree::ReturnItem.find(item_params[:id])
else
Spree::ReturnItem.new
end

return_item.assign_attributes(item_params)

return_item
end
end
end
end
end
19 changes: 17 additions & 2 deletions api/spec/requests/spree/api/customer_returns_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ module Spree
end

it "can create a new customer return" do
pending("fix for referrencing existing return items")
expect { subject }.to change { Spree::CustomerReturn.count }.
from(0).to(1)

Expand All @@ -184,14 +183,30 @@ module Spree
end

it "can create a new customer return" do
pending("fix for referrencing existing return items")
expect { subject }.to change { Spree::CustomerReturn.count }.
from(0).to(1)

expect(response).to have_http_status(:success)
expect(json_response).to have_attributes(attributes)
end
end

context "with reception_status_event provided for return item" do
let(:customer_return_params) do
{
stock_location_id: stock_location.id,
return_items_attributes: [
return_item.attributes.merge(reception_status_event: "receive")
]
}
end

it "updates the reception status of the return item" do
expect { subject }.
to change { return_item.reload.reception_status }.
from("awaiting").to("received")
end
end
end
end
end
Expand Down

0 comments on commit c99c98b

Please sign in to comment.