Skip to content

Commit

Permalink
close #2112 create job when add to cart
Browse files Browse the repository at this point in the history
  • Loading branch information
panhachom committed Dec 9, 2024
1 parent 2f1ad7f commit 2ee4cae
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ def ensure_cart_exist

@spree_current_order ||= create_service.call(create_cart_params).value
end

def enqueue_add_item
spree_authorize! :update, spree_current_order, order_token
spree_authorize! :show, @variant

SpreeCmCommissioner::Cart::AddItemJob.perform_later(
spree_current_order.id,
@variant.id,
add_item_params[:quantity],
add_item_params[:public_metadata],
add_item_params[:private_metadata],
add_item_params[:options]
)

render json: { message: 'Item is being added to the cart' }, status: :accepted
end
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions app/jobs/spree_cm_commissioner/cart/add_item_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module SpreeCmCommissioner
module Cart
class AddItemJob < ApplicationJob
def perform(order_id, variant_id, quantity, public_metadata, private_metadata, options) # rubocop:disable Metrics/ParameterLists
order = Spree::Order.find(order_id)
variant = Spree::Variant.find(variant_id)

Spree::Cart::AddItem.call(
order: order,
variant: variant,
quantity: quantity,
public_metadata: public_metadata,
private_metadata: private_metadata,
options: options
)
end
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@

resource :cart, controller: :cart, only: %i[show create destroy] do
patch :restart_checkout_flow
post :enqueue_add_item
end

resources :wished_items
Expand Down
25 changes: 25 additions & 0 deletions spec/jobs/spree_cm_commissioner/add_item_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'spec_helper'

RSpec.describe SpreeCmCommissioner::Cart::AddItemJob, type: :job do
describe '#perform' do
let(:order) { create(:order) }
let(:variant) { create(:variant) }
let(:quantity) { 2 }

it 'calls Spree::Cart::AddItem with correct arguments' do
add_item_service = instance_double(Spree::Cart::AddItem)
allow(Spree::Cart::AddItem).to receive(:new).and_return(add_item_service)
allow(add_item_service).to receive(:call).and_return(true)
described_class.new.perform(order.id, variant.id, quantity, {}, {}, {})

expect(add_item_service).to have_received(:call).with(
order: order,
variant: variant,
quantity: quantity,
public_metadata: {},
private_metadata: {},
options: {}
)
end
end
end

0 comments on commit 2ee4cae

Please sign in to comment.