Skip to content

Commit 6d400d0

Browse files
Merge pull request #6476 from NHSDigital/alistair/imms-api-save-reason-code
Save reason code and term from API records
2 parents c8df320 + b8f5d85 commit 6d400d0

7 files changed

Lines changed: 109 additions & 19 deletions

File tree

app/lib/fhir_mapper/vaccination_record.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ def self.from_fhir_record(fhir_record, patient:)
150150
notes << dose_number_string_note_from_fhir(fhir_record)
151151
end
152152

153+
reason_coding = reason_coding_from_fhir(fhir_record)
154+
attrs[:nhs_immunisations_api_snomed_reason_code] = reason_coding&.code
155+
attrs[:nhs_immunisations_api_snomed_reason_term] = reason_coding&.display
156+
153157
attrs[:vaccine] = Vaccine.from_fhir_record(fhir_record)
154158
attrs[:batch_number] = fhir_record.lotNumber&.to_s
155159

@@ -362,6 +366,12 @@ def fhir_org_performer
362366
&.find { it.system == "http://snomed.info/sct" }
363367
end
364368

369+
private_class_method def self.reason_coding_from_fhir(fhir_record)
370+
fhir_record.reasonCode&.first&.coding&.find do
371+
it.system == "http://snomed.info/sct"
372+
end
373+
end
374+
365375
def fhir_reason_code
366376
FHIR::CodeableConcept.new(
367377
coding: [

app/models/vaccination_record.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
# nhs_immunisations_api_primary_source :boolean
2323
# nhs_immunisations_api_snomed_procedure_code :string
2424
# nhs_immunisations_api_snomed_procedure_term :string
25+
# nhs_immunisations_api_snomed_reason_code :string
26+
# nhs_immunisations_api_snomed_reason_term :string
2527
# nhs_immunisations_api_sync_pending_at :datetime
2628
# nhs_immunisations_api_synced_at :datetime
2729
# notes :text
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
class AddReasonCodeToVaccinationRecords < ActiveRecord::Migration[8.1]
4+
def change
5+
change_table :vaccination_records, bulk: true do |t|
6+
t.string :nhs_immunisations_api_snomed_reason_code
7+
t.string :nhs_immunisations_api_snomed_reason_term
8+
end
9+
end
10+
end

db/schema.rb

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

13-
14-
ActiveRecord::Schema[8.1].define(version: 2026_03_30_130232) do
13+
ActiveRecord::Schema[8.1].define(version: 2026_03_31_100000) do
1514
# These are extensions that must be enabled in order to support this database
1615
enable_extension "pg_catalog.plpgsql"
1716
enable_extension "pg_trgm"
@@ -1007,6 +1006,8 @@
10071006
t.boolean "nhs_immunisations_api_primary_source"
10081007
t.string "nhs_immunisations_api_snomed_procedure_code"
10091008
t.string "nhs_immunisations_api_snomed_procedure_term"
1009+
t.string "nhs_immunisations_api_snomed_reason_code"
1010+
t.string "nhs_immunisations_api_snomed_reason_term"
10101011
t.datetime "nhs_immunisations_api_sync_pending_at"
10111012
t.datetime "nhs_immunisations_api_synced_at"
10121013
t.text "notes"

spec/factories/vaccination_records.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
# nhs_immunisations_api_primary_source :boolean
2323
# nhs_immunisations_api_snomed_procedure_code :string
2424
# nhs_immunisations_api_snomed_procedure_term :string
25+
# nhs_immunisations_api_snomed_reason_code :string
26+
# nhs_immunisations_api_snomed_reason_term :string
2527
# nhs_immunisations_api_sync_pending_at :datetime
2628
# nhs_immunisations_api_synced_at :datetime
2729
# notes :text

spec/lib/fhir_mapper/vaccination_record_spec.rb

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,12 @@
712712
its(:nhs_immunisations_api_snomed_procedure_term) do
713713
should eq "Seasonal influenza vaccination 111 (procedure)"
714714
end
715+
716+
its(:nhs_immunisations_api_snomed_reason_code) { should eq "453684005" }
717+
718+
its(:nhs_immunisations_api_snomed_reason_term) do
719+
should eq "Disease outbreak (event)"
720+
end
715721
end
716722

717723
context "with a record with not full dose" do
@@ -752,6 +758,12 @@
752758
its(:performed_ods_code) { should eq "B0C4P" }
753759
its(:nhs_immunisations_api_primary_source) { should be true }
754760

761+
its(:nhs_immunisations_api_snomed_reason_code) { should eq "453684005" }
762+
763+
its(:nhs_immunisations_api_snomed_reason_term) do
764+
should eq "Disease outbreak (event)"
765+
end
766+
755767
its(:notes) do
756768
should eq "Performing organisation display name: Acme Healthcare"
757769
end
@@ -796,6 +808,12 @@
796808
its(:location_name) { should be_nil }
797809
its(:performed_ods_code) { should eq "B0C4P" }
798810

811+
its(:nhs_immunisations_api_snomed_reason_code) { should eq "453684005" }
812+
813+
its(:nhs_immunisations_api_snomed_reason_term) do
814+
should eq "Disease outbreak (event)"
815+
end
816+
799817
its(:notes) do
800818
should eq "Performing organisation display name: Acme Healthcare"
801819
end
@@ -849,6 +867,9 @@
849867
should eq "Influenza vaccination given by other healthcare provider (situation)"
850868
end
851869

870+
its(:nhs_immunisations_api_snomed_reason_code) { should be_nil }
871+
its(:nhs_immunisations_api_snomed_reason_term) { should be_nil }
872+
852873
its(:notes) { should be_nil }
853874
end
854875

@@ -894,6 +915,12 @@
894915
should eq "Administration of first intranasal seasonal influenza vaccination"
895916
end
896917

918+
its(:nhs_immunisations_api_snomed_reason_code) { should eq "453684005" }
919+
920+
its(:nhs_immunisations_api_snomed_reason_term) do
921+
should eq "Disease outbreak (event)"
922+
end
923+
897924
its(:notes) do
898925
should include(
899926
"SNOMED product code: 43207411000001106",
@@ -932,19 +959,22 @@
932959
its(:performed_ods_code) { should eq "B12345" }
933960
its(:nhs_immunisations_api_primary_source) { should be false }
934961

935-
its(:notes) do
936-
should include(
937-
"Reported dose number string: Dose sequence not recorded"
938-
)
939-
end
940-
941962
its(:nhs_immunisations_api_snomed_procedure_code) do
942963
should eq "955651000000100"
943964
end
944965

945966
its(:nhs_immunisations_api_snomed_procedure_term) do
946967
should eq "Influenza vaccination given by other healthcare provider (situation)"
947968
end
969+
970+
its(:nhs_immunisations_api_snomed_reason_code) { should be_nil }
971+
its(:nhs_immunisations_api_snomed_reason_term) { should be_nil }
972+
973+
its(:notes) do
974+
should include(
975+
"Reported dose number string: Dose sequence not recorded"
976+
)
977+
end
948978
end
949979

950980
context "with a record that is nasal flu, and is missing dose quantity (from real GP)" do
@@ -982,6 +1012,9 @@
9821012
should eq "Administration of first intranasal seasonal influenza vaccination"
9831013
end
9841014

1015+
its(:nhs_immunisations_api_snomed_reason_code) { should be_nil }
1016+
its(:nhs_immunisations_api_snomed_reason_term) { should be_nil }
1017+
9851018
its(:notes) { should be_nil }
9861019
end
9871020

@@ -1018,6 +1051,9 @@
10181051
should eq "Administration of first intranasal seasonal influenza vaccination"
10191052
end
10201053

1054+
its(:nhs_immunisations_api_snomed_reason_code) { should be_nil }
1055+
its(:nhs_immunisations_api_snomed_reason_term) { should be_nil }
1056+
10211057
its(:notes) { should be_nil }
10221058
end
10231059

@@ -1058,6 +1094,9 @@
10581094
should eq "Administration of first inactivated seasonal influenza vaccination"
10591095
end
10601096

1097+
its(:nhs_immunisations_api_snomed_reason_code) { should be_nil }
1098+
its(:nhs_immunisations_api_snomed_reason_term) { should be_nil }
1099+
10611100
its(:notes) { should be_nil }
10621101
end
10631102

@@ -1103,6 +1142,9 @@
11031142
should eq "Administration of first intranasal seasonal influenza vaccination"
11041143
end
11051144

1145+
its(:nhs_immunisations_api_snomed_reason_code) { should eq "723620004" }
1146+
its(:nhs_immunisations_api_snomed_reason_term) { should be_nil }
1147+
11061148
its(:notes) { should be_nil }
11071149
end
11081150

@@ -1148,6 +1190,9 @@
11481190
should eq "Administration of first intranasal seasonal influenza vaccination"
11491191
end
11501192

1193+
its(:nhs_immunisations_api_snomed_reason_code) { should be_nil }
1194+
its(:nhs_immunisations_api_snomed_reason_term) { should be_nil }
1195+
11511196
its(:notes) { should be_nil }
11521197
end
11531198

@@ -1182,14 +1227,17 @@
11821227
its(:outcome) { should eq "administered" }
11831228
its(:performed_ods_code) { should eq "B12345" }
11841229

1230+
its(:nhs_immunisations_api_snomed_procedure_code) { should be_nil }
1231+
its(:nhs_immunisations_api_snomed_procedure_term) { should be_nil }
1232+
1233+
its(:nhs_immunisations_api_snomed_reason_code) { should be_nil }
1234+
its(:nhs_immunisations_api_snomed_reason_term) { should be_nil }
1235+
11851236
its(:notes) do
11861237
should include(
11871238
"Reported dose number string: Dose sequence not recorded"
11881239
)
11891240
end
1190-
1191-
its(:nhs_immunisations_api_snomed_procedure_code) { should be_nil }
1192-
its(:nhs_immunisations_api_snomed_procedure_term) { should be_nil }
11931241
end
11941242
end
11951243

@@ -1242,6 +1290,9 @@
12421290
should eq "Administration of vaccine product containing only Human papillomavirus antigen (procedure)"
12431291
end
12441292

1293+
its(:nhs_immunisations_api_snomed_reason_code) { should eq "723620004" }
1294+
its(:nhs_immunisations_api_snomed_reason_term) { should be_nil }
1295+
12451296
its(:notes) { should be_nil }
12461297
end
12471298
end
@@ -1287,8 +1338,6 @@
12871338
its(:performed_ods_code) { should eq "R1L" }
12881339
its(:nhs_immunisations_api_primary_source) { should be true }
12891340

1290-
its(:notes) { should include("Reported dose number string: Unknown") }
1291-
12921341
its(:nhs_immunisations_api_snomed_procedure_code) do
12931342
should eq "871874000"
12941343
end
@@ -1298,6 +1347,11 @@
12981347
"Neisseria meningitidis serogroup A, C, W135 and Y " \
12991348
"antigens (procedure)"
13001349
end
1350+
1351+
its(:nhs_immunisations_api_snomed_reason_code) { should eq "723620004" }
1352+
its(:nhs_immunisations_api_snomed_reason_term) { should be_nil }
1353+
1354+
its(:notes) { should include("Reported dose number string: Unknown") }
13011355
end
13021356
end
13031357

@@ -1342,8 +1396,6 @@
13421396
its(:performed_ods_code) { should eq "R1L" }
13431397
its(:nhs_immunisations_api_primary_source) { should be true }
13441398

1345-
its(:notes) { should include("Reported dose number string: Unknown") }
1346-
13471399
its(:nhs_immunisations_api_snomed_procedure_code) do
13481400
should eq "866186002"
13491401
end
@@ -1353,6 +1405,11 @@
13531405
"Clostridium tetani and Corynebacterium diphtheriae " \
13541406
"and Human poliovirus antigens (procedure)"
13551407
end
1408+
1409+
its(:nhs_immunisations_api_snomed_reason_code) { should eq "723620004" }
1410+
its(:nhs_immunisations_api_snomed_reason_term) { should be_nil }
1411+
1412+
its(:notes) { should include("Reported dose number string: Unknown") }
13561413
end
13571414
end
13581415

@@ -1401,8 +1458,6 @@
14011458
its(:performed_ods_code) { should eq "R1L" }
14021459
its(:nhs_immunisations_api_primary_source) { should be true }
14031460

1404-
its(:notes) { should include("Reported dose number string: Unknown") }
1405-
14061461
its(:nhs_immunisations_api_snomed_procedure_code) do
14071462
should eq "38598009"
14081463
end
@@ -1412,6 +1467,11 @@
14121467
"Measles morbillivirus and Mumps orthorubulavirus " \
14131468
"and Rubella virus antigens (procedure)"
14141469
end
1470+
1471+
its(:nhs_immunisations_api_snomed_reason_code) { should eq "723620004" }
1472+
its(:nhs_immunisations_api_snomed_reason_term) { should be_nil }
1473+
1474+
its(:notes) { should include("Reported dose number string: Unknown") }
14151475
end
14161476
end
14171477

@@ -1460,8 +1520,6 @@
14601520
its(:performed_ods_code) { should eq "R1L" }
14611521
its(:nhs_immunisations_api_primary_source) { should be true }
14621522

1463-
its(:notes) { should include("Reported dose number string: Unknown") }
1464-
14651523
its(:nhs_immunisations_api_snomed_procedure_code) do
14661524
should eq "432636005"
14671525
end
@@ -1471,6 +1529,11 @@
14711529
"Human alphaherpesvirus 3 and Measles morbillivirus " \
14721530
"and Mumps orthorubulavirus and Rubella virus antigens"
14731531
end
1532+
1533+
its(:nhs_immunisations_api_snomed_reason_code) { should eq "723620004" }
1534+
its(:nhs_immunisations_api_snomed_reason_term) { should be_nil }
1535+
1536+
its(:notes) { should include("Reported dose number string: Unknown") }
14741537
end
14751538
end
14761539
end

spec/models/vaccination_record_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
# nhs_immunisations_api_primary_source :boolean
2323
# nhs_immunisations_api_snomed_procedure_code :string
2424
# nhs_immunisations_api_snomed_procedure_term :string
25+
# nhs_immunisations_api_snomed_reason_code :string
26+
# nhs_immunisations_api_snomed_reason_term :string
2527
# nhs_immunisations_api_sync_pending_at :datetime
2628
# nhs_immunisations_api_synced_at :datetime
2729
# notes :text

0 commit comments

Comments
 (0)