Skip to content

Conversation

@AlexeyKasianenko
Copy link

Resolves #5275

Description

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

1/ Sign in as org admin.
3/ View a storage location. You're going to need an item that exists in it for the next step.
3/ Make a new kit (Inventory | Kits | New Kit) , giving it that item, and some market value, and visibility unchecked.
4/ For that kit, change its allocation, adding some to the storage location you looked at in step 1 (Modify allocation, choose the storage location, and put a positive # in "Change kit quantity by". Save.)
5) Check the value per item on the corresponding item for the kit: (Inventory | Items, the view the corresponding item) It should correspond to the value you entered for the kit. The visibility should also match.

Screenshots

@AlexeyKasianenko AlexeyKasianenko changed the title fix(kit_creation): update item visibility and value during kit creation WIP fix(kit_creation): update item visibility and value during kit creation Oct 7, 2025
@awwaiid awwaiid self-assigned this Oct 12, 2025
@github-actions github-actions bot added the stale label Nov 12, 2025
@github-actions
Copy link
Contributor

Automatically unassigned after 7 days of inactivity.

@janeewheatley
Copy link
Collaborator

@awwaiid and @dorner I noticed this PR was up for a review, so I requested your reviews. Thanks!

Copy link
Collaborator

@dorner dorner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work, and sorry for the delay - this one slipped under my radar. Please see my comments.

Comment on lines 29 to 31
kit_value_in_cents = item.kit.items.reduce(0) do |sum, i|
sum + i.value_in_cents.to_i * item.kit.line_items.find_by(item_id: i.id).quantity.to_i
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're doing a lot of extra queries this way... I'd recommend instead looping over the line items, so you have access to the quantity, and then looking at the item. E.g.

kit_value_in_cents = item.kit.line_items.includes(:item).reduce(0) do |sum, line_item|
  sum + line_item.item.value_in_cents.to_i * line_item.quantity.to_i
end

unless item_creation_result.success?
raise item_creation_result.error
end
kit.items.update_all(visible_to_partners: kit.visible_to_partners)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely shouldn't happen - the items' visibility shouldn't be affected by a kit's visibility. The ticket was asking for the kit item (i.e. kit.item, not kit.items) to have its visibility updated.

raise item_creation_result.error
end
kit.items.update_all(visible_to_partners: kit.visible_to_partners)
kit_value_in_cents = kit.items.reduce(0) do |sum, i|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably should be moved into a method on Kit since it's used in multiple places.

end
let(:request_unit_ids) { [] }
let(:kit_value_in_cents) do
kit.line_items.reduce(0) do |sum, li|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use actual values in the spec rather than calculating them from the factory. It's better to create explicit records with known values and use those values during checks.

end
end
let(:kit_value_in_cents) do
line_items_attr.sum do |li|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto here.

RSpec.describe "Item management", type: :system do
let(:organization) { create(:organization) }
let(:user) { create(:user, organization: organization) }
let(:user) { create(:organization_admin, organization: organization) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this change necessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect(Item.last.value_in_cents).to eq(123_456)
end

it "can update an existing item as a user" do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this test have to change? The changes happening elsewhere should not have affected existing behavior.


# Consolidated these into one to reduce the setup/teardown
it "should display items in separate tabs", js: true do
it "should display items in separate tabs", js: true, driver: :selenium_chrome do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this necessary?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Kits-- on create, set visibility and market value of corresponding item

4 participants