Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Admin Abilities #3149

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/controllers/admin/admins_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
class Admin::AdminsController < Admin::UsersController
before_action :find_resource, except: [:index, :new, :create, :login_as_assessor, :login_as_user]

expose(:collaborators) do
nil
end

def index
params[:search] ||= AdminSearch::DEFAULT_SEARCH
params[:search].permit!
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/admin/assessors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class Admin::AssessorsController < Admin::UsersController
:bulk_deactivate_dt,
]

expose(:collaborators) do
nil
end

def index
params[:search] ||= AssessorSearch::DEFAULT_SEARCH
params[:search].permit!
Expand Down
51 changes: 0 additions & 51 deletions app/controllers/admin/form_answers/collaborators_controller.rb

This file was deleted.

4 changes: 4 additions & 0 deletions app/controllers/admin/judges_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class Admin::JudgesController < Admin::UsersController
expose(:collaborators) do
nil
end

def index
params[:search] ||= JudgeSearch::DEFAULT_SEARCH
params[:search].permit!
Expand Down
52 changes: 52 additions & 0 deletions app/controllers/admin/users/collaborators_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Admin::Users::CollaboratorsController < Admin::BaseController
expose(:user) do
User.find(params[:user_id])
end

expose(:collaborator) do
User.find(params[:collaborator_id])
end

expose(:search_users) do
AdminActions::SearchCollaboratorCandidates.new(existing_collaborators: user.account.users, params: search_params)
end

expose(:add_collaborator_interactor) do
AdminActions::AddCollaborator.new(account: user.account, collaborator:, params: create_params)
end

expose(:candidates) do
search_users.candidates
end

def search
authorize user, :can_add_collaborators_to_account?
search_users.run if search_users.valid?
end

def create
authorize user, :can_add_collaborators_to_account?

add_collaborator_interactor.run.tap do |result|
if result.success?
redirect_to edit_admin_user_path(user), notice: "#{collaborator.email} successfully added to Collaborators!"
else
redirect_to edit_admin_user_path(user), notice: "#{collaborator.email} could not be added to Collaborators: #{result.error_messages}"
end
end
end

private

def create_params
params.require(:user).permit(:transfer_form_answers, :new_owner_id, :role).tap do |p|
p[:transfer_form_answers] = ActiveModel::Type::Boolean.new.cast(p[:transfer_form_answers])
end
end

def search_params
return if params[:search].blank?

params.require(:search).permit(:query)
end
end
4 changes: 4 additions & 0 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class Admin::UsersController < Admin::BaseController
before_action :find_resource, except: [:index, :new, :create]

expose(:collaborators) do
@resource.account.collaborators_without(@resource)
end

def index
params[:search] ||= UserSearch::DEFAULT_SEARCH
params[:search].permit!
Expand Down
102 changes: 86 additions & 16 deletions app/interactors/admin_actions/add_collaborator.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
module AdminActions
class AddCollaborator
attr_reader :form_answer,
:user,
:success,
:errors
attr_reader :account, :role, :transfer_form_answers, :new_owner_id, :collaborator, :success, :errors

def initialize(form_answer, user)
@form_answer = form_answer
@user = user
def initialize(account:, collaborator:, params:, existing_account: collaborator.account)
@account = account
@collaborator = collaborator
@existing_account = existing_account
@role = params.fetch(:role, "regular")
@transfer_form_answers = params.fetch(:transfer_form_answers, false)
@new_owner_id = params.fetch(:new_owner_id, nil)
@errors = []
end

def run
if user.can_be_added_to_collaborators_to_another_account?
persist!
if account_will_be_orphaned?
@errors << "User account has active users, ownership of the account must be transferred"
elsif progressed_form_answers_will_be_orphaned?
@errors << "User has applications in progress, and there are no other users on the account to transfer them to"
else
@errors = "can't be added as linked with another account!"
persist!
end

self
Expand All @@ -24,17 +28,83 @@ def success?
@success.present?
end

def error_messages
@errors.join(", ")
end

private

def persist!
user.role = "regular"
user.account = form_answer.account

if user.save
ActiveRecord::Base.transaction do
transfer_collaborator!
transfer_ownership!
handle_form_answers!
delete_existing_account!
@success = true
else
@errors = user.errors.full_messages.join(", ")
end
rescue ActiveRecord::RecordInvalid => e
@errors << e.message
end

def existing_account_has_other_collaborators?
return unless @existing_account

@existing_account.collaborators_without(collaborator).any?
end

def account_will_be_orphaned?
collaborator_is_owner? && existing_account_has_other_collaborators? && new_owner_id.blank?
end

def collaborator_is_owner?
return unless @existing_account

@existing_account.owner == collaborator
end

def progressed_form_answers_will_be_orphaned?
keep_form_answers_on_original_account? && progressed_form_answers? && !existing_account_has_other_collaborators?
end

def progressed_form_answers?
collaborator.form_answers.touched.any?
end

def transfer_form_answers?
transfer_form_answers
end

def keep_form_answers_on_original_account?
!transfer_form_answers?
end

def handle_form_answers!
return unless @collaborator.form_answers.any?

if transfer_form_answers?
@collaborator.form_answers.each { |f| f.update!(account: @account) }
elsif existing_account_has_other_collaborators?
@collaborator.form_answers.each { |f| f.update!(user: @existing_account.owner) }
elsif !progressed_form_answers?
@collaborator.form_answers.each(&:destroy!)
end
end

def transfer_ownership!
return unless @new_owner_id

@existing_account.update!(owner_id: @new_owner_id)
end

def transfer_collaborator!
collaborator.update!(role: role, account: account)
end

def delete_existing_account!
return unless @existing_account
return if existing_account_has_other_collaborators?

@existing_account.destroy!
end
end
end
15 changes: 4 additions & 11 deletions app/interactors/admin_actions/search_collaborator_candidates.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
module AdminActions
class SearchCollaboratorCandidates
attr_accessor :form_answer,
:account,
:existing_collaborators,
:candidates,
:query,
:error
attr_accessor :account, :existing_collaborators, :candidates, :query, :error

def initialize(form_answer, query = nil)
@query = query[:query]
@form_answer = form_answer
@account = form_answer.account
@existing_collaborators = account.users
def initialize(existing_collaborators:, params: {})
@query = params[:query]
@existing_collaborators = existing_collaborators
end

def run
Expand Down
11 changes: 10 additions & 1 deletion app/models/form_answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ def secondary
scope :vocf_free, -> { where(award_type: %w[mobility development]) }
scope :provided_estimates, -> { where("document #>> '{product_estimated_figures}' = 'yes'") }

scope :touched, -> {
joins("LEFT JOIN eligibilities ON eligibilities.form_answer_id = form_answers.id")
.where("eligibilities.id IS NOT NULL AND eligibilities.type = form_answers.award_type AND eligibilities.answers::jsonb <> '{}'::jsonb")
.or(where("(document::jsonb - 'organization_name' - 'company_name') <> '{}'::jsonb"))
}

# callbacks
before_save :set_award_year, unless: :award_year
before_save :set_urn
Expand Down Expand Up @@ -326,13 +332,16 @@ def head_of_business
end

def company_or_nominee_from_document
comp_attr = promotion? ? "organization_name" : "company_name"
name = document[comp_attr]
name = nominee_full_name_from_document if promotion? && name.blank?
name = name.try(:strip)
name.presence
end

def comp_attr
promotion? ? "organization_name" : "company_name"
end

def nominee_full_name_from_document
"#{document["nominee_info_first_name"]} #{document["nominee_info_last_name"]}".strip
end
Expand Down
8 changes: 0 additions & 8 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,6 @@ def reset_password_period_valid?
end
end

def can_be_added_to_collaborators_to_another_account?
account.blank? || (
account.present? &&
form_answers.blank? &&
account.form_answers.blank?
)
end

def new_member?
created_at > 3.days.ago
end
Expand Down
4 changes: 0 additions & 4 deletions app/policies/form_answer_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ def can_download_original_pdf_of_application_before_deadline?
record.pdf_version.present?
end

def can_add_collaborators_to_application?
admin?
end

private

def audit_certificate_available?
Expand Down
2 changes: 1 addition & 1 deletion app/policies/user_policy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class UserPolicy < ApplicationPolicy
%w[index? update? create? show? new?].each do |method|
%w[index? update? create? show? new? can_add_collaborators_to_account?].each do |method|
define_method method do
admin?
end
Expand Down

This file was deleted.

6 changes: 0 additions & 6 deletions app/views/admin/form_answers/collaborators/create.js.slim

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
.form-group
.form-container
label.form-label User accounts

= render "admin/form_answers/collaborators/list"

- if policy(resource).can_add_collaborators_to_application?
= render "admin/form_answers/collaborators/form"
Loading
Loading