feat(CommentairesController#destroy): implement destroy endpoint using CommentaireService

This commit is contained in:
Martin 2021-11-15 13:18:10 +01:00
parent 9d666b32bc
commit 8b931a57d4
2 changed files with 51 additions and 0 deletions

View file

@ -0,0 +1,17 @@
# frozen_string_literal: true
module Instructeurs
class CommentairesController < ProceduresController
def destroy
result = CommentaireService.soft_delete(current_instructeur, params.permit(:dossier_id, :commentaire_id))
if result.status
flash_message = { notice: 'Votre commentaire a bien été supprimé' }
else
flash_message = { error: "Votre commentaire ne peut être supprimé: #{result.error_messages}" }
end
redirect_to(messagerie_instructeur_dossier_path(params[:procedure_id], params[:dossier_id]),
flash: flash_message)
end
end
end

View file

@ -0,0 +1,34 @@
# frozen_string_literal: true
describe Instructeurs::CommentairesController, type: :controller do
let(:instructeur) { create(:instructeur) }
let(:procedure) { create(:procedure, :published, :for_individual, instructeurs: [instructeur]) }
let(:dossier) { create(:dossier, :en_construction, :with_individual, procedure: procedure) }
before { sign_in(instructeur.user) }
describe 'destroy' do
let(:commentaire) { create(:commentaire, instructeur: instructeur)}
subject { delete :destroy, params: { dossier_id: dossier.id, procedure_id: procedure.id, id: commentaire.id } }
it 'redirect to dossier' do
expect(subject).to redirect_to(messagerie_instructeur_dossier_path(dossier.procedure, dossier))
end
it 'flash success' do
subject
expect(flash[:success]).to eq('Votre commentaire a bien été supprimé')
end
context 'when it fails' do
let(:error) { OpenStruct.new(status: false, error_messages: "boom") }
before do
expect(CommentaireService).to receive(:soft_delete).and_return(error)
end
it 'redirect to dossier' do
expect(subject).to redirect_to(messagerie_instructeur_dossier_path(dossier.procedure, dossier))
end
it 'flash success' do
subject
expect(flash[:error]).to eq("Votre commentaire ne peut être supprimé: #{error.error_messages}")
end
end
end
end