When an outgoing exchange includes a variant that is not in an incoming exchange, remove it from the outgoing exchange

This commit is contained in:
Rohan Mitchell
2015-11-24 13:46:40 +11:00
parent ea6974d438
commit 243ef4ee16
3 changed files with 52 additions and 2 deletions

View File

@@ -167,13 +167,27 @@ module OpenFoodNetwork
sender = @order_cycle.coordinator
receiver = exchange.andand.receiver || Enterprise.find(attrs[:enterprise_id])
permitted = editable_variant_ids_for_outgoing_exchange_between(sender, receiver)
incoming = incoming_variant_ids
# Only change visibility for variants I have permission to edit
attrs[:variants].each do |variant_id, value|
variants[variant_id.to_i] = value if permitted.include?(variant_id.to_i)
variant_id = variant_id.to_i
if !incoming.include? variant_id
# When a variant has been removed from incoming but remains
# in outgoing, remove it from outgoing too
variants[variant_id] = false
elsif permitted.include? variant_id
variants[variant_id] = value
end
end
variants.select { |k, v| v }.keys.map { |k| k.to_i }.sort
variants.select { |k, v| v }.keys.map(&:to_i).sort
end
def incoming_variant_ids
@order_cycle.supplied_variants.map &:id
end
end
end

View File

@@ -106,6 +106,32 @@ module Admin
spree_put :update, id: order_cycle.id, reloading: '0', order_cycle: {}
flash[:notice].should be_nil
end
context "as a producer supplying to an order cycle" do
let(:producer) { create(:supplier_enterprise) }
let(:coordinator) { order_cycle.coordinator }
let(:hub) { create(:distributor_enterprise) }
before { login_as_enterprise_user [producer] }
describe "removing a variant from incoming" do
let(:v) { create(:variant) }
let!(:ex_i) { create(:exchange, order_cycle: order_cycle, sender: producer, receiver: coordinator, incoming: true, variants: [v]) }
let!(:ex_o) { create(:exchange, order_cycle: order_cycle, sender: coordinator, receiver: hub, incoming: false, variants: [v]) }
let(:params) do
{order_cycle: {
incoming_exchanges: [{id: ex_i.id, enterprise_id: producer.id, sender_id: producer.id, variants: {v.id => false}}],
outgoing_exchanges: [{id: ex_o.id, enterprise_id: hub.id, receiver_id: hub.id, variants: {v.id => false}}] }
}
end
it "removes the variant from outgoing also" do
spree_put :update, {id: order_cycle.id}.merge(params)
Exchange.where(order_cycle_id: order_cycle).with_variant(v).should be_empty
end
end
end
end
describe "bulk_update" do

View File

@@ -175,6 +175,7 @@ module OpenFoodNetwork
before do
applicator.stub(:find_outgoing_exchange) { nil }
applicator.stub(:incoming_variant_ids) { [1, 2, 3, 4] }
expect(applicator).to receive(:editable_variant_ids_for_outgoing_exchange_between).
with(coordinator_mock, enterprise_mock) { [1, 2, 3] }
end
@@ -207,6 +208,7 @@ module OpenFoodNetwork
before do
applicator.stub(:find_outgoing_exchange) { exchange_mock }
applicator.stub(:incoming_variant_ids) { [1, 2, 3, 4] }
expect(applicator).to receive(:editable_variant_ids_for_outgoing_exchange_between).
with(coordinator_mock, enterprise_mock) { [1, 2, 3] }
end
@@ -231,6 +233,14 @@ module OpenFoodNetwork
ids = applicator.send(:outgoing_exchange_variant_ids, {:enterprise_id => 123, :variants => {'1' => true, '2' => false, '3' => true}})
expect(ids).to eq [1, 3]
end
it "removes variants which are not included in incoming exchanges" do
applicator.stub(:incoming_variant_ids) { [1, 2] }
applicator.stub(:persisted_variants_hash) { {3 => true} }
expect(exchange_mock).to receive(:receiver) { enterprise_mock }
ids = applicator.send(:outgoing_exchange_variant_ids, {:enterprise_id => 123, :variants => {'1' => true, '2' => false, '3' => true}})
expect(ids).to eq [1]
end
end
end