Browse Source

Merge 69846a0494 into ac17087728

pull/3124/merge
Frank Ma 3 weeks ago committed by GitHub
parent
commit
d420b41469
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 18
      app/helpers/filters_helper.rb
  2. 23
      app/models/user.rb
  3. 8
      app/services/filter_manager.rb
  4. 30
      app/views/users/_user_filters.html.erb
  5. 47
      spec/services/filter_manager_spec.rb

18
app/helpers/filters_helper.rb

@ -52,6 +52,22 @@ module FiltersHelper
}.freeze }.freeze
end end
def user_role_type_filters(include_support: false)
roles = {
"1" => "Data provider",
"2" => "Coordinator",
}
roles["99"] = "Support" if include_support
roles.freeze
end
def user_additional_responsibilities_filters
{
"data_protection_officer" => "Data protection officer",
"key_contact" => "Key contact",
}.freeze
end
def scheme_status_filters def scheme_status_filters
{ {
"incomplete" => "Incomplete", "incomplete" => "Incomplete",
@ -306,7 +322,7 @@ private
def filters_count(filters) def filters_count(filters)
filters.each.sum do |category, category_filters| filters.each.sum do |category, category_filters|
if %w[years status needstypes bulk_upload_id].include?(category) if %w[years status needstypes bulk_upload_id role additional_responsibilities].include?(category)
category_filters.count(&:present?) category_filters.count(&:present?)
elsif %w[user owning_organisation managing_organisation user_text_search owning_organisation_text_search managing_organisation_text_search uploading_organisation].include?(category) elsif %w[user owning_organisation managing_organisation user_text_search owning_organisation_text_search managing_organisation_text_search uploading_organisation].include?(category)
1 1

23
app/models/user.rb

@ -81,6 +81,29 @@ class User < ApplicationRecord
filtered_records filtered_records
} }
scope :filter_by_role, ->(role, _user = nil) { where(role:) }
scope :filter_by_additional_responsibilities, lambda { |additional_responsibilities, _user|
filtered_records = all
scopes = []
additional_responsibilities.each do |responsibility|
case responsibility
when "key_contact"
scopes << is_key_contact
when "data_protection_officer"
scopes << is_data_protection_officer
end
end
if scopes.any?
filtered_records = filtered_records.merge(scopes.reduce(&:or))
end
filtered_records
}
scope :is_key_contact, -> { where(is_key_contact: true) }
scope :is_data_protection_officer, -> { where(is_dpo: true) }
scope :not_signed_in, -> { where(last_sign_in_at: nil, active: true) } scope :not_signed_in, -> { where(last_sign_in_at: nil, active: true) }
scope :deactivated, -> { where(active: false) } scope :deactivated, -> { where(active: false) }
scope :activated, -> { where(active: true) } scope :activated, -> { where(active: true) }

8
app/services/filter_manager.rb

@ -130,6 +130,14 @@ class FilterManager
new_filters["status"] = params["status"] new_filters["status"] = params["status"]
end end
if filter_type.include?("users") && params["role"].present?
new_filters["role"] = params["role"]
end
if filter_type.include?("users") && params["additional_responsibilities"].present?
new_filters["additional_responsibilities"] = params["additional_responsibilities"]
end
if filter_type.include?("schemes") if filter_type.include?("schemes")
current_user.scheme_filters(specific_org:).each do |filter| current_user.scheme_filters(specific_org:).each do |filter|
new_filters[filter] = params[filter] if params[filter].present? new_filters[filter] = params[filter] if params[filter].present?

30
app/views/users/_user_filters.html.erb

@ -17,12 +17,30 @@
<%= render partial: "filters/checkbox_filter", <%= render partial: "filters/checkbox_filter",
locals: { locals: {
f:, f:,
options: user_status_filters, options: user_status_filters,
label: "Status", label: "Status",
category: "status", category: "status",
size: "s", size: "s",
} %> } %>
<%= render partial: "filters/checkbox_filter",
locals: {
f:,
options: user_role_type_filters(include_support: current_user.support?),
label: "Role type",
category: "role",
size: "s",
} %>
<%= render partial: "filters/checkbox_filter",
locals: {
f:,
options: user_additional_responsibilities_filters,
label: "Additional responsibilities",
category: "additional_responsibilities",
size: "s",
} %>
<% if request.params["search"].present? %> <% if request.params["search"].present? %>
<%= f.hidden_field :search, value: request.params["search"] %> <%= f.hidden_field :search, value: request.params["search"] %>

47
spec/services/filter_manager_spec.rb

@ -94,4 +94,51 @@ describe FilterManager do
expect(described_class.filter_schemes(Scheme.all, nil, {}, nil, nil)).to eq(alphabetical_order_schemes) expect(described_class.filter_schemes(Scheme.all, nil, {}, nil, nil)).to eq(alphabetical_order_schemes)
end end
end end
describe "filter_users" do
let(:data_provider_user) { FactoryBot.create(:user, role: "data_provider") }
let(:data_coordinator_user) { FactoryBot.create(:user, role: "data_coordinator") }
let(:support_user) { FactoryBot.create(:user, role: "support") }
let(:key_contact_user) { FactoryBot.create(:user, is_key_contact: true) }
let(:dpo_user) { FactoryBot.create(:user, is_dpo: true) }
let(:key_contact_dpo_user) { FactoryBot.create(:user, is_key_contact: true, is_dpo: true) }
context "when filtering by role" do
it "returns users with the role" do
filter = { "role" => %w[data_provider] }
result = described_class.filter_users(User.all, nil, filter, nil)
expect(result).to include(data_provider_user)
expect(result).not_to include(data_coordinator_user)
expect(result).not_to include(support_user)
end
it "returns users with multiple roles selected" do
filter = { "role" => %w[data_provider data_coordinator] }
result = described_class.filter_users(User.all, nil, filter, nil)
expect(result).to include(data_provider_user)
expect(result).to include(data_coordinator_user)
expect(result).not_to include(support_user)
end
end
context "when filtering by additional responsibilities" do
it "returns users with the additional responsibilities" do
filter = { "additional_responsibilities" => %w[data_protection_officer] }
result = described_class.filter_users(User.all, nil, filter, nil)
expect(result).to include(dpo_user)
expect(result).to include(key_contact_dpo_user)
expect(result).not_to include(key_contact_user)
expect(result).not_to include(support_user)
end
it "returns users with multiple additional responsibilities selected" do
filter = { "additional_responsibilities" => %w[data_protection_officer key_contact] }
result = described_class.filter_users(User.all, nil, filter, nil)
expect(result).to include(dpo_user)
expect(result).to include(key_contact_dpo_user)
expect(result).to include(key_contact_user)
expect(result).not_to include(support_user)
end
end
end
end end

Loading…
Cancel
Save