diff --git a/app/controllers/support_controller.rb b/app/controllers/support_controller.rb
index b907e20a5..307407a79 100644
--- a/app/controllers/support_controller.rb
+++ b/app/controllers/support_controller.rb
@@ -64,7 +64,7 @@ class SupportController < ApplicationController
file: params[:file],
body: "[#{params[:subject]}]
#{params[:text]}"
}
- commentaire = CommentaireService.build_with_email(email, dossier, attributes)
+ commentaire = CommentaireService.build(current_user, dossier, attributes)
commentaire.save!
end
@@ -82,7 +82,7 @@ class SupportController < ApplicationController
end
def direct_message?
- user_signed_in? && params[:type] == Helpscout::FormAdapter::TYPE_INSTRUCTION && dossier.present? && !dossier.brouillon?
+ user_signed_in? && params[:type] == Helpscout::FormAdapter::TYPE_INSTRUCTION && dossier.present? && dossier.messagerie_available?
end
def dossier
diff --git a/app/helpers/commentaire_helper.rb b/app/helpers/commentaire_helper.rb
index 684ff2356..a92b48b3c 100644
--- a/app/helpers/commentaire_helper.rb
+++ b/app/helpers/commentaire_helper.rb
@@ -1,12 +1,12 @@
module CommentaireHelper
def commentaire_is_from_me_class(commentaire, connected_user)
- if commentaire_is_from_me(commentaire, connected_user)
+ if commentaire.sent_by?(connected_user)
"from-me"
end
end
def commentaire_answer_action(commentaire, connected_user)
- if commentaire_is_from_me(commentaire, connected_user)
+ if commentaire.sent_by?(connected_user)
"Envoyer un message à l’instructeur"
else
"Répondre dans la messagerie"
@@ -22,10 +22,4 @@ module CommentaireHelper
template = is_current_year ? :message_date : :message_date_with_year
I18n.l(commentaire.created_at, format: template)
end
-
- private
-
- def commentaire_is_from_me(commentaire, connected_user)
- commentaire.email == connected_user.email
- end
end
diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb
index 866593071..0bf5356df 100644
--- a/app/models/commentaire.rb
+++ b/app/models/commentaire.rb
@@ -7,6 +7,7 @@ class Commentaire < ApplicationRecord
mount_uploader :file, CommentaireFileUploader
validates :file, file_size: { maximum: 20.megabytes, message: "La taille du fichier doit être inférieure à 20 Mo" }
validate :is_virus_free?
+ validate :messagerie_available?, on: :create
validates :body, presence: { message: "Votre message ne peut être vide" }
default_scope { order(created_at: :asc) }
@@ -14,10 +15,6 @@ class Commentaire < ApplicationRecord
after_create :notify
- def self.columns
- super.reject { |c| c.name == "champ" }
- end
-
def email
if user
user.email
@@ -29,15 +26,26 @@ class Commentaire < ApplicationRecord
end
def header
- "#{sender}, #{I18n.l(created_at, format: '%d %b %Y %H:%M')}"
+ "#{redacted_email}, #{I18n.l(created_at, format: '%d %b %Y %H:%M')}"
end
- def sender
- if email.present?
- email.split('@').first
+ def redacted_email
+ if gestionnaire.present?
+ gestionnaire.email.split('@').first
+ else
+ email
end
end
+ def sent_by_system?
+ [CONTACT_EMAIL, OLD_CONTACT_EMAIL].include?(email) &&
+ user.nil? && gestionnaire.nil?
+ end
+
+ def sent_by?(someone)
+ email == someone.email
+ end
+
def file_url
if Flipflop.remote_storage?
RemoteDownloader.new(file.path).url
@@ -71,4 +79,11 @@ class Commentaire < ApplicationRecord
errors.add(:file, "Virus détecté dans le fichier joint, merci de changer de fichier")
end
end
+
+ def messagerie_available?
+ return if sent_by_system?
+ if dossier.present? && !dossier.messagerie_available?
+ errors.add(:dossier, "Il n’est pas possible d’envoyer un message sur un dossier archivé ou en brouillon")
+ end
+ end
end
diff --git a/app/views/shared/dossiers/_messagerie.html.haml b/app/views/shared/dossiers/_messagerie.html.haml
index c50c17eaa..de7c34713 100644
--- a/app/views/shared/dossiers/_messagerie.html.haml
+++ b/app/views/shared/dossiers/_messagerie.html.haml
@@ -4,7 +4,7 @@
%li.message{ class: commentaire_is_from_me_class(commentaire, connected_user) }
= render partial: "shared/dossiers/messages/message", locals: { commentaire: commentaire, connected_user: connected_user, messagerie_seen_at: messagerie_seen_at }
- - if dossier.archived?
- = render partial: "shared/dossiers/messages/messagerie_disabled", locals: { service: dossier.procedure.service }
- - else
+ - if dossier.messagerie_available?
= render partial: "shared/dossiers/messages/form", locals: { commentaire: new_commentaire, form_url: form_url }
+ - else
+ = render partial: "shared/dossiers/messages/messagerie_disabled", locals: { service: dossier.procedure.service }
diff --git a/app/views/shared/dossiers/messages/_message_icon.html.haml b/app/views/shared/dossiers/messages/_message_icon.html.haml
index cca3f3568..0d8eb2db1 100644
--- a/app/views/shared/dossiers/messages/_message_icon.html.haml
+++ b/app/views/shared/dossiers/messages/_message_icon.html.haml
@@ -1,8 +1,7 @@
-- case commentaire.email
-- when connected_user.email
- = image_tag('icons/account-circle.svg', class: 'person-icon')
-- when OLD_CONTACT_EMAIL
-- when CONTACT_EMAIL
+- if commentaire.sent_by_system?
= image_tag('icons/mail.svg', class: 'person-icon')
+- elsif commentaire.sent_by?(connected_user)
+ = image_tag('icons/account-circle.svg', class: 'person-icon')
- else
= image_tag('icons/blue-person.svg', class: 'person-icon')
+
diff --git a/app/views/shared/dossiers/messages/_message_issuer.html.haml b/app/views/shared/dossiers/messages/_message_issuer.html.haml
index 23cb7dbfc..899a76683 100644
--- a/app/views/shared/dossiers/messages/_message_issuer.html.haml
+++ b/app/views/shared/dossiers/messages/_message_issuer.html.haml
@@ -1,8 +1,6 @@
-- case commentaire.email
-- when connected_user.email
- Vous
-- when OLD_CONTACT_EMAIL
-- when CONTACT_EMAIL
+- if commentaire.sent_by_system?
Email automatique
+- elsif commentaire.sent_by?(connected_user)
+ Vous
- else
- = commentaire.sender
+ = commentaire.redacted_email
diff --git a/spec/factories/commentaire.rb b/spec/factories/commentaire.rb
index db2824699..811cf0501 100644
--- a/spec/factories/commentaire.rb
+++ b/spec/factories/commentaire.rb
@@ -4,7 +4,7 @@ FactoryBot.define do
before(:create) do |commentaire, _evaluator|
if !commentaire.dossier
- commentaire.dossier = create :dossier
+ commentaire.dossier = create :dossier, :en_construction
end
end
end
diff --git a/spec/helpers/commentaire_helper_spec.rb b/spec/helpers/commentaire_helper_spec.rb
index 80150e52b..c295535e7 100644
--- a/spec/helpers/commentaire_helper_spec.rb
+++ b/spec/helpers/commentaire_helper_spec.rb
@@ -32,7 +32,7 @@ RSpec.describe CommentaireHelper, type: :helper do
end
describe '.commentaire_is_from_guest' do
- let(:dossier) { create(:dossier) }
+ let(:dossier) { create(:dossier, :en_instruction) }
let!(:guest) { create(:invite, dossier: dossier) }
subject { commentaire_is_from_guest(commentaire) }
diff --git a/spec/models/commentaire_spec.rb b/spec/models/commentaire_spec.rb
index 9822f15db..fe27728ea 100644
--- a/spec/models/commentaire_spec.rb
+++ b/spec/models/commentaire_spec.rb
@@ -7,12 +7,66 @@ describe Commentaire do
it { is_expected.to have_db_column(:updated_at) }
it { is_expected.to belong_to(:dossier) }
+ describe 'messagerie_available validation' do
+ subject { commentaire.valid?(:create) }
+
+ context 'with a commentaire created by the DS system' do
+ let(:commentaire) { build :commentaire, email: CONTACT_EMAIL }
+
+ it { is_expected.to be_truthy }
+ end
+
+ context 'on an archived dossier' do
+ let(:dossier) { create :dossier, :archived }
+ let(:commentaire) { build :commentaire, dossier: dossier }
+
+ it { is_expected.to be_falsey }
+ end
+
+ context 'on a dossier en_construction' do
+ let(:dossier) { create :dossier, :en_construction }
+ let(:commentaire) { build :commentaire, dossier: dossier }
+
+ it { is_expected.to be_truthy }
+ end
+ end
+
+ describe "#sent_by_system?" do
+ subject { commentaire.sent_by_system? }
+
+ let(:commentaire) { build :commentaire, email: email }
+
+ context 'with a commentaire created by the DS system' do
+ let(:email) { CONTACT_EMAIL }
+
+ it { is_expected.to be_truthy }
+ end
+ end
+
+ describe "#redacted_email" do
+ subject { commentaire.redacted_email }
+
+ context 'with a commentaire created by a gestionnaire' do
+ let(:commentaire) { build :commentaire, gestionnaire: gestionnaire }
+ let(:gestionnaire) { build :gestionnaire, email: 'some_user@exemple.fr' }
+
+ it { is_expected.to eq 'some_user' }
+ end
+
+ context 'with a commentaire created by a user' do
+ let(:commentaire) { build :commentaire, user: user }
+ let(:user) { build :user, email: 'some_user@exemple.fr' }
+
+ it { is_expected.to eq 'some_user@exemple.fr' }
+ end
+ end
+
describe "#notify" do
let(:procedure) { create(:procedure) }
let(:gestionnaire) { create(:gestionnaire) }
let(:assign_to) { create(:assign_to, gestionnaire: gestionnaire, procedure: procedure) }
let(:user) { create(:user) }
- let(:dossier) { create(:dossier, procedure: procedure, user: user) }
+ let(:dossier) { create(:dossier, :en_construction, procedure: procedure, user: user) }
let(:commentaire) { Commentaire.new(dossier: dossier, body: "Mon commentaire") }
context "with a commentaire created by a gestionnaire" do
diff --git a/spec/views/shared/dossiers/messages/message.html.haml_spec.rb b/spec/views/shared/dossiers/messages/message.html.haml_spec.rb
index 585e3507c..725fdf50c 100644
--- a/spec/views/shared/dossiers/messages/message.html.haml_spec.rb
+++ b/spec/views/shared/dossiers/messages/message.html.haml_spec.rb
@@ -3,7 +3,7 @@ describe 'shared/dossiers/messages/message.html.haml', type: :view do
subject { render 'shared/dossiers/messages/message.html.haml', commentaire: commentaire, messagerie_seen_at: seen_at, connected_user: dossier.user }
- let(:dossier) { create(:dossier) }
+ let(:dossier) { create(:dossier, :en_construction) }
let(:commentaire) { create(:commentaire, dossier: dossier) }
let(:seen_at) { commentaire.created_at + 1.hour }