Skip to content

Commit bd2c3a2

Browse files
Add Unknown Local Authority fallback to reporting view
When a patient has no school LA and no postcode-derived LA, the view now returns 'UNKNOWN' for the code and 'Unknown Local Authority' for the display name instead of empty strings.
1 parent cf2adfa commit bd2c3a2

3 files changed

Lines changed: 51 additions & 5 deletions

File tree

db/schema.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[8.1].define(version: 2026_02_02_124516) do
13+
ActiveRecord::Schema[8.1].define(version: 2026_02_04_073325) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "pg_catalog.plpgsql"
1616
enable_extension "pg_trgm"
@@ -1356,8 +1356,8 @@
13561356
ELSE NULL::text
13571357
END AS patient_gender,
13581358
((pps.academic_year - pat.birth_academic_year) - 5) AS patient_year_group,
1359-
COALESCE(la.mhclg_code, pat.local_authority_mhclg_code, ''::character varying) AS patient_local_authority_code,
1360-
COALESCE(la.official_name, pat_la.official_name, ''::character varying) AS patient_local_authority_official_name,
1359+
COALESCE(la.mhclg_code, pat.local_authority_mhclg_code, 'UNKNOWN'::character varying) AS patient_local_authority_code,
1360+
COALESCE(la.official_name, pat_la.official_name, 'Unknown Local Authority'::character varying) AS patient_local_authority_official_name,
13611361
COALESCE(la.mhclg_code, ''::character varying) AS patient_school_local_authority_code,
13621362
CASE
13631363
WHEN (school.urn IS NOT NULL) THEN school.urn

db/views/reporting_api_totals_v07.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ SELECT
2626
pps.academic_year
2727
- pat.birth_academic_year - 5 AS patient_year_group, -- Filter: ?year_group=8,9
2828
COALESCE(la.mhclg_code,
29-
pat.local_authority_mhclg_code, '') AS patient_local_authority_code,
29+
pat.local_authority_mhclg_code,
30+
'UNKNOWN') AS patient_local_authority_code,
3031
COALESCE(la.official_name,
31-
pat_la.official_name, '') AS patient_local_authority_official_name,
32+
pat_la.official_name,
33+
'Unknown Local Authority') AS patient_local_authority_official_name,
3234
COALESCE(la.mhclg_code, '') AS patient_school_local_authority_code,
3335

3436
-- School info (for CSV grouping by school)

spec/controllers/api/reporting/totals_controller_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,50 @@
339339
"Test Authority 202"
340340
)
341341
end
342+
343+
it "returns Unknown Local Authority for patients without school or postcode LA" do
344+
team = Team.last
345+
programme = Programme.hpv
346+
team.programmes << programme
347+
session = create(:session, team:, programmes: [programme])
348+
349+
# Patient with school that has LA
350+
school =
351+
create(:school, name: "Known School", gias_local_authority_code: 201)
352+
create(:patient, session:, school:)
353+
354+
# Home-educated patient without postcode LA lookup
355+
create(
356+
:patient,
357+
session:,
358+
school: nil,
359+
home_educated: true,
360+
local_authority_mhclg_code: nil
361+
)
362+
363+
# Unknown school patient without postcode LA lookup
364+
create(
365+
:patient,
366+
session:,
367+
school: nil,
368+
home_educated: false,
369+
local_authority_mhclg_code: nil
370+
)
371+
372+
refresh_reporting_views!
373+
374+
request.headers["Accept"] = "text/csv"
375+
get :index, params: { group: "local_authority" }, format: :csv
376+
377+
expect(response).to have_http_status(:ok)
378+
379+
csv = CSV.parse(response.body, headers: true)
380+
local_authorities = csv.map { it["Local Authority"] }
381+
expect(local_authorities).to contain_exactly(
382+
"Test Authority 201",
383+
"Unknown Local Authority"
384+
)
385+
end
342386
end
343387

344388
describe "Dashboard acceptance criteria" do

0 commit comments

Comments
 (0)