Skip to content

Commit

Permalink
Implement revisions to creating support claims flow
Browse files Browse the repository at this point in the history
This should fix the issue where a support user edits a draft claim and
with every edit, the draft claim is updated for everyone.

This will only `update` the draft claim when the support user actually
clicks `Update claim`.

They way this works is by using the `internal_draft` status. A generated
revision starts with `internal_draft` as its status and is changed to
draft when the support user finishes editing the draft claim. The
previous revision is then set to `internal_draft`, to not show more than
1 claim per reference.
  • Loading branch information
CatalinVoineag committed Apr 24, 2024
1 parent 413bb01 commit c9b9315
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ class Claims::Support::Schools::Claims::MentorTrainingsController < Claims::Appl
before_action :authorize_claim
helper_method :mentor_training_form

def edit; end
def edit
if create_revision?
@claim_revision = claim.create_revision!
end
end

def update
if mentor_training_form.save
Expand Down Expand Up @@ -32,11 +36,20 @@ def mentor_training_params
end

def default_params
{ claim:, mentor_training: }
{ claim: @claim_revision || claim, mentor_training: }
end

def mentor_training
@mentor_training ||= @claim.mentor_trainings.find(params.require(:id))
current_claim = @claim_revision || claim
mentor_training = current_claim.mentor_trainings.find_by_id(params.require(:id))

@mentor_training ||= if mentor_training.nil?
current_claim.mentor_trainings.find_by(
mentor_id: current_claim.previous_revision.mentor_trainings.find(params.require(:id)).mentor_id,
)
else
mentor_training
end
end

def claim
Expand All @@ -46,4 +59,8 @@ def claim
def authorize_claim
authorize claim
end

def create_revision?
params[:revision] == "true"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ class Claims::Support::Schools::Claims::MentorsController < Claims::ApplicationC
include Claims::BelongsToSchool
before_action :authorize_claim

helper_method :claim_mentors_form

def new; end
def new
render locals: { claim_mentors_form: }
end

def create
if claim_mentors_form.save
Expand All @@ -16,17 +16,25 @@ def create
),
)
else
render :new
render :new, locals: { claim_mentors_form: }
end
end

def edit; end
def edit
if create_revision?
revision = claim.create_revision!

render locals: { claim_mentors_form: claim_mentors_form(revision) }
else
render locals: { claim_mentors_form: }
end
end

def update
if claim_mentors_form.save
redirect_to claim_mentors_form.update_success_path
else
render :edit
render :edit, locals: { claim_mentors_form: }
end
end

Expand All @@ -40,16 +48,25 @@ def claim
@claim ||= @school.claims.find(params.require(:claim_id))
end

def claim_mentors_form
def claim_mentors_form(claim_revision = nil)
claim_param = claim_revision || claim

@claim_mentors_form ||=
if params[:claims_claim].present?
Claims::Support::Claim::MentorsForm.new(claim:, mentor_ids: claim_params[:mentor_ids])
Claims::Support::Claim::MentorsForm.new(
claim: claim_param,
mentor_ids: claim_params[:mentor_ids],
)
else
Claims::Support::Claim::MentorsForm.new(claim:)
Claims::Support::Claim::MentorsForm.new(claim: claim_param)
end
end

def authorize_claim
authorize claim
end

def create_revision?
params[:revision] == "true"
end
end
28 changes: 21 additions & 7 deletions app/controllers/claims/support/schools/claims_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,24 @@ def create
end

def check
last_mentor_training = @claim.mentor_trainings.order_by_mentor_full_name.last
@valid_claim = @claim.get_valid_revision
last_mentor_training = @valid_claim.mentor_trainings.order_by_mentor_full_name.last

@back_path = edit_claims_support_school_claim_mentor_training_path(
@school,
@claim,
@valid_claim,
last_mentor_training,
params: {
claims_support_claim_mentor_training_form: { hours_completed: last_mentor_training.hours_completed },
},
)
Claims::Claim::RemoveEmptyMentorTrainingHours.call(claim: @claim)
end

def edit; end
def edit
if create_revision?
@claim_revision = @claim.create_revision!
end
end

def update
if claim_provider_form.save
Expand All @@ -54,8 +58,14 @@ def update
end

def draft
success_message = @claim.draft? ? t(".update_success") : t(".add_success")
Claims::Claim::CreateDraft.call(claim: @claim)
claim_was_draft = @claim.was_draft?
success_message = claim_was_draft ? t(".update_success") : t(".add_success")

if claim_was_draft
Claims::Claim::UpdateDraft.call(claim: @claim)
else
Claims::Claim::CreateDraft.call(claim: @claim)
end

redirect_to claims_support_school_claims_path(@school), flash: { success: success_message }
end
Expand All @@ -73,7 +83,7 @@ def default_params
end

def claim_id
params[:claim_id] || params[:id]
@claim_revision&.id || params[:claim_id] || params[:id]
end

def set_claim
Expand All @@ -92,4 +102,8 @@ def claim_provider_form
def authorize_claim
authorize @claim || Claims::Claim
end

def create_revision?
params[:revision] == "true"
end
end
28 changes: 28 additions & 0 deletions app/services/claims/claim/update_draft.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Claims::Claim::UpdateDraft < Claims::Claim::CreateDraft
include ServicePattern

def call
ActiveRecord::Base.transaction do
updated_claim.save!
update_previous_revisions_to_internal_draft
end
end

private

def update_previous_revisions_to_internal_draft
claim_record = updated_claim.previous_revision

while claim_record.present?
claim_record.update!(status: :internal_draft) if claim_record.draft?
claim_record = claim_record.previous_revision
end
end

def updated_claim
@updated_claim ||= begin
claim.status = :draft
claim
end
end
end
13 changes: 11 additions & 2 deletions app/views/claims/support/claims/_details.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
<% row.with_value(text: claim.provider.name) %>
<% if policy(claim).edit? %>
<% row.with_action(text: t("change"),
href: edit_claims_support_school_claim_path(claim.school, claim),
href: edit_claims_support_school_claim_path(
claim.school,
claim,
params: { revision: true },
),
html_attributes: {
class: "govuk-link--no-visited-state",
}) %>
Expand All @@ -34,7 +38,11 @@
<% end %>
<% if policy(claim).edit? %>
<% row.with_action(text: t("change"),
href: edit_claims_support_school_claim_mentor_path(claim.school, claim),
href: edit_claims_support_school_claim_mentor_path(
claim.school,
claim,
params: { revision: true },
),
html_attributes: {
class: "govuk-link--no-visited-state",
}) %>
Expand All @@ -56,6 +64,7 @@
mentor_training,
params: {
claims_support_claim_mentor_training_form: { hours_completed: mentor_training.hours_completed },
revision: true,
},
),
html_attributes: {
Expand Down
21 changes: 13 additions & 8 deletions app/views/claims/support/schools/claims/check.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
<%= govuk_summary_list do |summary_list| %>
<% summary_list.with_row do |row| %>
<% row.with_key(text: Claims::Claim.human_attribute_name(:accredited_provider)) %>
<% row.with_value(text: @claim.provider_name) %>
<% row.with_value(text: @valid_claim.provider_name) %>
<% row.with_action(text: t("change"),
href: edit_claims_support_school_claim_path(
@school,
@claim,
@valid_claim,
params: { revision: true },
),
html_attributes: {
class: "govuk-link--no-visited-state",
Expand All @@ -31,13 +32,16 @@
<% row.with_key(text: Claims::Claim.human_attribute_name(:mentors)) %>
<% row.with_value do %>
<ul class="govuk-list">
<% @claim.mentors.each do |mentor| %>
<% @valid_claim.mentors.each do |mentor| %>
<li><%= mentor.full_name %></li>
<% end %>
</ul>
<% end %>
<% row.with_action(text: t("change"),
href: edit_claims_support_school_claim_mentor_path(@school, @claim),
href: edit_claims_support_school_claim_mentor_path(
@school, @valid_claim,
params: { revision: true }
),
html_attributes: {
class: "govuk-link--no-visited-state",
}) %>
Expand All @@ -47,17 +51,18 @@
<h2 class="govuk-heading-m"><%= t(".hours_of_training") %></h2>

<%= govuk_summary_list do |summary_list| %>
<% @claim.mentor_trainings.order_by_mentor_full_name.each do |mentor_training| %>
<% @valid_claim.mentor_trainings.order_by_mentor_full_name.each do |mentor_training| %>
<% summary_list.with_row do |row| %>
<% row.with_key(text: mentor_training.mentor.full_name) %>
<% row.with_value(text: t(".hours", hours: mentor_training.hours_completed)) %>
<% row.with_action(text: t("change"),
href: edit_claims_support_school_claim_mentor_training_path(
@school,
@claim,
@valid_claim,
mentor_training,
params: {
claims_support_claim_mentor_training_form: { hours_completed: mentor_training.hours_completed },
revision: true,
},
),
html_attributes: {
Expand All @@ -71,11 +76,11 @@
<%= govuk_summary_list do |summary_list| %>
<% summary_list.with_row do |row| %>
<% row.with_key(text: Claims::Claim.human_attribute_name(:claim_amount)) %>
<% row.with_value(text: humanized_money_with_symbol(@claim.amount)) %>
<% row.with_value(text: humanized_money_with_symbol(@valid_claim.amount)) %>
<% end %>
<% end %>

<%= govuk_button_to (@claim.draft? ? t(".update") : t(".submit")), draft_claims_support_school_claim_path(@school, @claim) %>
<%= govuk_button_to (@valid_claim.was_draft? ? t(".update") : t(".submit")), draft_claims_support_school_claim_path(@school, @valid_claim) %>

<p class="govuk-body">
<%= govuk_link_to t("cancel"), claims_support_school_claims_path(@school), no_visited_state: true %>
Expand Down
19 changes: 19 additions & 0 deletions spec/services/claims/claim/update_draft_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "rails_helper"

describe Claims::Claim::UpdateDraft do
subject(:draft_service) { described_class.call(claim:) }

let!(:previous_revision) { create(:claim, :draft) }
let!(:claim) { create(:claim, :internal_draft, previous_revision:) }

it_behaves_like "a service object" do
let(:params) { { claim: } }
end

describe "#call" do
it "updates draft claim and sets the previous revisions to internal_draft status" do
expect { draft_service }.to change(claim, :status).from("internal_draft").to("draft")
expect(claim.previous_revision.status).to eq("internal_draft")
end
end
end
Loading

0 comments on commit c9b9315

Please sign in to comment.