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 <mike@super.gd>
  • Loading branch information
forkata and Mike Conlin committed Mar 29, 2021
1 parent 0a61b74 commit 0de072f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
23 changes: 22 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_return_items_from_params, 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,26 @@ def load_order
def customer_return_params
params.require(:customer_return).permit(permitted_customer_return_attributes)
end

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

@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.compact
end
end
end
end
18 changes: 17 additions & 1 deletion 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 @@ -172,6 +171,23 @@ module Spree
to_not change { return_item.reload.reception_status }.
from("awaiting")
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 0de072f

Please sign in to comment.