Skip to content

Include records for patients with newly added nhs numbers in automated careplus reports#6674

Open
jf-x-dev wants to merge 4 commits intonextfrom
include-records-for-patients-with-gained-nhs-numbers-in-automated-reports
Open

Include records for patients with newly added nhs numbers in automated careplus reports#6674
jf-x-dev wants to merge 4 commits intonextfrom
include-records-for-patients-with-gained-nhs-numbers-in-automated-reports

Conversation

@jf-x-dev
Copy link
Copy Markdown
Contributor

@jf-x-dev jf-x-dev commented Apr 22, 2026

From the ticket:

For a given team, if a patient goes from having no NHS number to having an NHS number, then we want to include all of the records for that patient in the next automated careplus export. However, we only want to include the records that were created after the automated careplus integration was enabled for the team - record prior to this will have been sent manually.

We accomplish this by adding a nhs_number_first_added_at field to Patient and a careplus_integration_enabled_at field to Team. This allows us to extend the scope of the automated careplus reporter.

Jira Issue - MAV-7091

@jf-x-dev jf-x-dev force-pushed the include-records-for-patients-with-gained-nhs-numbers-in-automated-reports branch from 2cff41e to d7ea6b3 Compare April 22, 2026 20:25
@jf-x-dev jf-x-dev changed the title Include records for patients with gained nhs numbers in automated reports Include records for patients with newly added nhs numbers in automated reports Apr 23, 2026
@jf-x-dev jf-x-dev force-pushed the include-records-for-patients-with-gained-nhs-numbers-in-automated-reports branch 6 times, most recently from c33b18d to 3276762 Compare April 23, 2026 07:14
@jf-x-dev jf-x-dev changed the title Include records for patients with newly added nhs numbers in automated reports Include records for patients with newly added nhs numbers in automated careplus reports Apr 23, 2026
@jf-x-dev jf-x-dev force-pushed the include-records-for-patients-with-gained-nhs-numbers-in-automated-reports branch from 3276762 to fae4447 Compare April 23, 2026 08:26
@jf-x-dev jf-x-dev marked this pull request as ready for review April 23, 2026 08:54
@jf-x-dev jf-x-dev requested a review from a team as a code owner April 23, 2026 08:54
@jf-x-dev jf-x-dev added regional-reporting data consistency Validations and data tweaks labels Apr 23, 2026
@jf-x-dev jf-x-dev added this to the v8.4.0 milestone Apr 23, 2026
Comment thread app/models/patient.rb Outdated
Comment on lines +500 to +501
before_validation :set_nhs_number_first_added_at,
if: :will_save_change_to_nhs_number?
Copy link
Copy Markdown
Contributor

@thomasleese thomasleese Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will always work, particularly if we're importing patients in bulk via Patient.import (where callbacks won't typically be called).

I think we might need to add something to

def new_patient_attributes
{
address_postcode: patient_postcode&.to_postcode,
date_of_birth: patient_date_of_birth&.to_date,
birth_academic_year: patient_date_of_birth&.to_date&.academic_year,
family_name: patient_last_name.to_s,
given_name: patient_first_name.to_s,
gender_code: patient_gender_code_value,
nhs_number: patient_nhs_number_value,
school: nil
}.compact
end
and https://github.com/NHSDigital/manage-vaccinations-in-schools/blob/main/app/models/patient_import_row.rb.

In general we've tried to avoid callbacks where possible due to the fact that they don't always execute and instead set the values explicitly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've added unit tests for each place where the nhs number can change. It looks like the callback works in almost all cases, but as you said it doesn't trigger during imports.

I was struggling to find a nice way to explicitly set nhs_number_added_at in all cases, since we'd need to do this for real changes, staged changes and bulk imports which would all be slightly different (plus we'd need to do this for any future mechanisms that set nhs numbers).

The callback does work in most cases, so any "standard" future mechanism that changes nhs numbers should be covered by it. If there are any new mechanisms that don't trigger callbacks, ideally we'd investigate and see that we need to update the nhs_number_first_added_at field as a part of the mechanism.

For now I've kept the callback and handled the bulk import case manually, but would be interested to hear what you think about it

Base automatically changed from add-sidekiq-job-for-careplus-export to next April 24, 2026 09:38
@jf-x-dev jf-x-dev force-pushed the include-records-for-patients-with-gained-nhs-numbers-in-automated-reports branch 2 times, most recently from 2c6745e to 6f3d36a Compare April 28, 2026 09:26
Comment on lines +5 to +6
add_column :patients, :nhs_number_first_added_at, :datetime
add_index :patients, :nhs_number_first_added_at
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we test how long this takes on data replication, as I believe it'll lock the table, particularly adding the index.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, we should definitely test this. I don't have DR access but I can find someone to test this with

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had another look and it seems we should be a bit more careful about this, so I've split the migrations up and moved the backfill column part to data - which will now be handled in batches and outside of a transaction

Comment on lines +386 to +389
existing_patient.public_send(
"#{attribute}=",
child_attributes[attribute.to_s]
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change necessary because the callbacks don't execute without it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the callbacks didn't execute otherwise

@jf-x-dev jf-x-dev force-pushed the include-records-for-patients-with-gained-nhs-numbers-in-automated-reports branch 2 times, most recently from 8154b94 to 34bf138 Compare May 1, 2026 08:23
jf-x-dev added 4 commits May 1, 2026 09:56
* Will be used by Automated Careplus reports

Jira-Issue: MAV-7091
* Will be used by Automated Careplus reports

Jira-Issue: MAV-7091
* Include records for patients that gained an nhs number in the last day
* Only include these records if they were created after the careplus integration was enabled

Jira-Issue: MAV-7091
@jf-x-dev jf-x-dev force-pushed the include-records-for-patients-with-gained-nhs-numbers-in-automated-reports branch from 34bf138 to dec3d61 Compare May 1, 2026 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data consistency Validations and data tweaks regional-reporting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants