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

3611: Refactor of storage location inventory updates #3614

Merged
merged 3 commits into from
Jun 9, 2023

Conversation

dorner
Copy link
Collaborator

@dorner dorner commented May 22, 2023

Resolves #3611

Description

This refactors the updating of storage location inventory to move it to a single place that works for distributions, purchases and donations. The centralized place uses the fix discovered in #3601 to address the race condition we found with distributions.

(Note - there are also adjustments, audits and transfers that affect inventory, but these do not make any changes to the line items themselves - in these cases the line items represent the transfer between locations rather than an update to an event that represents an increase or decrease in inventory.)

Type of change

Internal change

How Has This Been Tested?

Local and unit tests.

@cielf
Copy link
Collaborator

cielf commented May 22, 2023

We don't allow updates of adjustments. so they're definitely not a problem. I'm not sure I follow, yet, how audits and transfers aren't an issue.

@@ -129,6 +129,10 @@ def self.import_inventory(filename, org, loc)
adjustment.storage_location.increase_inventory(adjustment)
end

def remove_empty_items
inventory_items.where(quantity: 0).delete_all
Copy link
Collaborator

Choose a reason for hiding this comment

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

Beware, delete_all skips callbacks. Is that ok here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep. Confirmed inventory items has no callbacks.

Comment on lines 14 to 15
increase_method = (type == :increase) ? :increase_inventory : :decrease_inventory
decrease_method = (type == :increase) ? :decrease_inventory : :increase_inventory
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since the names flip around depending on the direction, how about we change to a name that describes the intent a bit better. Here's an idea:

Suggested change
increase_method = (type == :increase) ? :increase_inventory : :decrease_inventory
decrease_method = (type == :increase) ? :decrease_inventory : :increase_inventory
apply_change_method = (type == :increase) ? :increase_inventory : :decrease_inventory
undo_change_method = (type == :increase) ? :decrease_inventory : :increase_inventory

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Love it!

Comment on lines 20 to 25
update_storage_location(itemizable: itemizable,
increase_method: increase_method,
decrease_method: decrease_method,
params: params,
from_location: from_location,
to_location: to_location)
Copy link
Collaborator

Choose a reason for hiding this comment

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

So here is what that rename would be here:

Suggested change
update_storage_location(itemizable: itemizable,
increase_method: increase_method,
decrease_method: decrease_method,
params: params,
from_location: from_location,
to_location: to_location)
update_storage_location(itemizable: itemizable,
apply_change_method: apply_change_method,
undo_change_method: undo_change_method,
params: params,
from_location: from_location,
to_location: to_location)

Comment on lines 29 to 49
# @param itemizable [Itemizable]
# @param increase_method [Symbol]
# @param decrease_method [Symbol]
# @param params [Hash] Parameters passed from the controller. Should include `line_item_attributes`.
# @param from_location [StorageLocation]
# @param to_location [StorageLocation]
def self.update_storage_location(itemizable:, increase_method:, decrease_method:,
params:, from_location:, to_location:)
from_location.public_send(decrease_method, itemizable.to_a)
# Delete the line items -- they'll be replaced later
itemizable.line_items.delete_all
# Update the current model with the new parameters
itemizable.update!(params)
itemizable.reload
# Apply the new changes to the storage location inventory
to_location.public_send(increase_method, itemizable.to_a)

from_location.remove_empty_items
to_location.remove_empty_items
end
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

And here

Suggested change
# @param itemizable [Itemizable]
# @param increase_method [Symbol]
# @param decrease_method [Symbol]
# @param params [Hash] Parameters passed from the controller. Should include `line_item_attributes`.
# @param from_location [StorageLocation]
# @param to_location [StorageLocation]
def self.update_storage_location(itemizable:, increase_method:, decrease_method:,
params:, from_location:, to_location:)
from_location.public_send(decrease_method, itemizable.to_a)
# Delete the line items -- they'll be replaced later
itemizable.line_items.delete_all
# Update the current model with the new parameters
itemizable.update!(params)
itemizable.reload
# Apply the new changes to the storage location inventory
to_location.public_send(increase_method, itemizable.to_a)
from_location.remove_empty_items
to_location.remove_empty_items
end
end
# @param itemizable [Itemizable]
# @param apply_change_method [Symbol]
# @param undo_change_method [Symbol]
# @param params [Hash] Parameters passed from the controller. Should include `line_item_attributes`.
# @param from_location [StorageLocation]
# @param to_location [StorageLocation]
def self.update_storage_location(itemizable:, apply_change_method:, undo_change_method:,
params:, from_location:, to_location:)
from_location.public_send(undo_change_method, itemizable.to_a)
# Delete the line items -- they'll be replaced later
itemizable.line_items.delete_all
# Update the current model with the new parameters
itemizable.update!(params)
itemizable.reload
# Apply the new changes to the storage location inventory
to_location.public_send(apply_change_method, itemizable.to_a)
from_location.remove_empty_items
to_location.remove_empty_items
end
end

@awwaiid
Copy link
Collaborator

awwaiid commented May 24, 2023

I note that decrease will throw Errors::InsufficientAllotment if .... there aren't enough. You could add a test to validate that, but it 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.

Consider the method swappy swap rename.

@cielf
Copy link
Collaborator

cielf commented May 24, 2023

I note that decrease will throw Errors::InsufficientAllotment if .... there aren't enough. You could add a test to validate that, but it looks good.

I think we have an outstanding issue around catching that appropriately

item_quantity { 100 }
item { nil }
end

after(:create) do |storage_location, evaluator|
if evaluator.item.nil?
if evaluator.item.nil? && evaluator.item_count != 0

Choose a reason for hiding this comment

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

Suggested change
if evaluator.item.nil? && evaluator.item_count != 0
if evaluator.item.nil? && !evaluator.item_count.zero?

Just some syntactic sugar

@dorner
Copy link
Collaborator Author

dorner commented Jun 2, 2023

@awwaiid thanks for the suggestions - added in the fixes! Please check again.

@dorner dorner requested a review from awwaiid June 2, 2023 20:31
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 looks great! (some minor lint remains)

@dorner
Copy link
Collaborator Author

dorner commented Jun 4, 2023

Fixed!

@cielf
Copy link
Collaborator

cielf commented Jun 9, 2023

Any reason we haven't merged this yet?

@dorner
Copy link
Collaborator Author

dorner commented Jun 9, 2023

Don't think so! Let's go for it.

@dorner dorner merged commit 1365913 into main Jun 9, 2023
@dorner dorner deleted the 3611-purchase-donation-updates branch June 9, 2023 17:43
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.

Purchases and donations updates should be handled in the same fashion as distributions
4 participants