-
-
Notifications
You must be signed in to change notification settings - Fork 493
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
Add missing kit rspecs, DRY up kit base items and report service #4665
base: main
Are you sure you want to change the base?
Changes from 13 commits
f8b85d3
a4d1164
8ee83ee
7f776ab
48c1835
dd4ce7b
dd2b556
b59975a
64e5a52
240a32e
a74dcc9
8fde086
7d25b3a
94c1d91
94030f7
44c42be
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# [Super Admin] Manage the BaseItems -- this is the only place in the app where Base Items can be | ||
# added / modified. Base Items are both the template and common thread for regular Items | ||
# | ||
# See #4656, BaseItems are pending significant changes/possible deletion | ||
class Admin::BaseItemsController < AdminController | ||
def edit | ||
@base_item = BaseItem.find(params[:id]) | ||
|
@@ -40,7 +42,9 @@ def show | |
|
||
def destroy | ||
@base_item = BaseItem.includes(:items).find(params[:id]) | ||
if @base_item.items.any? && @base_item.destroy | ||
if (@base_item.id = KitCreateService.find_or_create_kit_base_item!.id) | ||
redirect_to admin_base_items_path, alert: "You cannot delete the Kits base item. This is reserved for all Kits." | ||
elsif @base_item.items.any? && @base_item.destroy | ||
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. Shouldn't this be 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. yes, fixed in commit 94030f7 |
||
redirect_to admin_base_items_path, notice: "Base Item deleted!" | ||
else | ||
redirect_to admin_base_items_path, alert: "Failed to delete Base Item. Are there still items attached?" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,3 @@ def to_h | |
{ partner_key: partner_key, name: name } | ||
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 | ||
organization_id = @organization.id | ||
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. (continuing conversation from #4585) these lines were removed because they're unused in this service and duplicated in AcquisitionReportService (probably the author copied AcquisitionReportService as a template when creating this service and forgot to delete them) |
||
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
def 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. Top-level functions look kind of weird in a Rails app. Can this be wrapped in a module? It worked in 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. added to module |
||
# 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.find_or_create_kit_base_item! | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
RSpec.describe "Admin::BaseItems", type: :request do | ||
let(:organization) { create(:organization) } | ||
let(:user) { create(:user, organization: organization) } | ||
let(:super_admin) { create(:super_admin, organization: organization) } | ||
let(:organization_admin) { create(:organization_admin, organization: organization) } | ||
|
||
# TODO: should this be testing something? | ||
context "while signed in as a super admin" do | ||
before do | ||
sign_in(@super_admin) | ||
sign_in(super_admin) | ||
end | ||
|
||
it "doesn't let you delete the Kit base item" do | ||
kit_base_item = KitCreateService.find_or_create_kit_base_item! | ||
delete admin_base_item_path(id: kit_base_item.id) | ||
expect(flash[:alert]).to include("You cannot delete the Kits base item") | ||
expect(response).to be_redirect | ||
expect(BaseItem.exists?(kit_base_item.id)).to be true | ||
end | ||
end | ||
|
||
# TODO aren't organization_admins not allowed to view base items? | ||
# also, some of these tests are sending organization.id instead of BaseItem.id as args | ||
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. Pretty sure they can't. @cielf ? 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. Org admins can't see the base items pages. They see a list of base items that they pick from when setting up items, and there are places they can filter by base item, but they can't view or edit the base item information. 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. fixed specs to clarify that org admins can't access or edit base items in commit 44c42be |
||
context "When logged in as an organization admin" do | ||
before do | ||
sign_in(organization_admin) | ||
|
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.
Assignment inside a condition is pretty prone to bugs. Can we separate them out into two lines?
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.
I think this was a typo, fixed to
==
in commit 94030f7