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

Bank Org Request Export - Include Custom Units #4608

Merged
merged 24 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
af2cfee
Modify exporter to use real item_requests
awwaiid Aug 5, 2024
3531d9b
Merge remote-tracking branch 'origin/main' into request-export-units
awwaiid Aug 11, 2024
d0cf9a5
Include the units of the requested items
awwaiid Aug 24, 2024
64970ec
Rename to more correct item_requests
awwaiid Aug 24, 2024
9786bec
Reorganize and stupify spec
awwaiid Aug 24, 2024
5157b4f
Use dash to separate unit
awwaiid Aug 24, 2024
c99c4d3
Re-arrange factory create a bit
awwaiid Aug 24, 2024
555daf3
Merge remote-tracking branch 'origin/main' into request-export-units
awwaiid Aug 24, 2024
2e82bbd
Create item_requests for realistic mock data
awwaiid Aug 24, 2024
ae5551b
Only add new export units when custom-units feature is enabled
awwaiid Aug 24, 2024
d58a312
Handle the case where the item custom-unit was deleted later
awwaiid Aug 24, 2024
09bc851
Fix request export for default-unit
awwaiid Sep 1, 2024
9424cba
Merge remote-tracking branch 'origin/main' into request-export-units
awwaiid Sep 1, 2024
7815c0a
Use empty string for request unit in spec
awwaiid Sep 1, 2024
be6b4c0
Merge branch 'main' into request-export-units
awwaiid Oct 13, 2024
84224ba
Merge remote-tracking branch 'origin/main' into request-export-units
awwaiid Nov 3, 2024
6b5f2bf
Extract common name_with_unit for request items
awwaiid Nov 3, 2024
57b00bd
Use set in item header calc to simplify duplicates
awwaiid Nov 3, 2024
fd41178
Remove old commented out code
awwaiid Nov 3, 2024
fcc4d91
Specify exact expected values
awwaiid Nov 3, 2024
11433cd
Merge branch 'main' into request-export-units
awwaiid Nov 24, 2024
685fb44
Deal with pluralization conflict and update spec [#4405]
awwaiid Nov 25, 2024
ec8c454
Prefetch some data and clarify all_item_requests name [#4405]
awwaiid Nov 25, 2024
d643bd3
Change spec to send AR relation
awwaiid Nov 25, 2024
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
61 changes: 37 additions & 24 deletions app/services/exports/export_request_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,25 @@ def item_headers
end

def compute_item_headers
item_names = items.pluck(:name)
# This reaches into the item, handling invalid deleted items
item_names = []
item_requests.each do |item_request|
if item_request.item
item = item_request.item
item_names << item.name
if Flipper.enabled?(:enable_packs)
item.request_units.each do |unit|
item_names << "#{item.name} - #{unit.name}"
end

# It's possible that the unit is no longer valid, so we'd
# add that individually
if item_request.request_unit.present? && !item.request_units.pluck(:name).include?(item_request.request_unit)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we grab all the request_units at the top rather than doing a separate DB query per item 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.

I followed another alternative of switching to a set and adding rather than re-testing; avoids the extra DB call.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm confused - this avoids an extra DB call per item, but you're still doing a DB call per item. I think you might be missing an includes somewhere? Or am I reading this wrong?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@dorner ok -- I followed some log/bullet.log advice and also watched the logs as I loaded this page. From that I sprinkled a few .includes around to pre load things. What do you think?

item_names << "#{item.name} - #{item_request.request_unit}"
end
end
end
end

# Adding this to handle cases in which a requested item
# has been deleted. Normally this wouldn't be neccessary,
Expand All @@ -75,38 +93,33 @@ def build_row_data(request)

row += Array.new(item_headers.size, 0)

request.request_items.each do |request_item|
item_name = fetch_item_name(request_item['item_id']) || DELETED_ITEMS_COLUMN_HEADER
request.item_requests.each do |item_request|
item_name = fetch_item_name(item_request) || DELETED_ITEMS_COLUMN_HEADER
item_column_idx = headers_with_indexes[item_name]

if item_name == DELETED_ITEMS_COLUMN_HEADER
# Add to the deleted column for every item that
# does not match any existing Item.
row[item_column_idx] ||= 0
end
row[item_column_idx] += request_item['quantity']
row[item_column_idx] ||= 0
row[item_column_idx] += item_request.quantity.to_i
end

row
end

def fetch_item_name(item_id)
@item_name_to_id_map ||= items.inject({}) do |acc, item|
acc[item.id] = item.name
acc
def fetch_item_name(item_request)
# The item_request has the item name, but we go ahead and try to get it
# off of the real item. Weirdly we do this because the item might have
# been deleted historically without deleting the request.
if item_request.item
if Flipper.enabled?(:enable_packs) && item_request.request_unit.present?
"#{item_request.name} - #{item_request.request_unit}"
else
item_request.name
end
awwaiid marked this conversation as resolved.
Show resolved Hide resolved
end

@item_name_to_id_map[item_id]
end

def items
return @items if @items

item_ids = requests.flat_map do |request|
request.request_items.map { |item| item['item_id'] }
end

@items ||= Item.where(id: item_ids)
def item_requests
return @item_requests if @item_requests
@item_requests ||= Set.new(requests.flat_map(&:item_requests)).to_a
@item_requests
end
end
end
Loading