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

Jw dashboard org filter #454

Merged
merged 6 commits into from
Jun 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/controllers/dashboard/protocols_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,27 @@ class Dashboard::ProtocolsController < Dashboard::BaseController
before_filter :find_service_provider_only_admin_organizations, only: [:show, :display_requests]

def index

admin_orgs = @user.authorized_admin_organizations
@admin = !admin_orgs.empty?

default_filter_params = { show_archived: 0 }

# if we are an admin we want to default to admin organizations
if @admin
default_filter_params[:filtered_for_admin] = @user.id.to_s
@organizations = IdentityOrganizations.new(@user.id).admin_organizations_with_protocols
default_filter_params[:for_admin] = @user.id.to_s
else
default_filter_params[:for_identity_id] = @user.id.to_s
@organizations = IdentityOrganizations.new(@user.id).general_user_organizations_with_protocols
default_filter_params[:for_identity_id] = @user.id.to_s
end

@filterrific =
initialize_filterrific(Protocol, params[:filterrific],
default_filter_params: default_filter_params,
select_options: {
with_status: AVAILABLE_STATUSES.invert,
with_organization: admin_orgs.map { |org| [org.name, org.id] }
with_organization: GroupedOrganizations.new(@organizations).collect_grouped_options
},
persistence_id: false #resets filters on page reload
) || return
Expand Down
20 changes: 20 additions & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class Organization < ActiveRecord::Base
has_many :identities, :through => :catalog_managers
has_many :services, :dependent => :destroy
has_many :sub_service_requests, :dependent => :destroy
has_many :protocols, through: :sub_service_requests
has_many :available_statuses, :dependent => :destroy
has_many :org_children, class_name: "Organization", foreign_key: :parent_id

attr_accessible :name
attr_accessible :order
Expand Down Expand Up @@ -154,6 +156,24 @@ def children orgs
children
end

def all_child_organizations
[
org_children,
org_children.map(&:all_child_organizations)
].flatten
end

def child_orgs_with_protocols
organizations = all_child_organizations
organizations_with_protocols = []
organizations.flatten.uniq.each do |organization|
if organization.protocols.any?
organizations_with_protocols << organization
end
end
organizations_with_protocols.flatten.uniq
end

# Returns an array of all children (and children of children) of this organization (deep search).
# Optionally includes self
def all_children (all_children=[], include_self=true, orgs)
Expand Down
2 changes: 1 addition & 1 deletion app/models/sub_service_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class SubServiceRequest < ActiveRecord::Base
has_many :subsidies
has_one :approved_subsidy, :dependent => :destroy
has_one :pending_subsidy, :dependent => :destroy
has_one :protocol, through: :service_request

delegate :protocol, to: :service_request, allow_nil: true
delegate :percent_subsidy, to: :approved_subsidy, allow_nil: true
delegate :approved_percent_of_total, to: :approved_subsidy, allow_nil: true
alias_attribute :approved_percent_subsidy, :approved_percent_of_total
Expand Down
20 changes: 20 additions & 0 deletions lib/dashboard/grouped_organizations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class GroupedOrganizations
def initialize(organizations)
@organizations = organizations
end

def collect_grouped_options
groups = @organizations.group_by(&:type)
options = ["Institution", "Provider", "Program", "Core"].map do |type|
next unless groups[type].present?
[type.pluralize, extract_name_and_id(groups[type])]
end
options.compact
end

private

def extract_name_and_id(orgs)
orgs.map { |org| [org.name, org.id] }
end
end
15 changes: 15 additions & 0 deletions lib/dashboard/identity_organizations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class IdentityOrganizations
def initialize(id)
@id = id
end

# returns organizations that have protocols and general user has access to
def general_user_organizations_with_protocols
Protocol.joins(:project_roles).where(project_roles: { identity_id: @id } ).where.not(project_roles: { project_rights: 'none' }).map(&:organizations).flatten.uniq
end

# returns organizations that have a service provider and super user access AND have protocols.
def admin_organizations_with_protocols
Organization.authorized_for_identity(@id).joins(:sub_service_requests)
end
end
140 changes: 140 additions & 0 deletions spec/lib/dashboard/identity_organizations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
require 'rails_helper'

RSpec.describe IdentityOrganizations do

describe '#admin_organizations_with_protocols' do
context "Identity is super user at provider level" do
context "Identity is service_provider at institution level" do
it "should return organizations for super_users and service_providers that have protocols" do
institution_organization = create(:institution_without_validations)
provider_organization = create(:provider_without_validations, parent_id: institution_organization.id)
program_organization = create(:program_without_validations, parent_id: provider_organization.id)

provider_protocol = create(:protocol_without_validations)
provider_sr = create(:service_request_without_validations, protocol_id: provider_protocol.id)
create(:sub_service_request_without_validations, service_request_id: provider_sr.id, organization_id: provider_organization.id)

program_protocol = create(:protocol_without_validations)
program_sr = create(:service_request_without_validations, protocol_id: program_protocol.id)
create(:sub_service_request_without_validations, service_request_id: program_sr.id, organization_id: program_organization.id)

identity = create(:identity)
create(:service_provider, identity: identity, organization: institution_organization)
create(:super_user, identity: identity, organization: provider_organization)

orgs_with_protocols = []
orgs_with_protocols << provider_organization.id # There is a protocol attached at the provider level
orgs_with_protocols << program_organization.id # There is a protocol attached at the program level

# Should return an array with the provider and program org, not the institution org since it does not have a protocol attached
expect(IdentityOrganizations.new(identity.id).admin_organizations_with_protocols.map(&:id)).to eq(orgs_with_protocols.flatten.sort)
end
end
end
context "Identity is not a super user or service provider" do

it "should not return any organizations" do
identity = create(:identity)
institution_organization = create(:institution_without_validations)
provider_organization = create(:provider_without_validations, parent_id: institution_organization.id)
program_organization = create(:program_without_validations, parent_id: provider_organization.id)

provider_protocol = create(:protocol_without_validations, primary_pi: identity)
provider_sr = create(:service_request_without_validations, protocol_id: provider_protocol.id)
create(:sub_service_request_without_validations, service_request_id: provider_sr.id, organization_id: provider_organization.id)

program_protocol = create(:protocol_without_validations, primary_pi: identity)
program_sr = create(:service_request_without_validations, protocol_id: program_protocol.id)
create(:sub_service_request_without_validations, service_request_id: program_sr.id, organization_id: program_organization.id)


# This Identity does not have super user or service provider status so we should expect an empty array
expect(IdentityOrganizations.new(identity.id).admin_organizations_with_protocols.map(&:id)).to eq([])
end
end
end

describe '#general_user_organizations_with_protocols' do
context "Identity is not a super user or service provider" do
context "Identity is service_provider at institution level" do
it "should not return any organizations" do
institution_organization = create(:institution_without_validations)
provider_organization = create(:provider_without_validations, parent_id: institution_organization.id)
program_organization = create(:program_without_validations, parent_id: provider_organization.id)

provider_protocol = create(:protocol_without_validations)
provider_sr = create(:service_request_without_validations, protocol_id: provider_protocol.id)
create(:sub_service_request_without_validations, service_request_id: provider_sr.id, organization_id: provider_organization.id)

program_protocol = create(:protocol_without_validations)
program_sr = create(:service_request_without_validations, protocol_id: program_protocol.id)
create(:sub_service_request_without_validations, service_request_id: program_sr.id, organization_id: program_organization.id)

identity = create(:identity)
create(:service_provider, identity: identity, organization: institution_organization)
create(:super_user, identity: identity, organization: provider_organization)

orgs_with_protocols = []
orgs_with_protocols << provider_organization.id # There is a protocol attached at the provider level
orgs_with_protocols << program_organization.id # There is a protocol attached at the program level

# Identity is not a general user, expect an empty array
expect(IdentityOrganizations.new(identity.id).general_user_organizations_with_protocols.map(&:id)).to eq([])
end
end
end
context "Identity is a general user" do
it "should return organizations that general user has access to through protocols" do
identity = create(:identity)

institution_organization = create(:institution_without_validations)
provider_organization = create(:provider_without_validations, parent_id: institution_organization.id)
program_organization = create(:program_without_validations, parent_id: provider_organization.id)

provider_protocol = create(:protocol_without_validations, primary_pi: identity)
provider_sr = create(:service_request_without_validations, protocol_id: provider_protocol.id)
create(:sub_service_request_without_validations, service_request_id: provider_sr.id, organization_id: provider_organization.id)

program_protocol = create(:protocol_without_validations, primary_pi: identity)
program_sr = create(:service_request_without_validations, protocol_id: program_protocol.id)
create(:sub_service_request_without_validations, service_request_id: program_sr.id, organization_id: program_organization.id)

orgs_with_protocols = []
orgs_with_protocols << provider_organization.id
orgs_with_protocols << program_organization.id

# Identity is a general user and has access to both protocols
expect(IdentityOrganizations.new(identity.id).general_user_organizations_with_protocols.map(&:id)).to eq(orgs_with_protocols.flatten.sort)
end
end
context "Identity is a general user" do
it "should return organizations that general user has access to through protocols" do
identity = create(:identity)

institution_organization = create(:institution_without_validations)
provider_organization = create(:provider_without_validations, parent_id: institution_organization.id)
program_organization = create(:program_without_validations, parent_id: provider_organization.id)
core_organization = create(:core_without_validations, parent_id: program_organization.id)

provider_protocol = create(:protocol_without_validations, primary_pi: identity)
provider_sr = create(:service_request_without_validations, protocol_id: provider_protocol.id)
create(:sub_service_request_without_validations, service_request_id: provider_sr.id, organization_id: provider_organization.id)

program_protocol = create(:protocol_without_validations, primary_pi: identity)
program_sr = create(:service_request_without_validations, protocol_id: program_protocol.id)
create(:sub_service_request_without_validations, service_request_id: program_sr.id, organization_id: program_organization.id)

unattached_protocol = create(:protocol_without_validations)
unattached_provider_sr = create(:service_request_without_validations, protocol_id: unattached_protocol.id)
create(:sub_service_request_without_validations, service_request_id: unattached_provider_sr.id, organization_id: core_organization.id)

orgs_with_protocols = []
orgs_with_protocols << provider_organization.id
orgs_with_protocols << program_organization.id

# Identity is a general user and has access to both protocols
expect(IdentityOrganizations.new(identity.id).general_user_organizations_with_protocols.map(&:id)).to eq(orgs_with_protocols.flatten.sort)
end
end
end
end