Skip to content

Commit

Permalink
Configure bank organization units (#4432)
Browse files Browse the repository at this point in the history
* WIP Added Custom request units at ogranizational level

* Add unique validation to custom organization unit name

* Added flipper conditionals and specs

* Fixed the button name and delete issue

* Updated to allow organizations to delete units

* Clean out some lint!

* Use select2 in tags-mode for Bank unit configuration

* Modify select2 to allow multiple instances on the same page

* Update spec to match request units param

* Hide the select2 drop-down for units [#4396]

* Use unit-names instead of id's when configuring bank [#4396]

* Update some org-unit config specs [#4396]

* Remove outdated comment [#4396]

* Use a dup of params and don't mutate the original [#4396]

* Remove implicit rails_helper require [#4396]

* Explicitly check for packs-disabled case [#4396]

* Move most organization system specs to request spec [#4396]

* Clean out some lint [#4396]

* Switch two more unit-name validation consts [#4396]

---------

Co-authored-by: Liz Catoe <liz.catoe@priviahealth.com>
Co-authored-by: srinikotla <38926870+srinikotla@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 16, 2024
1 parent 6e72b95 commit d4c64a0
Show file tree
Hide file tree
Showing 11 changed files with 339 additions and 161 deletions.
3 changes: 2 additions & 1 deletion app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ def organization_params
:ytd_on_distribution_printout, :one_step_partner_invite,
:hide_value_columns_on_receipt, :hide_package_column_on_receipt,
:signature_for_distribution_pdf,
partner_form_fields: []
partner_form_fields: [],
request_unit_names: []
)
end

Expand Down
22 changes: 17 additions & 5 deletions app/javascript/controllers/select2_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,29 @@ import "select2"
export default class extends Controller {
static values = {
config: { type: Object, default: {} },
}
hideDropdown: { type: Boolean, default: false }
};

connect() {
$(this.element).select2(this.configValue);
const select2 = $(this.element).select2(this.configValue);

if (this.hideDropdownValue) {
select2.on('select2:open', function (e) {
$('.select2-container--open .select2-dropdown--below').css('display','none');
});
}

/**
* This is a workaround to auto focus on the select2 input when it is opened.
*/
$(this.element).on('select2:open', function (e) {
$(".select2-search__field")[0].focus();
})
let select2Instance = $(e.target).data('select2');
if (select2Instance) {
let searchField = select2Instance.dropdown.$search || select2Instance.selection.$search;
if (searchField) {
searchField.focus();
}
}
});
}

}
2 changes: 1 addition & 1 deletion app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def flipper_id

has_one_attached :logo

accepts_nested_attributes_for :users, :account_request
accepts_nested_attributes_for :users, :account_request, :request_units

include Geocodable

Expand Down
1 change: 1 addition & 0 deletions app/models/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
#
class Unit < ApplicationRecord
belongs_to :organization
validates :name, uniqueness: {scope: :organization}
end
19 changes: 16 additions & 3 deletions app/services/organization_update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,23 @@ class << self
def update(organization, params)
return false unless valid?(organization, params)

if params.has_key?("partner_form_fields")
params["partner_form_fields"].delete_if { |field| field == "" }
org_params = params.dup

if org_params.has_key?("partner_form_fields")
org_params["partner_form_fields"] = org_params["partner_form_fields"].reject(&:blank?)
end

if Flipper.enabled?(:enable_packs) && org_params[:request_unit_names]
# Find or create units for the organization
request_unit_ids = org_params[:request_unit_names].reject(&:blank?).map do |request_unit_name|
Unit.find_or_create_by(organization: organization, name: request_unit_name).id
end
org_params.delete(:request_unit_names)
org_params[:request_unit_ids] = request_unit_ids
end
result = organization.update(params)

result = organization.update(org_params)

return false unless result
update_partner_flags(organization)
true
Expand Down
18 changes: 18 additions & 0 deletions app/views/organizations/_details.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@
<%= humanize_boolean(@organization.distribute_monthly) %>
</p>
</div>
<% if Flipper.enabled?(:enable_packs) %>
<div class="mb-4">
<h3 class='font-bold'>Custom Request Units</h3>
<p>
<% if @organization.request_units.length > 0 %>
<% @organization.request_units.map do |unit| %>
<%= fa_icon "angle-right" %>
<span>
<%= unit.name.titlecase %>
</span> <br>
<% end %>
<% else %>
<%= fa_icon "angle-right" %>
<span> None </span>
<% end %>
</p>
</div>
<% end %>
<div class="mb-4">
<h3 class='font-bold'>Child Based Requests?</h3>
<p>
Expand Down
25 changes: 24 additions & 1 deletion app/views/organizations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
</div><!-- /.container-fluid -->
</section>

<%= simple_form_for current_organization, url: {controller: "organizations", action: "update"} do |f| %>
<%= simple_form_for current_organization,
data: { controller: "form-input" },
url: {controller: "organizations", action: "update"} do |f| %>
<section class="content">
<div class="container-fluid">
<div class="row">
Expand Down Expand Up @@ -100,6 +102,27 @@
<%= f.input :repackage_essentials, label: 'Does your bank repackage essentials?', as: :radio_buttons, collection: [[true, 'Yes'], [false, 'No']], label_method: :second, value_method: :first %>
<%= f.input :distribute_monthly, label: 'Does your bank distribute monthly?', as: :radio_buttons, collection: [[true, 'Yes'], [false, 'No']], label_method: :second, value_method: :first %>
<% if Flipper.enabled?(:enable_packs) %>
<%= label_tag "organization[request_unit_names]", 'Custom request units used (please use singular form -- e.g. pack, not packs)' %>
<%= select_tag(
"organization[request_unit_names]",
options_from_collection_for_select(
current_organization.request_units,
'name',
'name',
->(_) { true } # Select all of the current request units
),
{
multiple: true,
class: 'form-control custom-select',
'data-controller': 'select2',
'data-select2-hide-dropdown-value': true,
'data-select2-config-value': '{"selectOnClose": "true", "tags": "true", "tokenSeparators": [",", "\t"]}'
}
) %>
<% end %>
<%= f.input :enable_child_based_requests, label: 'Enable partners to make child-based requests?', as: :radio_buttons, collection: [[true, 'Yes'], [false, 'No']], label_method: :second, value_method: :first %>
<%= f.input :enable_individual_requests, label: 'Enable partners to make requests for individuals?', as: :radio_buttons, collection: [[true, 'Yes'], [false, 'No']], label_method: :second, value_method: :first %>
<%= f.input :enable_quantity_based_requests, label: 'Enable partners to make quantity-based requests?', as: :radio_buttons, collection: [[true, 'Yes'], [false, 'No']], label_method: :second, value_method: :first %>
Expand Down
25 changes: 25 additions & 0 deletions spec/models/unit_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# == Schema Information
#
# Table name: units
#
# id :bigint not null, primary key
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# organization_id :bigint
#

RSpec.describe Unit, type: :model do
let!(:organization) { create(:organization) }
let!(:unit_1) { create(:unit, name: "WolfPack", organization: organization) }

describe "Validations" do
it "validates uniqueness of name in context of organization" do
expect { described_class.create!(name: "WolfPack", organization: organization) }.to raise_exception(ActiveRecord::RecordInvalid).with_message("Validation failed: Name has already been taken")
end
end

describe "Associations" do
it { should belong_to(:organization) }
end
end
Loading

0 comments on commit d4c64a0

Please sign in to comment.