fix(commentaire): unified destroy commentaire

This commit is contained in:
Paul Chavard 2022-04-19 09:40:59 +02:00
parent 009c44cc20
commit 91879b89ce
16 changed files with 88 additions and 51 deletions

View file

@ -1,21 +1,33 @@
# frozen_string_literal: true
module Instructeurs
class CommentairesController < ProceduresController
after_action :mark_messagerie_as_read
def destroy
commentaire = Dossier.find(params[:dossier_id]).commentaires.find(params[:id])
if commentaire.sent_by?(current_instructeur)
commentaire.piece_jointe.purge_later if commentaire.piece_jointe.attached?
commentaire.discard!
commentaire.update!(body: '')
flash[:notice] = t('views.shared.commentaires.destroy.notice')
if commentaire.sent_by?(current_instructeur) || commentaire.sent_by?(current_expert)
commentaire.soft_delete!
flash.notice = t('.notice')
else
flash[:alert] = I18n.t('views.shared.commentaires.destroy.alert_reasons.acl')
flash.alert = t('.alert_acl')
end
redirect_to(messagerie_instructeur_dossier_path(params[:procedure_id], params[:dossier_id]))
rescue Discard::RecordNotDiscarded
flash[:alert] = I18n.t('views.shared.commentaires.destroy.alert_reasons.already_discarded')
redirect_to(messagerie_instructeur_dossier_path(params[:procedure_id], params[:dossier_id]))
# i18n-tasks-use t('instructeurs.commentaires.destroy.alert_already_discarded')
flash.alert = t('.alert_already_discarded')
end
private
def mark_messagerie_as_read
if commentaire.sent_by?(current_instructeur)
current_instructeur.mark_tab_as_seen(commentaire.dossier, :messagerie)
end
end
def commentaire
@commentaire ||= Dossier
.find(params[:dossier_id])
.commentaires
.find(params[:id])
end
end
end

View file

@ -89,6 +89,12 @@ class Commentaire < ApplicationRecord
end
end
def soft_delete!
piece_jointe.purge_later if piece_jointe.attached?
discard!
update! body: ''
end
private
def notify

View file

@ -0,0 +1,2 @@
- if @commentaire.discarded?
= turbo_stream.update @commentaire, partial: "shared/dossiers/messages/message", locals: { commentaire: @commentaire, connected_user: @commentaire.instructeur || @commentaire.expert, messagerie_seen_at: nil, show_reply_button: false }

View file

@ -1,7 +1,7 @@
.messagerie.container
%ul.messages-list
- dossier.commentaires.with_attached_piece_jointe.each do |commentaire|
%li.message{ class: commentaire_is_from_me_class(commentaire, connected_user) }
%li.message{ class: commentaire_is_from_me_class(commentaire, connected_user), id: dom_id(commentaire) }
= render partial: "shared/dossiers/messages/message", locals: { commentaire: commentaire, connected_user: connected_user, messagerie_seen_at: messagerie_seen_at, show_reply_button: show_reply_button(commentaire, connected_user) }
- if dossier.messagerie_available?

View file

@ -5,17 +5,16 @@
%span.mail
= render partial: 'shared/dossiers/messages/message_issuer', locals: { commentaire: commentaire, connected_user: connected_user }
- if commentaire_is_from_guest(commentaire)
%span.guest= t('views.shared.dossiers.messages.message.guest')
%span.guest= t('.guest')
%span.date{ class: highlight_if_unseen_class(messagerie_seen_at, commentaire.created_at) }
= commentaire_date(commentaire)
.rich-text= pretty_commentaire(commentaire)
.message-extras.flex.justify-start
- if commentaire.soft_deletable?(connected_user)
- path = connected_user.is_a?(Instructeur) ? instructeur_commentaire_path(commentaire.dossier.procedure, commentaire.dossier, commentaire) : delete_commentaire_expert_avis_path(@avis.procedure, @avis, commentaire: commentaire)
= button_to path, method: :delete, class: 'button danger', data: { confirm: t('views.shared.commentaires.destroy.confirm') } do
= button_to instructeur_commentaire_path(commentaire.dossier.procedure, commentaire.dossier, commentaire), method: :delete, class: 'button danger', form: { data: { turbo: true, turbo_confirm: t('.confirm') } } do
%span.icon.delete
= t('views.shared.commentaires.destroy.button')
= t('.delete_button')
- if commentaire.piece_jointe.attached?
.attachment-link
@ -24,4 +23,4 @@
- if show_reply_button
= button_tag type: 'button', class: 'button small message-answer-button', onclick: 'document.querySelector("#commentaire_body").focus()' do
%span.icon.reply
= t('views.shared.dossiers.messages.message.reply')
= t('.reply')

View file

@ -130,9 +130,6 @@ en:
message_issuer:
automatic_email: "Automatic email"
you: "You"
message:
reply: "Reply"
guest: "Guest"
form:
send_message: "Send message"
attachment_size: "(attachment size max : 20 Mo)"

View file

@ -125,9 +125,6 @@ fr:
message_issuer:
automatic_email: "Email automatique"
you: "Vous"
message:
reply: "Répondre"
guest: "Invité"
form:
send_message: "Envoyer le message"
attachment_size: "(taille max : 20 Mo)"

View file

@ -0,0 +1,7 @@
en:
instructeurs:
commentaires:
destroy:
notice: Your message had been deleted
alert_acl: "Can not destroy message: it does not belong to you"
alert_already_discarded: "Can not destroy message: it was already destroyed"

View file

@ -0,0 +1,7 @@
fr:
instructeurs:
commentaires:
destroy:
notice: Votre message a été supprimé
alert_acl: Impossible de supprimer le message, celui ci ne vous appartient pas
alert_already_discarded: Ce message a déjà été supprimé

View file

@ -1,5 +1,7 @@
en:
instructeurs:
commentaires_controller:
alert_already_discarded: "Can not destroy message: it was already destroyed"
procedure:
archive_pending_html: Archive creation pending<br>(requested %{created_period} ago)
archive_ready_html: Download archive<br>(requested %{generated_period} ago)
archive_ready_html: Download archive<br>(requested %{generated_period} ago)

View file

@ -1,5 +1,7 @@
fr:
instructeurs:
commentaires_controller:
alert_already_discarded: Ce message a déjà été supprimé
procedure:
archive_pending_html: Archive en cours de création<br>(demandée il y a %{created_period})
archive_ready_html: Télécharger larchive<br>(demandée il y a %{generated_period})

View file

@ -0,0 +1,9 @@
en:
shared:
dossiers:
messages:
message:
reply: Reply
guest: Guest
delete_button: Delete this message
confirm: Are you sure you want to delete this message ?

View file

@ -0,0 +1,9 @@
fr:
shared:
dossiers:
messages:
message:
reply: Répondre
guest: Invité
delete_button: Supprimer le message
confirm: Êtes-vous sûr de vouloir supprimer ce message ?

View file

@ -22,10 +22,4 @@ en:
signin: 'Sign in'
commentaires:
destroy:
button: 'Destroy this message'
confirm: "Are you sure you want to destroy this message ?"
deleted_body: Message deleted
notice: 'Your message had been deleted'
alert_reasons:
acl: "Can not destroy message: it does not belong to you"
already_discarded: "Can not destroy message: it was already destroyed"

View file

@ -22,10 +22,4 @@ fr:
signin: 'Connexion'
commentaires:
destroy:
button: 'Supprimer le message'
confirm: "Êtes-vous sûr de vouloir supprimer ce message ?"
deleted_body: Message supprimé
notice: 'Votre message a été supprimé'
alert_reasons:
acl: "Impossible de supprimer le message, celui ci ne vous appartient pas"
already_discarded: "Ce message a déjà été supprimé"

View file

@ -4,33 +4,33 @@ 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) }
render_views
before { sign_in(instructeur.user) }
describe 'destroy' do
render_views
context 'when it works' do
let(:commentaire) { create(:commentaire, instructeur: instructeur, dossier: dossier) }
subject { delete :destroy, params: { dossier_id: dossier.id, procedure_id: procedure.id, id: commentaire.id } }
subject { delete :destroy, params: { dossier_id: dossier.id, procedure_id: procedure.id, id: commentaire.id }, format: :turbo_stream }
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[:notice]).to eq(I18n.t('views.shared.commentaires.destroy.notice'))
it 'respond with OK and flash' do
expect(subject).to have_http_status(:ok)
expect(subject.body).to include('Message supprimé')
expect(subject.body).to include('alert-success')
expect(subject.body).to include('Votre message a été supprimé')
end
end
context 'when dossier had been discarded' do
let(:commentaire) { create(:commentaire, instructeur: instructeur, dossier: dossier, discarded_at: 2.hours.ago) }
subject { delete :destroy, params: { dossier_id: dossier.id, procedure_id: procedure.id, id: commentaire.id } }
subject { delete :destroy, params: { dossier_id: dossier.id, procedure_id: procedure.id, id: commentaire.id }, format: :turbo_stream }
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[:alert]).to eq(I18n.t('views.shared.commentaires.destroy.alert_reasons.already_discarded'))
it 'respond with OK and flash' do
expect(subject).to have_http_status(:ok)
expect(subject.body).to include('alert-danger')
expect(subject.body).to include('Ce message a déjà été supprimé')
end
end
end