diff --git a/app/controllers/vaccinations_controller.rb b/app/controllers/vaccinations_controller.rb index 581c71902..c7b8487d9 100644 --- a/app/controllers/vaccinations_controller.rb +++ b/app/controllers/vaccinations_controller.rb @@ -56,9 +56,12 @@ def create &:reset! ) - @draft_vaccination_record.assign_attributes(create_params) + @draft_vaccination_record.patient_session = @patient_session - if @draft_vaccination_record.save + if create_params.nil? + @draft_vaccination_record.errors.add(:administered, :blank) + render "patient_sessions/show", status: :unprocessable_entity + elsif @draft_vaccination_record.update(create_params) steps = @draft_vaccination_record.wizard_steps steps.delete(:date_and_time) @@ -130,17 +133,16 @@ def vaccination_record_params :programme_id, :vaccine_id ) - .merge( - patient_session: @patient_session, - performed_at: Time.current, - performed_by_user: current_user - ) + .merge(performed_at: Time.current, performed_by_user: current_user) end def create_params - if vaccination_record_params[:administered] == "true" + administered = vaccination_record_params[:administered] + return if administered.blank? + + if administered == "true" create_params = - if delivery_site_param_other? + if vaccination_record_params[:delivery_site] == "other" vaccination_record_params.except(:delivery_site, :delivery_method) else vaccination_record_params @@ -155,10 +157,6 @@ def create_params end end - def delivery_site_param_other? - vaccination_record_params[:delivery_site] == "other" - end - def set_session @session = policy_scope(Session).includes(:location).find_by!( diff --git a/app/models/draft_vaccination_record.rb b/app/models/draft_vaccination_record.rb index 2f2ce5ed3..b5f9883b9 100644 --- a/app/models/draft_vaccination_record.rb +++ b/app/models/draft_vaccination_record.rb @@ -96,9 +96,13 @@ def wizard_steps end def administered? + return nil if outcome.nil? outcome == "administered" end + # So that a form error matches to a field in this model + alias_method :administered, :administered? + def batch BatchPolicy::Scope.new(@current_user, Batch).resolve.find_by(id: batch_id) end diff --git a/config/locales/en.yml b/config/locales/en.yml index 5b5824a83..4c418a4d2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -192,7 +192,7 @@ en: draft_vaccination_record: attributes: administered: - inclusion: Choose if they got the vaccine + blank: Choose if they got the vaccine batch_id: blank: Choose a batch incorrect_vaccine: Choose a batch of the %{vaccine_brand} vaccine