From 3759d4d34c1a17722c694796f5e469de3aa77572 Mon Sep 17 00:00:00 2001 From: Celia Collins Date: Tue, 2 Jan 2024 16:10:57 +0000 Subject: [PATCH] Save change access id in database --- app/controllers/land_owners_controller.rb | 18 +++---------- .../ownership_certificates_controller.rb | 25 +++++++------------ app/models/ownership_certificate.rb | 10 ++++++++ .../ownership_certificates/edit.html.erb | 3 +-- app/views/ownership_certificates/new.html.erb | 2 +- .../ownership_certificates/show.html.erb | 6 ++--- ...ange_access_id_to_ownership_certificate.rb | 7 ++++++ db/schema.rb | 3 ++- ...hip_certificate_validation_request_spec.rb | 9 ++++--- 9 files changed, 42 insertions(+), 41 deletions(-) create mode 100644 db/migrate/20240102155438_add_change_access_id_to_ownership_certificate.rb diff --git a/app/controllers/land_owners_controller.rb b/app/controllers/land_owners_controller.rb index 3a50018c..820a75b1 100644 --- a/app/controllers/land_owners_controller.rb +++ b/app/controllers/land_owners_controller.rb @@ -1,25 +1,23 @@ # frozen_string_literal: true class LandOwnersController < ApplicationController - before_action :set_planning_application, :set_certificate, :set_change_access_id + before_action :set_planning_application, :set_certificate def new @land_owner = LandOwner.new end def create - @land_owner = LandOwner.new(land_owner_params.except(:change_access_id)) + @land_owner = LandOwner.new(land_owner_params) if @land_owner.save redirect_to planning_application_ownership_certificate_path( @planning_application["id"], - @ownership_certificate, - change_access_id: land_owner_params[:change_access_id] + @ownership_certificate ) else respond_to do |format| format.html do - set_change_access_id render :new end end @@ -38,15 +36,7 @@ def set_certificate def land_owner_params params.require(:land_owner) - .permit(:name, :address_1, :address_2, :town, :country, :postcode, :notice_given_at, :change_access_id) + .permit(:name, :address_1, :address_2, :town, :country, :postcode, :notice_given_at) .to_h.merge(ownership_certificate_id: @ownership_certificate.id) end - - def set_change_access_id - @change_access_id = if params.has_key?(:change_access_id) - params[:change_access_id] - else - land_owner_params[:change_access_id] - end - end end diff --git a/app/controllers/ownership_certificates_controller.rb b/app/controllers/ownership_certificates_controller.rb index 02ea0fee..70df8a93 100644 --- a/app/controllers/ownership_certificates_controller.rb +++ b/app/controllers/ownership_certificates_controller.rb @@ -3,7 +3,7 @@ class OwnershipCertificatesController < ApplicationController before_action :set_planning_application before_action :set_certificate, except: %i[new create] - before_action :set_validation_request, only: %i[new submit edit] + before_action :set_validation_request, only: %i[new create submit] def new @ownership_certificate = OwnershipCertificate.new(planning_application_id: @planning_application["id"]) @@ -16,11 +16,11 @@ def edit end def update - if @ownership_certificate.update(ownership_certificate_params.except(:change_access_id)) + if @ownership_certificate.update(ownership_certificate_params) redirect_to planning_application_ownership_certificate_path( @planning_application["id"], @ownership_certificate, - change_access_id: ownership_certificate_params[:change_access_id] + change_access_id: @ownership_certificate.change_access_id ) else respond_to do |format| @@ -33,13 +33,12 @@ def update end def create - @ownership_certificate = OwnershipCertificate.new(ownership_certificate_params.except(:change_access_id)) + @ownership_certificate = OwnershipCertificate.new(ownership_certificate_params) if @ownership_certificate.save redirect_to planning_application_ownership_certificate_path( @planning_application["id"], - @ownership_certificate, - change_access_id: ownership_certificate_params[:change_access_id] + @ownership_certificate ) else respond_to do |format| @@ -55,19 +54,13 @@ def thank_you end def submit - change_access_id = if params.has_key?(:change_access_id) - params[:change_access_id] - else - ownership_certificate_params[:change_access_id] - end - if Bops::OwnershipCertificateValidationRequest.approve( @ownership_certificate_validation_request["id"], @planning_application["id"], - change_access_id, + @ownership_certificate.change_access_id, params: { certificate_type: @ownership_certificate.certificate_type, - land_owners_attributes: @ownership_certificate.land_owners + land_owners_attributes: @ownership_certificate.relevant_land_owners_attributes } ) @@ -94,13 +87,13 @@ def ownership_certificate_params end def set_validation_request - change_access_id = if params.has_key?(:change_access_id) + @change_access_id = if params.has_key?(:change_access_id) params[:change_access_id] else ownership_certificate_params[:change_access_id] end - validation_requests = Apis::Bops::Client.get_validation_requests(@planning_application["id"], change_access_id) + validation_requests = Apis::Bops::Client.get_validation_requests(@planning_application["id"], @change_access_id) @ownership_certificate_validation_request ||= validation_requests["data"]["ownership_certificate_validation_requests"].first end end diff --git a/app/models/ownership_certificate.rb b/app/models/ownership_certificate.rb index 09ef2afe..a295ca84 100644 --- a/app/models/ownership_certificate.rb +++ b/app/models/ownership_certificate.rb @@ -6,4 +6,14 @@ class OwnershipCertificate < ApplicationRecord accepts_nested_attributes_for :land_owners validates_presence_of :certificate_type + + def relevant_land_owners_attributes + if land_owners.any? + land_owners.map do |land_owner| + land_owner.attributes.slice("name", "address_1", "address_2", "town", "country", "postcode", "notice_given_at") + end + else + [] + end + end end diff --git a/app/views/ownership_certificates/edit.html.erb b/app/views/ownership_certificates/edit.html.erb index 8b64e012..ac5effd2 100644 --- a/app/views/ownership_certificates/edit.html.erb +++ b/app/views/ownership_certificates/edit.html.erb @@ -41,10 +41,9 @@

<% end %> <% end %> - <%= form.hidden_field :change_access_id, value: params[:change_access_id] %> <%= form.govuk_submit "Continue" %> <%= link_to "Back", - edit_ownership_certificate_validation_request_path(@ownership_certificate_validation_request["id"], change_access_id: params[:change_access_id], planning_application_id: @planning_application["id"]), + edit_ownership_certificate_validation_request_path(@ownership_certificate_validation_request["id"], change_access_id: @ownership_certificate.change_access_id, planning_application_id: @planning_application["id"]), class: "govuk-button govuk-button--secondary" %> <% end %> diff --git a/app/views/ownership_certificates/new.html.erb b/app/views/ownership_certificates/new.html.erb index 0b11a6a8..864907f4 100644 --- a/app/views/ownership_certificates/new.html.erb +++ b/app/views/ownership_certificates/new.html.erb @@ -40,7 +40,7 @@

<% end %> <% end %> - <%= form.hidden_field :change_access_id, value: params[:change_access_id] %> + <%= form.hidden_field :change_access_id, value: @change_access_id %> <%= form.govuk_submit "Continue" %> <%= link_to "Back", edit_ownership_certificate_validation_request_path(@ownership_certificate_validation_request["id"], change_access_id: params[:change_access_id], planning_application_id: @planning_application["id"]), diff --git a/app/views/ownership_certificates/show.html.erb b/app/views/ownership_certificates/show.html.erb index 544d1110..0d9a8465 100644 --- a/app/views/ownership_certificates/show.html.erb +++ b/app/views/ownership_certificates/show.html.erb @@ -65,7 +65,7 @@ <% end %> - <%= link_to "Add another owner", new_planning_application_ownership_certificate_land_owner_path(@planning_application["id"], @ownership_certificate, change_access_id: params[:change_access_id]), class: "govuk-button govuk-button--secondary" %> + <%= link_to "Add another owner", new_planning_application_ownership_certificate_land_owner_path(@planning_application["id"], @ownership_certificate, change_access_id: @ownership_certificate.change_access_id), class: "govuk-button govuk-button--secondary" %> <% end %>

@@ -76,7 +76,7 @@ the details you are providing are correct.

<%= hidden_field :change_access_id, value: params[:change_access_id] %> - <%= link_to "Accept and send", planning_application_ownership_certificate_submit_path(@planning_application["id"], @ownership_certificate, change_access_id: params[:change_access_id]), method: :post, class: "govuk-button" %> - <%= link_to "Back", edit_planning_application_ownership_certificate_path(@planning_application["id"], change_access_id: params[:change_access_id]), class: "govuk-button govuk-button--secondary" %> + <%= link_to "Accept and send", planning_application_ownership_certificate_submit_path(@planning_application["id"], @ownership_certificate, change_access_id: @ownership_certificate.change_access_id), method: :post, class: "govuk-button" %> + <%= link_to "Back", edit_planning_application_ownership_certificate_path(@planning_application["id"], change_access_id: @ownership_certificate.change_access_id), class: "govuk-button govuk-button--secondary" %> diff --git a/db/migrate/20240102155438_add_change_access_id_to_ownership_certificate.rb b/db/migrate/20240102155438_add_change_access_id_to_ownership_certificate.rb new file mode 100644 index 00000000..67afed67 --- /dev/null +++ b/db/migrate/20240102155438_add_change_access_id_to_ownership_certificate.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddChangeAccessIdToOwnershipCertificate < ActiveRecord::Migration[7.0] + def change + add_column :ownership_certificates, :change_access_id, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 936f8276..5f2abddc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_12_18_131345) do +ActiveRecord::Schema[7.0].define(version: 2024_01_02_155438) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -38,6 +38,7 @@ t.bigint "planning_application_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "change_access_id" end end diff --git a/spec/system/ownership_certificate_validation_request_spec.rb b/spec/system/ownership_certificate_validation_request_spec.rb index 53683af0..42730098 100644 --- a/spec/system/ownership_certificate_validation_request_spec.rb +++ b/spec/system/ownership_certificate_validation_request_spec.rb @@ -171,15 +171,18 @@ address_1: "Flat 1", address_2: "23 Street", town: "London", + country: "", postcode: "E16LT", - notice_given_at: "1/2/2023" + notice_given_at: "2023-02-01T00:00:00.000Z" }, { name: "Bob", address_1: "Flat 2", address_2: "24 Street", town: "London", - postcode: "E16LT" + country: "", + postcode: "E16LT", + notice_given_at: nil } ] } @@ -187,8 +190,6 @@ click_link "Accept and send" - stub_successful_get_change_requests - expect(page).to have_content "Thank you for confirming ownership" end