Skip to content

Commit 498f3b2

Browse files
committed
Add validation on vaccine_method
This adds some validation on the `vaccine_method` column of the `Consent` model to ensure that it's always set when the parent has given consent. This value will be used as part of generating the overall consent status so it's important that it's always present. We can't validate that the `vaccine_method` is not set when refused consent because it's possible for a parent to give consent and then withdrawn it, in which case we would want to see what the method was when consent was given. Jira-Issue: MAV-1230
1 parent 20df538 commit 498f3b2

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

app/models/consent.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ class Consent < ApplicationRecord
101101
presence: true,
102102
unless: -> { via_self_consent? || via_website? }
103103

104+
validates :vaccine_methods, presence: true, if: :response_given?
105+
104106
def self.verbal_routes = routes.except("website", "self_consent")
105107

106108
def name

spec/factories/consent_form_programmes.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
consent_form
2626
programme
2727

28-
traits_for_enum :response
28+
trait :given do
29+
response { "given" }
30+
vaccine_methods { %w[injection] }
31+
end
32+
33+
trait :refused do
34+
response { "refused" }
35+
vaccine_methods { [] }
36+
end
2937
end
3038
end

spec/factories/consent_forms.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,11 @@
123123
end
124124

125125
after(:create) do |consent_form, evaluator|
126+
vaccine_methods = evaluator.response == "given" ? %w[injection] : []
127+
126128
consent_form.consent_form_programmes.update_all(
127-
response: evaluator.response
129+
response: evaluator.response,
130+
vaccine_methods:
128131
)
129132
end
130133

spec/factories/consents.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
end
6666

6767
response { "given" }
68+
vaccine_methods { %w[injection] }
6869
route { "website" }
6970

7071
health_answers do
@@ -75,8 +76,6 @@
7576

7677
submitted_at { consent_form&.recorded_at || Time.current }
7778

78-
traits_for_enum :response
79-
8079
trait :given_verbally do
8180
given
8281
route { "phone" }
@@ -94,10 +93,16 @@
9493
end
9594

9695
trait :refused do
97-
response { :refused }
98-
reason_for_refusal { :personal_choice }
96+
response { "refused" }
97+
reason_for_refusal { "personal_choice" }
9998
health_answers { [] }
10099
notes { "Refused." }
100+
vaccine_methods { [] }
101+
end
102+
103+
trait :not_provided do
104+
response { "not_provided" }
105+
vaccine_methods { [] }
101106
end
102107

103108
trait :from_mum do

spec/models/consent_spec.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@
4343
describe Consent do
4444
describe "validations" do
4545
it { should validate_length_of(:notes).is_at_most(1000) }
46+
47+
context "when response is given" do
48+
subject { build(:consent, :given) }
49+
50+
it { should validate_presence_of(:vaccine_methods) }
51+
end
52+
53+
context "when response is refused" do
54+
subject { build(:consent, :refused) }
55+
56+
it { should_not validate_presence_of(:vaccine_methods) }
57+
end
4658
end
4759

4860
describe "#verbal_routes" do
@@ -202,8 +214,14 @@
202214
end
203215

204216
before do
205-
consent_form.consent_form_programmes.first.update!(response: "given")
206-
consent_form.consent_form_programmes.second.update!(response: "refused")
217+
consent_form.consent_form_programmes.first.update!(
218+
response: "given",
219+
vaccine_methods: %w[injection]
220+
)
221+
consent_form.consent_form_programmes.second.update!(
222+
response: "refused",
223+
vaccine_methods: []
224+
)
207225
end
208226

209227
it "creates a consent per programme" do

0 commit comments

Comments
 (0)