mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-06 22:36:07 +00:00
13024: fix dfc catalog sync for blank cart
This commit is contained in:
@@ -11,7 +11,7 @@ class CartController < BaseController
|
||||
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
|
||||
|
||||
@@ -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)
|
||||
@@ -72,4 +58,18 @@ class StockSyncJob < ApplicationJob
|
||||
.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
|
||||
|
||||
@@ -14,8 +14,8 @@ RSpec.describe StockSyncJob do
|
||||
"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
|
||||
@@ -35,6 +35,15 @@ RSpec.describe StockSyncJob do
|
||||
|
||||
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
|
||||
@@ -59,6 +68,15 @@ RSpec.describe StockSyncJob do
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user