Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3986: Disallow duplicate items in audit #4025

Merged
merged 4 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/controllers/audits_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def update
if @audit.update(audit_params)
save_audit_status_and_redirect(params)
else
flash[:error] = "Something didn't work quite right -- try again?"
flash[:error] = @audit.errors.full_messages.join("\n")
@storage_locations = [@audit.storage_location]
set_items
@audit.line_items.build if @audit.line_items.empty?
Expand Down Expand Up @@ -93,7 +93,8 @@ def destroy

def handle_audit_errors
error_message = @audit.errors.uniq(&:attribute).map do |error|
"#{error.attribute.capitalize} ".tr("_", " ") + error.message
attr = (error.attribute.to_s == 'base') ? '' : error.attribute.capitalize
"#{attr} ".tr("_", " ") + error.message
end
flash[:error] = error_message.join(", ")
end
Expand Down
11 changes: 11 additions & 0 deletions app/models/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Audit < ApplicationRecord
validates :storage_location, :organization, presence: true
validate :line_items_exist_in_inventory
validate :line_items_quantity_is_not_negative
validate :line_items_unique_by_item_id
validate :user_is_organization_admin_of_the_organization

def self.storage_locations_audited_for(organization)
Expand All @@ -46,6 +47,16 @@ def user_is_organization_admin_of_the_organization

private

def line_items_unique_by_item_id
item_ids = line_items.map(&:item_id)
duplicate_ids = item_ids.select { |i| item_ids.count(i) > 1 }
if duplicate_ids.any?
item_names = Item.where(id: duplicate_ids).map(&:name)
errors.add(:base,
"You have entered at least one duplicate item: #{item_names.join(", ")}")
end
end

def line_items_quantity_is_not_negative
line_items_quantity_is_at_least(0)
end
Expand Down
14 changes: 14 additions & 0 deletions spec/models/audit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@
expect(audit.save).to be_falsey
end

it 'cannot have duplicate line items' do
item = create(:item, name: "Dupe Item")
storage_location = create(:storage_location, :with_items, item: item, item_quantity: 10)
audit = build(:audit,
storage_location: storage_location,
line_items_attributes: [
{ item_id: item.id, quantity: 3 },
{ item_id: item.id, quantity: 5 }
])

expect(audit.save).to be_falsey
expect(audit.errors.full_messages).to eq(["You have entered at least one duplicate item: Dupe Item"])
end

it "can have line items that has quantity as a positive integer" do
item = create(:item)
storage_location = create(:storage_location, :with_items, item: item, item_quantity: 10)
Expand Down
Loading