From 70d011fe1670541cb71951cf9cb45e007bf7d804 Mon Sep 17 00:00:00 2001 From: Yuri Bocharov Date: Wed, 21 Aug 2024 18:39:53 -0400 Subject: [PATCH] feat: added drafts page for supes and admins (#6002) * feat: added drafts page for supes and admins * feat: add tests --- app/controllers/case_contacts_controller.rb | 6 +---- app/policies/case_contact_policy.rb | 2 +- app/views/case_contacts/drafts.html.erb | 1 + config/routes.rb | 1 + spec/policies/case_contact_policy_spec.rb | 2 +- spec/requests/case_contacts_spec.rb | 16 ++++++++++++ spec/system/case_contacts/drafts_spec.rb | 27 +++++++++++++++++++++ 7 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 app/views/case_contacts/drafts.html.erb create mode 100644 spec/system/case_contacts/drafts_spec.rb diff --git a/app/controllers/case_contacts_controller.rb b/app/controllers/case_contacts_controller.rb index 96fdf6bc14..be54dca135 100644 --- a/app/controllers/case_contacts_controller.rb +++ b/app/controllers/case_contacts_controller.rb @@ -28,7 +28,7 @@ def index def drafts authorize CaseContact - @case_contacts = case_contact_drafts + @case_contacts = current_organization.case_contacts.not_active end def new @@ -116,10 +116,6 @@ def additional_expense_params @additional_expense_params ||= AdditionalExpenseParamsService.new(params).calculate end - def case_contact_drafts - CaseContact.where(creator: current_user).not_active - end - def set_case_contact if current_organization.case_contacts.exists?(params[:id]) @case_contact = authorize(current_organization.case_contacts.find(params[:id])) diff --git a/app/policies/case_contact_policy.rb b/app/policies/case_contact_policy.rb index d406307299..9df9ce29d3 100644 --- a/app/policies/case_contact_policy.rb +++ b/app/policies/case_contact_policy.rb @@ -19,7 +19,7 @@ def additional_expenses_allowed? alias_method :index?, :admin_or_supervisor_or_volunteer? alias_method :new?, :admin_or_supervisor_or_volunteer? alias_method :show?, :is_creator_or_casa_admin? - alias_method :drafts?, :admin_or_supervisor_or_volunteer? + alias_method :drafts?, :admin_or_supervisor? alias_method :update?, :is_creator_or_supervisor_or_casa_admin? alias_method :edit?, :is_creator_or_supervisor_or_casa_admin? alias_method :restore?, :is_admin? diff --git a/app/views/case_contacts/drafts.html.erb b/app/views/case_contacts/drafts.html.erb new file mode 100644 index 0000000000..4a1920afe6 --- /dev/null +++ b/app/views/case_contacts/drafts.html.erb @@ -0,0 +1 @@ +<%= render partial: "case_contacts/case_contact", collection: @case_contacts, as: :contact %> diff --git a/config/routes.rb b/config/routes.rb index 84dcdb1c51..4352a14943 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -85,6 +85,7 @@ end get "case_contacts/leave", to: "case_contacts#leave", as: "leave_case_contacts_form" + get "case_contacts/drafts", to: "case_contacts#drafts" resources :case_contacts, except: %i[create update show] do member do post :restore diff --git a/spec/policies/case_contact_policy_spec.rb b/spec/policies/case_contact_policy_spec.rb index 084af4fb66..37ac8c83e2 100644 --- a/spec/policies/case_contact_policy_spec.rb +++ b/spec/policies/case_contact_policy_spec.rb @@ -76,7 +76,7 @@ end end - permissions :update? do + permissions :update?, :drafts? do it "allows casa_admins" do is_expected.to permit(casa_admin, case_contact) end diff --git a/spec/requests/case_contacts_spec.rb b/spec/requests/case_contacts_spec.rb index 8d1fa11c8f..c484e35913 100644 --- a/spec/requests/case_contacts_spec.rb +++ b/spec/requests/case_contacts_spec.rb @@ -86,6 +86,22 @@ it { is_expected.to have_http_status(:redirect) } end + describe "GET /drafts" do + subject(:request) do + get case_contacts_drafts_path + + response + end + + it { is_expected.to have_http_status(:success) } + + context "when user is volunteer" do + before { sign_in volunteer } + + it { is_expected.to have_http_status(:redirect) } + end + end + describe "DELETE /destroy" do let(:case_contact) { create(:case_contact) } diff --git a/spec/system/case_contacts/drafts_spec.rb b/spec/system/case_contacts/drafts_spec.rb new file mode 100644 index 0000000000..868546d0d9 --- /dev/null +++ b/spec/system/case_contacts/drafts_spec.rb @@ -0,0 +1,27 @@ +require "rails_helper" + +RSpec.describe "case_contacts/drafts", js: true, type: :system do + let(:organization) { create(:casa_org) } + let(:admin) { create(:casa_admin, casa_org: organization) } + + context "with case contacts" do + let!(:casa_case) { create(:casa_case, casa_org: organization) } + let!(:other_org_case) { create(:case_contact, notes: "NOTE_A") } + let!(:past_contact) { create(:case_contact, casa_case: casa_case, occurred_at: 3.weeks.ago, notes: "NOTE_B") } + let!(:past_contact_draft) { create(:case_contact, :started_status, casa_case: casa_case, occurred_at: 3.weeks.ago, notes: "NOTE_C") } + let!(:recent_contact) { create(:case_contact, casa_case: casa_case, occurred_at: 3.days.ago, notes: "NOTE_D") } + let!(:recent_contact_draft) { create(:case_contact, :started_status, casa_case: casa_case, occurred_at: 3.days.ago, notes: "NOTE_E") } + + it "shows only same orgs drafts" do + sign_in admin + + visit case_contacts_drafts_path + + expect(page).to_not have_content("NOTE_A") + expect(page).to_not have_content("NOTE_B") + expect(page).to have_content("NOTE_C") + expect(page).to_not have_content("NOTE_D") + expect(page).to have_content("NOTE_E") + end + end +end