Skip to content

Commit 01dad10

Browse files
Sc 14101 (#5524)
**Story card:** [sc-14101](https://app.shortcut.com/simpledotorg/story/14101/add-national-dashboard-link-in-the-top-navigation-bar-in-the-dashboard-ethiopia-bangladesh-sri-lanka) ## Because Navigation is not working for navigational breadcrumb from srilanka. ## This addresses Fix for the navigational breadcrumb ## Test instructions Enter detailed instructions for how to test this PR...
1 parent 283a684 commit 01dad10

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

app/controllers/reports/regions_controller.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,8 @@ def find_region
430430
@region ||= authorize {
431431
case report_scope
432432
when "organization"
433-
organization = current_admin.user_access.accessible_organizations(:view_reports).find_by!(slug: report_params[:id])
434-
organization.region
433+
organization = current_admin.user_access.accessible_organizations(:view_reports).find_by(slug: report_params[:id]) || find_organization_slug(report_params[:id])
434+
organization&.region || (raise ActiveRecord::RecordNotFound, "Organization not found for slug: #{report_params[:id]}")
435435
when "state"
436436
current_admin.user_access.accessible_state_regions(:view_reports).find_by!(slug: report_params[:id])
437437
when "district"
@@ -446,6 +446,13 @@ def find_region
446446
}
447447
end
448448

449+
def find_organization_slug(slug)
450+
region = Region.find_by(slug: slug, region_type: "organization")
451+
return unless region&.source_type == "Organization"
452+
453+
current_admin.user_access.accessible_organizations(:view_reports).find_by(id: region.source_id)
454+
end
455+
449456
def report_params
450457
params.permit(:id, :bust_cache, :v2, :report_scope, {period: [:type, :value]})
451458
end

spec/controllers/reports/regions_controller_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,29 @@
1818
@facility_region = @facility.region
1919
end
2020

21+
context "when report_scope is organization" do
22+
let(:organization) { create(:organization, slug: "valid-org-slug") }
23+
it "finds the organization by its slug when accessible" do
24+
sign_in(cvho.email_authentication)
25+
get :show, params: {id: organization.slug, report_scope: "organization"}
26+
expect(assigns(:region)).to eq(organization.region)
27+
end
28+
29+
it "finds the organization using its region slug as a fallback" do
30+
sign_in(cvho.email_authentication)
31+
organization.region.update(slug: "valid-org-slug-organization")
32+
get :show, params: {id: organization.region.slug, report_scope: "organization"}
33+
expect(assigns(:region).slug).to eq(organization.region.slug)
34+
end
35+
36+
it "redirects with an alert if the region slug does not match any accessible organization" do
37+
sign_in(cvho.email_authentication)
38+
get :show, params: {id: "unknown-region-slug", report_scope: "organization"}
39+
expect(flash[:alert]).to eq("You are not authorized to perform this action.")
40+
expect(response).to be_redirect
41+
end
42+
end
43+
2144
it "redirects if matching region slug not found" do
2245
sign_in(cvho.email_authentication)
2346
get :show, params: {id: "String-unknown", report_scope: "bad-report_scope"}

0 commit comments

Comments
 (0)