Skip to content

Commit

Permalink
Allow deleting payroll runs on review apps
Browse files Browse the repository at this point in the history
When testing we need to spin up a new review app for each payroll run we
want to test. This commit adds a button, for review apps and development
only, to allow admins to delete a payroll run on a review app.
  • Loading branch information
rjlynch committed Sep 27, 2024
1 parent 2dc4877 commit 8b34dbd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/controllers/admin/payroll_runs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,16 @@ def show
@payroll_run = PayrollRun.where(id: params[:id]).includes({claims: [:eligibility]}, {payments: [{claims: [:eligibility]}]}).first
@pagy, @payments = pagy(@payroll_run.payments.ordered.includes(claims: [:eligibility]).includes(:topups))
end

def destroy
if PayrollRun.allow_destroy?
PayrollRun.find(params[:id]).destroy!
redirect_to admin_payroll_runs_path, notice: "Payroll run deleted"
else
redirect_to(
admin_payroll_runs_path, alert: "Payroll run deletion is not allowed"
)
end
end
end
end
6 changes: 6 additions & 0 deletions app/models/payroll_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class PayrollRun < ApplicationRecord

scope :this_month, -> { where(created_at: DateTime.now.all_month) }

def self.allow_destroy?
ENV["ENVIRONMENT_NAME"].start_with?("review") ||
ENV["ENVIRONMENT_NAME"] == "test" ||
Rails.env.development?
end

def total_award_amount
payments.sum(:award_amount)
end
Expand Down
11 changes: 11 additions & 0 deletions app/views/admin/payroll_runs/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,15 @@

<%== render partial: 'pagination', locals: { pagy: @pagy } %>
</div>

<% if PayrollRun.allow_destroy? %>
<div class="govuk-grid-column-two-thirds">
<%= button_to(
"Delete payroll run",
admin_payroll_run_path(@payroll_run),
method: :delete,
class: "govuk-button govuk-button--warning",
) %>
</div>
<% end %>
</div>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def matches?(request)
resources :student_loans_data_uploads, only: [:new, :create]
resources :tps_data_uploads, only: [:new, :create]

resources :payroll_runs, only: [:index, :new, :create, :show] do
resources :payroll_runs, only: [:index, :new, :create, :show, :destroy] do
resources :payment_confirmation_report_uploads, only: [:new, :create]
resource :download, only: [:new, :create, :show], controller: "payroll_run_downloads"
resources :payments, only: [:destroy] do
Expand Down

0 comments on commit 8b34dbd

Please sign in to comment.