Skip to content

Commit abac329

Browse files
awwaiidSukhpreet-s
authored andcommitted
Bank Org Request Export - Include Custom Units (rubyforgood#4608)
* Modify exporter to use real item_requests * Include the units of the requested items * Rename to more correct item_requests * Reorganize and stupify spec Use fixes specific values instead of calculated ones in most places * Use dash to separate unit * Re-arrange factory create a bit * Create item_requests for realistic mock data * Only add new export units when custom-units feature is enabled * Handle the case where the item custom-unit was deleted later * Fix request export for default-unit * Use empty string for request unit in spec * Extract common name_with_unit for request items * Use set in item header calc to simplify duplicates Also pluralize consistently * Remove old commented out code * Specify exact expected values * Deal with pluralization conflict and update spec [rubyforgood#4405] * Prefetch some data and clarify all_item_requests name [rubyforgood#4405] * Change spec to send AR relation
1 parent 0ac32ee commit abac329

File tree

6 files changed

+294
-76
lines changed

6 files changed

+294
-76
lines changed

app/models/partners/item_request.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ def request_unit_is_supported
3636
end
3737
end
3838

39-
def name_with_unit
39+
def name_with_unit(quantity_override = nil)
4040
if item
4141
if Flipper.enabled?(:enable_packs) && request_unit.present?
42-
"#{name} - #{request_unit.pluralize(quantity.to_i)}"
42+
"#{name} - #{request_unit.pluralize(quantity_override || quantity.to_i)}"
4343
else
4444
name
4545
end

app/services/exports/export_request_service.rb

+28-28
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class ExportRequestService
33
DELETED_ITEMS_COLUMN_HEADER = '<DELETED_ITEMS>'.freeze
44

55
def initialize(requests)
6-
@requests = requests.includes(:partner)
6+
@requests = requests.includes(:partner, {item_requests: :item})
77
end
88

99
def generate_csv
@@ -61,7 +61,25 @@ def item_headers
6161
end
6262

6363
def compute_item_headers
64-
item_names = items.pluck(:name)
64+
# This reaches into the item, handling invalid deleted items
65+
item_names = Set.new
66+
all_item_requests.each do |item_request|
67+
if item_request.item
68+
item = item_request.item
69+
item_names << item.name
70+
if Flipper.enabled?(:enable_packs)
71+
item.request_units.each do |unit|
72+
item_names << "#{item.name} - #{unit.name.pluralize}"
73+
end
74+
75+
# It's possible that the unit is no longer valid, so we'd
76+
# add that individually
77+
if item_request.request_unit.present?
78+
item_names << "#{item.name} - #{item_request.request_unit.pluralize}"
79+
end
80+
end
81+
end
82+
end
6583

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

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

78-
request.request_items.each do |request_item|
79-
item_name = fetch_item_name(request_item['item_id']) || DELETED_ITEMS_COLUMN_HEADER
96+
request.item_requests.each do |item_request|
97+
item_name = item_request.name_with_unit(0) || DELETED_ITEMS_COLUMN_HEADER
8098
item_column_idx = headers_with_indexes[item_name]
81-
82-
if item_name == DELETED_ITEMS_COLUMN_HEADER
83-
# Add to the deleted column for every item that
84-
# does not match any existing Item.
85-
row[item_column_idx] ||= 0
86-
end
87-
row[item_column_idx] += request_item['quantity']
99+
row[item_column_idx] ||= 0
100+
row[item_column_idx] += item_request.quantity.to_i
88101
end
89102

90103
row
91104
end
92105

93-
def fetch_item_name(item_id)
94-
@item_name_to_id_map ||= items.inject({}) do |acc, item|
95-
acc[item.id] = item.name
96-
acc
97-
end
98-
99-
@item_name_to_id_map[item_id]
100-
end
101-
102-
def items
103-
return @items if @items
104-
105-
item_ids = requests.flat_map do |request|
106-
request.request_items.map { |item| item['item_id'] }
107-
end
108-
109-
@items ||= Item.where(id: item_ids)
106+
def all_item_requests
107+
return @all_item_requests if @all_item_requests
108+
@all_item_requests ||= Partners::ItemRequest.where(request: requests).includes(item: :request_units)
109+
@all_item_requests
110110
end
111111
end
112112
end

app/services/requests_total_items_service.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class RequestsTotalItemsService
22
def initialize(requests:)
3-
@requests = requests
3+
@requests = requests.includes(item_requests: {item: :request_units})
44
end
55

66
def calculate

0 commit comments

Comments
 (0)