Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/controllers/api/testing/teams_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ def destroy
log_destroy(SchoolMoveLogEntry.where(patient_id: patient_ids))
log_destroy(VaccinationRecord.where(patient_id: patient_ids))

log_destroy(Consent.where(team:))
log_destroy(ConsentForm.where(id: consent_form_ids))

log_destroy(SessionDate.where(session: sessions))

log_destroy(ArchiveReason.where(team:))
log_destroy(Consent.where(team:))
log_destroy(Triage.where(team:))

Patient
Expand Down
4 changes: 4 additions & 0 deletions app/jobs/consent_form_matching_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class ConsentFormMatchingJob < ApplicationJob
def perform(consent_form)
@consent_form = consent_form

return if already_matched?

# Match if we find a patient with the PDS NHS number
return if match_with_exact_nhs_number

Expand Down Expand Up @@ -47,6 +49,8 @@ def pds_patient
@pds_patient ||= PDS::Patient.search(**query)
end

def already_matched? = @consent_form.matched?

def match_with_exact_nhs_number
return false unless pds_patient

Expand Down
26 changes: 11 additions & 15 deletions app/lib/generate/consents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,27 @@ def create_consents(response, count)
traits = [response]
end

consents =
consent_forms =
available_patient_sessions.map do |patient, session|
school = session.location.school? ? session.location : patient.school

@updated_patients << patient
@updated_sessions << session

FactoryBot.build(
:consent,
*traits,
patient:,
programme:,
:consent_form,
team:,
consent_form:
FactoryBot.build(
:consent_form,
team:,
programmes: [programme],
session:,
school:,
response:
)
programmes: [programme],
session:,
school:,
response:,
consents: [
FactoryBot.build(:consent, *traits, patient:, programme:, team:)
]
)
end
Consent.import!(consents, recursive: true)

ConsentForm.import!(consent_forms, recursive: true)
end

def validate_programme_and_session(programme, session)
Expand Down
5 changes: 4 additions & 1 deletion app/models/consent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# withdrawn_at :datetime
# created_at :datetime not null
# updated_at :datetime not null
# consent_form_id :bigint
# parent_id :bigint
# patient_id :bigint not null
# programme_id :bigint not null
Expand All @@ -28,6 +29,7 @@
# Indexes
#
# index_consents_on_academic_year (academic_year)
# index_consents_on_consent_form_id (consent_form_id)
# index_consents_on_parent_id (parent_id)
# index_consents_on_patient_id (patient_id)
# index_consents_on_programme_id (programme_id)
Expand All @@ -36,6 +38,7 @@
#
# Foreign Keys
#
# fk_rails_... (consent_form_id => consent_forms.id)
# fk_rails_... (parent_id => parents.id)
# fk_rails_... (patient_id => patients.id)
# fk_rails_... (programme_id => programmes.id)
Expand All @@ -55,7 +58,7 @@ class Consent < ApplicationRecord
belongs_to :programme
belongs_to :team

has_one :consent_form
belongs_to :consent_form, optional: true
belongs_to :parent, optional: true
belongs_to :recorded_by,
class_name: "User",
Expand Down
12 changes: 6 additions & 6 deletions app/models/consent_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,20 @@
# use_preferred_name :boolean
# created_at :datetime not null
# updated_at :datetime not null
# consent_id :bigint
# location_id :bigint not null
# school_id :bigint
# team_id :bigint not null
#
# Indexes
#
# index_consent_forms_on_academic_year (academic_year)
# index_consent_forms_on_consent_id (consent_id)
# index_consent_forms_on_location_id (location_id)
# index_consent_forms_on_nhs_number (nhs_number)
# index_consent_forms_on_school_id (school_id)
# index_consent_forms_on_team_id (team_id)
#
# Foreign Keys
#
# fk_rails_... (consent_id => consents.id)
# fk_rails_... (location_id => locations.id)
# fk_rails_... (school_id => locations.id)
# fk_rails_... (team_id => teams.id)
Expand All @@ -68,7 +65,7 @@ class ConsentForm < ApplicationRecord

before_save :reset_unused_attributes

scope :unmatched, -> { where(consent_id: nil) }
scope :unmatched, -> { where.missing(:consents) }
scope :recorded, -> { where.not(recorded_at: nil) }

attr_accessor :health_question_number,
Expand All @@ -77,14 +74,15 @@ class ConsentForm < ApplicationRecord
:chosen_programme,
:injection_alternative

audited associated_with: :consent
audited associated_with: :team
has_associated_audits

belongs_to :consent, optional: true
belongs_to :location
belongs_to :school, class_name: "Location", optional: true
belongs_to :team

has_many :consents

has_many :notify_log_entries
has_many :consent_form_programmes,
-> { ordered },
Expand Down Expand Up @@ -320,6 +318,8 @@ def wizard_steps

def recorded? = recorded_at != nil

def matched? = consents.exists?

def response_given? = consent_form_programmes.any?(&:response_given?)

def response_refused? = consent_form_programmes.any?(&:response_refused?)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

class SwapConsentAndConsentFormAssociations < ActiveRecord::Migration[8.0]
def change
add_reference :consents, :consent_form, foreign_key: true

reversible do |direction|
direction.up do
ConsentForm
.where.not(consent_id: nil)
.find_each do |consent_form|
consent = Consent.find(consent_form.consent_id)
consent.update_column(:consent_form_id, consent_form.id)
end
end

direction.down do
Consent
.where.not(consent_form_id: nil)
.find_each do |consent|
consent_form = ConsentForm.find(consent.consent_form_id)
consent_form.update_column(:consent_id, consent.id)
end
end
end

remove_reference :consent_forms, :consent, foreign_key: true
end
end
8 changes: 4 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.0].define(version: 2025_09_16_074716) do
ActiveRecord::Schema[8.0].define(version: 2025_09_18_122640) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"
enable_extension "pg_trgm"
Expand Down Expand Up @@ -204,7 +204,6 @@
t.string "address_town"
t.string "address_postcode"
t.jsonb "health_answers", default: [], null: false
t.bigint "consent_id"
t.string "parent_contact_method_other_details"
t.string "parent_contact_method_type"
t.string "parent_email"
Expand All @@ -225,7 +224,6 @@
t.text "notes", default: "", null: false
t.integer "academic_year", null: false
t.index ["academic_year"], name: "index_consent_forms_on_academic_year"
t.index ["consent_id"], name: "index_consent_forms_on_consent_id"
t.index ["location_id"], name: "index_consent_forms_on_location_id"
t.index ["nhs_number"], name: "index_consent_forms_on_nhs_number"
t.index ["school_id"], name: "index_consent_forms_on_school_id"
Expand Down Expand Up @@ -270,7 +268,9 @@
t.integer "vaccine_methods", default: [], null: false, array: true
t.integer "academic_year", null: false
t.boolean "notify_parent_on_refusal"
t.bigint "consent_form_id"
t.index ["academic_year"], name: "index_consents_on_academic_year"
t.index ["consent_form_id"], name: "index_consents_on_consent_form_id"
t.index ["parent_id"], name: "index_consents_on_parent_id"
t.index ["patient_id"], name: "index_consents_on_patient_id"
t.index ["programme_id"], name: "index_consents_on_programme_id"
Expand Down Expand Up @@ -992,7 +992,6 @@
add_foreign_key "cohort_imports_patients", "patients"
add_foreign_key "consent_form_programmes", "consent_forms"
add_foreign_key "consent_form_programmes", "programmes"
add_foreign_key "consent_forms", "consents"
add_foreign_key "consent_forms", "locations"
add_foreign_key "consent_forms", "locations", column: "school_id"
add_foreign_key "consent_forms", "teams"
Expand All @@ -1001,6 +1000,7 @@
add_foreign_key "consent_notifications", "patients"
add_foreign_key "consent_notifications", "sessions"
add_foreign_key "consent_notifications", "users", column: "sent_by_user_id"
add_foreign_key "consents", "consent_forms"
add_foreign_key "consents", "parents"
add_foreign_key "consents", "patients"
add_foreign_key "consents", "programmes"
Expand Down
3 changes: 0 additions & 3 deletions spec/factories/consent_forms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,20 @@
# use_preferred_name :boolean
# created_at :datetime not null
# updated_at :datetime not null
# consent_id :bigint
# location_id :bigint not null
# school_id :bigint
# team_id :bigint not null
#
# Indexes
#
# index_consent_forms_on_academic_year (academic_year)
# index_consent_forms_on_consent_id (consent_id)
# index_consent_forms_on_location_id (location_id)
# index_consent_forms_on_nhs_number (nhs_number)
# index_consent_forms_on_school_id (school_id)
# index_consent_forms_on_team_id (team_id)
#
# Foreign Keys
#
# fk_rails_... (consent_id => consents.id)
# fk_rails_... (location_id => locations.id)
# fk_rails_... (school_id => locations.id)
# fk_rails_... (team_id => teams.id)
Expand Down
3 changes: 3 additions & 0 deletions spec/factories/consents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# withdrawn_at :datetime
# created_at :datetime not null
# updated_at :datetime not null
# consent_form_id :bigint
# parent_id :bigint
# patient_id :bigint not null
# programme_id :bigint not null
Expand All @@ -28,6 +29,7 @@
# Indexes
#
# index_consents_on_academic_year (academic_year)
# index_consents_on_consent_form_id (consent_form_id)
# index_consents_on_parent_id (parent_id)
# index_consents_on_patient_id (patient_id)
# index_consents_on_programme_id (programme_id)
Expand All @@ -36,6 +38,7 @@
#
# Foreign Keys
#
# fk_rails_... (consent_form_id => consent_forms.id)
# fk_rails_... (parent_id => parents.id)
# fk_rails_... (patient_id => patients.id)
# fk_rails_... (programme_id => programmes.id)
Expand Down
24 changes: 23 additions & 1 deletion spec/jobs/consent_form_matching_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)
end

before do
let!(:stub) do
stub_request(
:get,
"https://sandbox.api.service.nhs.uk/personal-demographics/FHIR/R4/Patient"
Expand All @@ -34,6 +34,28 @@
it "doesn't create a consent" do
expect { perform }.not_to change(Consent, :count)
end

it "makes requests to PDS" do
perform
expect(stub).to have_been_requested.twice
end
end

context "with a consent form that's already been matched" do
let(:response_file) { "pds/search-patients-no-results-response.json" }

before do
create(:consent, programme: session.programmes.first, consent_form:)
end

it "doesn't create a consent" do
expect { perform }.not_to change(Consent, :count)
end

it "doesn't make a request to PDS" do
perform
expect(stub).not_to have_been_requested
end
end

context "with one matching patient" do
Expand Down
Loading