expert can only revoke avis claimed by him/her
This commit is contained in:
parent
3ea34834a7
commit
52e8f32e19
5 changed files with 66 additions and 11 deletions
|
@ -117,7 +117,7 @@ module Instructeurs
|
||||||
|
|
||||||
def revoquer
|
def revoquer
|
||||||
avis = Avis.find(params[:id])
|
avis = Avis.find(params[:id])
|
||||||
if avis.revoke!
|
if avis.revoke_by!(current_instructeur)
|
||||||
flash.notice = "#{avis.email_to_display} ne peut plus donner son avis sur ce dossier."
|
flash.notice = "#{avis.email_to_display} ne peut plus donner son avis sur ce dossier."
|
||||||
redirect_back(fallback_location: avis_instructeur_dossier_path(avis.procedure, avis.dossier))
|
redirect_back(fallback_location: avis_instructeur_dossier_path(avis.procedure, avis.dossier))
|
||||||
end
|
end
|
||||||
|
|
|
@ -60,7 +60,13 @@ class Avis < ApplicationRecord
|
||||||
revoked_at.present?
|
revoked_at.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def revoke!
|
def revokable_by?(revocator)
|
||||||
|
revocator.dossiers.include?(dossier) || revocator == claimant
|
||||||
|
end
|
||||||
|
|
||||||
|
def revoke_by!(revocator)
|
||||||
|
return false if !revokable_by?(revocator)
|
||||||
|
|
||||||
if answer.present?
|
if answer.present?
|
||||||
update!(revoked_at: Time.zone.now)
|
update!(revoked_at: Time.zone.now)
|
||||||
else
|
else
|
||||||
|
|
|
@ -28,13 +28,15 @@
|
||||||
%span.waiting{ class: highlight_if_unseen_class(avis_seen_at, avis.revoked_at) }
|
%span.waiting{ class: highlight_if_unseen_class(avis_seen_at, avis.revoked_at) }
|
||||||
Demande d'avis révoquée le #{l(avis.revoked_at, format: '%d/%m/%y à %H:%M')}
|
Demande d'avis révoquée le #{l(avis.revoked_at, format: '%d/%m/%y à %H:%M')}
|
||||||
- else
|
- else
|
||||||
%span.waiting= link_to("Révoquer l'avis", revoquer_instructeur_avis_path(avis.procedure, avis), data: { confirm: "Souhaitez-vous révoquer la demande d'avis à #{avis.email_to_display} ?" }, method: :patch)
|
- if avis.revokable_by?(current_instructeur)
|
||||||
|
%span.waiting= link_to("Révoquer l'avis", revoquer_instructeur_avis_path(avis.procedure, avis), data: { confirm: "Souhaitez-vous révoquer la demande d'avis à #{avis.email_to_display} ?" }, method: :patch)
|
||||||
%span.date{ class: highlight_if_unseen_class(avis_seen_at, avis.updated_at) }
|
%span.date{ class: highlight_if_unseen_class(avis_seen_at, avis.updated_at) }
|
||||||
Réponse donnée le #{l(avis.updated_at, format: '%d/%m/%y à %H:%M')}
|
Réponse donnée le #{l(avis.updated_at, format: '%d/%m/%y à %H:%M')}
|
||||||
- else
|
- else
|
||||||
%span.waiting
|
%span.waiting
|
||||||
En attente de réponse
|
En attente de réponse
|
||||||
= link_to("Révoquer l'avis", revoquer_instructeur_avis_path(avis.procedure, avis), data: { confirm: "Souhaitez-vous révoquer la demande d'avis à #{avis.email_to_display} ?" }, method: :patch)
|
- if avis.revokable_by?(current_instructeur)
|
||||||
|
= link_to("Révoquer l'avis", revoquer_instructeur_avis_path(avis.procedure, avis), data: { confirm: "Souhaitez-vous révoquer la demande d'avis à #{avis.email_to_display} ?" }, method: :patch)
|
||||||
- if avis.piece_justificative_file.attached?
|
- if avis.piece_justificative_file.attached?
|
||||||
= render partial: 'shared/attachment/show', locals: { attachment: avis.piece_justificative_file.attachment }
|
= render partial: 'shared/attachment/show', locals: { attachment: avis.piece_justificative_file.attachment }
|
||||||
.answer-body
|
.answer-body
|
||||||
|
|
|
@ -48,7 +48,7 @@ describe Instructeurs::AvisController, type: :controller do
|
||||||
|
|
||||||
context 'with a revoked avis' do
|
context 'with a revoked avis' do
|
||||||
it "refuse l'accès au dossier" do
|
it "refuse l'accès au dossier" do
|
||||||
avis_with_answer.revoke!
|
avis_with_answer.update!(revoked_at: Time.zone.now)
|
||||||
subject
|
subject
|
||||||
expect(flash.alert).to eq("Vous n'avez plus accès à ce dossier.")
|
expect(flash.alert).to eq("Vous n'avez plus accès à ce dossier.")
|
||||||
expect(response).to redirect_to(root_path)
|
expect(response).to redirect_to(root_path)
|
||||||
|
@ -273,7 +273,7 @@ describe Instructeurs::AvisController, type: :controller do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#revoker" do
|
describe "#revoker" do
|
||||||
let(:avis) { create(:avis) }
|
let(:avis) { create(:avis, claimant: instructeur) }
|
||||||
let(:procedure) { avis.procedure }
|
let(:procedure) { avis.procedure }
|
||||||
|
|
||||||
it "revoke the dossier" do
|
it "revoke the dossier" do
|
||||||
|
|
|
@ -132,25 +132,72 @@ RSpec.describe Avis, type: :model do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".revoke!" do
|
describe ".revoke_by!" do
|
||||||
|
let(:claimant) { create(:instructeur) }
|
||||||
|
|
||||||
context "when no answer" do
|
context "when no answer" do
|
||||||
let(:avis) { create(:avis) }
|
let(:avis) { create(:avis, claimant: claimant) }
|
||||||
|
|
||||||
it "supprime l'avis" do
|
it "supprime l'avis" do
|
||||||
avis.revoke!
|
avis.revoke_by!(claimant)
|
||||||
expect(avis).to be_destroyed
|
expect(avis).to be_destroyed
|
||||||
expect(Avis.count).to eq 0
|
expect(Avis.count).to eq 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with answer" do
|
context "with answer" do
|
||||||
let(:avis) { create(:avis, :with_answer) }
|
let(:avis) { create(:avis, :with_answer, claimant: claimant) }
|
||||||
|
|
||||||
it "revoque l'avis" do
|
it "revoque l'avis" do
|
||||||
avis.revoke!
|
avis.revoke_by!(claimant)
|
||||||
expect(avis).not_to be_destroyed
|
expect(avis).not_to be_destroyed
|
||||||
expect(avis).to be_revoked
|
expect(avis).to be_revoked
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "by an instructeur who can't revoke" do
|
||||||
|
let(:avis) { create(:avis, :with_answer, claimant: claimant) }
|
||||||
|
let(:expert) { create(:instructeur) }
|
||||||
|
|
||||||
|
it "doesn't revoke avis and returns false" do
|
||||||
|
result = avis.revoke_by!(expert)
|
||||||
|
expect(result).to be_falsey
|
||||||
|
expect(avis).not_to be_destroyed
|
||||||
|
expect(avis).not_to be_revoked
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "revokable_by?" do
|
||||||
|
let(:instructeur) { create(:instructeur) }
|
||||||
|
let(:instructeurs) { [instructeur] }
|
||||||
|
let(:procedure) { create(:procedure, :published, instructeurs: instructeurs) }
|
||||||
|
let(:dossier) { create(:dossier, :en_instruction, procedure: procedure) }
|
||||||
|
let(:claimant_expert) { create(:instructeur) }
|
||||||
|
let(:expert) { create(:instructeur) }
|
||||||
|
let(:another_expert) { create(:instructeur) }
|
||||||
|
|
||||||
|
context "when avis claimed by an expert" do
|
||||||
|
let(:avis) { create(:avis, dossier: dossier, claimant: claimant_expert, instructeur: expert) }
|
||||||
|
let(:another_avis) { create(:avis, dossier: dossier, claimant: instructeur, instructeur: another_expert) }
|
||||||
|
it "is revokable by this expert or any instructeurs of the dossier" do
|
||||||
|
expect(avis.revokable_by?(claimant_expert)).to be_truthy
|
||||||
|
expect(avis.revokable_by?(another_expert)).to be_falsy
|
||||||
|
expect(avis.revokable_by?(instructeur)).to be_truthy
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when avis claimed by an instructeur" do
|
||||||
|
let(:avis) { create(:avis, dossier: dossier, claimant: instructeur, instructeur: expert) }
|
||||||
|
let(:another_avis) { create(:avis, dossier: dossier, claimant: expert, instructeur: another_expert) }
|
||||||
|
let(:another_instructeur) { create(:instructeur) }
|
||||||
|
let(:instructeurs) { [instructeur, another_instructeur] }
|
||||||
|
|
||||||
|
it "is revokable by any instructeur of the dossier, not by an expert" do
|
||||||
|
expect(avis.revokable_by?(instructeur)).to be_truthy
|
||||||
|
expect(avis.revokable_by?(another_expert)).to be_falsy
|
||||||
|
expect(avis.revokable_by?(another_instructeur)).to be_truthy
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue