mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-03 22:06:07 +00:00
Move variant setup to new factory trait
This commit is contained in:
@@ -460,43 +460,13 @@ describe OrderManagement::Reports::EnterpriseFeeSummary::ReportService do
|
||||
end
|
||||
|
||||
def default_variant_options
|
||||
{ product: product, producer: producer, coordinator: coordinator, distributor: distributor,
|
||||
order_cycle: order_cycle }
|
||||
{ product: product, producer: producer, is_master: false, coordinator: coordinator,
|
||||
distributor: distributor, order_cycle: order_cycle }
|
||||
end
|
||||
|
||||
def prepare_variant(options = {})
|
||||
target = default_variant_options.merge(options)
|
||||
|
||||
create(:variant, product: target[:product], is_master: false).tap do |variant|
|
||||
exchange_options = { producer: target[:producer], coordinator: target[:coordinator],
|
||||
distributor: target[:distributor],
|
||||
incoming_exchange_fees: target[:incoming_exchange_fees],
|
||||
outgoing_exchange_fees: target[:outgoing_exchange_fees] }
|
||||
setup_exchanges(target[:order_cycle], variant, exchange_options)
|
||||
end
|
||||
end
|
||||
|
||||
def setup_exchanges(order_cycle, variant, options)
|
||||
setup_exchange(order_cycle, variant, true, sender: options[:producer],
|
||||
receiver: options[:coordinator],
|
||||
enterprise_fees: options[:incoming_exchange_fees])
|
||||
setup_exchange(order_cycle, variant, false, sender: options[:coordinator],
|
||||
receiver: options[:distributor],
|
||||
enterprise_fees: options[:outgoing_exchange_fees])
|
||||
end
|
||||
|
||||
def setup_exchange(order_cycle, variant, incoming, options)
|
||||
exchange_attributes = { order_cycle_id: order_cycle.id, incoming: incoming,
|
||||
sender_id: options[:sender].id, receiver_id: options[:receiver].id }
|
||||
exchange = Exchange.where(exchange_attributes).first || create(:exchange, exchange_attributes)
|
||||
exchange.variants << variant
|
||||
attach_enterprise_fees(exchange, options[:enterprise_fees] || [])
|
||||
end
|
||||
|
||||
def attach_enterprise_fees(exchange, enterprise_fees)
|
||||
enterprise_fees.each do |enterprise_fee|
|
||||
exchange.enterprise_fees << enterprise_fee
|
||||
end
|
||||
target_options = default_variant_options.merge(options)
|
||||
create(:variant, :with_order_cycle, target_options)
|
||||
end
|
||||
|
||||
def per_item_calculator(amount)
|
||||
|
||||
34
spec/factories/variants.rb
Normal file
34
spec/factories/variants.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
FactoryBot.modify do
|
||||
factory :variant do
|
||||
trait :with_order_cycle do
|
||||
transient do
|
||||
order_cycle { create(:order_cycle) }
|
||||
producer { product.supplier }
|
||||
coordinator { create(:distributor_enterprise) }
|
||||
distributor { create(:distributor_enterprise) }
|
||||
incoming_exchange_fees { [] }
|
||||
outgoing_exchange_fees { [] }
|
||||
end
|
||||
|
||||
after(:create) do |variant, evaluator|
|
||||
exchange_attributes = { order_cycle_id: evaluator.order_cycle.id, incoming: true,
|
||||
sender_id: evaluator.producer.id,
|
||||
receiver_id: evaluator.coordinator.id }
|
||||
exchange = Exchange.where(exchange_attributes).first_or_create!(exchange_attributes)
|
||||
exchange.variants << variant
|
||||
evaluator.incoming_exchange_fees.each do |enterprise_fee|
|
||||
exchange.enterprise_fees << enterprise_fee
|
||||
end
|
||||
|
||||
exchange_attributes = { order_cycle_id: evaluator.order_cycle.id, incoming: false,
|
||||
sender_id: evaluator.coordinator.id,
|
||||
receiver_id: evaluator.distributor.id }
|
||||
exchange = Exchange.where(exchange_attributes).first_or_create!(exchange_attributes)
|
||||
exchange.variants << variant
|
||||
(evaluator.outgoing_exchange_fees || []).each do |enterprise_fee|
|
||||
exchange.enterprise_fees << enterprise_fee
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user