@@ -6,23 +6,21 @@ class TriageForm
6
6
7
7
attr_accessor :patient_session , :programme , :current_user
8
8
9
- attribute :status_and_vaccine_method , :string
10
- attribute :add_psd , :boolean
9
+ attribute :add_patient_specific_direction , :boolean
11
10
attribute :notes , :string
11
+ attribute :status_and_vaccine_method , :string
12
12
attribute :vaccine_methods , array : true , default : [ ]
13
13
14
+ validates :add_patient_specific_direction ,
15
+ inclusion : {
16
+ in : [ true , false ]
17
+ } ,
18
+ if : :requires_add_patient_specific_direction?
14
19
validates :status_and_vaccine_method ,
15
20
inclusion : {
16
21
in : :status_and_vaccine_method_options
17
22
}
18
23
validates :notes , length : { maximum : 1000 }
19
- validates :add_psd ,
20
- inclusion : {
21
- in : [ true , false ]
22
- } ,
23
- if : -> { add_psd . present? }
24
-
25
- def add_psd? = add_psd
26
24
27
25
def triage = ( triage )
28
26
self . status_and_vaccine_method =
@@ -40,11 +38,14 @@ def triage=(triage)
40
38
end
41
39
42
40
def save
43
- Triage . create! ( triage_attributes ) if valid?
41
+ save! if valid?
44
42
end
45
43
46
44
def save!
47
- Triage . create! ( triage_attributes )
45
+ ActiveRecord ::Base . transaction do
46
+ handle_patient_specific_direction
47
+ Triage . create! ( triage_attributes )
48
+ end
48
49
end
49
50
50
51
def safe_to_vaccinate_options
@@ -67,6 +68,11 @@ def consented_to_injection? = consented_vaccine_methods.include?("injection")
67
68
68
69
def consented_to_injection_only? = consented_vaccine_methods == [ "injection" ]
69
70
71
+ def show_add_patient_specific_direction? ( option )
72
+ session . psd_enabled? && option == "safe_to_vaccinate_nasal" &&
73
+ can_create_patient_specific_directions?
74
+ end
75
+
70
76
private
71
77
72
78
delegate :team , :patient , :session , to : :patient_session
@@ -87,7 +93,7 @@ def triage_attributes
87
93
programme :,
88
94
status :,
89
95
vaccine_method :,
90
- academic_year : session . academic_year
96
+ academic_year :
91
97
}
92
98
end
93
99
@@ -113,4 +119,51 @@ def vaccine_method
113
119
"nasal"
114
120
end
115
121
end
122
+
123
+ def requires_add_patient_specific_direction?
124
+ show_add_patient_specific_direction? ( status_and_vaccine_method )
125
+ end
126
+
127
+ def can_create_patient_specific_directions?
128
+ PatientSpecificDirectionPolicy . new (
129
+ current_user ,
130
+ PatientSpecificDirection
131
+ ) . create?
132
+ end
133
+
134
+ def handle_patient_specific_direction
135
+ if add_patient_specific_direction
136
+ ensure_psd_exists!
137
+ elsif add_patient_specific_direction == false
138
+ remove_existing_psd!
139
+ end
140
+ end
141
+
142
+ def ensure_psd_exists!
143
+ # TODO: Handle programmes with multiple nasal vaccines.
144
+ vaccine = programme . vaccines . nasal . first
145
+
146
+ psd_attributes = {
147
+ academic_year :,
148
+ delivery_site : "nose" ,
149
+ patient :,
150
+ programme :,
151
+ vaccine :,
152
+ vaccine_method : "nasal"
153
+ }
154
+
155
+ return if PatientSpecificDirection . exists? ( **psd_attributes )
156
+
157
+ PatientSpecificDirection . create! (
158
+ psd_attributes . merge ( created_by : current_user )
159
+ )
160
+ end
161
+
162
+ def remove_existing_psd!
163
+ PatientSpecificDirection . find_by (
164
+ academic_year :,
165
+ patient :,
166
+ programme :
167
+ ) &.destroy!
168
+ end
116
169
end
0 commit comments