From 87a9697f29a7a535db699c88283597dec9c057e6 Mon Sep 17 00:00:00 2001 From: johan buitrago Date: Sat, 7 Sep 2024 14:13:53 -0500 Subject: [PATCH] handling account requests to close --- .../admin/account_requests_controller.rb | 18 +++++++- app/models/account_request.rb | 15 ++++++- .../_open_account_request.html.erb | 7 ++- .../_rejection_modal.html.erb | 25 +++++++---- .../admin/account_requests/index.html.erb | 11 ----- config/routes.rb | 1 + .../admin/account_requests_controller_spec.rb | 15 +++++++ spec/models/account_request_spec.rb | 18 ++++++++ .../admin/account_requests_system_spec.rb | 44 ++++++++++++++++++- 9 files changed, 128 insertions(+), 26 deletions(-) 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..5ab55e3309 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,24 @@ 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 + @account_request.close!(account_request_params[:rejection_reason]) + redirect_to admin_account_requests_path, notice: "Account request closed!" + rescue => e + redirect_to admin_account_requests_path, alert: e.message + 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..c68fe75559 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) + raise 'Cannot be closed from this state' 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/_open_account_request.html.erb b/app/views/admin/account_requests/_open_account_request.html.erb index d06023658d..4f05036f0a 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: 'reject' }) %> + <%= 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..4dc350165f 100644 --- a/app/views/admin/account_requests/_rejection_modal.html.erb +++ b/app/views/admin/account_requests/_rejection_modal.html.erb @@ -1,19 +1,18 @@ -