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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
46 changes: 46 additions & 0 deletions app/controllers/admin/users/collaborators_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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:)
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!"
end
end
end

private

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
22 changes: 10 additions & 12 deletions app/interactors/admin_actions/add_collaborator.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
module AdminActions
class AddCollaborator
attr_reader :form_answer,
:user,
:success,
:errors
attr_reader :account, :role, :collaborator, :success, :errors

def initialize(form_answer, user)
@form_answer = form_answer
@user = user
def initialize(account:, collaborator:, role: "regular")
@account = account
@collaborator = collaborator
@role = role
end

def run
if user.can_be_added_to_collaborators_to_another_account?
if collaborator.can_be_added_to_collaborators_to_another_account?
persist!
else
@errors = "can't be added as linked with another account!"
Expand All @@ -27,13 +25,13 @@ def success?
private

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

if user.save
if collaborator.save
@success = true
else
@errors = user.errors.full_messages.join(", ")
@errors = collaborator.errors.full_messages.join(", ")
end
end
end
Expand Down
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
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"
5 changes: 3 additions & 2 deletions app/views/admin/users/_fields_collaborators.html.slim
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/ TODO collaborators new/delete, also the collaborators variable
- if action_name == "edit"
- collaborators = resource.account.collaborators_without(resource)
- if collaborators.any?
= render "admin/collaborators/list", collaborators: collaborators
- else
Expand All @@ -11,3 +9,6 @@
br
p.p-empty This user has not added any collaborators.
br

- if policy(resource).can_add_collaborators_to_account?
= render "admin/users/collaborators/search_form", resource: resource
10 changes: 0 additions & 10 deletions app/views/admin/users/_form.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@
.panel-body
= render "fields_contact_preferences", f: f

- unless action_name == "new"
.panel.panel-default[data-controller="element-focus"]
.panel-heading id="section-collaborators-header"
h2.panel-title
a.collapsed data-toggle="collapse" data-parent="#user-form-panel" href="#section-collaborators" aria-expanded="false" aria-controls="section-collaborators" data-element-focus-target="reveal"
' Collaborators
#section-collaborators.section-collaborators.panel-collapse.collapse[aria-labelledby="section-collaborators-header" data-element-scroll-target="accordion"]
.panel-body
= render "fields_collaborators", f: f, resource: resource

br
.clearfix
.pull-right
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
= simple_form_for :search,
url: search_admin_form_answer_collaborators_url(resource),
url: search_admin_user_collaborators_url(resource),
remote: true,
method: :get,
as: nil,
Expand Down
10 changes: 10 additions & 0 deletions app/views/admin/users/collaborators/_search_results.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- if candidates.present?
- candidates.each do |candidate|
li id="user_#{candidate.id}"
= link_to "#{candidate.full_name} (#{candidate.email})", edit_admin_user_path(candidate)
- if candidate.can_be_added_to_collaborators_to_another_account?
= link_to "Add", admin_user_collaborators_url(collaborator_id: candidate.id), method: :post, class: "pull-right btn btn-default"
- else
br
i.cant_add_to_collaborators_message
| can not be added as linked with another account!
10 changes: 10 additions & 0 deletions app/views/admin/users/edit.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@
= "Edit #{controller_name == "users" ? "applicant" : controller_name.singularize}"

= render 'form', resource: @resource

- if collaborators
.panel.panel-default[data-controller="element-focus"]
.panel-heading id="section-collaborators-header"
h2.panel-title
a.collapsed data-toggle="collapse" data-parent="#user-form-panel" href="#section-collaborators" aria-expanded="false" aria-controls="section-collaborators" data-element-focus-target="reveal"
' Collaborators
#section-collaborators.section-collaborators.panel-collapse.collapse[aria-labelledby="section-collaborators-header" data-element-scroll-target="accordion"]
.panel-body
= render "fields_collaborators", resource: @resource
7 changes: 4 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@
patch :unlock
post :scan_via_debounce_api
end

resources :collaborators, only: [:create], module: :users do
get :search, on: :collection
end
end

resources :collaborator_deletion, only: [:destroy]
Expand Down Expand Up @@ -351,9 +355,6 @@
resources :case_summaries, only: [:index]
resources :draft_notes, only: [:create, :update]
resources :review_corp_responsibility, only: [:create]
resources :collaborators, only: [:create], module: "form_answers" do
get :search, on: :collection
end
end

resource :settings, only: [:show] do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

it "can see the edit buttons" do
within ".company-details-forms" do
expect(page).to have_selector("input[type='submit']", count: 13)
expect(page).to have_selector("input[type='submit']", count: 12)
end
end
end
Loading
Loading