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

4397 - Add Request units to item new/edit #4418

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
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
30 changes: 27 additions & 3 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def index
end

def create
create = ItemCreateService.new(organization_id: current_organization.id, item_params: item_params)
create = ItemCreateService.new(organization_id: current_organization.id, item_params: item_params, request_unit_ids:)
sean-dickinson marked this conversation as resolved.
Show resolved Hide resolved
result = create.call

if result.success?
Expand Down Expand Up @@ -81,7 +81,6 @@ def show
def update
@item = current_organization.items.find(params[:id])
@item.attributes = item_params

deactivated = @item.active_changed? && [email protected]
if deactivated && [email protected]_deactivate?
@base_items = BaseItem.without_kit.alphabetized
Expand All @@ -90,7 +89,7 @@ def update
return
end

if @item.save
if update_item
redirect_to items_path, notice: "#{@item.name} updated!"
else
@base_items = BaseItem.without_kit.alphabetized
Expand Down Expand Up @@ -182,6 +181,31 @@ def item_params
)
end

def request_unit_ids
params.require(:item).permit(request_unit_ids: []).fetch(:request_unit_ids, [])
end

# We need to update both the item and the request_units together and fail together
def update_item
if Flipper.enabled?(:enable_packs)
update_item_and_request_units
else
@item.save
end
end

def update_item_and_request_units
begin
Item.transaction do
@item.save!
@item.sync_request_units!(request_unit_ids)
end
rescue
return false
end
true
end

helper_method \
def filter_params(_parameters = nil)
return {} unless params.key?(:filters)
Expand Down
7 changes: 7 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ def inventory_item_at(storage_location_id)
inventory_items.find_by(storage_location_id: storage_location_id)
end

def sync_request_units!(unit_ids)
request_units.clear
organization.request_units.where(id: unit_ids).pluck(:name).each do |name|
request_units.create!(name:)
end
end

private

def update_associated_kit_name
Expand Down
7 changes: 5 additions & 2 deletions app/services/item_create_service.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
class ItemCreateService
def initialize(organization_id:, item_params:)
def initialize(organization_id:, item_params:, request_unit_ids: [])
@organization_id = organization_id
@request_unit_ids = request_unit_ids
@item_params = item_params
end

def call
new_item = organization.items.new(item_params)

organization.transaction do
new_item.save!
if Flipper.enabled?(:enable_packs)
new_item.sync_request_units!(@request_unit_ids)
end

organization.storage_locations.each do |sl|
InventoryItem.create!(
Expand Down
8 changes: 8 additions & 0 deletions app/views/items/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
<%= f.input_field :package_size, class: "form-control" %>
<% end %>

<% if Flipper.enabled?(:enable_packs) %>
sean-dickinson marked this conversation as resolved.
Show resolved Hide resolved
<%= f.input :request_units, label: "Additional Custom Request Units" do %>
<% item_request_unit_names = @item.persisted? ? @item.request_units.pluck(:name) : [] %>
<% selected_request_units = current_organization.request_units.select { |unit| item_request_unit_names.include?(unit.name)}.pluck(:id) %>
<%= f.association :request_units, as: :check_boxes, collection: current_organization.request_units, checked: selected_request_units, label_method: :name, value_method: :id, class: "form-check-input" %>
<% end %>
<% end %>

<%= f.input :visible, label: "Item is Visible to Partners?", wrapper: :input_group do %>
<%= f.check_box :visible_to_partners, {class: "input-group-text", id: "visible_to_partners"}, "true", "false" %>
<% end %>
Expand Down
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_05_19_201258) do
ActiveRecord::Schema[7.1].define(version: 2024_05_27_151622) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -484,6 +484,7 @@
t.boolean "one_step_partner_invite", default: false, null: false
t.boolean "hide_value_columns_on_receipt", default: false
t.boolean "hide_package_column_on_receipt", default: false
t.boolean "signature_for_distribution_pdf", default: false
t.index ["latitude", "longitude"], name: "index_organizations_on_latitude_and_longitude"
t.index ["short_name"], name: "index_organizations_on_short_name"
end
Expand Down
45 changes: 45 additions & 0 deletions spec/controllers/items_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,41 @@
expect(item.reload.visible_to_partners).to be false
end
end

context "request units" do
before(:each) { Flipper.enable(:enable_packs) }
let(:item) { create(:item, organization:) }
let(:unit) { create(:unit, organization:) }
it "should add new item's request units" do
expect(item.request_units).to be_empty
request = put :update, params: { id: item.id, item: { request_unit_ids: [unit.id] } }
expect(request).to redirect_to(items_path)
expect(response).not_to have_error
expect(item.request_units.reload.pluck(:name)).to match_array [unit.name]
end

it "should remove item request units" do
# add an existing unit
create(:item_unit, item:, name: unit.name)
expect(item.request_units.size).to eq 1
request = put :update, params: { id: item.id, item: { request_unit_ids: [""] } }
expect(response).not_to have_error
expect(request).to redirect_to(items_path)
expect(item.request_units.reload).to be_empty
end

it "should add and remove request units at the same time" do
# attach a different unit to the item
unit_to_remove = create(:unit, organization:)
create(:item_unit, item:, name: unit_to_remove.name)
expect(item.request_units.pluck(:name)).to match_array [unit_to_remove.name]
request = put :update, params: { id: item.id, item: { request_unit_ids: [unit.id] } }
expect(response).not_to have_error
expect(request).to redirect_to(items_path)
# We should have removed the existing unit and replaced it with the new one
expect(item.request_units.reload.pluck(:name)).to match_array [unit.name]
end
end
end

describe "GET #show" do
Expand Down Expand Up @@ -119,6 +154,16 @@
expect(response).not_to have_error
end

it "should accept request_unit ids and create request_units" do
Flipper.enable(:enable_packs)
unit = create(:unit, organization: organization)
item_params[:item] = item_params[:item].merge({request_unit_ids: [unit.id]})
post :create, params: item_params
expect(response).not_to have_error
newly_created_item = Item.last
expect(newly_created_item.request_units.pluck(:name)).to match_array [unit.name]
end

it "should redirect to the item page" do
post :create, params: item_params

Expand Down
34 changes: 34 additions & 0 deletions spec/requests/items_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,40 @@
end
end

describe "GET #new" do
it "shows the organization request_units options if they exist" do
Flipper.enable(:enable_packs)
organization_units = create_list(:unit, 3, organization: organization)
get new_item_path
organization_units.each do |unit|
expect(response.body).to include unit.name
end
end
end

describe "GET #edit" do
it "shows the selected request_units" do
Flipper.enable(:enable_packs)
organization_units = create_list(:unit, 3, organization: organization)
selected_unit = organization_units.first
item = create(:item, organization: organization)
create(:item_unit, item: item, name: selected_unit.name)

get edit_item_path(item)

parsed_body = Nokogiri::HTML(response.body)
sean-dickinson marked this conversation as resolved.
Show resolved Hide resolved
checkboxes = parsed_body.css("input[type='checkbox'][name='item[request_unit_ids][]']")
expect(checkboxes.length).to eq organization_units.length
checkboxes.each do |checkbox|
if checkbox['value'] == selected_unit.id.to_s
expect(checkbox['checked']).to eq('checked')
else
expect(checkbox['checked']).to be_nil
end
end
end
end

describe 'DELETE #deactivate' do
let(:item) { create(:item, organization: organization, active: true) }
let(:storage_location) { create(:storage_location, organization: organization) }
Expand Down