@@ -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,53 @@ 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
+ create_patient_specific_direction!
137
+ elsif add_patient_specific_direction == false
138
+ destroy_patient_specific_directions!
139
+ end
140
+ end
141
+
142
+ def create_patient_specific_direction!
143
+ vaccine_method = "nasal"
144
+
145
+ # TODO: Handle programmes with multiple nasal vaccines.
146
+ vaccine = programme . vaccines . find_by ( method : vaccine_method )
147
+
148
+ attributes = {
149
+ academic_year :,
150
+ delivery_site : "nose" ,
151
+ patient :,
152
+ programme :,
153
+ vaccine :,
154
+ vaccine_method :
155
+ }
156
+
157
+ return if patient . patient_specific_directions . exists? ( attributes )
158
+
159
+ patient . patient_specific_directions . create! (
160
+ created_by : current_user ,
161
+ **attributes
162
+ )
163
+ end
164
+
165
+ def destroy_patient_specific_directions!
166
+ patient
167
+ . patient_specific_directions
168
+ . where ( academic_year :, programme :)
169
+ . destroy_all
170
+ end
116
171
end
0 commit comments