Skip to content

Commit

Permalink
Merge pull request #9818 from colinux/correction-remove-from-message
Browse files Browse the repository at this point in the history
ETQ instructeur, ma demande de correction est supprimée (résolue) lorsque je supprime son message associé
  • Loading branch information
colinux authored Dec 11, 2023
2 parents aaf0bf5 + 17c4b62 commit 968b2cf
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 9 deletions.
12 changes: 12 additions & 0 deletions app/components/dossiers/message_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@ def correction_badge

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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ en:
reply: Reply
guest: Guest
delete_button: Delete this message
delete_with_correction_button: Delete the correction request and this message
confirm: Are you sure you want to delete this message ?
automatic_email: Automatic email
you: You
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
= 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
= render SimpleFormatComponent.new(commentaire.body, allow_a: false, allow_autolink: commentaire.sent_by_instructeur?)


.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
Expand Down
8 changes: 6 additions & 2 deletions app/models/commentaire.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ def file_url
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?
Expand Down
4 changes: 1 addition & 3 deletions app/models/concerns/dossier_correctable_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ def pending_correction?
end

def resolve_pending_correction
return if pending_correction.nil?

pending_correction.resolved_at = Time.current
pending_correction&.resolve
end

def resolve_pending_correction!
Expand Down
11 changes: 11 additions & 0 deletions app/models/dossier_correction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions app/views/instructeurs/commentaires/destroy.turbo_stream.haml
Original file line number Diff line number Diff line change
@@ -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 }
12 changes: 11 additions & 1 deletion spec/components/dossiers/message_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@
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
Expand All @@ -96,6 +99,13 @@

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
Expand Down
23 changes: 23 additions & 0 deletions spec/controllers/instructeurs/commentaires_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@
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

Expand Down

0 comments on commit 968b2cf

Please sign in to comment.