Skip to content

Commit a36bcf5

Browse files
committed
Add AppPatientSessionRegistrationComponent
Shows a registration card on the patient session page for sessions that require registration. Displays a form to mark the child as attending or absent when not yet registered, a status summary with an update link when already registered, and a completion message when the session is done
1 parent 6f99c90 commit a36bcf5

6 files changed

Lines changed: 207 additions & 28 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<%= render AppCardComponent.new(section: true) do |card| %>
2+
<% card.with_heading(level: 2) { "Register attendance" } %>
3+
4+
<% if registration_status.completed? %>
5+
<p><%= patient.given_name %> has completed this session.</p>
6+
<% elsif registration_status.unknown? %>
7+
<p><%= patient.full_name %> is not registered yet.</p>
8+
<% if can_edit? %>
9+
<hr class="nhsuk-section-break nhsuk-section-break--visible nhsuk-section-break--m">
10+
<%= form_with model: attendance_record,
11+
url: session_patient_attendance_path(session, patient),
12+
method: :put,
13+
builder: MavisFormBuilder do |f| %>
14+
<%= f.govuk_radio_buttons_fieldset(
15+
:attending,
16+
legend: { size: "s", text: "Is #{patient.full_name} attending today’s session?" },
17+
) do %>
18+
<%= f.govuk_radio_button(:attending, true,
19+
label: { text: "Yes, they are attending today’s session" },
20+
link_errors: true) %>
21+
<%= f.govuk_radio_button(:attending, false,
22+
label: { text: "No, they are absent from today’s session" }) %>
23+
<% end %>
24+
<%= f.govuk_submit "Update attendance", class: "nhsuk-u-margin-bottom-0" %>
25+
<% end %>
26+
<% end %>
27+
<% elsif registration_status.attending? %>
28+
<p><%= patient.full_name %> is attending today’s session.</p>
29+
<% if can_edit? %>
30+
<%= render AppActionLinkComponent.new(
31+
text: "Update attendance",
32+
href: edit_session_patient_attendance_path(session, patient),
33+
) %>
34+
<% end %>
35+
<% elsif registration_status.not_attending? %>
36+
<p><%= patient.full_name %> is absent from today’s session.</p>
37+
<% if can_edit? %>
38+
<%= render AppActionLinkComponent.new(
39+
text: "Update attendance",
40+
href: edit_session_patient_attendance_path(session, patient),
41+
) %>
42+
<% end %>
43+
<% end %>
44+
<% end %>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
class AppPatientSessionRegistrationComponent < ViewComponent::Base
4+
def initialize(patient:, session:)
5+
@patient = patient
6+
@session = session
7+
end
8+
9+
def render?
10+
session.requires_registration? && session.today?
11+
end
12+
13+
private
14+
15+
attr_reader :patient, :session
16+
17+
delegate :policy, to: :helpers
18+
19+
def registration_status
20+
@registration_status ||= patient.registration_status(session:)
21+
end
22+
23+
def attendance_record
24+
@attendance_record ||=
25+
patient
26+
.attendance_records
27+
.find_or_initialize_by(location: session.location, date: Date.current)
28+
.tap { it.session = session }
29+
end
30+
31+
def can_edit?
32+
@can_edit ||= policy(attendance_record).edit?
33+
end
34+
end

app/views/patient_sessions/_header.html.erb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,6 @@
3737
<% end %>
3838
<% end %>
3939
<% end %>
40-
41-
<% if @session.requires_registration? && (date = @session.dates.find(&:today?)) %>
42-
<% attendance_record = @patient.attendance_records.find_or_initialize_by(location: @session.location, date:) %>
43-
44-
<% action_list.with_item do %>
45-
<%= render AppStatusTagComponent.new(@patient.registration_status(session: @session)&.status || "unknown", context: :registration) %>
46-
<% end %>
47-
48-
<% attendance_record.session = @session %>
49-
<% if policy(attendance_record).edit? %>
50-
<% action_list.with_item(
51-
text: "Update attendance",
52-
href: edit_session_patient_attendance_path(@session, @patient),
53-
) %>
54-
<% end %>
55-
<% end %>
5640
<% end %>
5741

5842
<%= render AppStickyNavigationComponent.new do %>

app/views/patient_sessions/programmes/show.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
<%= render AppPatientSessionTriageComponent.new(patient: @patient, session: @session, programme: @programme, current_user:, triage_form: @triage_form) %>
1414

15+
<%= render AppPatientSessionRegistrationComponent.new(patient: @patient, session: @session) %>
16+
1517
<%= render AppPatientSessionRecordComponent.new(patient: @patient, session: @session, programme: @programme, current_user:, vaccinate_form: @vaccinate_form) %>
1618
</div>
1719
</div>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# frozen_string_literal: true
2+
3+
describe AppPatientSessionRegistrationComponent do
4+
subject(:rendered) { render_inline(component) }
5+
6+
let(:component) { described_class.new(patient:, session:) }
7+
let(:programme) { Programme.flu }
8+
let(:session) { create(:session, :today, programmes: [programme]) }
9+
let(:patient) { create(:patient, session:) }
10+
11+
before { stub_authorization(allowed: true) }
12+
13+
context "when the session does not require registration" do
14+
let(:session) do
15+
create(
16+
:session,
17+
:today,
18+
:requires_no_registration,
19+
programmes: [programme]
20+
)
21+
end
22+
23+
it "does not render" do
24+
expect(component.render?).to be false
25+
end
26+
end
27+
28+
context "when the session is not active today" do
29+
let(:session) { create(:session, :scheduled, programmes: [programme]) }
30+
31+
it "does not render" do
32+
expect(component.render?).to be false
33+
end
34+
end
35+
36+
context "when the child has not been registered yet" do
37+
it { should have_heading("Register attendance") }
38+
it { should have_css("form") }
39+
it { should have_text("Yes, they are attending today’s session") }
40+
it { should have_text("No, they are absent from today’s session") }
41+
it { should have_button("Update attendance") }
42+
it { should_not have_link("Update attendance") }
43+
44+
context "when the user cannot edit" do
45+
before { stub_authorization(allowed: false) }
46+
47+
it { should have_text(patient.full_name) }
48+
it { should_not have_css("form") }
49+
end
50+
end
51+
52+
context "when the child is attending" do
53+
before do
54+
create(:patient_registration_status, :attending, patient:, session:)
55+
end
56+
57+
it { should have_heading("Register attendance") }
58+
59+
it do
60+
expect(rendered).to have_text(
61+
"#{patient.full_name} is attending today’s session."
62+
)
63+
end
64+
65+
it { should have_link("Update attendance") }
66+
it { should_not have_css("form") }
67+
68+
context "when the user cannot edit" do
69+
before { stub_authorization(allowed: false) }
70+
71+
it do
72+
expect(rendered).to have_text(
73+
"#{patient.full_name} is attending today’s session."
74+
)
75+
end
76+
77+
it { should_not have_link("Update attendance") }
78+
end
79+
end
80+
81+
context "when the child is absent" do
82+
before do
83+
create(:patient_registration_status, :not_attending, patient:, session:)
84+
end
85+
86+
it do
87+
expect(rendered).to have_text(
88+
"#{patient.full_name} is absent from today’s session."
89+
)
90+
end
91+
92+
it { should have_link("Update attendance") }
93+
it { should_not have_css("form") }
94+
95+
context "when the user cannot edit" do
96+
before { stub_authorization(allowed: false) }
97+
98+
it do
99+
expect(rendered).to have_text(
100+
"#{patient.full_name} is absent from today’s session."
101+
)
102+
end
103+
104+
it { should_not have_link("Update attendance") }
105+
end
106+
end
107+
108+
context "when the child has completed the session" do
109+
before do
110+
create(:patient_registration_status, :completed, patient:, session:)
111+
end
112+
113+
it { should have_text("#{patient.given_name} has completed this session.") }
114+
it { should_not have_link("Update registration") }
115+
it { should_not have_css("form") }
116+
end
117+
end

spec/features/manage_attendance_spec.rb

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
then_the_patient_is_not_registered_yet
2323
and_i_am_not_able_to_vaccinate
2424

25-
when_i_choose_the_patient_has_not_been_registered_yet
26-
then_the_patient_is_not_registered_yet
27-
and_i_see_the_not_registered_flash
28-
2925
when_i_choose_the_patient_is_absent
3026
then_the_patient_is_absent
3127
and_i_see_the_absent_flash
3228

29+
when_i_choose_the_patient_has_not_been_registered_yet
30+
then_the_patient_is_not_registered_yet
31+
and_i_see_the_not_registered_flash
32+
3333
when_i_choose_the_patient_is_attending
3434
then_the_patient_is_attending
3535
and_i_see_the_attending_flash
@@ -192,7 +192,7 @@ def and_i_go_to_a_patient
192192
end
193193

194194
def then_the_patient_is_not_registered_yet
195-
expect(page).to have_content("Not registered yet")
195+
expect(page).to have_content("registered yet")
196196
end
197197

198198
def and_i_am_not_able_to_vaccinate
@@ -210,13 +210,12 @@ def and_i_see_the_not_registered_flash
210210
end
211211

212212
def when_i_choose_the_patient_is_absent
213-
click_link "Update attendance"
214-
choose "No, they are absent"
215-
click_button "Save changes"
213+
choose "No, they are absent from today’s session"
214+
click_button "Update attendance"
216215
end
217216

218217
def then_the_patient_is_absent
219-
expect(page).to have_content("Absent from session")
218+
expect(page).to have_content("is absent from today’s session")
220219
end
221220

222221
def and_i_see_the_absent_flash
@@ -225,13 +224,12 @@ def and_i_see_the_absent_flash
225224
alias_method :then_i_see_the_absent_flash, :and_i_see_the_absent_flash
226225

227226
def when_i_choose_the_patient_is_attending
228-
click_link "Update attendance"
229227
choose "Yes, they are attending"
230-
click_button "Save changes"
228+
click_button "Update attendance"
231229
end
232230

233231
def then_the_patient_is_attending
234-
expect(page).to have_content("Attending session")
232+
expect(page).to have_content("is attending today’s session")
235233
end
236234

237235
def and_i_see_the_attending_flash

0 commit comments

Comments
 (0)