diff --git a/app/services/subscription_variants_service.rb b/app/services/subscription_variants_service.rb index eb6dcf0cf2..855c200303 100644 --- a/app/services/subscription_variants_service.rb +++ b/app/services/subscription_variants_service.rb @@ -4,21 +4,11 @@ class SubscriptionVariantsService # - Variants of hub # - Variants that are in outgoing exchanges where the hub is receiver def self.eligible_variants(distributor) - permitted_order_cycle_enterprise_ids = EnterpriseRelationship.permitting(distributor) - .with_permission(:add_to_order_cycle).pluck(:parent_id) - permitted_producer_ids = Enterprise.is_primary_producer - .where('enterprises.id IN (?)', permitted_order_cycle_enterprise_ids).pluck(:id) - - outgoing_exchange_variant_ids = ExchangeVariant - .select("DISTINCT exchange_variants.variant_id") - .joins(:exchange) - .where(exchanges: { incoming: false, receiver_id: distributor.id }) - .pluck(:variant_id) - - variant_conditions = ["spree_products.supplier_id IN (?)", permitted_producer_ids | [distributor.id]] - if outgoing_exchange_variant_ids.present? + variant_conditions = ["spree_products.supplier_id IN (?)", permitted_producer_ids(distributor)] + exchange_variant_ids = outgoing_exchange_variant_ids(distributor) + if exchange_variant_ids.present? variant_conditions[0] << " OR spree_variants.id IN (?)" - variant_conditions << outgoing_exchange_variant_ids + variant_conditions << exchange_variant_ids end Spree::Variant.joins(:product).where(is_master: false).where(*variant_conditions) @@ -31,4 +21,19 @@ class SubscriptionVariantsService scope = scope.where(schedules: { id: schedule }) scope.any? end + + def self.permitted_producer_ids(distributor) + other_permitted_producer_ids = EnterpriseRelationship.joins(:parent) + .permitting(distributor).with_permission(:add_to_order_cycle) + .merge(Enterprise.is_primary_producer) + .pluck(:parent_id) + + other_permitted_producer_ids | [distributor.id] + end + + def self.outgoing_exchange_variant_ids(distributor) + ExchangeVariant.select("DISTINCT exchange_variants.variant_id").joins(:exchange) + .where(exchanges: { incoming: false, receiver_id: distributor.id }) + .pluck(:variant_id) + end end