Skip to content

Commit 1025cf5

Browse files
awwaiiddorner
andauthored
Product totals - custom units (#4609)
* Implement custom units in request total popup * Sort request counts by name instead of by count * Remove unneeded test, this case doesn't happen Verified that in production there are zero of these instances * Pluralize units and update spec [#4408] * Do not show dash for blank unit * Sort by the lower-cased name for consistency * Use default hash value Co-authored-by: Daniel Orner <[email protected]> * Spec description improvement Co-authored-by: Daniel Orner <[email protected]> * Spec description improvement Co-authored-by: Daniel Orner <[email protected]> * Make item names explicit in test [#4408] * Unify on request item name with unit refactor across PRs [#4408] --------- Co-authored-by: Daniel Orner <[email protected]>
1 parent 99d417d commit 1025cf5

File tree

4 files changed

+58
-45
lines changed

4 files changed

+58
-45
lines changed

app/models/partners/item_request.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,15 @@ def request_unit_is_supported
3535
errors.add(:request_unit, "is not supported")
3636
end
3737
end
38+
39+
def name_with_unit
40+
if item
41+
if Flipper.enabled?(:enable_packs) && request_unit.present?
42+
"#{name} - #{request_unit.pluralize(quantity.to_i)}"
43+
else
44+
name
45+
end
46+
end
47+
end
3848
end
3949
end

app/services/requests_total_items_service.rb

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,19 @@ def initialize(requests:)
66
def calculate
77
return unless requests
88

9-
request_items_array = []
10-
11-
request_items.each do |items|
12-
items.each do |json|
13-
request_items_array << [item_name(json['item_id']), json['quantity']]
14-
end
9+
totals = Hash.new(0)
10+
item_requests.each do |item_request|
11+
totals[item_request.name_with_unit] += item_request.quantity.to_i
1512
end
1613

17-
request_items_array.inject({}) do |item, (quantity, total)|
18-
item[quantity] ||= 0
19-
item[quantity] += total.to_i
20-
item
21-
end
14+
totals
2215
end
2316

2417
private
2518

2619
attr_accessor :requests
2720

28-
def request_items
29-
@request_items ||= requests.pluck(:request_items)
30-
end
31-
32-
def request_items_ids
33-
request_items.flat_map { |jitem| jitem.map { |item| item["item_id"] } }
34-
end
35-
36-
def items_names
37-
@items_names ||= Item.where(id: request_items_ids).as_json(only: [:id, :name])
38-
end
39-
40-
def item_name(id)
41-
item_found = items_names.find { |item| item["id"] == id }
42-
item_found&.fetch('name') || '*Unknown Item*'
21+
def item_requests
22+
@item_requests ||= requests.flat_map(&:item_requests)
4323
end
4424
end

app/views/requests/_calculate_product_totals.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</tr>
1717
</thead>
1818
<tbody>
19-
<% @calculate_product_totals.sort_by { |name, quantity| -quantity}.each do |name, quantity| %>
19+
<% @calculate_product_totals.sort_by { |name, quantity| name.downcase }.each do |name, quantity| %>
2020
<tr>
2121
<td><%= name %></td>
2222
<td><%= quantity %></td>

spec/services/requests_total_items_service_spec.rb

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,65 @@
44
subject { described_class.new(requests: requests).calculate }
55

66
context 'when the request items is not blank' do
7-
let(:sample_items) { create_list(:item, 3, organization: organization) }
7+
let(:sample_items) do
8+
create_list(:item, 3, :with_unit, organization: organization, unit: "bundle") do |item, n|
9+
item.name = "item_name_#{n}"
10+
item.save!
11+
end
12+
end
813
let(:item_names) { sample_items.pluck(:name) }
914
let(:item_ids) { sample_items.pluck(:id) }
1015
let(:requests) do
1116
[
12-
create(:request, request_items: item_ids.map { |k| { "item_id" => k, "quantity" => 20 } }),
13-
create(:request, request_items: item_ids.map { |k| { "item_id" => k, "quantity" => 10 } })
17+
create(:request, :with_item_requests, request_items: item_ids.map { |k| { "item_id" => k, "quantity" => 20 } }),
18+
create(:request, :with_item_requests, request_items: item_ids.map { |k| { "item_id" => k, "quantity" => 10, "request_unit" => "bundle" } }),
19+
create(:request, :with_item_requests, request_items: item_ids.map { |k| { "item_id" => k, "quantity" => 50, "request_unit" => "bundle" } })
1420
]
1521
end
1622

1723
it 'return items with correct quantities calculated' do
18-
expect(subject.first.last).to eq(30)
24+
expect(subject.first.last).to eq(80)
1925
end
2026

2127
it 'return the names of items correctly' do
22-
expect(subject.keys).to eq(item_names)
28+
expect(subject.keys).to eq([
29+
"item_name_0",
30+
"item_name_1",
31+
"item_name_2"
32+
])
2333
end
24-
end
2534

26-
context 'when item name is nil' do
27-
let(:item) do
28-
i = build(:item, name: nil)
29-
i.save(validate: false)
30-
i
31-
end
32-
let(:requests) do
33-
[create(:request, request_items: [{ "item_id" => item.id, "quantity" => 20 }])]
34-
end
35+
context 'when custom request units are specified and enabled' do
36+
before do
37+
Flipper.enable(:enable_packs)
38+
end
39+
40+
it 'returns the names of items correctly' do
41+
expect(subject.keys).to eq([
42+
"item_name_0",
43+
"item_name_1",
44+
"item_name_2",
45+
"item_name_0 - bundles",
46+
"item_name_1 - bundles",
47+
"item_name_2 - bundles"
48+
])
49+
end
3550

36-
it 'return Unknown Item' do
37-
expect(subject.first.first).to eq('*Unknown Item*')
51+
it 'returns items with correct quantities calculated' do
52+
expect(subject).to eq({
53+
"item_name_0" => 20,
54+
"item_name_0 - bundles" => 60,
55+
"item_name_1" => 20,
56+
"item_name_1 - bundles" => 60,
57+
"item_name_2" => 20,
58+
"item_name_2 - bundles" => 60
59+
})
60+
end
3861
end
3962
end
4063

4164
context 'when provided with requests that have no request items' do
42-
let(:requests) { [create(:request, request_items: {})] }
65+
let(:requests) { [create(:request, :with_item_requests, request_items: {})] }
4366

4467
it { is_expected.to be_blank }
4568
end

0 commit comments

Comments
 (0)