Skip to content

Commit

Permalink
13024: fix dfc catalog sync for blank cart
Browse files Browse the repository at this point in the history
  • Loading branch information
chahmedejaz committed Dec 11, 2024
1 parent 9870abf commit b6eca58
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/controllers/cart_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def populate
order.cap_quantity_at_stock!
order.recreate_all_fees!

StockSyncJob.sync_linked_catalogs(order)
StockSyncJob.sync_linked_catalogs_later(order)

render json: { error: false, stock_levels: stock_levels(order) }, status: :ok
else
Expand Down
34 changes: 17 additions & 17 deletions app/jobs/stock_sync_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,12 @@ class StockSyncJob < ApplicationJob
# product. These variants are rare though and we check first before we
# enqueue a new job. That should save some time loading the order with
# all the stock data to make this decision.
def self.sync_linked_catalogs(order)
user = order.distributor.owner
catalog_ids(order).each do |catalog_id|
perform_later(user, catalog_id)
end
rescue StandardError => e
# Errors here shouldn't affect the shopping. So let's report them
# separately:
Alert.raise_with_record(e, order)
def self.sync_linked_catalogs_later(order)
sync_categories_by_perform_method(order, :perform_later)
end

def self.sync_linked_catalogs_now(order)
user = order.distributor.owner
catalog_ids(order).each do |catalog_id|
perform_now(user, catalog_id)
end
rescue StandardError => e
# Errors here shouldn't affect the shopping. So let's report them
# separately:
Alert.raise_with_record(e, order)
sync_categories_by_perform_method(order, :perform_now)
end

def self.catalog_ids(order)
Expand Down Expand Up @@ -72,4 +58,18 @@ def linked_variants(enterprises, product_ids)
.includes(:semantic_links).references(:semantic_links)
.where(semantic_links: { semantic_id: product_ids })
end

def self.sync_categories_by_perform_method(order, perform_method)
distributor = order.distributor
return unless distributor

user = distributor.owner
catalog_ids(order).each do |catalog_id|
public_send(perform_method, user, catalog_id)
end
rescue StandardError => e
# Errors here shouldn't affect the shopping. So let's report them
# separately:
Alert.raise_with_record(e, order)
end
end
22 changes: 20 additions & 2 deletions spec/jobs/stock_sync_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts"
}

describe ".sync_linked_catalogs" do
subject { StockSyncJob.sync_linked_catalogs(order) }
describe ".sync_linked_catalogs_later" do
subject { StockSyncJob.sync_linked_catalogs_later(order) }
it "ignores products without semantic link" do
expect { subject }.not_to enqueue_job(StockSyncJob)
end
Expand All @@ -35,6 +35,15 @@

expect { subject }.not_to raise_error
end

context "when order has no distributor" do
let!(:order) { create(:order) }

it 'should not raise error' do
expect(Bugsnag).not_to receive(:notify).and_call_original
expect { subject }.not_to raise_error
end
end
end

describe ".sync_linked_catalogs_now" do
Expand All @@ -59,6 +68,15 @@

expect { subject }.not_to raise_error
end

context "when order has no distributor" do
let!(:order) { create(:order) }

it 'should not raise error' do
expect(Bugsnag).not_to receive(:notify).and_call_original
expect { subject }.not_to raise_error
end
end
end

describe "#perform" do
Expand Down

0 comments on commit b6eca58

Please sign in to comment.