From 06edd0298ced5e08c2df813bc77185b816c001cc Mon Sep 17 00:00:00 2001 From: johan buitrago Date: Fri, 30 Aug 2024 15:12:33 -0500 Subject: [PATCH] handling old/stale account requests --- .../admin/account_requests_controller.rb | 19 +++++++++-- app/models/account_request.rb | 15 +++++++-- .../_close_admin_modal.html.erb | 25 ++++++++++++++ .../_open_account_request.html.erb | 7 +++- .../_rejection_modal.html.erb | 8 ++--- .../account_requests/for_rejection.html.erb | 1 + .../admin/account_requests/index.html.erb | 8 +++-- config/routes.rb | 1 + .../admin/account_requests_controller_spec.rb | 33 +++++++++++++++++++ spec/models/account_request_spec.rb | 18 ++++++++++ .../admin/account_requests_system_spec.rb | 30 +++++++++++++++-- 11 files changed, 151 insertions(+), 14 deletions(-) create mode 100644 app/views/admin/account_requests/_close_admin_modal.html.erb create mode 100644 spec/controllers/admin/account_requests_controller_spec.rb diff --git a/app/controllers/admin/account_requests_controller.rb b/app/controllers/admin/account_requests_controller.rb index e93495a555..508a42444f 100644 --- a/app/controllers/admin/account_requests_controller.rb +++ b/app/controllers/admin/account_requests_controller.rb @@ -1,4 +1,6 @@ class Admin::AccountRequestsController < AdminController + before_action :set_account_request, only: [:reject, :close] + def index @open_account_requests = AccountRequest.requested.order('created_at DESC') .page(params[:open_page]).per(15) @@ -11,12 +13,25 @@ def for_rejection end def reject - account_request = AccountRequest.find(account_request_params[:id]) - account_request.reject!(account_request_params[:rejection_reason]) + @account_request.reject!(account_request_params[:rejection_reason]) redirect_to admin_account_requests_path, notice: "Account request rejected!" end + def close + if @account_request.close!(account_request_params[:rejection_reason]) + redirect_to admin_account_requests_path, notice: "Account request closed!" + else + redirect_to admin_account_requests_path, error: "Account cannot be closed" + end + end + def account_request_params params.require(:account_request).permit(:id, :rejection_reason) end + + private + + def set_account_request + @account_request = AccountRequest.find(account_request_params[:id]) + end end diff --git a/app/models/account_request.rb b/app/models/account_request.rb index a704cddcdb..ac8de492eb 100644 --- a/app/models/account_request.rb +++ b/app/models/account_request.rb @@ -30,10 +30,10 @@ class AccountRequest < ApplicationRecord has_one :organization, dependent: :nullify - enum status: %w[started user_confirmed admin_approved rejected].map { |v| [v, v] }.to_h + enum status: %w[started user_confirmed admin_approved rejected admin_closed].map { |v| [v, v] }.to_h scope :requested, -> { where(status: %w[started user_confirmed]) } - scope :closed, -> { where(status: %w[admin_approved rejected]) } + scope :closed, -> { where(status: %w[admin_approved rejected admin_closed]) } def self.get_by_identity_token(identity_token) decrypted_token = JWT.decode(identity_token, Rails.application.secret_key_base, true, { algorithm: 'HS256' }) @@ -62,6 +62,11 @@ def processed? organization.present? end + # @return [Boolean] + def can_be_closed? + started? || user_confirmed? + end + def confirm! update!(confirmed_at: Time.current, status: 'user_confirmed') AccountRequestMailer.approval_request(account_request_id: id).deliver_later @@ -73,6 +78,12 @@ def reject!(reason) AccountRequestMailer.rejection(account_request_id: id).deliver_later end + # @param reason [String] + def close!(reason) + return false unless can_be_closed? + update!(status: 'admin_closed', rejection_reason: reason) + end + private def email_not_already_used_by_organization diff --git a/app/views/admin/account_requests/_close_admin_modal.html.erb b/app/views/admin/account_requests/_close_admin_modal.html.erb new file mode 100644 index 0000000000..14a863a97e --- /dev/null +++ b/app/views/admin/account_requests/_close_admin_modal.html.erb @@ -0,0 +1,25 @@ + diff --git a/app/views/admin/account_requests/_open_account_request.html.erb b/app/views/admin/account_requests/_open_account_request.html.erb index d06023658d..83271e6c0f 100644 --- a/app/views/admin/account_requests/_open_account_request.html.erb +++ b/app/views/admin/account_requests/_open_account_request.html.erb @@ -13,5 +13,10 @@ <%= js_button(text: 'Reject', icon: 'ban', class: 'reject-button', - data: { request_id: open_account_request.id }) %> + data: { request_id: open_account_request.id, modal: 'rejection' }) %> + <%= js_button(text: 'Close(Admin)', + icon: 'times', + class: 'reject-button', + data: { request_id: open_account_request.id, modal: 'close' }) %> + diff --git a/app/views/admin/account_requests/_rejection_modal.html.erb b/app/views/admin/account_requests/_rejection_modal.html.erb index 1a4d198d38..89bcd76a0a 100644 --- a/app/views/admin/account_requests/_rejection_modal.html.erb +++ b/app/views/admin/account_requests/_rejection_modal.html.erb @@ -23,14 +23,14 @@ - diff --git a/app/views/admin/account_requests/for_rejection.html.erb b/app/views/admin/account_requests/for_rejection.html.erb index b5df3776fd..c1098b424a 100644 --- a/app/views/admin/account_requests/for_rejection.html.erb +++ b/app/views/admin/account_requests/for_rejection.html.erb @@ -40,6 +40,7 @@ <%= render partial: 'rejection_modal' %> + <%= render partial: 'close_admin_modal' %> <% else %>

Account Request not found!

<% end %> diff --git a/app/views/admin/account_requests/index.html.erb b/app/views/admin/account_requests/index.html.erb index 341212634b..a09511244a 100644 --- a/app/views/admin/account_requests/index.html.erb +++ b/app/views/admin/account_requests/index.html.erb @@ -44,6 +44,7 @@ <%= render partial: 'rejection_modal' %> +<%= render partial: 'close_admin_modal' %>
@@ -108,9 +109,10 @@ $(() => { $('.reject-button').click((event) => { event.preventDefault(); - $('#account_request_rejection_reason').val(''); - $('#reject_account_request_id').val($(event.target).data('requestId')); - $('#rejection-modal').modal('show'); + let classModal = $(event.target).data('modal') + $(`#${classModal}-modal #account_request_rejection_reason`).val(''); + $(`#${classModal}-modal #reject_account_request_id`).val($(event.target).data('requestId')); + $(`#${classModal}-modal`).modal('show'); }) }) diff --git a/config/routes.rb b/config/routes.rb index a97548c5cd..17e4398dc0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -76,6 +76,7 @@ def set_up_flipper resources :barcode_items resources :account_requests, only: [:index] do post :reject, on: :collection + post :close, on: :collection get :for_rejection, on: :collection end resources :questions diff --git a/spec/controllers/admin/account_requests_controller_spec.rb b/spec/controllers/admin/account_requests_controller_spec.rb new file mode 100644 index 0000000000..f749434046 --- /dev/null +++ b/spec/controllers/admin/account_requests_controller_spec.rb @@ -0,0 +1,33 @@ +RSpec.describe Admin::AccountRequestsController, type: :controller do + before do + sign_in(create(:super_admin, organization: nil)) + end + + let(:account_request) { create(:account_request) } + let(:rejection_params) do + { + account_request: { + id: account_request.id, + rejection_reason: "some rejection reason" + } + } + end + + describe "POST #reject" do + it "should reject the account request" do + post :reject, params: rejection_params + expect(account_request.reload).to be_rejected + expect(flash[:notice]).to eq("Account request rejected!") + expect(response).to redirect_to(admin_account_requests_path) + end + end + + describe "POST #close" do + it "should close the account request" do + post :close, params: rejection_params + expect(account_request.reload).to be_admin_closed + expect(flash[:notice]).to eq("Account request closed!") + expect(response).to redirect_to(admin_account_requests_path) + end + end +end diff --git a/spec/models/account_request_spec.rb b/spec/models/account_request_spec.rb index 50570a8665..a3e5c412c9 100644 --- a/spec/models/account_request_spec.rb +++ b/spec/models/account_request_spec.rb @@ -129,6 +129,18 @@ end end + describe '#can_be_closed?' do + it 'returns true when the status can be closed' do + subject.status = %w[started user_confirmed].sample + expect(subject.can_be_closed?).to eq(true) + end + + it 'returns false when the status cannot be closed' do + subject.status = 'rejected' + expect(subject.can_be_closed?).to eq(false) + end + end + specify '#confirm!' do mail_double = instance_double(ActionMailer::MessageDelivery, deliver_later: nil) allow(AccountRequestMailer).to receive(:approval_request).and_return(mail_double) @@ -159,6 +171,12 @@ expect(mail_double).to have_received(:deliver_later) end + specify "#close!" do + account_request.close!('because I said so') + expect(account_request.reload.rejection_reason).to eq('because I said so') + expect(account_request).to be_admin_closed + end + describe "versioning" do it { is_expected.to be_versioned } end diff --git a/spec/system/admin/account_requests_system_spec.rb b/spec/system/admin/account_requests_system_spec.rb index dd79ed4a58..5d9a9fe023 100644 --- a/spec/system/admin/account_requests_system_spec.rb +++ b/spec/system/admin/account_requests_system_spec.rb @@ -29,7 +29,7 @@ end it 'should reject the account', js: true do - find(%(a[data-request-id="#{request4.id}"])).click + find(%(a[data-modal="rejection"][data-request-id="#{request4.id}"])).click fill_in 'account_request_rejection_reason', with: 'Because I said so' click_on 'Save' expect(request4.reload).to be_rejected @@ -40,6 +40,19 @@ expect(page).not_to have_content(request4.name) end end + + it 'should close the account', js: true do + find(%(a[data-modal="close"][data-request-id="#{request4.id}"])).click + fill_in 'account_request_rejection_reason', with: 'Because I said so' + click_on 'Save' + expect(request4.reload).to be_admin_closed + within "#closed-account-requests" do + expect(page).to have_content(request4.name) + end + within '#open-account-requests' do + expect(page).not_to have_content(request4.name) + end + end end context "user visits the index page" do @@ -89,7 +102,7 @@ end it 'should reject the account', js: true do - find(%(a[data-request-id="#{request4.id}"])).click + find(%(a[data-modal="rejection"][data-request-id="#{request4.id}"])).click fill_in 'account_request_rejection_reason', with: 'Because I said so' click_on 'Save' expect(request4.reload).to be_rejected @@ -100,6 +113,19 @@ expect(page).not_to have_content(request4.name) end end + + it 'should close the account', js: true do + find(%(a[data-modal="close"][data-request-id="#{request4.id}"])).click + fill_in 'account_request_rejection_reason', with: 'Because I said so' + click_on 'Save' + expect(request4.reload).to be_admin_closed + within "#closed-account-requests" do + expect(page).to have_content(request4.name) + end + within '#open-account-requests' do + expect(page).not_to have_content(request4.name) + end + end end end end