From 8b34dbd6565844c0e68e00f9570c4bbc1000ea97 Mon Sep 17 00:00:00 2001 From: Richard Lynch Date: Wed, 25 Sep 2024 16:46:48 +0100 Subject: [PATCH] Allow deleting payroll runs on review apps 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. --- app/controllers/admin/payroll_runs_controller.rb | 11 +++++++++++ app/models/payroll_run.rb | 6 ++++++ app/views/admin/payroll_runs/show.html.erb | 11 +++++++++++ config/routes.rb | 2 +- 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/payroll_runs_controller.rb b/app/controllers/admin/payroll_runs_controller.rb index 8b0c0524f2..1604cd1a3a 100644 --- a/app/controllers/admin/payroll_runs_controller.rb +++ b/app/controllers/admin/payroll_runs_controller.rb @@ -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 diff --git a/app/models/payroll_run.rb b/app/models/payroll_run.rb index c65d603c56..2c6adde813 100644 --- a/app/models/payroll_run.rb +++ b/app/models/payroll_run.rb @@ -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 diff --git a/app/views/admin/payroll_runs/show.html.erb b/app/views/admin/payroll_runs/show.html.erb index 9a050dc397..fee1b631b5 100644 --- a/app/views/admin/payroll_runs/show.html.erb +++ b/app/views/admin/payroll_runs/show.html.erb @@ -157,4 +157,15 @@ <%== render partial: 'pagination', locals: { pagy: @pagy } %> + + <% if PayrollRun.allow_destroy? %> +
+ <%= button_to( + "Delete payroll run", + admin_payroll_run_path(@payroll_run), + method: :delete, + class: "govuk-button govuk-button--warning", + ) %> +
+ <% end %> diff --git a/config/routes.rb b/config/routes.rb index 3eab37bc12..b8b19f1d62 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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