Skip to content

Commit cb3bfb7

Browse files
committed
Set submitted_at on consents
When creating a consent (either by automatic matching with a consent form, manual matching with a consent form, or when recording a verbal consent), this ensures that the `submitted_at` column is set approriately.
1 parent b1fc7e5 commit cb3bfb7

7 files changed

Lines changed: 72 additions & 3 deletions

File tree

app/models/consent.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ def reasons_triage_needed
150150
end
151151

152152
def self.from_consent_form!(consent_form, patient:, current_user:)
153+
raise ConsentFormNotRecorded unless consent_form.recorded?
154+
153155
ActiveRecord::Base.transaction do
154156
parent =
155157
consent_form.find_or_create_parent_with_relationship_to!(patient:)
@@ -165,7 +167,8 @@ def self.from_consent_form!(consent_form, patient:, current_user:)
165167
response: "given",
166168
route: "website",
167169
health_answers: consent_form.health_answers,
168-
recorded_by: current_user
170+
recorded_by: current_user,
171+
submitted_at: consent_form.recorded_at
169172
)
170173
end
171174

@@ -181,7 +184,8 @@ def self.from_consent_form!(consent_form, patient:, current_user:)
181184
response: "refused",
182185
route: "website",
183186
health_answers: consent_form.health_answers,
184-
recorded_by: current_user
187+
recorded_by: current_user,
188+
submitted_at: consent_form.recorded_at
185189
)
186190
end
187191

@@ -195,4 +199,7 @@ def notes_required?
195199
withdrawn? || invalidated? ||
196200
(response_refused? && !reason_for_refusal_personal_choice?)
197201
end
202+
203+
class ConsentFormNotRecorded < StandardError
204+
end
198205
end

app/models/consent_form.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ def wizard_steps
278278
].compact
279279
end
280280

281+
def recorded?
282+
recorded_at != nil
283+
end
284+
281285
def each_health_answer
282286
return if health_answers.empty?
283287
return to_enum(:each_health_answer) unless block_given?

app/models/draft_consent.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ def write_to!(consent, triage:)
248248
super(consent)
249249

250250
consent.parent = parent
251+
consent.submitted_at ||= Time.current
251252

252253
if triage_allowed? && response_given?
253254
triage.notes = triage_notes || ""

spec/factories/consents.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
end
7373
end
7474

75+
submitted_at { Time.current }
76+
7577
traits_for_enum :response
7678

7779
trait :given_verbally do

spec/jobs/consent_form_matching_job_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
let(:consent_form) do
88
create(
99
:consent_form,
10+
:recorded,
1011
session:,
1112
given_name: "John",
1213
family_name: "Smith",
@@ -70,6 +71,7 @@
7071
let(:consent_form) do
7172
create(
7273
:consent_form,
74+
:recorded,
7375
session:,
7476
given_name: "john",
7577
family_name: "SMITH",

spec/models/consent_form_spec.rb

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,9 +743,25 @@
743743
create(:notify_log_entry, :email, consent_form:, patient: nil)
744744
end
745745

746+
context "when the consent form is draft" do
747+
let(:consent_form) { create(:consent_form, organisation:, session:) }
748+
749+
it "raises an error" do
750+
expect { match_with_patient! }.to raise_error(
751+
Consent::ConsentFormNotRecorded
752+
)
753+
end
754+
end
755+
746756
context "when consent form confirms the school" do
747757
let(:consent_form) do
748-
create(:consent_form, organisation:, session:, school_confirmed: true)
758+
create(
759+
:consent_form,
760+
:recorded,
761+
organisation:,
762+
session:,
763+
school_confirmed: true
764+
)
749765
end
750766

751767
it "creates a consent" do
@@ -763,10 +779,29 @@
763779
end
764780
end
765781

782+
context "when the consent form was submitted a week ago" do
783+
let(:consent_form) do
784+
create(
785+
:consent_form,
786+
recorded_at: 1.week.ago,
787+
organisation:,
788+
session:,
789+
school_confirmed: true
790+
)
791+
end
792+
793+
it "creates a consent submitted a week ago" do
794+
expect { match_with_patient! }.to change(Consent, :count).by(1)
795+
796+
expect(Consent.last.submitted_at).to eq(consent_form.recorded_at)
797+
end
798+
end
799+
766800
context "when the patient goes to a different school" do
767801
let(:consent_form) do
768802
create(
769803
:consent_form,
804+
:recorded,
770805
organisation:,
771806
session:,
772807
school_confirmed: false,
@@ -803,6 +838,7 @@
803838
let(:consent_form) do
804839
create(
805840
:consent_form,
841+
:recorded,
806842
organisation:,
807843
session:,
808844
school_confirmed: nil,

spec/models/draft_consent_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@
5353
end
5454
end
5555

56+
describe "#write_to" do
57+
subject(:write_to) { draft_consent.write_to!(consent, triage:) }
58+
59+
let(:consent) { Consent.new }
60+
let(:triage) { Triage.new }
61+
62+
let(:attributes) { valid_given_attributes }
63+
64+
it "sets the submitted at to today" do
65+
freeze_time do
66+
expect { write_to }.to change(consent, :submitted_at).from(nil).to(
67+
Time.current
68+
)
69+
end
70+
end
71+
end
72+
5673
describe "#reset_unused_fields" do
5774
subject(:save!) { draft_consent.save! }
5875

0 commit comments

Comments
 (0)