From b6eca5879810ed54a06bd955cc5ccd81a91046d2 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Thu, 12 Dec 2024 04:28:15 +0500 Subject: [PATCH] 13024: fix dfc catalog sync for blank cart --- app/controllers/cart_controller.rb | 2 +- app/jobs/stock_sync_job.rb | 34 +++++++++++++++--------------- spec/jobs/stock_sync_job_spec.rb | 22 +++++++++++++++++-- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/app/controllers/cart_controller.rb b/app/controllers/cart_controller.rb index 5eadc172d18..e0081451685 100644 --- a/app/controllers/cart_controller.rb +++ b/app/controllers/cart_controller.rb @@ -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 diff --git a/app/jobs/stock_sync_job.rb b/app/jobs/stock_sync_job.rb index d4df9c3c613..05b7888bad2 100644 --- a/app/jobs/stock_sync_job.rb +++ b/app/jobs/stock_sync_job.rb @@ -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 @@ 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 diff --git a/spec/jobs/stock_sync_job_spec.rb b/spec/jobs/stock_sync_job_spec.rb index 8fd7029e183..de709f1aeb5 100644 --- a/spec/jobs/stock_sync_job_spec.rb +++ b/spec/jobs/stock_sync_job_spec.rb @@ -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 @@ -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 @@ -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