refactor(correction): rename kind => reason with default correction => incorrect

This commit is contained in:
Colin Darie 2023-07-12 12:00:52 +02:00 committed by Paul Chavard
parent aceaf24e5f
commit 5998cfe31f
17 changed files with 82 additions and 52 deletions

View file

@ -48,7 +48,7 @@
- menu.with_item(class: "inactive form-inside fr-pt-1v") do
= render partial: 'instructeurs/dossiers/instruction_button_motivation', locals: { dossier:,
visible: false,
form_path: pending_correction_instructeur_dossier_path(dossier.procedure, dossier, kind: :incomplete),
form_path: pending_correction_instructeur_dossier_path(dossier.procedure, dossier, reason: :incomplete),
placeholder: 'Expliquez au demandeur comment compléter son dossier',
popup_class: 'pending_completion',
button_justificatif_label: "Ajouter une pièce jointe (facultatif)",

View file

@ -235,7 +235,7 @@ module Instructeurs
commentaire = CommentaireService.build(current_instructeur, dossier, { body: message, piece_jointe: })
if commentaire.valid?
dossier.flag_as_pending_correction!(commentaire, params[:kind].presence)
dossier.flag_as_pending_correction!(commentaire, params[:reason].presence)
dossier.update!(last_commentaire_updated_at: Time.zone.now)
current_instructeur.follow(dossier)

View file

@ -8,14 +8,14 @@ module DossierCorrectableConcern
scope :with_pending_corrections, -> { joins(:corrections).where(corrections: { resolved_at: nil }) }
def flag_as_pending_correction!(commentaire, kind = nil)
def flag_as_pending_correction!(commentaire, reason = nil)
return unless may_flag_as_pending_correction?
kind ||= :correction
reason ||= :incorrect
corrections.create!(commentaire:, kind:)
corrections.create!(commentaire:, reason:)
log_pending_correction_operation(commentaire, kind) if procedure.sva_svr_enabled?
log_pending_correction_operation(commentaire, reason) if procedure.sva_svr_enabled?
return if en_construction?
@ -48,9 +48,9 @@ module DossierCorrectableConcern
private
def log_pending_correction_operation(commentaire, kind)
operation = case kind.to_sym
when :correction
def log_pending_correction_operation(commentaire, reason)
operation = case reason.to_sym
when :incorrect
"demander_une_correction"
when :incomplete
"demander_a_completer"

View file

@ -3,7 +3,7 @@
# Table name: dossier_corrections
#
# id :bigint not null, primary key
# kind :string default("correction"), not null
# reason :string default("incorrect"), not null
# resolved_at :datetime
# created_at :datetime not null
# updated_at :datetime not null
@ -14,11 +14,13 @@ class DossierCorrection < ApplicationRecord
belongs_to :dossier
belongs_to :commentaire
self.ignored_columns += ['kind']
validates_associated :commentaire
scope :pending, -> { where(resolved_at: nil) }
enum kind: { correction: 'correction', incomplete: 'incomplete' }
enum reason: { incorrect: 'incorrect', incomplete: 'incomplete' }, _prefix: :dossier
def resolved?
resolved_at.present?

View file

@ -47,13 +47,13 @@ class SVASVRDecisionDateCalculatorService
def determine_start_date
return dossier.depose_at.to_date if dossier.corrections.empty?
return latest_correction_date if resume_method == :reset
return latest_incomplete_correction_date if dossier.corrections.any?(&:incomplete?)
return latest_incomplete_correction_date if dossier.corrections.any?(&:dossier_incomplete?)
dossier.depose_at.to_date
end
def latest_incomplete_correction_date
correction_date dossier.corrections.filter(&:incomplete?).max_by(&:resolved_at)
correction_date dossier.corrections.filter(&:dossier_incomplete?).max_by(&:resolved_at)
end
def latest_correction_date

View file

@ -3,12 +3,12 @@
%p= t(:hello, scope: [:views, :shared, :greetings])
%p= t(".#{@correction.kind}.explanation_html", dossier_id: @dossier.id, libelle_demarche: @dossier.procedure.libelle)
%p= t(".#{@correction.reason}.explanation_html", dossier_id: @dossier.id, libelle_demarche: @dossier.procedure.libelle)
%p= t('.link')
= round_button(t('.access_message'), messagerie_dossier_url(@dossier), :primary)
- if @dossier.sva_svr_decision_on.present?
%p= t(".#{@correction.kind}.sva_svr", rule_name: t(@dossier.procedure.sva? ? :sva : :svr, scope: 'shared.procedures.sva_svr_rule_name'))
%p= t(".#{@correction.reason}.sva_svr", rule_name: t(@dossier.procedure.sva? ? :sva : :svr, scope: 'shared.procedures.sva_svr_rule_name'))
= render 'layouts/mailers/signature', service: @service

View file

@ -3,7 +3,7 @@ en:
dossier_mailer:
notify_pending_correction:
subject: You need to modify your file no. %{dossier_id} « %{libelle_demarche} »
correction:
incorrect:
explanation_html:
In order to continue its instruction, <strong>an instructor requested you to edit information</strong> to your file no. %{dossier_id} of the « %{libelle_demarche} » procedure.
sva_svr:

View file

@ -3,7 +3,7 @@ fr:
dossier_mailer:
notify_pending_correction:
subject: Vous devez corriger votre dossier nº %{dossier_id} « %{libelle_demarche} »
correction:
incorrect:
explanation_html:
Afin de poursuivre son instruction, <strong>un instructeur vous demande dapporter des corrections</strong> à votre dossier nº %{dossier_id} de la démarche « %{libelle_demarche} ».
sva_svr:

View file

@ -0,0 +1,5 @@
class AddReasonToDossierCorrections < ActiveRecord::Migration[7.0]
def change
add_column :dossier_corrections, :reason, :string, default: 'incorrect', null: false
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_07_18_113920) do
ActiveRecord::Schema[7.0].define(version: 2023_07_19_112020) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
@ -337,6 +337,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_07_18_113920) do
t.datetime "resolved_at", precision: 6
t.datetime "updated_at", precision: 6, null: false
t.string "kind", default: "correction", null: false
t.string "reason", default: "incorrect", null: false
t.index ["commentaire_id"], name: "index_dossier_corrections_on_commentaire_id"
t.index ["dossier_id"], name: "index_dossier_corrections_on_dossier_id"
t.index ["resolved_at"], name: "index_dossier_corrections_on_resolved_at", where: "((resolved_at IS NULL) OR (resolved_at IS NOT NULL))"

View file

@ -0,0 +1,14 @@
namespace :after_party do
desc 'Deployment task: backfill_default_dossier_correction_reason'
task backfill_dossier_correction_reason: :environment do
puts "Running deploy task 'backfill_default_dossier_correction_reason'"
DossierCorrection.where(kind: 'correction').update_all(reason: 'incorrect')
DossierCorrection.where(kind: 'incomplete').update_all(reason: 'incomplete')
# Update task as completed. If you remove the line below, the task will
# run with every deploy (or every time you call after_party:run).
AfterParty::TaskRecord
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
end
end

View file

@ -503,13 +503,13 @@ describe Instructeurs::DossiersController, type: :controller do
describe '#pending_correction' do
let(:message) { 'do that' }
let(:justificatif) { nil }
let(:kind) { nil }
let(:reason) { nil }
subject do
post :pending_correction, params: {
procedure_id: procedure.id, dossier_id: dossier.id,
dossier: { motivation: message, justificatif_motivation: justificatif },
kind:
reason:
}, format: :turbo_stream
end
@ -535,7 +535,7 @@ describe Instructeurs::DossiersController, type: :controller do
expect(dossier.reload).to be_en_construction
expect(dossier).to be_pending_correction
expect(dossier.corrections.last).to be_correction
expect(dossier.corrections.last).to be_dossier_incorrect
end
it 'create a comment with text body' do
@ -544,10 +544,10 @@ describe Instructeurs::DossiersController, type: :controller do
end
context 'flagged as incomplete' do
let(:kind) { 'incomplete' }
let(:reason) { 'incomplete' }
it 'create a correction of incomplete kind' do
expect(dossier.corrections.last).to be_incomplete
it 'create a correction of incomplete reason' do
expect(dossier.corrections.last).to be_dossier_incomplete
end
end

View file

@ -1,8 +1,8 @@
FactoryBot.define do
factory :dossier_correction do
dossier
commentaire
kind { :correction }
commentaire { association :commentaire, dossier: dossier }
reason { :incorrect }
resolved_at { nil }
trait :resolved do

View file

@ -233,21 +233,21 @@ RSpec.describe DossierMailer, type: :mailer do
let(:procedure) { create(:procedure) }
let(:dossier) { create(:dossier, :en_construction, procedure:, sva_svr_decision_on:) }
let(:sva_svr_decision_on) { nil }
let(:kind) { :correction }
let(:reason) { :incorrect }
let(:commentaire) { create(:commentaire, dossier:) }
subject {
dossier.flag_as_pending_correction!(commentaire, kind)
dossier.flag_as_pending_correction!(commentaire, reason)
described_class.with(commentaire:).notify_pending_correction
}
context 'kind is correction' do
context 'reason is incorrect' do
it { expect(subject.subject).to eq("Vous devez corriger votre dossier nº #{dossier.id} « #{dossier.procedure.libelle} »") }
it { expect(subject.body).to include("apporter des corrections") }
it { expect(subject.body).not_to include("Silence") }
end
context 'sva with kind is correction' do
context 'sva with reason is incorrect' do
let(:sva_svr_decision_on) { Date.tomorrow }
let(:procedure) { create(:procedure, :sva) }
@ -257,9 +257,9 @@ RSpec.describe DossierMailer, type: :mailer do
it { expect(subject.body).to include("suspendu") }
end
context 'sva with kind is incomplete' do
context 'sva with reason is incomplete' do
let(:sva_svr_decision_on) { Date.tomorrow }
let(:kind) { :incomplete }
let(:reason) { :incomplete }
let(:procedure) { create(:procedure, :sva) }
it { expect(subject.body).to include("compléter") }
@ -267,9 +267,9 @@ RSpec.describe DossierMailer, type: :mailer do
it { expect(subject.body).to include("réinitialisé") }
end
context 'svr with kind is incomplete' do
context 'svr with reason is incomplete' do
let(:sva_svr_decision_on) { Date.tomorrow }
let(:kind) { :incomplete }
let(:reason) { :incomplete }
let(:procedure) { create(:procedure, :svr) }
it { expect(subject.body).to include("compléter") }

View file

@ -36,12 +36,12 @@ describe DossierCorrectableConcern do
context 'when dossier is en_construction' do
it 'creates a correction' do
expect { flag }.to change { dossier.corrections.pending.count }.by(1)
expect(dossier.corrections.last).to be_correction
expect(dossier.corrections.last).to be_dossier_incorrect
end
it 'created a correction of incomplete kind' do
expect { dossier.flag_as_pending_correction!(commentaire, "incomplete") }.to change { dossier.corrections.pending.count }.by(1)
expect(dossier.corrections.last).to be_incomplete
expect(dossier.corrections.last).to be_dossier_incomplete
end
it 'does not change dossier state' do

View file

@ -1072,30 +1072,38 @@ describe Dossier, type: :model do
let(:last_operation) { dossier.dossier_operation_logs.last }
let(:operation_serialized) { last_operation.data }
let(:instructeur) { create(:instructeur) }
let!(:correction) { create(:dossier_correction, dossier:) }
let!(:correction) { create(:dossier_correction, dossier:) } # correction has a commentaire
before { dossier.passer_en_instruction!(instructeur: instructeur) }
subject(:passer_en_instruction) { dossier.passer_en_instruction!(instructeur: instructeur) }
it { expect(dossier.state).to eq('en_instruction') }
it { expect(dossier.followers_instructeurs).to include(instructeur) }
it { expect(dossier.en_construction_close_to_expiration_notice_sent_at).to be_nil }
it { expect(last_operation.operation).to eq('passer_en_instruction') }
it { expect(last_operation.automatic_operation?).to be_falsey }
it { expect(operation_serialized['operation']).to eq('passer_en_instruction') }
it { expect(operation_serialized['dossier_id']).to eq(dossier.id) }
it { expect(operation_serialized['executed_at']).to eq(last_operation.executed_at.iso8601) }
it { expect(dossier.commentaires.count).to eq(1) }
it do
passer_en_instruction
expect(dossier.state).to eq('en_instruction')
expect(dossier.followers_instructeurs).to include(instructeur)
expect(dossier.en_construction_close_to_expiration_notice_sent_at).to be_nil
expect(last_operation.operation).to eq('passer_en_instruction')
expect(last_operation.automatic_operation?).to be_falsey
expect(operation_serialized['operation']).to eq('passer_en_instruction')
expect(operation_serialized['dossier_id']).to eq(dossier.id)
expect(operation_serialized['executed_at']).to eq(last_operation.executed_at.iso8601)
end
it { expect { passer_en_instruction }.to change { dossier.commentaires.count }.by(1) }
it "resolve pending correction" do
passer_en_instruction
expect(dossier.pending_correction?).to be_falsey
expect(correction.reload.resolved_at).to be_present
end
it 'creates a commentaire in the messagerie with expected wording' do
email_template = dossier.procedure.mail_template_for(Dossier.states.fetch(:en_instruction))
commentaire = dossier.commentaires.first
passer_en_instruction
email_template = dossier.procedure.mail_template_for(Dossier.states.fetch(:en_instruction))
commentaire = dossier.commentaires.last
expect(dossier.commentaires.count).to eq(1)
expect(commentaire.body).to include(email_template.subject_for_dossier(dossier), email_template.body_for_dossier(dossier))
expect(commentaire.dossier).to eq(dossier)
end

View file

@ -59,7 +59,7 @@ describe SVASVRDecisionDateCalculatorService do
end
end
context 'there is a pending correction kind = correct' do
context 'there is a pending correction reason = incorrect' do
before do
travel_to DateTime.new(2023, 5, 30, 18) do
dossier.flag_as_pending_correction!(build(:commentaire, dossier:))
@ -73,7 +73,7 @@ describe SVASVRDecisionDateCalculatorService do
end
end
context 'there is a pending correction kind = incomplete' do
context 'there is a pending correction reason = incomplete' do
before do
travel_to DateTime.new(2023, 5, 30, 18) do
dossier.flag_as_pending_correction!(build(:commentaire, dossier:), :incomplete)