Skip to content

Commit

Permalink
Include historical records in offline reporting (#3104)
Browse files Browse the repository at this point in the history
This is important to include as a record of previous vaccinations so
that the nurses have all the information they need in a context where
access to Mavis is unavailable due to a lack off Internet connection.

The `SESSION_ID` will not be provided for these, which allows them to be
re-imported successfully, and there's no risk of the records being
duplicated as we rely on the `UUID` column to uniquely reference the
records.
  • Loading branch information
thomasleese authored Feb 27, 2025
2 parents bd74532 + 572f227 commit b8c29e1
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 13 deletions.
43 changes: 30 additions & 13 deletions app/lib/reports/offline_session_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ def rows(patient_session:)
}
}

vaccination_records =
patient_session.vaccination_records(programme:, for_session: true)
vaccination_records = patient_session.vaccination_records(programme:)

if vaccination_records.any?
vaccination_records.map do |vaccination_record|
Expand All @@ -173,7 +172,7 @@ def rows(patient_session:)
[
Row.new(columns, style: row_style) do |row|
add_patient_cells(row, patient_session:, programme:)
add_new_row_cells(row, session: patient_session.session, programme:)
add_new_row_cells(row, patient_session:, programme:)
end
]
end
Expand All @@ -188,13 +187,6 @@ def add_patient_cells(row, patient_session:, programme:)
triage = patient_session.latest_triage(programme:)

row[:organisation_code] = organisation.ods_code
row[:school_urn] = school_urn(location:, patient:)
row[:school_name] = school_name(location:, patient:)
row[:care_setting] = Cell.new(
care_setting(location:),
type: :integer,
allowed_values: [1, 2]
)
row[:person_forename] = patient.given_name
row[:person_surname] = patient.family_name
row[:person_dob] = patient.date_of_birth
Expand Down Expand Up @@ -232,14 +224,29 @@ def add_patient_cells(row, patient_session:, programme:)

def add_existing_row_cells(row, vaccination_record:)
batch = vaccination_record.batch
patient = vaccination_record.patient
programme = vaccination_record.programme
session = vaccination_record.session
vaccine = vaccination_record.vaccine
location = session&.location

row[:vaccinated] = Cell.new(
vaccinated(vaccination_record:),
allowed_values: %w[Y N]
)
row[:school_urn] = location ? school_urn(location:, patient:) : "888888"
row[:school_name] = (
if location
school_name(location:, patient:)
else
vaccination_record.location_name
end
)
row[:care_setting] = Cell.new(
location ? care_setting(location:) : nil,
type: :integer,
allowed_values: [1, 2]
)
row[:date_of_vaccination] = vaccination_record.performed_at.to_date
row[:time_of_vaccination] = vaccination_record.performed_at.strftime(
"%H:%M:%S"
Expand Down Expand Up @@ -268,20 +275,30 @@ def add_existing_row_cells(row, vaccination_record:)
allowed_values: ImmunisationImportRow::REASONS.keys
)
row[:notes] = vaccination_record.notes
row[:session_id] = session.id
row[:session_id] = session&.id
row[:uuid] = vaccination_record.uuid

if location.generic_clinic?
if location&.generic_clinic?
row[:clinic_name] = Cell.new(
vaccination_record.location_name,
allowed_values: clinic_name_values
)
end
end

def add_new_row_cells(row, session:, programme:)
def add_new_row_cells(row, patient_session:, programme:)
patient = patient_session.patient
location = patient_session.session.location

row[:vaccinated] = Cell.new(allowed_values: %w[Y N])
row[:date_of_vaccination] = Cell.new(type: :date)
row[:school_urn] = school_urn(location:, patient:)
row[:school_name] = school_name(location:, patient:)
row[:care_setting] = Cell.new(
care_setting(location:),
type: :integer,
allowed_values: [1, 2]
)
row[:programme] = programme.import_names.first
row[:vaccine_given] = Cell.new(
allowed_values: vaccine_values_for_programme(programme)
Expand Down
70 changes: 70 additions & 0 deletions spec/lib/reports/offline_session_exporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,76 @@ def validation_formula(worksheet:, column_name:, row: 1)
end
end

context "with a vaccinated patient outside the session" do
before { create(:patient_session, patient:, session:) }

let!(:vaccination_record) do
create(
:vaccination_record,
performed_at:,
batch:,
patient:,
programme:,
performed_by: user,
notes: "Some notes.",
location_name: "Waterloo Road"
)
end

it "adds a row with the vaccination details" do
expect(rows.count).to eq(1)
expect(
rows.first.except(
"BATCH_EXPIRY_DATE",
"PERSON_DOB",
"DATE_OF_VACCINATION"
)
).to eq(
{
"ANATOMICAL_SITE" => "left upper arm",
"BATCH_NUMBER" => batch.name,
"CARE_SETTING" => nil,
"CONSENT_DETAILS" => "",
"CONSENT_STATUS" => "",
"DOSE_SEQUENCE" => 1,
"GILLICK_ASSESSED_BY" => nil,
"GILLICK_ASSESSMENT_DATE" => nil,
"GILLICK_ASSESSMENT_NOTES" => nil,
"GILLICK_STATUS" => "",
"HEALTH_QUESTION_ANSWERS" => "",
"NHS_NUMBER" => patient.nhs_number,
"NOTES" => "Some notes.",
"ORGANISATION_CODE" => organisation.ods_code,
"PERFORMING_PROFESSIONAL_EMAIL" => "[email protected]",
"PERSON_ADDRESS_LINE_1" => patient.address_line_1,
"PERSON_FORENAME" => patient.given_name,
"PERSON_GENDER_CODE" => "Not known",
"PERSON_POSTCODE" => patient.address_postcode,
"PERSON_SURNAME" => patient.family_name,
"PROGRAMME" => "HPV",
"REASON_NOT_VACCINATED" => "",
"SCHOOL_NAME" => "Waterloo Road",
"SCHOOL_URN" => "888888",
"SESSION_ID" => nil,
"TIME_OF_VACCINATION" => "12:05:20",
"TRIAGED_BY" => nil,
"TRIAGE_DATE" => nil,
"TRIAGE_NOTES" => nil,
"TRIAGE_STATUS" => nil,
"VACCINATED" => "Y",
"VACCINE_GIVEN" => "Gardasil9",
"UUID" => vaccination_record.uuid,
"YEAR_GROUP" => patient.year_group
}
)
expect(rows.first["BATCH_EXPIRY_DATE"].to_date).to eq(batch.expiry)
expect(rows.first["PERSON_DOB"].to_date).to eq(patient.date_of_birth)
expect(rows.first["DATE_OF_VACCINATION"].to_date).to eq(
performed_at.to_date
)
end
end

context "with a patient who couldn't be vaccinated" do
before { create(:patient_session, patient:, session:) }

Expand Down

0 comments on commit b8c29e1

Please sign in to comment.