diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 233b3f383..32da14ea7 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -23,77 +23,114 @@ class LocationsController < ApplicationController def postcode if params[:location].present? @location.postcode = params[:location][:postcode] - @location.save! - if @location.location_code.blank? || @location.location_admin_district.blank? - redirect_to scheme_location_local_authority_path(@scheme, @location, referrer: params[:referrer]) - elsif params[:referrer] == "check_answers" - redirect_to scheme_location_check_answers_path(@scheme, @location) + @location.location_admin_district = nil + @location.location_code = nil + @location.validate_postcode + if @location.errors.blank? + @location.save! + if @location.location_code.blank? || @location.location_admin_district.blank? + redirect_to scheme_location_local_authority_path(@scheme, @location, referrer: params[:referrer]) + elsif params[:referrer] == "check_answers" + redirect_to scheme_location_check_answers_path(@scheme, @location) + else + redirect_to scheme_location_name_path(@scheme, @location) + end else - redirect_to scheme_location_name_path(@scheme, @location) + render :postcode, status: :unprocessable_entity end end end def local_authority if params[:location].present? - @location.location_admin_district = params[:location][:location_admin_district] - @location.location_code = Location.local_authorities.key(params[:location][:location_admin_district]) - @location.save! - if params[:referrer] == "check_answers" - redirect_to scheme_location_check_answers_path(@scheme, @location) + if params[:location][:location_admin_district] != "Select an option" + @location.location_admin_district = params[:location][:location_admin_district] + @location.location_code = Location.local_authorities.key(params[:location][:location_admin_district]) + @location.save! + if params[:referrer] == "check_answers" + redirect_to scheme_location_check_answers_path(@scheme, @location) + else + redirect_to scheme_location_name_path(@scheme, @location) + end else - redirect_to scheme_location_name_path(@scheme, @location) + error_message = I18n.t("validations.location_admin_district") + @location.errors.add :local_authority, error_message + render :local_authority, status: :unprocessable_entity end end end def name if params[:location].present? - @location.name = params[:location][:name] - @location.save! - case params[:referrer] - when "check_answers" - redirect_to scheme_location_check_answers_path(@scheme, @location) - when "details" - redirect_to scheme_location_path(@scheme, @location) + if params[:location][:name].present? + @location.name = params[:location][:name] + @location.save! + case params[:referrer] + when "check_answers" + redirect_to scheme_location_check_answers_path(@scheme, @location) + when "details" + redirect_to scheme_location_path(@scheme, @location) + else + redirect_to scheme_location_units_path(@scheme, @location) + end else - redirect_to scheme_location_units_path(@scheme, @location) + error_message = I18n.t("validations.location.name") + @location.errors.add :name, error_message + render :name, status: :unprocessable_entity end end end def units if params[:location].present? - @location.units = params[:location][:units] - @location.save! - if params[:referrer] == "check_answers" - redirect_to scheme_location_check_answers_path(@scheme, @location) + if params[:location][:units].present? + @location.units = params[:location][:units] + @location.save! + if params[:referrer] == "check_answers" + redirect_to scheme_location_check_answers_path(@scheme, @location) + else + redirect_to scheme_location_type_of_unit_path(@scheme, @location) + end else - redirect_to scheme_location_type_of_unit_path(@scheme, @location) + error_message = I18n.t("validations.location.units") + @location.errors.add :units, error_message + render :units, status: :unprocessable_entity end end end def type_of_unit if params[:location].present? - @location.type_of_unit = params[:location][:type_of_unit] - @location.save! - if params[:referrer] == "check_answers" - redirect_to scheme_location_check_answers_path(@scheme, @location) + if params[:location][:type_of_unit].present? + @location.type_of_unit = params[:location][:type_of_unit] + @location.save! + if params[:referrer] == "check_answers" + redirect_to scheme_location_check_answers_path(@scheme, @location) + else + redirect_to scheme_location_mobility_standards_path(@scheme, @location) + end else - redirect_to scheme_location_mobility_standards_path(@scheme, @location) + error_message = I18n.t("validations.location.type_of_unit") + @location.errors.add :type_of_unit, error_message + render :type_of_unit, status: :unprocessable_entity end end end def mobility_standards if params[:location].present? - @location.mobility_type = params[:location][:mobility_type] - @location.save! - if params[:referrer] == "check_answers" - redirect_to scheme_location_check_answers_path(@scheme, @location) + if params[:location][:mobility_type].present? + @location.mobility_type = params[:location][:mobility_type] + @location.save! + if params[:referrer] == "check_answers" + redirect_to scheme_location_check_answers_path(@scheme, @location) + else + redirect_to scheme_location_availability_path(@scheme, @location) + end else - redirect_to scheme_location_availability_path(@scheme, @location) + error_message = I18n.t("validations.location.mobility_standards") + @location.errors.add :mobility_type, error_message + render :mobility_standards, status: :unprocessable_entity end end end @@ -103,9 +140,21 @@ class LocationsController < ApplicationController day = params[:location]["startdate(3i)"] month = params[:location]["startdate(2i)"] year = params[:location]["startdate(1i)"] - @location.startdate = Time.zone.local(year.to_i, month.to_i, day.to_i) - @location.save! - redirect_to scheme_location_check_answers_path(@scheme, @location) + if [day, month, year].none?(&:blank?) + if Date.valid_date?(day.to_i, month.to_i, year.to_i) + @location.startdate = Time.zone.local(year.to_i, month.to_i, day.to_i) + @location.save! + redirect_to scheme_location_check_answers_path(@scheme, @location) + else + error_message = I18n.t("validations.location.availability_invalid") + @location.errors.add :startdate, error_message + render :availability, status: :unprocessable_entity + end + else + error_message = I18n.t("validations.location.availability_blank") + @location.errors.add :startdate, error_message + render :availability, status: :unprocessable_entity + end end end diff --git a/app/models/location.rb b/app/models/location.rb index 221cba7d5..f91abbb2b 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -1,6 +1,4 @@ class Location < ApplicationRecord - # validate :validate_postcode - # validates :units, :type_of_unit, :mobility_type, presence: true belongs_to :scheme has_many :lettings_logs, class_name: "LettingsLog" has_many :location_deactivation_periods, class_name: "LocationDeactivationPeriod" @@ -393,10 +391,6 @@ class Location < ApplicationRecord status == :reactivating_soon end -private - - PIO = PostcodeService.new - def validate_postcode if postcode.nil? || !postcode&.match(POSTCODE_REGEXP) error_message = I18n.t("validations.postcode") @@ -404,6 +398,10 @@ private end end +private + + PIO = PostcodeService.new + def lookup_postcode! result = PIO.lookup(postcode) if result diff --git a/config/locales/en.yml b/config/locales/en.yml index 674d31485..886b540c4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -325,6 +325,12 @@ en: location: + name: "Enter the name of the location" + units: "The units at this location must be a number" + type_of_unit: "Select the most common type of unit at this location" + mobility_standards: "Select the mobility standard for the majority of the units at this location" + availability_blank: "Enter the day, month and year when the first property became available at this location" + availability_invalid: "Enter a valid day, month and year when the first property became available at this location" toggle_date: not_selected: "Select one of the options" invalid: "Enter a valid day, month and year"