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 ]
9+ before_action :ensure_can_follow_up ,
10+ only : %i[
11+ edit_follow_up
12+ update_follow_up
13+ edit_confirm_refusal
14+ update_confirm_refusal
15+ ]
516 before_action :ensure_can_withdraw , only : %i[ edit_withdraw update_withdraw ]
617 before_action :ensure_can_invalidate ,
718 only : %i[ edit_invalidate update_invalidate ]
@@ -51,6 +62,69 @@ def send_request
5162 def show
5263 end
5364
65+ def edit_follow_up
66+ render :follow_up
67+ end
68+
69+ def update_follow_up
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
75+ else
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
90+ end
91+ else
92+ render :follow_up , status : :unprocessable_content
93+ end
94+ end
95+
96+ def edit_confirm_refusal
97+ render :confirm_refusal
98+ end
99+
100+ def update_confirm_refusal
101+ @form . assign_attributes ( confirm_refusal_params )
102+
103+ if @form . valid?
104+ if @form . confirmed?
105+ @consent . resolve_follow_up! (
106+ outcome : :confirmed ,
107+ notes : confirm_refusal_params [ :notes ] . to_s
108+ )
109+
110+ @consent . notifier . send_confirmation (
111+ session : @session ,
112+ triage : nil ,
113+ sent_by : current_user
114+ )
115+
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
123+ else
124+ render :confirm_refusal , status : :unprocessable_content
125+ end
126+ end
127+
54128 def edit_withdraw
55129 render :withdraw
56130 end
@@ -104,16 +178,28 @@ def set_consent
104178 @patient
105179 . consents
106180 . where ( academic_year : @session . academic_year )
107- . includes ( :consent_form , :parent , patient : :parent_relationships )
181+ . includes ( :consent_form , :parent , :team , patient : :parent_relationships )
108182 . find ( params [ :id ] )
109183 end
110184
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+
111193 def update_patient_status
112194 @consent . invalidate_all_triages_and_patient_specific_directions!
113195
114196 PatientStatusUpdater . call ( patient : @patient )
115197 end
116198
199+ def ensure_can_follow_up
200+ redirect_to action : :show unless @consent . can_follow_up?
201+ end
202+
117203 def ensure_can_withdraw
118204 redirect_to action : :show unless @consent . can_withdraw?
119205 end
@@ -131,6 +217,14 @@ def create_params
131217 }
132218 end
133219
220+ def follow_up_params
221+ params . fetch ( :consent_follow_up_form , { } ) . permit ( :decision_stands )
222+ end
223+
224+ def confirm_refusal_params
225+ params . fetch ( :consent_confirm_refusal_form , { } ) . permit ( :confirmed , :notes )
226+ end
227+
134228 def withdraw_params
135229 params . expect ( consent : %i[ reason_for_refusal notes ] ) . merge (
136230 response : "refused" ,
0 commit comments