From dd96fa13d8dee08a5b138135a96be02d0f5c06f5 Mon Sep 17 00:00:00 2001 From: phil-l-brockwell Date: Wed, 27 Nov 2024 16:56:21 +0000 Subject: [PATCH] Add collaborator actions to Admin::Users#edit --- app/controllers/admin/admins_controller.rb | 5 ++ app/controllers/admin/assessors_controller.rb | 4 + app/controllers/admin/judges_controller.rb | 4 + .../admin/users/collaborators_controller.rb | 46 +++++++++++ app/controllers/admin/users_controller.rb | 4 + app/policies/user_policy.rb | 2 +- .../users/_fields_collaborators.html.slim | 5 +- .../collaborators/_search_form.html.slim | 27 +++++++ .../collaborators/_search_results.html.slim | 10 +++ .../admin/users/collaborators/search.js.slim | 6 ++ app/views/admin/users/edit.html.slim | 10 +++ config/routes.rb | 4 + .../admin/users/collaborators_spec.rb | 76 +++++++++++++++++++ 13 files changed, 200 insertions(+), 3 deletions(-) create mode 100644 app/controllers/admin/users/collaborators_controller.rb create mode 100644 app/views/admin/users/collaborators/_search_form.html.slim create mode 100644 app/views/admin/users/collaborators/_search_results.html.slim create mode 100644 app/views/admin/users/collaborators/search.js.slim create mode 100644 spec/features/admin/users/collaborators_spec.rb diff --git a/app/controllers/admin/admins_controller.rb b/app/controllers/admin/admins_controller.rb index 10cadf06f0..0120049cf0 100644 --- a/app/controllers/admin/admins_controller.rb +++ b/app/controllers/admin/admins_controller.rb @@ -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! diff --git a/app/controllers/admin/assessors_controller.rb b/app/controllers/admin/assessors_controller.rb index b232f3ddf1..35720c3f2f 100644 --- a/app/controllers/admin/assessors_controller.rb +++ b/app/controllers/admin/assessors_controller.rb @@ -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! diff --git a/app/controllers/admin/judges_controller.rb b/app/controllers/admin/judges_controller.rb index 558eeba3a4..d0c1df7299 100644 --- a/app/controllers/admin/judges_controller.rb +++ b/app/controllers/admin/judges_controller.rb @@ -1,4 +1,8 @@ class Admin::JudgesController < Admin::UsersController + expose(:collaborators) do + nil + end + def index params[:search] ||= JudgeSearch::DEFAULT_SEARCH params[:search].permit! diff --git a/app/controllers/admin/users/collaborators_controller.rb b/app/controllers/admin/users/collaborators_controller.rb new file mode 100644 index 0000000000..4f3ddf6ed9 --- /dev/null +++ b/app/controllers/admin/users/collaborators_controller.rb @@ -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 diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 699a28d68d..2ececa26b8 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -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! diff --git a/app/policies/user_policy.rb b/app/policies/user_policy.rb index 447b9822fb..d95c723ffb 100644 --- a/app/policies/user_policy.rb +++ b/app/policies/user_policy.rb @@ -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 diff --git a/app/views/admin/users/_fields_collaborators.html.slim b/app/views/admin/users/_fields_collaborators.html.slim index 3816ed319f..96d172afd1 100644 --- a/app/views/admin/users/_fields_collaborators.html.slim +++ b/app/views/admin/users/_fields_collaborators.html.slim @@ -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 @@ -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 diff --git a/app/views/admin/users/collaborators/_search_form.html.slim b/app/views/admin/users/collaborators/_search_form.html.slim new file mode 100644 index 0000000000..501d74b48e --- /dev/null +++ b/app/views/admin/users/collaborators/_search_form.html.slim @@ -0,0 +1,27 @@ += simple_form_for :search, + url: search_admin_user_collaborators_url(resource), + remote: true, + method: :get, + as: nil, + html: { class: "admin-search-collaborators-form" } do |f| + + .form-container + label.form-label for="admin-search-collaborators-query" Add collaborator + + .alert.alert-danger.hidden.js-admin-search-collaborators-error-box role="alert" + + ul.list-unstyled.list-actions.hidden.js-admin-search-collaborators-results-box + + .form-block + .row + .col-md-12 + = f.input :query, + as: :string, + label: false, + input_html: { class: "form-control", id: "admin-search-collaborators-query" }, + wrapper_html: { class: 'pull-left col-md-10 admin-search-collaborators-query' }, + placeholder: "Type part of email, first name or last name" + + .text-right + = f.submit "Search", class: "btn btn-primary pull-right" + .clear diff --git a/app/views/admin/users/collaborators/_search_results.html.slim b/app/views/admin/users/collaborators/_search_results.html.slim new file mode 100644 index 0000000000..e84d625f26 --- /dev/null +++ b/app/views/admin/users/collaborators/_search_results.html.slim @@ -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! diff --git a/app/views/admin/users/collaborators/search.js.slim b/app/views/admin/users/collaborators/search.js.slim new file mode 100644 index 0000000000..267e91b09c --- /dev/null +++ b/app/views/admin/users/collaborators/search.js.slim @@ -0,0 +1,6 @@ +- if search_users.valid? + | $('.js-admin-search-collaborators-results-box').html("#{j render("search_results")}").removeClass("hidden"); + | $(".js-admin-search-collaborators-error-box").addClass("hidden"); +- else + | $('.js-admin-search-collaborators-results-box').addClass("hidden"); + | $(".js-admin-search-collaborators-error-box").text("#{search_users.error.html_safe}").removeClass("hidden"); diff --git a/app/views/admin/users/edit.html.slim b/app/views/admin/users/edit.html.slim index fb5c4e9ad1..ff353e103e 100644 --- a/app/views/admin/users/edit.html.slim +++ b/app/views/admin/users/edit.html.slim @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 644fbbbf6b..6e93fb78e0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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] diff --git a/spec/features/admin/users/collaborators_spec.rb b/spec/features/admin/users/collaborators_spec.rb new file mode 100644 index 0000000000..4826acaceb --- /dev/null +++ b/spec/features/admin/users/collaborators_spec.rb @@ -0,0 +1,76 @@ +require "rails_helper" + +describe "Collaborators", ' +As a an Admin +I want to be able to add collaborators to any account +So that they can collaborate applications +' do + include ActiveJob::TestHelper + + let!(:admin) { create(:admin) } + let(:existing_user) { create(:user, :completed_profile) } + + before do + login_admin admin + visit edit_admin_user_path(existing_user) + end + + describe "Add new Collaborator" do + describe "Invalid Attempts", js: true do + describe "Attempt to add person, which is already associated with another account which has application" do + let!(:user_associated_with_another_account) do + create :user, + :completed_profile, + first_name: "Applicant with account", + role: "account_admin" + end + + let!(:another_form_answer) do + create :form_answer, + :innovation, + :submitted, + user: user_associated_with_another_account + end + + before do + find("a[aria-controls='section-collaborators']").click + + within(".admin-search-collaborators-form") do + fill_in "search[query]", with: "plicant with acc" + first("input[type='submit']").click + end + end + + it "can't add" do + within(".js-admin-search-collaborators-results-box") do + expect_to_see(user_associated_with_another_account.first_name) + expect_to_see("can not be added as linked with another account!") + expect(page).to have_no_link("Add") + end + end + end + end + + describe "Success Add to Collaborators", js: true do + let(:email) { generate(:email) } + let!(:user) { create(:user, email: email) } + + before do + find("a[aria-controls='section-collaborators']").click + + within(".admin-search-collaborators-form") do + fill_in "search[query]", with: email.to_s[2..-2] + first("input[type='submit']").click + end + end + + it "should add user to collaborators with regular role" do + within(".js-admin-search-collaborators-results-box") do + expect_to_see(user.email) + expect_to_see_no("can not be added as linked with another account!") + expect(page).to have_link("Add") + end + end + end + end +end