22
33class PatientSessions ::ConsentsController < PatientSessions ::BaseController
44 before_action :set_consent , except : %i[ create send_request ]
5+ before_action :set_consent_follow_up_form ,
6+ only : %i[ edit_follow_up update_follow_up ]
7+ before_action :set_consent_confirm_refusal_form ,
8+ only : %i[ edit_confirm_refusal update_confirm_refusal ]
59 before_action :ensure_can_follow_up ,
610 only : %i[
711 edit_follow_up
@@ -63,25 +67,29 @@ def edit_follow_up
6367 end
6468
6569 def update_follow_up
66- if follow_up_params [ :decision_stands ] . nil?
67- @consent . errors . add ( :decision_stands , :blank )
68- render :follow_up , status : :unprocessable_content
69- elsif follow_up_params [ :decision_stands ] == "true"
70- redirect_to confirm_refusal_session_patient_programme_consent_path
71- else
72- @draft_consent = DraftConsent . new ( request_session : session , current_user :)
73- @draft_consent . clear_attributes
74- @draft_consent . assign_attributes ( create_params )
75- @draft_consent . follow_up_consent_id = @consent . id
76- @draft_consent . follow_up_flow = true
77- @draft_consent . new_or_existing_contact = @consent . parent_id . to_s
78- @draft_consent . route = "phone"
79-
80- if @draft_consent . save
81- redirect_to draft_consent_path ( "agree" )
70+ @form . assign_attributes ( follow_up_params )
71+
72+ if @form . valid?
73+ if @form . decision_stands?
74+ redirect_to confirm_refusal_session_patient_programme_consent_path
8275 else
83- render :follow_up , status : :unprocessable_content
76+ @draft_consent =
77+ DraftConsent . new ( request_session : session , current_user :)
78+ @draft_consent . clear_attributes
79+ @draft_consent . assign_attributes ( create_params )
80+ @draft_consent . follow_up_consent_id = @consent . id
81+ @draft_consent . follow_up_flow = true
82+ @draft_consent . new_or_existing_contact = @consent . parent_id . to_s
83+ @draft_consent . route = "phone"
84+
85+ if @draft_consent . save
86+ redirect_to draft_consent_path ( "agree" )
87+ else
88+ render :follow_up , status : :unprocessable_content
89+ end
8490 end
91+ else
92+ render :follow_up , status : :unprocessable_content
8593 end
8694 end
8795
@@ -90,29 +98,30 @@ def edit_confirm_refusal
9098 end
9199
92100 def update_confirm_refusal
93- if confirm_refusal_params [ :confirmed ] . nil?
94- @consent . errors . add ( :confirmed , :blank )
95- render :confirm_refusal , status : :unprocessable_content
96- elsif confirm_refusal_params [ :confirmed ] == "true"
97- ActiveRecord ::Base . transaction do
101+ @form . assign_attributes ( confirm_refusal_params )
102+
103+ if @form . valid?
104+ if @form . confirmed?
98105 @consent . resolve_follow_up! (
99106 outcome : :confirmed ,
100107 notes : confirm_refusal_params [ :notes ] . to_s
101108 )
102- end
103109
104- @consent . notifier . send_confirmation (
105- session : @session ,
106- triage : nil ,
107- sent_by : current_user
108- )
110+ @consent . notifier . send_confirmation (
111+ session : @session ,
112+ triage : nil ,
113+ sent_by : current_user
114+ )
109115
110- redirect_to session_patient_programme_consent_path ,
111- flash : {
112- success : "Consent from #{ @consent . name } updated."
113- }
116+ redirect_to session_patient_programme_consent_path ,
117+ flash : {
118+ success : "Consent from #{ @consent . name } updated."
119+ }
120+ else
121+ redirect_to session_patient_programme_consent_path
122+ end
114123 else
115- redirect_to session_patient_programme_consent_path
124+ render :confirm_refusal , status : :unprocessable_content
116125 end
117126 end
118127
@@ -173,6 +182,14 @@ def set_consent
173182 . find ( params [ :id ] )
174183 end
175184
185+ def set_consent_follow_up_form
186+ @form = ConsentFollowUpForm . new
187+ end
188+
189+ def set_consent_confirm_refusal_form
190+ @form = ConsentConfirmRefusalForm . new
191+ end
192+
176193 def update_patient_status
177194 @consent . invalidate_all_triages_and_patient_specific_directions!
178195
@@ -201,11 +218,11 @@ def create_params
201218 end
202219
203220 def follow_up_params
204- params . permit ( consent : :decision_stands ) [ :consent ] || { }
221+ params . fetch ( :consent_follow_up_form , { } ) . permit ( :decision_stands )
205222 end
206223
207224 def confirm_refusal_params
208- params . permit ( consent : %i[ confirmed notes ] ) [ :consent ] || { }
225+ params . fetch ( :consent_confirm_refusal_form , { } ) . permit ( : confirmed, : notes)
209226 end
210227
211228 def withdraw_params
0 commit comments