Skip to content

Commit

Permalink
REFACTOR remove unused code childrenservedreportservice
Browse files Browse the repository at this point in the history
* All SQL code is duplicated in AcquisitionReportService
* RSpec is close enough to what is in AcquisitionReportService Spec that it can be removed without merging in
  (only difference is Diapers - Adult Briefs category is not created, but that shouldn't matter
  because the SQL looks for %diaper% so this category isn't testing anything different)
  • Loading branch information
jimmyli97 committed Aug 15, 2024
1 parent 2c5ed05 commit 141d959
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 99 deletions.
39 changes: 0 additions & 39 deletions app/services/reports/children_served_report_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,47 +31,8 @@ def average_children_monthly
total_children_served / 12.0
end

def disposable_diapers_from_kits_total
organization_id = @organization.id
year = @year

sql_query = <<-SQL
SELECT SUM(line_items.quantity * kit_line_items.quantity)
FROM distributions
INNER JOIN line_items ON line_items.itemizable_type = 'Distribution' AND line_items.itemizable_id = distributions.id
INNER JOIN items ON items.id = line_items.item_id
INNER JOIN kits ON kits.id = items.kit_id
INNER JOIN line_items AS kit_line_items ON kits.id = kit_line_items.itemizable_id
INNER JOIN items AS kit_items ON kit_items.id = kit_line_items.item_id
INNER JOIN base_items ON base_items.partner_key = kit_items.partner_key
WHERE distributions.organization_id = ?
AND EXTRACT(year FROM issued_at) = ?
AND LOWER(base_items.category) LIKE '%diaper%'
AND NOT (LOWER(base_items.category) LIKE '%cloth%' OR LOWER(base_items.name) LIKE '%cloth%')
AND NOT (LOWER(base_items.category) LIKE '%adult%')
SQL

sanitized_sql = ActiveRecord::Base.send(:sanitize_sql_array, [sql_query, organization_id, year])

result = ActiveRecord::Base.connection.execute(sanitized_sql)
result.first['sum'].to_i
end

private

def total_disposable_diapers_distributed
loose_disposable_distribution_total + disposable_diapers_from_kits_total
end

def loose_disposable_distribution_total
organization
.distributions
.for_year(year)
.joins(line_items: :item)
.merge(Item.disposable)
.sum("line_items.quantity")
end

def total_children_served_with_loose_disposables
organization
.distributions
Expand Down
61 changes: 1 addition & 60 deletions spec/services/reports/children_served_report_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
create_list(:line_item, 5, :distribution, quantity: 200, item: disposable_item, itemizable: dist)
create_list(:line_item, 5, :distribution, quantity: 300, item: non_disposable_item, itemizable: dist)
end
# within_time total distributed i

infant_distribution = create(:distribution, organization: organization, issued_at: within_time)
toddler_distribution = create(:distribution, organization: organization, issued_at: within_time)
Expand Down Expand Up @@ -105,64 +106,4 @@
})
end
end
describe "#disposable_diapers_from_kits_total" do
it "calculates the number of disposable diapers that have been distributed within kits this year" do
organization = create(:organization)

# create disposable/ nondisposable base items
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")
create(:base_item, name: "Infant Cloth Diaper", partner_key: "infant cloth diapers", category: "cloth diaper")
create(:base_item, name: "Adult Brief LXL Test", partner_key: "adult lxl test", category: "Diapers - Adult Briefs")

# create disposable/ nondisposable items
toddler_disposable_kit_item = create(:item, name: "Toddler Disposable Diaper", partner_key: "toddler diapers", organization: organization)
infant_disposable_kit_item = create(:item, name: "Infant Disposable Diapers", partner_key: "infant diapers", organization: organization)
infant_cloth_kit_item = create(:item, name: "Infant Cloth Diapers", partner_key: "infant cloth diapers", organization: organization)
adult_brief_kit_item = create(:item, name: "Adult Brief L/XL", partner_key: "adult lxl test", organization: organization)

# create line items that contain the d/nd items
toddler_disposable_line_item = create(:line_item, item: toddler_disposable_kit_item, quantity: 5)
infant_disposable_line_item = create(:line_item, item: infant_disposable_kit_item, quantity: 5)
infant_cloth_line_item = create(:line_item, item: infant_cloth_kit_item, quantity: 5)
adult_brief_line_item = create(:line_item, item: adult_brief_kit_item, quantity: 5)

# create kits that contain the d/nd line items
toddler_disposable_kit = create(:kit, organization: organization, line_items: [toddler_disposable_line_item])
infant_disposable_kit = create(:kit, organization: organization, line_items: [infant_disposable_line_item])
infant_cloth_kit = create(:kit, organization: organization, line_items: [infant_cloth_line_item])
adult_brief_kit = create(:kit, organization: organization, line_items: [adult_brief_line_item])

# create items which have the kits
create(:base_item, name: "Unrelated Base", partner_key: "unrelated base", category: "unrelated base")
infant_disposable_dist_item = create(:item, name: "Dist Item 1", organization: organization, partner_key: "unrelated base", kit: toddler_disposable_kit)
toddler_disposable_dist_item = create(:item, name: "Dist Item 2", organization: organization, partner_key: "unrelated base", kit: infant_disposable_kit)
infant_cloth_dist_item = create(:item, name: "Dist Item 3", organization: organization, partner_key: "unrelated base", kit: infant_cloth_kit)
adult_brief_dist_item = create(:item, name: "Dist Item 4", organization: organization, partner_key: "unrelated base", kit: adult_brief_kit)

within_time = Time.zone.parse("2020-05-31 14:00:00")

# create empty distributions
infant_distribution = create(:distribution, organization: organization, issued_at: within_time)
toddler_distribution = create(:distribution, organization: organization, issued_at: within_time)
adult_distribution = create(:distribution, organization: organization, issued_at: within_time)

# add line items to distributions which contain the d/nd kits
create(:line_item, quantity: 10, item: toddler_disposable_dist_item, itemizable: toddler_distribution)
create(:line_item, quantity: 10, item: infant_disposable_dist_item, itemizable: infant_distribution)
create(:line_item, quantity: 10, item: infant_cloth_dist_item, itemizable: infant_distribution)
create(:line_item, quantity: 10, item: adult_brief_dist_item, itemizable: adult_distribution)

service = described_class.new(organization: organization, year: within_time.year)

# Find distributions, that has a
# Line item, that has an
# Item, which has a
# Kit, which has a
# Line item, which has an
# Item, which is a disposable diaper.
# And then add all those quantities up
expect(service.disposable_diapers_from_kits_total).to eq(100)
end
end
end

0 comments on commit 141d959

Please sign in to comment.