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

#4217 kit factory refactor #4585

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7a351cd
Update bundler version
jimmyli97 Aug 7, 2024
ceec002
REFACTOR clean up unused line item trait introduced in #4163
jimmyli97 Aug 1, 2024
4e1bc8b
REFACTOR replace kit :with_item with KitCreateService, hard code rspec
jimmyli97 Aug 1, 2024
7cabfa6
FIX typo in params in donation and purchase controllers
jimmyli97 Aug 1, 2024
1109170
DOCS add comment clarifying itemizable works with line items
jimmyli97 Jul 26, 2024
b9b8755
REFACTOR rename Item:kits scope to :housing_a_kit, add rspecs
jimmyli97 Aug 1, 2024
8c18e42
REFACTOR DRY up kit base_item, seed_base_items, prevent kit base_item…
jimmyli97 Aug 2, 2024
e1e2c31
Add rspec for item.is_in_kit?
jimmyli97 Aug 2, 2024
7f166cc
Fix lint
jimmyli97 Aug 8, 2024
2c5ed05
rename findorcreatebaseitem to add !
jimmyli97 Aug 9, 2024
141d959
REFACTOR remove unused code childrenservedreportservice
jimmyli97 Aug 15, 2024
080721e
Fix bin/setup so it's working with seed_base_item move
jimmyli97 Aug 15, 2024
e26f441
Merge branch 'main' into 4217-kit-various-refactors
jimmyli97 Aug 15, 2024
a111f74
Revert "REFACTOR clean up unused line item trait introduced in #4163"
jimmyli97 Sep 24, 2024
81c99af
Revert "FIX typo in params in donation and purchase controllers"
jimmyli97 Sep 24, 2024
7b4da7d
Revert "DOCS add comment clarifying itemizable works with line items"
jimmyli97 Sep 24, 2024
04917ba
Revert "REFACTOR rename Item:kits scope to :housing_a_kit, add rspecs"
jimmyli97 Sep 24, 2024
e4de92c
Revert "REFACTOR DRY up kit base_item, seed_base_items, prevent kit b…
jimmyli97 Sep 24, 2024
96b3ceb
Revert "Add rspec for item.is_in_kit?"
jimmyli97 Sep 24, 2024
af1dfd5
Revert "REFACTOR remove unused code childrenservedreportservice"
jimmyli97 Sep 24, 2024
6b50518
Revert "Fix bin/setup so it's working with seed_base_item move"
jimmyli97 Sep 24, 2024
730371a
REFACTOR move kit creation in specs into helper method
jimmyli97 Sep 25, 2024
51f0585
Merge branch 'main' into 4217-kit-various-refactors
jimmyli97 Sep 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
1 change: 0 additions & 1 deletion app/models/base_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ def to_h
{ partner_key: partner_key, name: name }
end
end

3 changes: 1 addition & 2 deletions app/services/kit_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def call
# kit item created.
kit_base_item = fetch_or_create_kit_base_item

# Create the Item.
# Create the item
item_creation = ItemCreateService.new(
organization_id: organization.id,
item_params: {
Expand Down Expand Up @@ -100,4 +100,3 @@ def partner_key_for(name)
"kit_#{name.underscore.gsub(/\s+/, '_')}"
end
end

17 changes: 9 additions & 8 deletions spec/events/inventory_aggregate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,11 @@
end

it "should process a kit allocation event" do
kit = FactoryBot.create(:kit, :with_item, organization: organization)
kit = create_kit(organization: organization, line_items_attributes: [
{item_id: item1.id, quantity: 10},
{item_id: item2.id, quantity: 3}
])

kit.line_items = []
kit.line_items << build(:line_item, quantity: 10, item: item1, itemizable: kit)
kit.line_items << build(:line_item, quantity: 3, item: item2, itemizable: kit)
KitAllocateEvent.publish(kit, storage_location1.id, 2)

# 30 - (10*2) = 10, 10 - (3*2) = 4
Expand Down Expand Up @@ -416,7 +416,11 @@
end

it "should process a kit deallocation event" do
kit = FactoryBot.create(:kit, :with_item, organization: organization)
kit = create_kit(organization: organization, line_items_attributes: [
{item_id: item1.id, quantity: 20},
{item_id: item2.id, quantity: 5}
])

TestInventory.create_inventory(organization,
{
storage_location1.id => {
Expand All @@ -432,9 +436,6 @@
})
inventory = InventoryAggregate.inventory_for(organization.id) # reload

kit.line_items = []
kit.line_items << build(:line_item, quantity: 20, item: item1, itemizable: kit)
kit.line_items << build(:line_item, quantity: 5, item: item2, itemizable: kit)
KitDeallocateEvent.publish(kit, storage_location1, 2)

# 30 + (20*2) = 70, 10 + (5*2) = 20
Expand Down
12 changes: 5 additions & 7 deletions spec/factories/kits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@
#
FactoryBot.define do
factory :kit do
sequence(:name) { |n| "Test Kit #{n}" }
sequence(:name) { |n| "Default Kit Name #{n} - Don't Match" }
organization

after(:build) do |instance, _|
if instance.line_items.blank?
instance.line_items << create(:line_item, item: create(:item, organization: instance.organization), itemizable: instance)
instance.line_items << build(:line_item, item: create(:item, organization: instance.organization), itemizable: nil)
end
end

trait :with_item do
after(:create) do |instance, _|
create(:item, kit: instance, organization: instance.organization)
end
end
# See #3652, changes to this factory are in progress
# For now, to create corresponding item and line item and persist to database call create_kit
# from spec/support/kit_helper.rb
end
end
16 changes: 9 additions & 7 deletions spec/models/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,10 @@
end

context "in a kit" do
let(:kit) { create(:kit, organization: organization) }
before do
create(:line_item, itemizable: kit, item: item)
create_kit(organization: organization, line_items_attributes: [
{item_id: item.id, quantity: 1}
])
end

it "should return false" do
Expand Down Expand Up @@ -271,10 +272,10 @@
end

context "in a kit" do
let(:kit) { create(:kit, organization: organization) }

before do
create(:line_item, itemizable: kit, item: item)
create_kit(organization: organization, line_items_attributes: [
{item_id: item.id, quantity: 1}
])
end

it "should return false" do
Expand Down Expand Up @@ -424,8 +425,9 @@
let(:kit) { create(:kit, name: "my kit") }

it "updates kit name" do
item.update(name: "my new name")
expect(item.name).to eq kit.name
name = "my new name"
item.update(name: name)
expect(kit.name).to eq name
end
end

Expand Down
15 changes: 9 additions & 6 deletions spec/models/kit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@
end

it "->alphabetized retrieves items in alphabetical order" do
kit_c = create(:kit, name: "KitC")
kit_b = create(:kit, name: "KitB")
kit_a = create(:kit, name: "KitA")
alphabetized_list = [kit_a.name, kit_b.name, kit_c.name]
a_name = "KitA"
b_name = "KitB"
c_name = "KitC"
create(:kit, name: c_name)
create(:kit, name: b_name)
create(:kit, name: a_name)
alphabetized_list = [a_name, b_name, c_name]

expect(Kit.alphabetized.count).to eq(3)
expect(Kit.alphabetized.map(&:name)).to eq(alphabetized_list)
Expand Down Expand Up @@ -107,7 +110,7 @@
end

describe '#can_deactivate?' do
let(:kit) { create(:kit, :with_item, organization: organization) }
let(:kit) { create(:kit, organization: organization) }

context 'with inventory' do
it 'should return false' do
Expand All @@ -131,7 +134,7 @@
end

specify 'deactivate and reactivate' do
kit = create(:kit, :with_item)
kit = create_kit(organization: organization)
expect(kit.active).to eq(true)
expect(kit.item.active).to eq(true)
kit.deactivate
Expand Down
6 changes: 4 additions & 2 deletions spec/requests/kit_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
let(:user) { create(:user, organization: organization) }
let(:organization_admin) { create(:organization_admin, organization: organization) }

let!(:kit) { create(:kit, :with_item, organization: organization) }
let!(:kit) {
create_kit(organization: organization)
}

describe "while signed in" do
before do
Expand All @@ -13,7 +15,7 @@
describe "GET #index" do
before do
# this shouldn't be shown
create(:kit, :with_item, active: false, name: "DOOBIE KIT", organization: organization)
create_kit(organization: organization, active: false, name: "DOOBIE KIT")
end

it "should include deactivate" do
Expand Down
16 changes: 10 additions & 6 deletions spec/services/reports/acquisition_report_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@
disposable_kit_item = create(:item, name: "Adult Disposable Diapers", partner_key: "adult diapers")
another_disposable_kit_item = create(:item, name: "Infant Disposable Diapers", partner_key: "infant diapers")

disposable_line_item = create(:line_item, item: disposable_kit_item, quantity: 5)
another_disposable_line_item = create(:line_item, item: another_disposable_kit_item, quantity: 5)
disposable_kit = create_kit(organization: organization, line_items_attributes: [
{item_id: disposable_kit_item.id, quantity: 5}
])

disposable_kit = create(:kit, :with_item, organization: organization, line_items: [disposable_line_item])
another_disposable_kit = create(:kit, :with_item, organization: organization, line_items: [another_disposable_line_item])
another_disposable_kit = create_kit(organization: organization, line_items_attributes: [
{item_id: another_disposable_kit_item.id, quantity: 5}
])

disposable_kit_item_distribution = create(:distribution, organization: organization, issued_at: within_time)
another_disposable_kit_item_distribution = create(:distribution, organization: organization, issued_at: within_time)

create(:line_item, :distribution, quantity: 10, item: disposable_kit.item, itemizable: disposable_kit_item_distribution)
create(:line_item, :distribution, quantity: 10, item: another_disposable_kit.item, itemizable: another_disposable_kit_item_distribution)
# Total disposable items distributed so far: 5*10 + 5*10 = 100

# create disposable and non disposable items
create(:base_item, name: "3T Diaper", partner_key: "toddler diapers", category: "disposable diaper")
Expand All @@ -42,6 +45,7 @@
create_list(:line_item, 5, :distribution, quantity: 20, item: disposable_item, itemizable: dist)
create_list(:line_item, 5, :distribution, quantity: 30, item: non_disposable_item, itemizable: dist)
end
# Total disposable items distributed: (100) + 2*5*20 = 300

# Donations outside drives
non_drive_donations = create_list(:donation, 2,
Expand Down Expand Up @@ -155,9 +159,9 @@

it 'should return the proper results on #report' do
expect(subject.report).to eq({
entries: { "Disposable diapers distributed" => "320",
entries: { "Disposable diapers distributed" => "300",
"Cloth diapers distributed" => "300",
"Average monthly disposable diapers distributed" => "27",
"Average monthly disposable diapers distributed" => "25",
"Total product drives" => 2,
"Disposable diapers collected from drives" => "600",
"Cloth diapers collected from drives" => "900",
Expand Down
4 changes: 2 additions & 2 deletions spec/services/reports/children_served_report_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
non_disposable_item = organization.items.where.not(id: organization.items.disposable).first

# Kits
kit = create(:kit, :with_item, organization: organization)
kit = create(:kit, organization: organization)

create(:base_item, name: "Toddler Disposable Diaper", partner_key: "toddler diapers", category: "disposable diaper")
create(:base_item, name: "Infant Disposable Diaper", partner_key: "infant diapers", category: "infant disposable diaper")
Expand Down Expand Up @@ -71,7 +71,7 @@
non_disposable_item = organization.items.where.not(id: organization.items.disposable).first

# Kits
kit = create(:kit, :with_item, organization: organization)
kit = create(:kit, organization: organization)

create(:base_item, name: "Toddler Disposable Diaper", partner_key: "toddler diapers", category: "disposable diaper")
create(:base_item, name: "Infant Disposable Diaper", partner_key: "infant diapers", category: "infant disposable diaper")
Expand Down
10 changes: 10 additions & 0 deletions spec/support/kit_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def create_kit(name: nil, active: true, organization: create(:organization), line_items_attributes: nil)
params = FactoryBot.attributes_for(:kit, active: active)
params[:name] = name if name

params[:line_items_attributes] = (line_items_attributes || [
{item_id: create(:item, organization: organization).id, quantity: 1}
])

KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit
end