-
-
Notifications
You must be signed in to change notification settings - Fork 480
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
base: main
Are you sure you want to change the base?
Changes from 13 commits
7a351cd
ceec002
4e1bc8b
7cabfa6
1109170
b9b8755
8c18e42
e1e2c31
7f166cc
2c5ed05
141d959
080721e
e26f441
a111f74
81c99af
7b4da7d
04917ba
e4de92c
96b3ceb
af1dfd5
6b50518
730371a
51f0585
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -800,4 +800,4 @@ DEPENDENCIES | |
webmock (~> 3.23) | ||
|
||
BUNDLED WITH | ||
2.5.16 | ||
2.5.17 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,5 +28,24 @@ class BaseItem < ApplicationRecord | |
def to_h | ||
{ partner_key: partner_key, name: name } | ||
end | ||
end | ||
|
||
def self.seed_base_items | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only used in specs and seeds. Not sure this belongs on the main BaseItem class. I'd probably create a module call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
# Initial starting qty for our test organizations | ||
base_items = File.read(Rails.root.join("db", "base_items.json")) | ||
items_by_category = JSON.parse(base_items) | ||
|
||
items_by_category.each do |category, entries| | ||
entries.each do |entry| | ||
BaseItem.find_or_create_by!( | ||
name: entry["name"], | ||
category: category, | ||
partner_key: entry["key"], | ||
updated_at: Time.zone.now, | ||
created_at: Time.zone.now | ||
) | ||
end | ||
end | ||
# Create global 'Kit' base item | ||
KitCreateService.FindOrCreateKitBaseItem! | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
class KitCreateService | ||
include ServiceObjectErrorsMixin | ||
|
||
KIT_BASE_ITEM_ATTRS = { | ||
name: 'Kit', | ||
category: 'kit', | ||
partner_key: 'kit' | ||
} | ||
|
||
attr_reader :kit | ||
|
||
def self.FindOrCreateKitBaseItem! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Idiomatic Ruby uses snake_case: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
BaseItem.find_or_create_by!(KIT_BASE_ITEM_ATTRS) | ||
end | ||
|
||
def initialize(organization_id:, kit_params:) | ||
@organization_id = organization_id | ||
@kit_params = kit_params | ||
|
@@ -16,16 +26,15 @@ def call | |
@kit = Kit.new(kit_params_with_organization) | ||
@kit.save! | ||
|
||
# Create a BaseItem that houses each | ||
# kit item created. | ||
kit_base_item = fetch_or_create_kit_base_item | ||
# Find or create the BaseItem for all items housing kits | ||
item_housing_a_kit_base_item = KitCreateService.FindOrCreateKitBaseItem! | ||
|
||
# Create the Item. | ||
# Create the item | ||
item_creation = ItemCreateService.new( | ||
organization_id: organization.id, | ||
item_params: { | ||
name: kit.name, | ||
partner_key: kit_base_item.partner_key, | ||
partner_key: item_housing_a_kit_base_item.partner_key, | ||
kit_id: kit.id | ||
} | ||
) | ||
|
@@ -45,7 +54,6 @@ def call | |
private | ||
|
||
attr_reader :organization_id, :kit_params | ||
|
||
def organization | ||
@organization ||= Organization.find_by(id: organization_id) | ||
end | ||
|
@@ -56,18 +64,6 @@ def kit_params_with_organization | |
}) | ||
end | ||
|
||
def fetch_or_create_kit_base_item | ||
BaseItem.find_or_create_by!({ | ||
name: 'Kit', | ||
category: 'kit', | ||
partner_key: 'kit' | ||
}) | ||
end | ||
|
||
def partner_key_for_kits | ||
'kit' | ||
end | ||
|
||
def valid? | ||
if organization.blank? | ||
errors.add(:organization_id, 'does not match any Organization') | ||
|
@@ -89,15 +85,4 @@ def kit_validation_errors | |
|
||
@kit_validation_errors = kit.errors | ||
end | ||
|
||
def associated_item_params | ||
{ | ||
kit: kit.name | ||
} | ||
end | ||
|
||
def partner_key_for(name) | ||
"kit_#{name.underscore.gsub(/\s+/, '_')}" | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,47 +31,8 @@ def average_children_monthly | |
total_children_served / 12.0 | ||
end | ||
|
||
def disposable_diapers_from_kits_total | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why were these lines removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continued conversation in #4665 |
||
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -381,11 +381,14 @@ | |
end | ||
|
||
it "should process a kit allocation event" do | ||
kit = FactoryBot.create(:kit, :with_item, organization: organization) | ||
params = FactoryBot.attributes_for(:kit) | ||
params[:line_items_attributes] = [ | ||
{item_id: item1.id, quantity: 10}, | ||
{item_id: item2.id, quantity: 3} | ||
] | ||
|
||
kit = KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way for us to just update the factory itself to call If not, can we create a reusable method to do this work, allowing you just to pass in the LineItems you want to create, and provide a sane default if we don't care what they are? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I considered overriding I created a helper method under |
||
|
||
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 | ||
|
@@ -416,7 +419,14 @@ | |
end | ||
|
||
it "should process a kit deallocation event" do | ||
kit = FactoryBot.create(:kit, :with_item, organization: organization) | ||
params = FactoryBot.attributes_for(:kit) | ||
params[:line_items_attributes] = [ | ||
{item_id: item1.id, quantity: 20}, | ||
{item_id: item2.id, quantity: 5} | ||
] | ||
|
||
kit = KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit | ||
|
||
TestInventory.create_inventory(organization, | ||
{ | ||
storage_location1.id => { | ||
|
@@ -432,9 +442,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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're going to be removing the current approach to base items at some point in the future. I think for now it's safe to just leave this method alone if nothing calls it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a note for this in PR #4665 a74dcc9