Merge pull request #7987 from tchak/fix-graphql-envoyer-message

fix(messages): retry stale blobs
This commit is contained in:
Paul Chavard 2022-11-02 09:26:12 +01:00 committed by GitHub
commit e9f90d54ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 66 additions and 34 deletions

View file

@ -109,9 +109,9 @@ module Experts
end
def create_commentaire
@commentaire = CommentaireService.build(current_expert, avis.dossier, commentaire_params)
@commentaire = CommentaireService.create(current_expert, avis.dossier, commentaire_params)
if @commentaire.save
if @commentaire.errors.empty?
@commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now)
flash.notice = "Message envoyé"
redirect_to messagerie_expert_avis_path(avis.procedure, avis)

View file

@ -183,9 +183,9 @@ module Instructeurs
end
def create_commentaire
@commentaire = CommentaireService.build(current_instructeur, dossier, commentaire_params)
@commentaire = CommentaireService.create(current_instructeur, dossier, commentaire_params)
if @commentaire.save
if @commentaire.errors.empty?
@commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now)
current_instructeur.follow(dossier)
flash.notice = "Message envoyé"

View file

@ -236,8 +236,8 @@ module Instructeurs
errors = []
email_usagers_dossiers.each do |dossier|
commentaire = CommentaireService.build(current_instructeur, dossier, commentaire_params)
if commentaire.save
commentaire = CommentaireService.create(current_instructeur, dossier, commentaire_params)
if commentaire.errors.empty?
commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now)
else
errors << dossier.id

View file

@ -66,8 +66,7 @@ class SupportController < ApplicationController
piece_jointe: params[:piece_jointe],
body: "[#{params[:subject]}]<br><br>#{params[:text]}"
}
commentaire = CommentaireService.build(current_user, dossier, attributes)
commentaire.save!
CommentaireService.create!(current_user, dossier, attributes)
end
def tags

View file

@ -232,9 +232,9 @@ module Users
end
def create_commentaire
@commentaire = CommentaireService.build(current_user, dossier, commentaire_params)
@commentaire = CommentaireService.create(current_user, dossier, commentaire_params)
if @commentaire.save
if @commentaire.errors.empty?
@commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now)
dossier.followers_instructeurs
.with_instant_email_message_notifications

View file

@ -11,9 +11,9 @@ module Mutations
field :errors, [Types::ValidationErrorType], null: true
def resolve(dossier:, instructeur:, body:, attachment: nil)
message = CommentaireService.build(instructeur, dossier, body: body, piece_jointe: attachment)
message = CommentaireService.create(instructeur, dossier, body: body, piece_jointe: attachment)
if message.save
if message.errors.empty?
{ message: message }
else
{ errors: message.errors.full_messages }

View file

@ -70,7 +70,6 @@ class NotificationMailer < ApplicationMailer
def create_commentaire_for_notification
body = ["[#{@subject}]", @body].join("<br><br>")
commentaire = CommentaireService.build_with_email(CONTACT_EMAIL, @dossier, body: body)
commentaire.save!
CommentaireService.create!(CONTACT_EMAIL, @dossier, body: body)
end
end

View file

@ -1,24 +1,58 @@
class CommentaireService
class << self
def build(sender, dossier, params)
case sender
when Instructeur
params[:instructeur] = sender
when Expert
params[:expert] = sender
end
def self.create(sender, dossier, params)
save(dossier, prepare_params(sender, params))
end
build_with_email(sender.email, dossier, params)
def self.create!(sender, dossier, params)
save!(dossier, prepare_params(sender, params))
end
def self.build(sender, dossier, params)
dossier.commentaires.build(prepare_params(sender, params))
end
def self.prepare_params(sender, params)
case sender
when String
params[:email] = sender
when Instructeur
params[:instructeur] = sender
params[:email] = sender.email
when Expert
params[:expert] = sender
params[:email] = sender.email
else
params[:email] = sender.email
end
def build_with_email(email, dossier, params)
attributes = params.merge(email: email, dossier: dossier)
# For some reason ActiveStorage trows an error in tests if we passe an empty string here.
# I suspect it could be resolved in rails 6 by using explicit `attach()`
if attributes[:piece_jointe].blank?
attributes.delete(:piece_jointe)
end
Commentaire.new(attributes)
# For some reason ActiveStorage trows an error in tests if we passe an empty string here.
# I suspect it could be resolved in rails 6 by using explicit `attach()`
if params[:piece_jointe].blank?
params.delete(:piece_jointe)
end
params
end
def self.save(dossier, params)
build_and_save(dossier, params)
rescue ActiveRecord::StaleObjectError
build_and_save(dossier, params)
end
def self.save!(dossier, params)
build_and_save(dossier, params, raise_exception: true)
rescue ActiveRecord::StaleObjectError
build_and_save(dossier, params, raise_exception: true)
end
def self.build_and_save(dossier, params, raise_exception: false)
message = dossier.commentaires.build(params)
if raise_exception
message.save!
else
message.save
message
end
end
end

View file

@ -102,7 +102,7 @@ describe Commentaire do
end
context "with a commentaire automatically created (notification)" do
let(:commentaire) { CommentaireService.build_with_email(CONTACT_EMAIL, dossier, body: "Mon commentaire") }
let(:commentaire) { CommentaireService.build(CONTACT_EMAIL, dossier, body: "Mon commentaire") }
it "does not call notify_user" do
expect(commentaire).not_to receive(:notify_user).with(no_args)

View file

@ -54,10 +54,10 @@ RSpec.describe Expert, type: :model do
context 'when an old expert has a commentaire' do
let(:dossier) { create(:dossier) }
let(:commentaire) { CommentaireService.build(old_expert, dossier, body: "Mon commentaire") }
let(:commentaire) { CommentaireService.create(old_expert, dossier, body: "Mon commentaire") }
before do
commentaire.save
commentaire
subject
end