-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #454 from sparc-request/jw_dashboard_org_filter
Jw dashboard org filter
- Loading branch information
Showing
6 changed files
with
202 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |