feat(correction): deleting a comment resolves the related correction

Cf #9731
This commit is contained in:
Colin Darie 2023-12-07 15:41:50 +01:00
parent f27ef80553
commit accd7864ff
10 changed files with 72 additions and 9 deletions

View file

@ -17,10 +17,22 @@ class Dossiers::MessageComponent < ApplicationComponent
private
def soft_deletable?
commentaire.soft_deletable?(connected_user)
end
def show_reply_button?
@show_reply_button
end
def delete_button_text
if commentaire.dossier_correction&.pending?
t('.delete_with_correction_button')
else
t('.delete_button')
end
end
def highlight_if_unseen_class
if highlight?
'highlighted'

View file

@ -3,6 +3,7 @@ en:
reply: Reply
guest: Guest
delete_button: Delete this message
delete_with_correction_button: Delete the correction and this message
confirm: Are you sure you want to delete this message ?
automatic_email: Automatic email
you: You

View file

@ -3,6 +3,7 @@ fr:
reply: Répondre
guest: Invité
delete_button: Supprimer le message
delete_with_correction_button: Supprimer la demande de correction et ce message
confirm: Êtes-vous sûr de vouloir supprimer ce message ?
automatic_email: Email automatique
you: Vous

View file

@ -13,7 +13,7 @@
= commentaire_date
.rich-text
- if commentaire.discarded?
%p= t('.deleted_body')
%p= "(#{t('.deleted_body')})"
- elsif commentaire.sent_by_system?
= sanitize(commentaire.body, scrubber: Sanitizers::MailScrubber.new)
- else
@ -21,9 +21,9 @@
.message-extras.flex.justify-start
- if commentaire.soft_deletable?(connected_user)
- if soft_deletable?
= button_to instructeur_commentaire_path(commentaire.dossier.procedure, commentaire.dossier, commentaire), method: :delete, class: 'fr-btn fr-btn--sm fr-btn--tertiary fr-icon-delete-line fr-btn--icon-left fr-text-default--warning', form: { data: { turbo: true, turbo_confirm: t('.confirm') } } do
= t('.delete_button')
= delete_button_text
- if commentaire.piece_jointe.attached?
.fr-ml-2w

View file

@ -74,9 +74,13 @@ class Commentaire < ApplicationRecord
end
def soft_delete!
transaction do
discard!
dossier_correction&.resolve!
update! body: ''
end
piece_jointe.purge_later if piece_jointe.attached?
discard!
update! body: ''
end
def flagged_pending_correction?

View file

@ -41,9 +41,7 @@ module DossierCorrectableConcern
end
def resolve_pending_correction
return if pending_correction.nil?
pending_correction.resolved_at = Time.current
pending_correction&.resolve
end
def resolve_pending_correction!

View file

@ -15,4 +15,15 @@ class DossierCorrection < ApplicationRecord
def resolved?
resolved_at.present?
end
def resolve
self.resolved_at = Time.current
end
def resolve!
resolve
save!
end
def pending? = !resolved?
end

View file

@ -1,3 +1,6 @@
- if @commentaire.discarded?
= turbo_stream.update @commentaire do
= render Dossiers::MessageComponent.new(commentaire: @commentaire, connected_user: @commentaire.instructeur || @commentaire.expert)
- if current_user.instructeur? && @commentaire.dossier_correction.present?
= turbo_stream.replace 'header-top', partial: 'instructeurs/dossiers/header_top', locals: { dossier: @commentaire.dossier }

View file

@ -69,7 +69,10 @@ RSpec.describe Dossiers::MessageComponent, type: :component do
context 'on a procedure where commentaire had been written by connected instructeur' do
let(:commentaire) { create(:commentaire, instructeur: instructeur, body: 'Second message') }
it { is_expected.to have_selector("form[action=\"#{form_url}\"]") }
it do
is_expected.to have_selector("form[action=\"#{form_url}\"]")
is_expected.to have_button(component.t('.delete_button'))
end
end
context 'on a procedure where commentaire had been written by connected instructeur and discarded' do
@ -96,6 +99,13 @@ RSpec.describe Dossiers::MessageComponent, type: :component do
it { is_expected.not_to have_selector("form[action=\"#{form_url}\"]") }
end
context 'when commentaire is a correction' do
let(:commentaire) { create(:commentaire, instructeur:, body: 'Please fix this') }
before { create(:dossier_correction, commentaire:, dossier:) }
it { is_expected.to have_button(component.t('.delete_with_correction_button')) }
end
end
describe 'autolink simple urls' do

View file

@ -20,6 +20,29 @@ describe Instructeurs::CommentairesController, type: :controller do
expect(subject.body).to include('Message supprimé')
expect(subject.body).to include('alert-success')
expect(subject.body).to include('Votre message a été supprimé')
expect(commentaire.reload).to be_discarded
expect(commentaire.body).to be_empty
end
context 'when instructeur is not owner' do
let(:commentaire) { create(:commentaire, dossier: dossier) }
it 'does not delete the message' do
expect(subject.body).to include('alert-danger')
expect(commentaire.reload).not_to be_discarded
expect(commentaire.body).not_to be_empty
end
end
context 'when a correction is attached' do
let!(:correction) { create(:dossier_correction, commentaire:, dossier:) }
it 'removes the correction' do
expect(subject).to have_http_status(:ok)
expect(subject.body).to include('en construction') # update the header
expect(subject.body).not_to include('en attente')
expect(correction.reload).to be_resolved
end
end
end