diff --git a/app/assets/javascripts/darkswarm/services/order_cycle_resource.js.coffee b/app/assets/javascripts/darkswarm/services/order_cycle_resource.js.coffee index 32d70d54a5..ba8d5266fe 100644 --- a/app/assets/javascripts/darkswarm/services/order_cycle_resource.js.coffee +++ b/app/assets/javascripts/darkswarm/services/order_cycle_resource.js.coffee @@ -1,21 +1,21 @@ Darkswarm.factory 'OrderCycleResource', ($resource) -> - $resource('/api/order_cycles/:id', {}, { + $resource('/api/order_cycles/:id.json', {}, { 'products': method: 'GET' isArray: true - url: '/api/order_cycles/:id/products' + url: '/api/order_cycles/:id/products.json' params: id: '@id' 'taxons': method: 'GET' isArray: true - url: '/api/order_cycles/:id/taxons' + url: '/api/order_cycles/:id/taxons.json' params: id: '@id' 'properties': method: 'GET' isArray: true - url: '/api/order_cycles/:id/properties' + url: '/api/order_cycles/:id/properties.json' params: id: '@id' }) diff --git a/app/controllers/api/base_controller.rb b/app/controllers/api/base_controller.rb index b75c92e22e..54feee3802 100644 --- a/app/controllers/api/base_controller.rb +++ b/app/controllers/api/base_controller.rb @@ -10,6 +10,9 @@ module Api include Spree::Core::ControllerHelpers::SSL include ::ActionController::Head include ::ActionController::ConditionalGet + include ActionView::Layouts + + layout false attr_accessor :current_api_user diff --git a/app/controllers/api/order_cycles_controller.rb b/app/controllers/api/order_cycles_controller.rb index ddc7b15fa5..c40ec641b2 100644 --- a/app/controllers/api/order_cycles_controller.rb +++ b/app/controllers/api/order_cycles_controller.rb @@ -20,7 +20,7 @@ module Api search_params ).products_json - render plain: products, content_type: "application/json" + render plain: products rescue ProductsRenderer::NoProducts render_no_products end @@ -33,19 +33,19 @@ module Api render plain: ActiveModel::ArraySerializer.new( taxons, each_serializer: Api::TaxonSerializer - ).to_json, content_type: "application/json" + ).to_json end def properties render plain: ActiveModel::ArraySerializer.new( product_properties | producer_properties, each_serializer: Api::PropertySerializer - ).to_json, content_type: "application/json" + ).to_json end private def render_no_products - render status: :not_found, json: '' + render status: :not_found, json: {} end def product_properties diff --git a/app/models/concerns/api_action_caching.rb b/app/controllers/concerns/api_action_caching.rb similarity index 100% rename from app/models/concerns/api_action_caching.rb rename to app/controllers/concerns/api_action_caching.rb diff --git a/spec/controllers/api/order_cycles_controller_spec.rb b/spec/controllers/api/order_cycles_controller_spec.rb index aacbeda57b..d41d26aa43 100644 --- a/spec/controllers/api/order_cycles_controller_spec.rb +++ b/spec/controllers/api/order_cycles_controller_spec.rb @@ -148,6 +148,21 @@ module Api expect(product_ids).to include product3.id end end + + context "when the order cycle is closed" do + before do + allow(controller).to receive(:order_cycle) { order_cycle } + allow(order_cycle).to receive(:open?) { false } + end + + # Regression test for https://github.com/openfoodfoundation/openfoodnetwork/issues/6491 + it "renders no products without error" do + api_get :products, id: order_cycle.id, distributor: distributor.id + + expect(json_response).to eq({}) + expect(response).to have_http_status :not_found + end + end end describe "#taxons" do diff --git a/spec/features/consumer/caching/shops_caching_spec.rb b/spec/features/consumer/caching/shops_caching_spec.rb index c525e569d9..6f71868753 100644 --- a/spec/features/consumer/caching/shops_caching_spec.rb +++ b/spec/features/consumer/caching/shops_caching_spec.rb @@ -47,8 +47,8 @@ feature "Shops caching", js: true, caching: true do let(:exchange) { order_cycle.exchanges.to_enterprises(distributor).outgoing.first } let(:test_domain) { "#{Capybara.current_session.server.host}:#{Capybara.current_session.server.port}" } - let(:taxons_key) { "views/#{test_domain}/api/order_cycles/#{order_cycle.id}/taxons?distributor=#{distributor.id}.json" } - let(:properties_key) { "views/#{test_domain}/api/order_cycles/#{order_cycle.id}/properties?distributor=#{distributor.id}.json" } + let(:taxons_key) { "views/#{test_domain}/api/order_cycles/#{order_cycle.id}/taxons.json?distributor=#{distributor.id}" } + let(:properties_key) { "views/#{test_domain}/api/order_cycles/#{order_cycle.id}/properties.json?distributor=#{distributor.id}" } let(:options) { { expires_in: CacheService::FILTERS_EXPIRY } } before do diff --git a/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee index db9ed1e628..d0114c05dd 100644 --- a/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/products_spec.js.coffee @@ -13,7 +13,7 @@ describe 'Products service', -> properties = null taxons = null GmapsGeo = {} - endpoint = "/api/order_cycles/1/products?distributor=1" + endpoint = "/api/order_cycles/1/products.json?distributor=1" beforeEach -> product =