|
13 | 13 | # response :integer not null
|
14 | 14 | # route :integer not null
|
15 | 15 | # submitted_at :datetime not null
|
| 16 | +# vaccine_methods :integer default([]), not null, is an Array |
16 | 17 | # withdrawn_at :datetime
|
17 | 18 | # created_at :datetime not null
|
18 | 19 | # updated_at :datetime not null
|
|
42 | 43 | class Consent < ApplicationRecord
|
43 | 44 | include Invalidatable
|
44 | 45 | include HasHealthAnswers
|
| 46 | + include HasVaccineMethods |
45 | 47 |
|
46 | 48 | audited associated_with: :patient
|
47 | 49 |
|
@@ -99,6 +101,8 @@ class Consent < ApplicationRecord
|
99 | 101 | presence: true,
|
100 | 102 | unless: -> { via_self_consent? || via_website? }
|
101 | 103 |
|
| 104 | + validates :vaccine_methods, presence: true, if: :response_given? |
| 105 | + |
102 | 106 | def self.verbal_routes = routes.except("website", "self_consent")
|
103 | 107 |
|
104 | 108 | def name
|
@@ -158,42 +162,43 @@ def self.from_consent_form!(consent_form, patient:, current_user:)
|
158 | 162 | parent =
|
159 | 163 | consent_form.find_or_create_parent_with_relationship_to!(patient:)
|
160 | 164 |
|
161 |
| - consent_given = |
162 |
| - consent_form.given_programmes.map do |programme| |
163 |
| - patient.consents.create!( |
164 |
| - consent_form:, |
165 |
| - organisation: consent_form.organisation, |
166 |
| - programme:, |
167 |
| - parent:, |
168 |
| - notes: "", |
169 |
| - response: "given", |
170 |
| - route: "website", |
171 |
| - health_answers: consent_form.health_answers, |
172 |
| - recorded_by: current_user, |
173 |
| - submitted_at: consent_form.recorded_at |
174 |
| - ) |
175 |
| - end |
176 |
| - |
177 |
| - consent_refused = |
178 |
| - consent_form.refused_programmes.map do |programme| |
179 |
| - patient.consents.create!( |
180 |
| - consent_form:, |
181 |
| - organisation: consent_form.organisation, |
182 |
| - programme:, |
183 |
| - parent:, |
184 |
| - reason_for_refusal: consent_form.reason, |
185 |
| - notes: consent_form.reason_notes.presence || "", |
186 |
| - response: "refused", |
187 |
| - route: "website", |
188 |
| - health_answers: consent_form.health_answers, |
189 |
| - recorded_by: current_user, |
190 |
| - submitted_at: consent_form.recorded_at |
191 |
| - ) |
192 |
| - end |
| 165 | + consents = |
| 166 | + consent_form |
| 167 | + .consent_form_programmes |
| 168 | + .includes(:programme) |
| 169 | + .map do |consent_form_programme| |
| 170 | + notes = |
| 171 | + if consent_form_programme.response_given? |
| 172 | + "" |
| 173 | + else |
| 174 | + consent_form.reason_notes.presence || "" |
| 175 | + end |
| 176 | + reason_for_refusal = |
| 177 | + if consent_form_programme.response_given? |
| 178 | + nil |
| 179 | + else |
| 180 | + consent_form.reason |
| 181 | + end |
| 182 | + |
| 183 | + patient.consents.create!( |
| 184 | + consent_form:, |
| 185 | + health_answers: consent_form.health_answers, |
| 186 | + notes:, |
| 187 | + organisation: consent_form.organisation, |
| 188 | + parent:, |
| 189 | + programme: consent_form_programme.programme, |
| 190 | + reason_for_refusal:, |
| 191 | + recorded_by: current_user, |
| 192 | + response: consent_form_programme.response, |
| 193 | + route: "website", |
| 194 | + submitted_at: consent_form.recorded_at, |
| 195 | + vaccine_methods: consent_form_programme.vaccine_methods |
| 196 | + ) |
| 197 | + end |
193 | 198 |
|
194 | 199 | StatusUpdater.call(patient:)
|
195 | 200 |
|
196 |
| - consent_given + consent_refused |
| 201 | + consents |
197 | 202 | end
|
198 | 203 | end
|
199 | 204 |
|
|
0 commit comments