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

Invalid item inventory #3982

Merged
merged 12 commits into from
Mar 3, 2024
Merged

Invalid item inventory #3982

merged 12 commits into from
Mar 3, 2024

Conversation

dorner
Copy link
Collaborator

@dorner dorner commented Dec 22, 2023

As per discussion with @cielf .

Currently, we allow items to be turned inactive even when it has inventory in a storage location, or is included in an active kit. Both of those things shouldn't be allowed, because it puts the system in an unknown state (what does it mean to have inventory of an item that isn't valid? Or to distribute kits that have invalid items in them?)

This PR makes a number of changes to make this flow stricter.

  • First, a migration to reactivate any inactive items that are currently in active kits.
  • Second, a migration to delete any inventory of inactive items.
  • Remove options to show inactive inventory (e.g. on the storage location or audit page).
  • Do not allow an item to be deactivated if it's in an active kit or in storage location inventory.
  • Do not allow a kit to be reactivated if it contains an inactive item (the user will be prompted to reactivate the item first).

This will help with the event sourcing project as we will not need to track the active status of items for querying them on an inventory basis.

@dorner dorner requested a review from cielf December 22, 2023 16:50
@cielf
Copy link
Collaborator

cielf commented Dec 22, 2023

Noting for @awwaiid 's benefit that we are going to have to communicate to the affected banks when this goes out

@cielf
Copy link
Collaborator

cielf commented Dec 22, 2023

But wait! What about the historical trends graph?

@cielf
Copy link
Collaborator

cielf commented Dec 22, 2023

But wait! What about the historical trends graph?

I don't think they use inventory.

@cielf
Copy link
Collaborator

cielf commented Dec 22, 2023

However there is another edge case I just thought of -- reclaiming distributions. Might never happen, but..

@cielf
Copy link
Collaborator

cielf commented Dec 22, 2023

And, of course, what happens if someone edits a donation or purchase that has an inactive item in it. Le Sigh

@dorner
Copy link
Collaborator Author

dorner commented Dec 24, 2023

I think if we're talking event-based, neither of those should be an issue. Editing something updates it at the time it was done - so if there are items that are now inactive, they may not have been inactive at the time of the donation etc. and they should show up on the edit page and in the event. Same with reclaiming (aka destroying) a distribution - we fire a new event that just says "this distribution never happened".

return
end

if @item.save
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 we change from update to save?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

update both sets the parameters and saves in a single function. In this case we're setting the parameters early (to check if the active parameter changed) and then saving it after our check.

@kit.reactivate
redirect_back(fallback_location: dashboard_path, notice: "Kit has been reactivated!")
else
redirect_back(fallback_location: dashboard_path, alert: "Cannot reactivate kit - it has inactive item! Please reactivate the items first.")
Copy link
Collaborator

Choose a reason for hiding this comment

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

tiny grammar thing -- s.b. "it has inactive items!"

@cielf cielf requested a review from awwaiid December 29, 2023 15:06
Copy link
Collaborator

@cielf cielf left a comment

Choose a reason for hiding this comment

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

Couple more things to think about, if we haven't already (I just woke up, so the memory is not completely online yet)
What happens if editing a dist, and decreasing the # of the inactive item?
What happens if a request comes in with an inactive item?
What happens if a purchase/donation is edited and the amount on an inactive item is changed?

@dorner
Copy link
Collaborator Author

dorner commented Jan 1, 2024

  1. Editing a distribution and decreasing the inactive item - this should work as the user expects and decrease the amount of that item, since at the time the distribution was made the item wasn't inactive. The user should be able to make whatever changes necessary at that time. Having said that, since the item is inactive now, it won't show up on any inventory reports anyway, so practically speaking it won't make much difference.
  2. Request comes in with inactive item - this shouldn't be possible already:
    partner.requestable_items.active.visible
  3. Same answer as 1 above.

@cielf
Copy link
Collaborator

cielf commented Jan 2, 2024

Yeah, all of this really hinges on the idea that editing a distribution is fixing a clerical error. If it actually represents real life changes, then it's potentially a different matter.

@rowenwillabus
Copy link
Contributor

While working on #3998 I noticed that inactive items are being reactivated upon editing distribution. I found that to be odd and may need some help understanding that.

@dorner
Copy link
Collaborator Author

dorner commented Jan 3, 2024

Ah, I knew I saw something weird. Yeah, it's very odd that we automatically make an item active whenever e.g. editing a distribution to change the amount of an item that's now inactive. It's like we kind of knew that inactive items were bad in a storage location, but also kind of didn't?

In any case we probably should not be doing this without any notification to the user. This might be why I saw some organizations with three items with almost identical names, all with different quantities.

@dorner
Copy link
Collaborator Author

dorner commented Jan 3, 2024

I'll make some updates on Friday hopefully.

@dorner
Copy link
Collaborator Author

dorner commented Jan 21, 2024

Per discussion on Jan. 21 - we should prevent changing the inventory values for inactive items, by editing any Itemizable (i.e. if the edit would change the values of an item that is now inactive, it should fail).

@dorner
Copy link
Collaborator Author

dorner commented Jan 22, 2024

Fix pushed as per discussion @cielf

@cielf
Copy link
Collaborator

cielf commented Jan 31, 2024

Hey @dorner. Here's a thought: Regarding the migration making inactive items that are in (active) kits active, I suggest that we should make them not visible to partners. Reasoning: If they are inactive, the bank is definitely not offering them to the partners, and this would be the path of least surprise.

@cielf
Copy link
Collaborator

cielf commented Feb 27, 2024

@dorner I think the status on this one is that I'm expecting a change regarding the above comment, then to have @awwaiid do the tech review and I'll do a last kick at the can.

dorner added 3 commits March 1, 2024 15:42
# Conflicts:
#	app/controllers/items_controller.rb
#	app/controllers/storage_locations_controller.rb
#	app/queries/items_by_storage_collection_and_quantity_query.rb
#	app/views/distributions/_form.html.erb
#	spec/models/storage_location_spec.rb
#	spec/services/itemizable_update_service_spec.rb
@cielf
Copy link
Collaborator

cielf commented Mar 1, 2024

Over to @awwaiid, then!

@@ -142,7 +142,6 @@ def increase_inventory(itemizable_array)
# This is, at least for now, how we log changes to the inventory made in this call
log = {}
# Iterate through each of the line-items in the moving box
Item.reactivate(itemizable_array.map { |item_hash| item_hash[:item_id] })
Copy link
Collaborator

Choose a reason for hiding this comment

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

Very good

@@ -0,0 +1,23 @@
class RemoveInvalidInventory < ActiveRecord::Migration[7.0]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Certainly the scariest thing here. Looks good.

Copy link
Collaborator

@awwaiid awwaiid left a comment

Choose a reason for hiding this comment

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

This all look great and I'm happy for the cleanup. I'm not merging it immediately until I confirm the timing.

@awwaiid awwaiid merged commit 6059f8e into main Mar 3, 2024
20 checks passed
@awwaiid awwaiid deleted the invalid-item-inventory branch March 3, 2024 15:58
Copy link
Contributor

@dorner: Your PR Invalid item inventory is part of today's Human Essentials production release: 2024.03.17.
Thank you very much for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants