fix(messages): retry stale blobs

This commit is contained in:
Paul Chavard 2021-11-30 18:56:12 +01:00 committed by Paul Chavard
parent 704ae54ebe
commit e7766694d3
10 changed files with 66 additions and 34 deletions

View file

@ -109,9 +109,9 @@ module Experts
end end
def create_commentaire 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) @commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now)
flash.notice = "Message envoyé" flash.notice = "Message envoyé"
redirect_to messagerie_expert_avis_path(avis.procedure, avis) redirect_to messagerie_expert_avis_path(avis.procedure, avis)

View file

@ -183,9 +183,9 @@ module Instructeurs
end end
def create_commentaire 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) @commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now)
current_instructeur.follow(dossier) current_instructeur.follow(dossier)
flash.notice = "Message envoyé" flash.notice = "Message envoyé"

View file

@ -236,8 +236,8 @@ module Instructeurs
errors = [] errors = []
email_usagers_dossiers.each do |dossier| email_usagers_dossiers.each do |dossier|
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) commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now)
else else
errors << dossier.id errors << dossier.id

View file

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

View file

@ -232,9 +232,9 @@ module Users
end end
def create_commentaire 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) @commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now)
dossier.followers_instructeurs dossier.followers_instructeurs
.with_instant_email_message_notifications .with_instant_email_message_notifications

View file

@ -11,9 +11,9 @@ module Mutations
field :errors, [Types::ValidationErrorType], null: true field :errors, [Types::ValidationErrorType], null: true
def resolve(dossier:, instructeur:, body:, attachment: nil) 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 } { message: message }
else else
{ errors: message.errors.full_messages } { errors: message.errors.full_messages }

View file

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

View file

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

View file

@ -102,7 +102,7 @@ describe Commentaire do
end end
context "with a commentaire automatically created (notification)" do 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 it "does not call notify_user" do
expect(commentaire).not_to receive(:notify_user).with(no_args) 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 context 'when an old expert has a commentaire' do
let(:dossier) { create(:dossier) } 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 before do
commentaire.save commentaire
subject subject
end end