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 all 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
4 changes: 2 additions & 2 deletions app/models/partners/item_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ def request_unit_is_supported
end
end

def name_with_unit
def name_with_unit(quantity_override = nil)
if item
if Flipper.enabled?(:enable_packs) && request_unit.present?
"#{name} - #{request_unit.pluralize(quantity.to_i)}"
"#{name} - #{request_unit.pluralize(quantity_override || quantity.to_i)}"
else
name
end
Expand Down
56 changes: 28 additions & 28 deletions app/services/exports/export_request_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class ExportRequestService
DELETED_ITEMS_COLUMN_HEADER = '<DELETED_ITEMS>'.freeze

def initialize(requests)
@requests = requests.includes(:partner)
@requests = requests.includes(:partner, {item_requests: :item})
end

def generate_csv
Expand Down 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 = Set.new
all_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.pluralize}"
end

# It's possible that the unit is no longer valid, so we'd
# add that individually
if item_request.request_unit.present?
item_names << "#{item.name} - #{item_request.request_unit.pluralize}"
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,20 @@ 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 = item_request.name_with_unit(0) || 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
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 all_item_requests
return @all_item_requests if @all_item_requests
@all_item_requests ||= Partners::ItemRequest.where(request: requests).includes(item: :request_units)
@all_item_requests
end
end
end
2 changes: 1 addition & 1 deletion app/services/requests_total_items_service.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class RequestsTotalItemsService
def initialize(requests:)
@requests = requests
@requests = requests.includes(item_requests: {item: :request_units})
end

def calculate
Expand Down
Loading