Refactor methods in SubscriptionVariantsService

This commit is contained in:
Kristina Lim
2019-01-20 23:46:56 +11:00
parent b691d727a7
commit 9965e95c65

View File

@@ -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