fix(dossier_correction): don't allow to save with a missing (invalid) commentaire

This commit is contained in:
Colin Darie 2023-06-02 12:43:08 +02:00
parent b495e0aff0
commit 52c8553576
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
5 changed files with 24 additions and 7 deletions

View file

@ -1,4 +1,4 @@
= render Dropdown::MenuComponent.new(wrapper: :div, button_options: { class: "fr-btn--secondary" }, wrapper_options: { data: {'turbo-force': true} }, role: :region) do |menu|
= render Dropdown::MenuComponent.new(wrapper: :div, menu_options: { id: "menu-en-construction" }, button_options: { class: "fr-btn--secondary" }, role: :region) do |menu|
- menu.with_button_inner_html do
= menu_label

View file

@ -232,11 +232,16 @@ module Instructeurs
flash.alert = dossier.termine? ? "Impossible de demander de corriger un dossier terminé." : "Le dossier est déjà en attente de correction."
else
commentaire = CommentaireService.build(current_instructeur, dossier, { body: message, piece_jointe: })
dossier.flag_as_pending_correction!(commentaire)
dossier.update!(last_commentaire_updated_at: Time.zone.now)
current_instructeur.follow(dossier)
flash.notice = "Dossier marqué comme en attente de correction."
if commentaire.valid?
dossier.flag_as_pending_correction!(commentaire)
dossier.update!(last_commentaire_updated_at: Time.zone.now)
current_instructeur.follow(dossier)
flash.notice = "Dossier marqué comme en attente de correction."
else
flash.alert = commentaire.errors.full_messages.map { "Commentaire : #{_1}" }
end
end
respond_to do |format|

View file

@ -7,7 +7,7 @@ module DossierCorrectableConcern
def flag_as_pending_correction!(commentaire)
return unless may_flag_as_pending_correction?
corrections.create(commentaire:)
corrections.create!(commentaire:)
return if en_construction?
@ -32,7 +32,7 @@ module DossierCorrectableConcern
end
def resolve_pending_correction!
corrections.pending.update(resolved_at: Time.current)
corrections.pending.update!(resolved_at: Time.current)
end
end
end

View file

@ -13,5 +13,7 @@ class DossierCorrection < ApplicationRecord
belongs_to :dossier
belongs_to :commentaire
validates_associated :commentaire
scope :pending, -> { where(resolved_at: nil) }
end

View file

@ -547,6 +547,16 @@ describe Instructeurs::DossiersController, type: :controller do
end
end
context 'with an invalid comment / attachment' do
let(:justificatif) { Rack::Test::UploadedFile.new(Rails.root.join('Gemfile.lock'), 'text/lock') }
it 'does not save anything' do
expect(dossier.reload).not_to be_pending_correction
expect(dossier.commentaires.count).to eq(0)
expect(response.body).to include('pas dun type accepté')
end
end
context 'with an empty message' do
let(:message) { '' }