-
-
Notifications
You must be signed in to change notification settings - Fork 573
fix(kit_creation): update item visibility and value during kit creation #5411
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
base: main
Are you sure you want to change the base?
fix(kit_creation): update item visibility and value during kit creation #5411
Conversation
43d771e to
984aea5
Compare
|
Automatically unassigned after 7 days of inactivity. |
dorner
left a comment
There was a problem hiding this 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.
app/services/item_update_service.rb
Outdated
| 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 |
There was a problem hiding this comment.
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
app/services/kit_create_service.rb
Outdated
| unless item_creation_result.success? | ||
| raise item_creation_result.error | ||
| end | ||
| kit.items.update_all(visible_to_partners: kit.visible_to_partners) |
There was a problem hiding this comment.
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.
app/services/kit_create_service.rb
Outdated
| 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| |
There was a problem hiding this comment.
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| |
There was a problem hiding this comment.
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| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto here.
spec/system/item_system_spec.rb
Outdated
| RSpec.describe "Item management", type: :system do | ||
| let(:organization) { create(:organization) } | ||
| let(:user) { create(:user, organization: organization) } | ||
| let(:user) { create(:organization_admin, organization: organization) } |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
spec/system/item_system_spec.rb
Outdated
|
|
||
| # 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this necessary?
Resolves #5275
Description
Type of change
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