Skip to content

Commit

Permalink
Fix the transfer line item quantity can be negative.
Browse files Browse the repository at this point in the history
  • Loading branch information
nozomirin committed Jan 1, 2025
1 parent 1957a79 commit 7fa75cd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/models/transfer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Transfer < ApplicationRecord
validate :storage_locations_belong_to_organization
validate :storage_locations_must_be_different
validate :from_storage_quantities
validate :line_items_quantity_is_positive

def self.csv_export_headers
["From", "To", "Comment", "Total Moved"]
Expand Down Expand Up @@ -83,4 +84,8 @@ def insufficient_items
inventory = View::Inventory.new(organization_id)
line_items.select { |i| i.quantity > inventory.quantity_for(item_id: i.item_id) }
end

def line_items_quantity_is_positive
line_items_quantity_is_at_least(1)
end
end
17 changes: 16 additions & 1 deletion spec/controllers/transfers_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RSpec.describe TransfersController, type: :controller do
let(:organization) { create(:organization) }
let(:organization) { create(:organization, :with_items) }
let(:user) { create(:user, organization: organization) }

context "While signed in" do
Expand Down Expand Up @@ -60,6 +60,21 @@
expect(response).to render_template("new")
expect(flash.keys).to match_array(['error'])
end

it "should raise an exception when quantity is negative" do
attributes = attributes_for(
:transfer,
organization_id: organization.id,
to_id: create(:storage_location, organization: organization).id,
from_id: create(:storage_location, organization: organization).id,
line_items_attributes: [
item_id: organization.items.first.id,
quantity: -1
]
)
post :create, params: { transfer: attributes }
expect(flash.keys).to match_array(['error'])
end
end

describe "GET #new" do
Expand Down

0 comments on commit 7fa75cd

Please sign in to comment.