Skip to content

Commit

Permalink
Refactor how conflicting consent status is calculated (#2888)
Browse files Browse the repository at this point in the history
This extends #2883 to ensure that when calculating the stats for a list
of patient sessions we're correctly exclusing consent conflicted results
when the consent has actually been given, via self-consent.
  • Loading branch information
thomasleese authored Jan 28, 2025
2 parents eea331f + d06aaaa commit 90d1991
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
13 changes: 9 additions & 4 deletions app/models/concerns/patient_session_status_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def status
def consent_given?
return false if no_consent?

if !(self_consents = latest_consents.select(&:via_self_consent?)).empty?
self_consents.all?(&:response_given?)
if latest_self_consents.present?
latest_self_consents.all?(&:response_given?)
else
latest_consents.all?(&:response_given?)
end
Expand All @@ -70,8 +70,13 @@ def consent_refused?
def consent_conflicts?
return false if no_consent?

latest_consents.any?(&:response_refused?) &&
latest_consents.any?(&:response_given?)
if latest_self_consents.present?
latest_self_consents.any?(&:response_refused?) &&
latest_self_consents.any?(&:response_given?)
else
latest_consents.any?(&:response_refused?) &&
latest_consents.any?(&:response_given?)
end
end

def no_consent?
Expand Down
4 changes: 4 additions & 0 deletions app/models/patient_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ def latest_consents
.map { |_, consents| consents.max_by(&:created_at) }
end

def latest_self_consents
@latest_self_consents ||= latest_consents.select(&:via_self_consent?)
end

def latest_gillick_assessment
@latest_gillick_assessment ||= gillick_assessments.max_by(&:updated_at)
end
Expand Down
21 changes: 16 additions & 5 deletions spec/models/patient_session_stats_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,28 @@

create(:consent_form, :recorded, programme:, session:, consent_id: nil) # => unmatched response
create(:consent_form, :draft, programme:, session:, consent_id: nil) # => still draft, should not be counted

create(:patient_session, :consent_conflicting, programme:, session:) # conflicting consent

gillick_patient =
create(
:patient_session,
:consent_conflicting,
programme:,
session:
).patient
create(:consent, :self_consent, patient: gillick_patient, programme:) # conflicting consent with gillick
end

it "returns a hash of session stats" do
expect(to_h).to eq(
could_not_vaccinate: 1,
could_not_vaccinate: 2,
needing_triage: 2,
not_registered: 6,
vaccinate: 2,
not_registered: 8,
vaccinate: 3,
vaccinated: 0,
with_conflicting_consent: 0,
with_consent_given: 4,
with_conflicting_consent: 1,
with_consent_given: 5,
with_consent_refused: 1,
without_a_response: 1
)
Expand Down

0 comments on commit 90d1991

Please sign in to comment.