Skip to content

Commit a96b468

Browse files
author
Olexii Kasianenko
committed
fix(kit_creation): calculate total value in cents for kits and update UI accordingly
1 parent 9e41917 commit a96b468

File tree

6 files changed

+59
-25
lines changed

6 files changed

+59
-25
lines changed

app/services/kit_create_service.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ def call
4444
raise item_creation_result.error
4545
end
4646
kit.items.update_all(visible_to_partners: kit.visible_to_partners)
47-
kit.items.each do |item|
48-
item.update!(value_in_dollars: kit.value_in_dollars)
47+
kit_value_in_cents = kit.items.reduce(0) do |sum, i|
48+
sum + i.value_in_cents.to_i * kit.line_items.find_by(item_id: i.id).quantity.to_i
4949
end
50+
kit.update!(value_in_cents: kit_value_in_cents)
5051
rescue StandardError => e
5152
errors.add(:base, e.message)
5253
raise ActiveRecord::Rollback

app/views/kits/_form.html.erb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414
<%= f.check_box :visible_to_partners, {class: "input-group-text", id: "visible_to_partners"}, "true", "false" %>
1515
<% end %>
1616

17-
<%= f.input :value_in_cents, label: "Value for kit", wrapper: :input_group do %>
18-
<span class="input-group-text"><i class="fa fa-dollar"></i></span>
19-
<%= f.input_field :value_in_dollars, class: "form-control", min: 0 %>
20-
<% end %>
21-
2217
<fieldset style="margin-bottom: 2rem;" class='w-70'>
2318
<legend>Items in this Kit</legend>
2419
<div id="kit_line_items" class="line-item-fields" data-capture-barcode="true">
@@ -38,5 +33,6 @@
3833
</div>
3934
</div>
4035
</div>
36+
</div>
4137
</section>
4238
<% end %>

app/views/kits/_table.html.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<tr>
44
<th>Name</th>
55
<th>Items</th>
6+
<th>Value for kit</th>
67
<th>Allocations</th>
78
<th class="text-right">Actions</th>
89
</tr>
@@ -18,6 +19,12 @@
1819
<% end %>
1920
</ul>
2021
</td>
22+
<td>
23+
<span class="tooltip-target" data-toggle="tooltip" data-placement="top" title="Total value of all items in this kit based on current inventory item values">
24+
<%= number_to_currency(kit.value_in_dollars) %>
25+
<i class="fa fa-info-circle"></i>
26+
</span>
27+
</td>
2128
<td class='d-flex flex-column'>
2229
<table>
2330
<thead>

app/views/line_items/_line_item_fields.html.erb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<%= field.input :item_id,
1717
disabled: requested.present?,
1818
collection: @item_labels_with_quantities || @items, prompt: "Choose an item",
19-
include_blank: "",
2019
label: false,
2120
input_html: { class: "my-0 line_item_name", "data-controller": "select2" } %>
2221
<% if requested.present? %>

spec/services/kit_create_service_spec.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,25 @@
2424
}
2525
end
2626
end
27+
let(:kit_value_in_cents) do
28+
line_items_attr.sum do |li|
29+
item = Item.find(li[:item_id])
30+
item.value_in_cents.to_i * li[:quantity].to_i
31+
end
32+
end
2733

2834
it 'should return an the instance' do
2935
expect(subject).to be_a_kind_of(described_class)
3036
end
3137

3238
context 'when the parameters are valid' do
3339
it 'should create a new Kit' do
34-
expect { subject }.to change { Kit.all.count }.by(1)
40+
expect { subject }.to change { Kit.count }.by(1)
41+
expect(Kit.last.value_in_cents).to eq(kit_value_in_cents)
3542
end
3643

3744
it 'should create a new Item' do
38-
expect { subject }.to change { Item.all.count }.by(1)
39-
end
40-
41-
it 'should create the new Item associated with the Kit' do
42-
expect { subject }.to change { Kit.all.count }.by(1)
45+
expect { subject }.to change { Item.count }.by(1)
4346
end
4447

4548
context 'but an unexpected error gets raised' do
@@ -92,6 +95,14 @@
9295
end
9396
end
9497

98+
context 'line_items_attributes is empty' do
99+
let(:line_items_attr) { [] }
100+
101+
it 'should have an error At least one item is required' do
102+
expect(subject.errors.full_messages).to include("At least one item is required")
103+
end
104+
end
105+
95106
context 'because the kit_params is invalid for kit creation' do
96107
let(:kit_params) { { organization_id: organization_id } }
97108
let(:kit_validation_errors) do

spec/system/kit_system_spec.rb

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,21 @@
3535

3636
context "kit creation" do
3737
let(:kit_traits) { attributes_for(:kit) }
38-
let(:item) { Item.last }
39-
let(:quantity_per_kit) { 5 }
4038
let(:value_in_dollars) { 10.10 }
39+
let(:value_in_cents) { value_in_dollars * 100 }
40+
let!(:items) { create_list(:item, 2, value_in_cents: value_in_cents, organization: organization) }
41+
let(:quantity_per_kit) { 5 }
42+
let(:kit_value_in_dollars) { (items.count * quantity_per_kit * value_in_dollars).round(2) }
4143

4244
before do
4345
visit new_kit_path
4446
fill_in "Name", with: kit_traits[:name]
4547
find(:css, '#visible_to_partners').set(false)
46-
find(:css, '#kit_value_in_dollars').set(value_in_dollars)
47-
select item.name, from: "kit_line_items_attributes_0_item_id"
48-
find(:css, '#kit_line_items_attributes_0_quantity').set(quantity_per_kit)
48+
all(:css, '.line_item_name').first.select(items[0].name)
49+
all(:css, '.quantity').first.set(quantity_per_kit)
50+
click_link "Add Another Item"
51+
all(:css, '.line_item_name').last.select(items[1].name)
52+
all(:css, '.quantity').last.set(quantity_per_kit)
4953
end
5054

5155
subject { click_button "Save" }
@@ -55,14 +59,32 @@
5559
subject
5660
expect(page.find(".alert")).to have_content "Kit created successfully"
5761
expect(page).to have_content(kit_traits[:name])
58-
expect(page).to have_content("#{quantity_per_kit} #{item.name}")
62+
expect(page).to have_content("#{quantity_per_kit} #{items[0].name}")
63+
expect(page).to have_content("#{quantity_per_kit} #{items[1].name}")
5964
expect(Kit.last.name).to eq(kit_traits[:name])
6065
expect(Kit.last.visible_to_partners).to eq(false)
61-
expect(Kit.last.value_in_dollars).to eq(value_in_dollars)
62-
expect(item.reload.visible_to_partners).to eq(false)
63-
expect(item.reload.value_in_dollars).to eq(value_in_dollars)
66+
expect(Kit.last.value_in_dollars).to eq(kit_value_in_dollars)
67+
expect(items[0].reload.visible_to_partners).to eq(false)
68+
expect(items[1].reload.visible_to_partners).to eq(false)
69+
expect(items[0].reload.value_in_dollars).to eq(value_in_dollars)
70+
expect(items[1].reload.value_in_dollars).to eq(value_in_dollars)
6471
}.to change(Kit, :count).by(1)
6572
end
73+
74+
context "items not selected" do
75+
before do
76+
visit new_kit_path
77+
fill_in "Name", with: kit_traits[:name]
78+
find(:css, '#visible_to_partners').set(false)
79+
end
80+
81+
it "displays error indicating at least one item is required" do
82+
expect {
83+
subject
84+
expect(page.find(".alert")).to have_content "At least one item is required"
85+
}.not_to change(Kit, :count)
86+
end
87+
end
6688
end
6789

6890
it "can add items correctly" do
@@ -225,8 +247,6 @@
225247
visit new_kit_path
226248
kit_traits = attributes_for(:kit)
227249

228-
find(:css, '#kit_value_in_dollars').set('10.10')
229-
230250
item = Item.last
231251
quantity_per_kit = 5
232252
select item.name, from: "kit_line_items_attributes_0_item_id"

0 commit comments

Comments
 (0)