Skip to content

Commit

Permalink
add eligibility bulk assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamfall committed Sep 24, 2024
1 parent f3ee276 commit e0cea90
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 8 deletions.
5 changes: 5 additions & 0 deletions app/assets/stylesheets/admin/page-applications.scss
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,8 @@
.js-remove-audit-certificate-link span {
margin-top: 0;
}

.govuk-bulk-error-message {
margin-top: 20px;
color: rgb(212, 53, 28);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Admin::EligibilityAssignmentCollectionsController < Admin::BaseController
def create
@form = EligibilityAssignmentCollection.new(create_params)
authorize @form, :create?

@form.subject = current_subject

if @form.save
redirect_to admin_form_answers_path(year: params[:year], search_id: params[:search_id]), notice: @form.notice_message
else
# Ensure form_answer_ids is an array
@form.form_answer_ids = @form.form_answer_ids.split(",") if @form.form_answer_ids.is_a?(String)
render "admin/form_answers/bulk_assign_eligibility"
end
end

private

def create_params
params.require(:eligibility_assignment_collection)
.permit(:form_answer_ids, :state)
end
end
12 changes: 10 additions & 2 deletions app/controllers/admin/form_answers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,20 +262,28 @@ def bulk_assign_lieutenants
# get form_answer_ids after validation error
params[:lieutenant_assignment_collection][:form_answer_ids]
end

@form = LieutenantAssignmentCollection.new(form_answer_ids: form_answer_ids, params: bulk_assign_params)
@form.form_answer_ids = form_answer_ids
end

def bulk_assign_assessors
authorize :assessor_assignment_collection, :create?

form_answer_ids = params[:bulk_action][:ids]
@form = AssessorAssignmentCollection.new(form_answer_ids: form_answer_ids)
@form.form_answer_ids = form_answer_ids
end

def bulk_assign_eligibility
authorize :eligibility_assignment_collection, :create?

form_answer_ids = if params[:bulk_action]
params[:bulk_action][:ids]
elsif params[:lieutenant_assignment_collection]
# get form_answer_ids after validation error
params[:lieutenant_assignment_collection][:form_answer_ids]
end

@form = EligibilityAssignmentCollection.new(form_answer_ids: form_answer_ids)
end

# / batch actions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ def create

def create_params
params.require(:lieutenant_assignment_collection)
.permit(:form_answer_ids, :ceremonial_county_id, :year)
.permit(:form_answer_ids, :ceremonial_county_id)
end
end
5 changes: 3 additions & 2 deletions app/controllers/concerns/form_answer_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def save_or_load_search!
:year,
:bulk_assign_lieutenants,
:bulk_assign_assessors,
# :bulk_assign_eligibility,
:bulk_assign_eligibility,
search: {},
bulk_action: { ids: [] }
)
Expand All @@ -110,7 +110,8 @@ def save_or_load_search!

unless @processor.valid?
# raise "Invalid bulk action parameters: #{params.to_yaml}\nErrors: #{@processor.errors.full_messages.to_yaml}"
redirect_to admin_form_answers_path(year: params[:year], search_id: @processor.search_id), notice: "You must select at least one group from the list below before clicking a bulk action button." and return
flash[:bulk_error] = @processor.base_error_messages
redirect_to admin_form_answers_path(year: params[:year], search_id: @processor.search_id) and return
end

redirect_url = @processor.redirect_url
Expand Down
31 changes: 31 additions & 0 deletions app/models/eligibility_assignment_collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class EligibilityAssignmentCollection < AssignmentCollection
VALID_STATES = %w[submitted admin_pending_eligibility].freeze


attribute :state, String

validates :state, presence: true, inclusion: { in: FormAnswerStateMachine::ELIGIBILITY_STATES.map(&:to_s) }

validate :form_answers_state_validation

def save
return unless valid?
form_answers.update_all(state: state)
end

def notice_message
if ids.length > 1
"Groups have"
else
"Group has"
end.concat " been assigned the #{state} status."
end

private

def form_answers_state_validation
unless form_answers.all? { |form_answer| VALID_STATES.include?(form_answer.state) }
errors.add(:form_answer_ids, "To assign the eligibility status, you must only select groups that currently have ‘Nomination submitted’ or ‘Eligibility pending’ status.")
end
end
end
5 changes: 5 additions & 0 deletions app/policies/eligibility_assignment_collection_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class EligibilityAssignmentCollectionPolicy < ApplicationPolicy
def create?
admin?
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
strong
' Bulk actions:
' select groups from the list below and click an appropriate bulk action button.
- if flash[:bulk_error].present?
p.govuk-body.govuk-bulk-error-message
= flash[:bulk_error]
.bulk-assignment-buttons-container
= f.submit "Bulk assign to Lord Lieutenancy office", class: "govuk-button bulk-assign-lieutenants-link", name: "bulk_assign_lieutenants"
= f.submit "Bulk assign to national assessor sub-group", class: "govuk-button bulk-assign-assessors-link", name: "bulk_assign_assessors"
Expand Down
24 changes: 24 additions & 0 deletions app/views/admin/form_answers/bulk_assign_eligibility.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

h3.govuk-heading-m
| Assign eligibility status to selected groups

= simple_form_for [namespace_name, @form] do |f|
.govuk-form-group.primary-subgroup-group
= f.input :state,
as: :radio_buttons,
collection: FormAnswerStateMachine::ELIGIBILITY_STATES.map { |s| [t(s, scope: "form_answers.state"), s] },
input_html: { class: "admin-eligibility-option" },
label: "Select the eligibility status",
label_html: { class: "govuk-!-font-size-19 govuk-!-margin-bottom-0" }
- if @form.errors[:sub_group].any?
.govuk-error-message
| Please select a national assessor sub-group

= f.hidden_field :search_id, value: params[:search_id], name: :search_id
= f.hidden_field :year, value: params[:year], name: :year

= f.hidden_field :form_answer_ids, value: @form.form_answer_ids.join(",")

.govuk-button-group
= f.submit "Bulk assign eligibility status", class: "govuk-button"
= link_to "Cancel", [namespace_name, :form_answers, year: params[:year], search_id: params[:search_id]], class: "govuk-link"
3 changes: 1 addition & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,6 @@
%w[bulk_assign_lieutenants bulk_assign_assessors bulk_assign_eligibility].each do |action|
get action
post action
get "#{action}_confirm"
post "#{action}_confirm"
end
end

Expand Down Expand Up @@ -271,6 +269,7 @@

resources :lieutenant_assignment_collections, only: [:create]
resources :assessor_assignment_collections, only: [:create]
resources :eligibility_assignment_collections, only: [:create]

resource :account, only: [:edit] do
collection do
Expand Down
21 changes: 20 additions & 1 deletion forms/nominations_bulk_action_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ def valid?
end

case @kind
when "lieutenants", "assessors", "eligibility"
when "lieutenants", "assessors"
params[:bulk_action].present?
when "eligibility"
params[:bulk_action].present? && form_answers_eligible_for_state_change?
else
@errors.add(:bulk_base, "Invalid bulk action type.")
false
end
end

def base_error_messages
@errors[:bulk_base].join(" ")
end

def redirect_url
case @kind
when "lieutenants"
Expand All @@ -47,6 +53,8 @@ def determine_kind
end
end

private

def save_search_and_clean_params(params)
if params[:search] && params[:search][:search_filter] != FormAnswerSearch.default_search[:search_filter]
search = NominationSearch.create(serialized_query: params[:search].to_json)
Expand All @@ -58,4 +66,15 @@ def save_search_and_clean_params(params)

params
end

def form_answers_eligible_for_state_change?
if FormAnswer.where(id: params[:bulk_action][:ids]).all? do |form_answer|
EligibilityAssignmentCollection::VALID_STATES.include?(form_answer.state)
end
true
else
@errors.add(:bulk_base, "To assign the eligibility status, you must only select groups that currently have ‘Nomination submitted’ or ‘Eligibility pending’ status.")
false
end
end
end

0 comments on commit e0cea90

Please sign in to comment.