Skip to content

Commit e26d02b

Browse files
Merge pull request #5821 from nhsuk/alistair/national-reporting-edit-vaccine-and-batch
Allow national reporting users to edit vaccine and batch
2 parents 0601532 + 597b0d1 commit e26d02b

10 files changed

Lines changed: 386 additions & 24 deletions

File tree

app/components/app_vaccination_record_summary_component.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ def call
5151
if @vaccine
5252
row.with_value { vaccine_value }
5353

54-
if (href = @change_links[:vaccine])
54+
if @current_user.selected_team.has_upload_only_access?
55+
row.with_action(
56+
text: "Change",
57+
href: @change_links[:batch],
58+
visually_hidden_text: "vaccine"
59+
)
60+
elsif (href = @change_links[:vaccine])
5561
row.with_action(
5662
text: "Change",
5763
visually_hidden_text: "vaccine",
@@ -89,6 +95,14 @@ def call
8995
summary_list.with_row do |row|
9096
row.with_key { "Batch expiry date" }
9197
row.with_value { batch_expiry_value }
98+
99+
if @current_user.selected_team.has_upload_only_access?
100+
row.with_action(
101+
text: "Change",
102+
href: @change_links[:batch],
103+
visually_hidden_text: "batch expiry date"
104+
)
105+
end
92106
end
93107
end
94108

@@ -325,7 +339,13 @@ def programme_value
325339
end
326340

327341
def vaccine_value
328-
highlight_if(@vaccine.brand, @vaccination_record.vaccine_id_changed?)
342+
display_name =
343+
if @current_user.selected_team.has_upload_only_access?
344+
@vaccine.nivs_name.presence || @vaccine.brand
345+
else
346+
@vaccine.brand
347+
end
348+
highlight_if(display_name, @vaccination_record.vaccine_id_changed?)
329349
end
330350

331351
def delivery_method_value

app/controllers/draft_vaccination_records_controller.rb

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ class DraftVaccinationRecordsController < ApplicationController
1414
include WizardControllerConcern
1515

1616
before_action :validate_params, only: :update
17-
before_action :set_batches, if: -> { current_step == :batch }
17+
before_action :set_batches,
18+
if: -> do
19+
current_step == :batch &&
20+
!@draft_vaccination_record.bulk_upload_user_and_record?
21+
end
22+
before_action :set_vaccines,
23+
if: -> do
24+
current_step == :batch &&
25+
@draft_vaccination_record.bulk_upload_user_and_record?
26+
end
1827
before_action :set_locations, if: -> { current_step == :location }
1928
before_action :set_supplied_by_users, if: -> { current_step == :supplier }
2029
before_action :set_back_link_path
@@ -73,6 +82,20 @@ def validate_params
7382
@draft_vaccination_record.errors.add(:performed_at, :invalid)
7483
render_wizard nil, status: :unprocessable_content
7584
end
85+
elsif current_step == :batch &&
86+
@draft_vaccination_record.bulk_upload_user_and_record?
87+
validator =
88+
DateParamsValidator.new(
89+
field_name: :batch_expiry,
90+
object: @draft_vaccination_record,
91+
params: update_params
92+
)
93+
94+
unless validator.date_params_valid?
95+
@draft_vaccination_record.errors.add(:batch_expiry, :invalid)
96+
set_vaccines
97+
render_wizard nil, status: :unprocessable_content
98+
end
7699
end
77100
end
78101

@@ -176,7 +199,7 @@ def finish_wizard_path
176199

177200
def update_params
178201
permitted_attributes = {
179-
batch: %i[batch_id],
202+
batch: %i[batch_id vaccine_id batch_name batch_expiry],
180203
confirm: @draft_vaccination_record.editing? ? [] : %i[notes],
181204
date_and_time: %i[performed_at],
182205
delivery: %i[delivery_site delivery_method],
@@ -231,6 +254,10 @@ def set_steps
231254
self.steps = @draft_vaccination_record.wizard_steps
232255
end
233256

257+
def set_vaccines
258+
@vaccines = @programme.vaccines.select(&:nivs_name)
259+
end
260+
234261
def set_batches
235262
vaccines = vaccine_criteria.apply(@programme.vaccines)
236263

app/forms/batch_form.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,7 @@ class BatchForm
99
attribute :name, :string
1010
attribute :expiry, :date
1111

12-
NAME_FORMAT = /\A[A-Za-z0-9]+\z/
13-
14-
validates :name,
15-
presence: true,
16-
format: {
17-
with: NAME_FORMAT
18-
},
19-
length: {
20-
minimum: 2,
21-
maximum: 100
22-
}
12+
validates :name, batch_name: true
2313

2414
validates :expiry,
2515
comparison: {

app/models/draft_vaccination_record.rb

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class DraftVaccinationRecord
1010
include VaccinationRecordPerformedByConcern
1111

1212
attribute :batch_id, :integer
13+
attribute :batch_name, :string
14+
attribute :batch_expiry, :date
1315
attribute :delivery_method, :string
1416
attribute :delivery_site, :string
1517
attribute :disease_types, array: true
@@ -34,6 +36,7 @@ class DraftVaccinationRecord
3436
attribute :session_id, :integer
3537
attribute :source, :string
3638
attribute :supplied_by_user_id, :integer
39+
attribute :vaccine_id, :integer
3740

3841
def initialize(current_user:, **attributes)
3942
@current_user = current_user
@@ -83,7 +86,11 @@ def wizard_steps
8386
end
8487

8588
on_wizard_step :batch, exact: true do
86-
validates :batch_id, presence: true
89+
validates :batch_id, presence: true, unless: :bulk_upload_user_and_record?
90+
91+
validates :vaccine_id, presence: true, if: :bulk_upload_user_and_record?
92+
validates :batch_name, batch_name: true, if: :bulk_upload_user_and_record?
93+
validates :batch_expiry, presence: true, if: :bulk_upload_user_and_record?
8794
end
8895

8996
on_wizard_step :dose, exact: true do
@@ -155,6 +162,17 @@ def already_had?
155162
alias_method :administered, :administered?
156163

157164
def batch
165+
if batch_expiry && batch_name && vaccine_id && bulk_upload_user_and_record?
166+
return(
167+
Batch.create_with(archived_at: Time.current).find_or_create_by!(
168+
expiry: batch_expiry,
169+
name: batch_name,
170+
team_id: nil,
171+
vaccine_id: vaccine_id
172+
)
173+
)
174+
end
175+
158176
return nil if batch_id.nil?
159177
Batch.find(batch_id)
160178
end
@@ -236,8 +254,6 @@ def delivery_method=(value)
236254

237255
delegate :vaccine, to: :batch, allow_nil: true
238256

239-
delegate :id, to: :vaccine, prefix: true, allow_nil: true
240-
241257
def vaccine_id_changed? = batch_id_changed?
242258

243259
def location_is_school
@@ -331,6 +347,24 @@ def bulk_upload_user_and_record?
331347
sourced_from_bulk_upload?
332348
end
333349

350+
def read_from!(vaccination_record)
351+
self.batch_name = vaccination_record.batch&.name
352+
self.batch_expiry = vaccination_record.batch&.expiry
353+
self.vaccine_id = vaccination_record.vaccine&.id
354+
355+
super(vaccination_record)
356+
end
357+
358+
def write_to!(vaccination_record)
359+
super(vaccination_record)
360+
361+
if batch_expiry && batch_name && vaccine_id && bulk_upload_user_and_record?
362+
vaccination_record.batch_id = batch&.id
363+
end
364+
365+
vaccination_record.vaccine_id = batch&.vaccine_id
366+
end
367+
334368
private
335369

336370
def readable_attribute_names

app/models/immunisation_import_row.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ def validate_batch_name
630630
errors.add(batch_name.header, "must be at most 100 characters long")
631631
elsif batch_name.to_s.length < 2
632632
errors.add(batch_name.header, "must be at least 2 characters long")
633-
elsif batch_name.to_s !~ BatchForm::NAME_FORMAT
633+
elsif batch_name.to_s !~ BatchNameValidator::FORMAT
634634
errors.add(batch_name.header, "must be only letters and numbers")
635635
end
636636
elsif offline_recording? || bulk?
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
class BatchNameValidator < ActiveModel::EachValidator
4+
FORMAT = /\A[A-Za-z0-9]+\z/
5+
MIN_LENGTH = 2
6+
MAX_LENGTH = 100
7+
8+
def validate_each(record, attribute, value)
9+
if value.blank?
10+
record.errors.add(attribute, :blank)
11+
elsif value.length < MIN_LENGTH
12+
record.errors.add(attribute, :too_short, count: MIN_LENGTH)
13+
elsif value.length > MAX_LENGTH
14+
record.errors.add(attribute, :too_long, count: MAX_LENGTH)
15+
elsif value !~ FORMAT
16+
record.errors.add(attribute, :invalid)
17+
end
18+
end
19+
end

app/views/draft_vaccination_records/batch.html.erb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@
77
<%= form_with model: @draft_vaccination_record, url: wizard_path, method: :put do |f| %>
88
<%= f.govuk_error_summary %>
99

10+
<% if @draft_vaccination_record.bulk_upload_user_and_record? %>
11+
12+
<span class="nhsuk-caption-l"><%= @patient.full_name %></span>
13+
<%= h1 "Which vaccine and batch did you use?" %>
14+
15+
<%= f.govuk_radio_buttons_fieldset :vaccine_id,
16+
legend: { text: "Vaccine", size: "m" } do %>
17+
<% @vaccines.each do |vaccine| %>
18+
<%= f.govuk_radio_button :vaccine_id, vaccine.id, label: { text: vaccine.nivs_name } %>
19+
<% end %>
20+
<% end %>
21+
22+
<%= f.govuk_text_field :batch_name,
23+
label: { text: "Batch number", size: "m" }, width: 10, class: "nhsuk-input--code" %>
24+
25+
<%= f.govuk_date_field :batch_expiry,
26+
legend: { text: "Batch expiry date", size: "m" },
27+
hint: { text: "For example, 27 10 2025" } %>
28+
<% else %>
1029
<%= f.govuk_radio_buttons_fieldset :batch_id,
1130
caption: { text: @patient.full_name, size: "l" },
1231
legend: { size: "l", tag: "h1", text: "Which batch did you use?" } do %>
@@ -31,6 +50,7 @@
3150
<% end %>
3251
<% end %>
3352
<% end %>
53+
<% end %>
3454

3555
<%= f.govuk_submit "Continue" %>
3656
<% end %>

config/locales/en.yml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ en:
1313
missing_year: Enter a year
1414
taken: This batch already exists
1515
name:
16-
blank: Enter a batch
17-
invalid: Enter a batch with only letters and numbers
18-
too_short: Enter a batch that is more than %{count} characters long
19-
too_long: Enter a batch that is less than %{count} characters long
16+
blank: Enter a batch number
17+
invalid: Enter a batch number with only letters and numbers
18+
too_short: Enter a batch number that is more than %{count} characters long
19+
too_long: Enter a batch number that is less than %{count} characters long
2020
bulk_remove_parents_form:
2121
attributes:
2222
remove_option:
@@ -114,6 +114,16 @@ en:
114114
attributes:
115115
batch_id:
116116
blank: Choose a batch
117+
batch_name:
118+
blank: Enter a batch number
119+
invalid: Enter a batch number with only letters and numbers
120+
too_short: Enter a batch number that is more than %{count} characters long
121+
too_long: Enter a batch number that is less than %{count} characters long
122+
batch_expiry:
123+
blank: Enter an expiry date
124+
missing_day: Enter a day
125+
missing_month: Enter a month
126+
missing_year: Enter a year
117127
delivery_method:
118128
blank: Choose a method of delivery
119129
inclusion: Choose a method of delivery

0 commit comments

Comments
 (0)