diff --git a/app/controllers/api/exchange_products_controller.rb b/app/controllers/api/exchange_products_controller.rb index a4daa17c1c..362937ff89 100644 --- a/app/controllers/api/exchange_products_controller.rb +++ b/app/controllers/api/exchange_products_controller.rb @@ -28,24 +28,22 @@ module Api private def render_variant_count - variants_relation = Spree::Variant. - not_master. - where(product_id: products.select(&:id)) - - if @order_cycle.present? && - @order_cycle.prefers_product_selection_from_coordinator_inventory_only? - variants_relation = variants_relation.visible_for(@order_cycle.coordinator) - end - render text: { - count: variants_relation.count + count: variants.count }.to_json end + def variants + renderer.exchange_variants(@incoming, @enterprise) + end + def products - ExchangeProductsRenderer. - new(@order_cycle, spree_current_user). - exchange_products(@incoming, @enterprise) + renderer.exchange_products(@incoming, @enterprise) + end + + def renderer + @renderer ||= ExchangeProductsRenderer. + new(@order_cycle, spree_current_user) end def paginated_products diff --git a/app/services/exchange_products_renderer.rb b/app/services/exchange_products_renderer.rb index faadba34f1..bffb9b8cbd 100644 --- a/app/services/exchange_products_renderer.rb +++ b/app/services/exchange_products_renderer.rb @@ -12,6 +12,14 @@ class ExchangeProductsRenderer end end + def exchange_variants(incoming, enterprise) + variants_relation = Spree::Variant. + not_master. + where(product_id: exchange_products(incoming, enterprise).select(&:id)) + + filter_visible(variants_relation) + end + private def products_for_incoming_exchange(enterprise) @@ -21,12 +29,16 @@ class ExchangeProductsRenderer def supplied_products(enterprises_query_matcher) products_relation = Spree::Product.where(supplier_id: enterprises_query_matcher) + filter_visible(products_relation) + end + + def filter_visible(relation) if @order_cycle.present? && @order_cycle.prefers_product_selection_from_coordinator_inventory_only? - products_relation = products_relation.visible_for(@order_cycle.coordinator) + relation = relation.visible_for(@order_cycle.coordinator) end - products_relation + relation end def products_for_outgoing_exchange