fix(dossier): an invited user can remove (leave) a dossier

This commit is contained in:
Paul Chavard 2023-04-03 20:00:52 +02:00
parent cd74ea46cc
commit 0864666d0a
8 changed files with 35 additions and 16 deletions

View file

@ -6,7 +6,7 @@ module Users
layout 'procedure_context', only: [:identite, :update_identite, :siret, :update_siret]
ACTIONS_ALLOWED_TO_ANY_USER = [:index, :recherche, :new, :transferer_all]
ACTIONS_ALLOWED_TO_OWNER_OR_INVITE = [:show, :demande, :messagerie, :brouillon, :update_brouillon, :submit_brouillon, :modifier, :update, :create_commentaire, :papertrail, :restore]
ACTIONS_ALLOWED_TO_OWNER_OR_INVITE = [:show, :destroy, :demande, :messagerie, :brouillon, :update_brouillon, :submit_brouillon, :modifier, :update, :create_commentaire, :papertrail, :restore]
before_action :ensure_ownership!, except: ACTIONS_ALLOWED_TO_ANY_USER + ACTIONS_ALLOWED_TO_OWNER_OR_INVITE
before_action :ensure_ownership_or_invitation!, only: ACTIONS_ALLOWED_TO_OWNER_OR_INVITE
@ -244,9 +244,13 @@ module Users
end
end
def delete_dossier
def destroy
if dossier.can_be_deleted_by_user?
dossier.hide_and_keep_track!(current_user, :user_request)
if current_user.owns?(dossier)
dossier.hide_and_keep_track!(current_user, :user_request)
elsif current_user.invite?(dossier)
current_user.invites.where(dossier:).destroy_all
end
flash.notice = t('users.dossiers.ask_deletion.soft_deleted_dossier')
redirect_to dossiers_path
else

View file

@ -94,12 +94,12 @@ class User < ApplicationRecord
dossier.user_id == id
end
def invite?(dossier_id)
invites.pluck(:dossier_id).include?(dossier_id.to_i)
def invite?(dossier)
invites.pluck(:dossier_id).include?(dossier.id)
end
def owns_or_invite?(dossier)
owns?(dossier) || invite?(dossier.id)
owns?(dossier) || invite?(dossier)
end
def invite!

View file

@ -47,7 +47,7 @@
- if has_delete_action
- menu.with_item(class: 'danger') do
= link_to(delete_dossier_dossier_path(dossier), role: 'menuitem', method: :patch, data: { disable: true, confirm: "En continuant, vous allez supprimer ce dossier ainsi que les informations quil contient. Toute suppression entraîne lannulation de la démarche en cours.\n\nConfirmer la suppression ?" }) do
= link_to(dossier_path(dossier), role: 'menuitem', method: :delete, data: { disable: true, confirm: "En continuant, vous allez supprimer ce dossier ainsi que les informations quil contient. Toute suppression entraîne lannulation de la démarche en cours.\n\nConfirmer la suppression ?" }) do
%span.icon.delete
.dropdown-description

View file

@ -306,7 +306,7 @@ Rails.application.routes.draw do
get '/:path/france_connect', action: 'france_connect', as: :france_connect
end
resources :dossiers, only: [:index, :show, :new] do
resources :dossiers, only: [:index, :show, :destroy, :new] do
member do
get 'identite'
patch 'update_identite'
@ -323,7 +323,6 @@ Rails.application.routes.draw do
get 'demande'
get 'messagerie'
post 'commentaire' => 'dossiers#create_commentaire'
patch 'delete_dossier'
patch 'restore', to: 'dossiers#restore'
get 'attestation'
get 'transferer', to: 'dossiers#transferer'

View file

@ -958,10 +958,10 @@ describe Users::DossiersController, type: :controller do
end
end
describe '#delete_dossier' do
describe '#destroy' do
before { sign_in(user) }
subject { patch :delete_dossier, params: { id: dossier.id } }
subject { delete :destroy, params: { id: dossier.id } }
shared_examples_for "the dossier can not be deleted" do
it "doesnt notify the deletion" do
@ -1015,6 +1015,22 @@ describe Users::DossiersController, type: :controller do
it_behaves_like "the dossier can not be deleted"
it { is_expected.to redirect_to(root_path) }
context 'but user is invited' do
before { dossier.invites.create(user:, email: user.email, message: 'Salut', email_sender: user2.email) }
it do
procedure = dossier.procedure
dossier_id = dossier.id
expect(user.invite?(dossier)).to be_truthy
is_expected.to redirect_to(dossiers_path)
expect(Dossier.find_by(id: dossier_id)).to be_present
expect(Dossier.find_by(id: dossier_id).hidden_by_user_at).to be_nil
expect(procedure.deleted_dossiers.count).to eq(0)
expect(user.invite?(dossier)).to be_falsy
end
end
end
end

View file

@ -53,7 +53,7 @@ describe User, type: :model do
let(:dossier) { create :dossier }
let(:user) { dossier.user }
subject { user.invite? dossier.id }
subject { user.invite? dossier }
context 'when user is invite at the dossier' do
before do

View file

@ -62,9 +62,9 @@ describe 'user access to the list of their dossiers', js: true do
describe 'deletion' do
it 'should have links to delete dossiers' do
expect(page).to have_link(nil, href: delete_dossier_dossier_path(dossier_brouillon))
expect(page).to have_link(nil, href: delete_dossier_dossier_path(dossier_en_construction))
expect(page).not_to have_link(nil, href: delete_dossier_dossier_path(dossier_en_instruction))
expect(page).to have_link('Supprimer le dossier', href: dossier_path(dossier_brouillon))
expect(page).to have_link('Supprimer le dossier', href: dossier_path(dossier_en_construction))
expect(page).not_to have_link('Supprimer le dossier', href: dossier_path(dossier_en_instruction))
end
context 'when user clicks on delete button', js: true do

View file

@ -6,7 +6,7 @@ describe 'users/dossiers/dossier_actions.html.haml', type: :view do
subject { render 'users/dossiers/dossier_actions.html.haml', dossier: dossier, current_user: user }
it { is_expected.to have_link('Commencer un autre dossier', href: commencer_url(path: procedure.path)) }
it { is_expected.to have_link('Supprimer le dossier', href: delete_dossier_dossier_path(dossier)) }
it { is_expected.to have_link('Supprimer le dossier', href: dossier_path(dossier)) }
it { is_expected.to have_link('Transférer le dossier', href: transferer_dossier_path(dossier)) }
context 'when the dossier is termine' do