Skip to content

Commit

Permalink
Save change access id in database
Browse files Browse the repository at this point in the history
  • Loading branch information
cdccollins committed Jan 3, 2024
1 parent 9f4549b commit 3759d4d
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 41 deletions.
18 changes: 4 additions & 14 deletions app/controllers/land_owners_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
25 changes: 9 additions & 16 deletions app/controllers/ownership_certificates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand All @@ -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|
Expand All @@ -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|
Expand All @@ -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
}
)

Expand All @@ -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
10 changes: 10 additions & 0 deletions app/models/ownership_certificate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions app/views/ownership_certificates/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@
</p>
<% 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 %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/ownership_certificates/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</p>
<% 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"]),
Expand Down
6 changes: 3 additions & 3 deletions app/views/ownership_certificates/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<% end %>
</tbody>
</table>
<%= 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 %>

<h3 class="govuk-heading-s">
Expand All @@ -76,7 +76,7 @@
the details you are providing are correct.
</p>
<%= 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" %>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
9 changes: 5 additions & 4 deletions spec/system/ownership_certificate_validation_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,24 +171,25 @@
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
}
]
}
)

click_link "Accept and send"

stub_successful_get_change_requests

expect(page).to have_content "Thank you for confirming ownership"
end

Expand Down

0 comments on commit 3759d4d

Please sign in to comment.