feat(sva): log operation when instructeur requests a correction

This commit is contained in:
Colin Darie 2023-06-20 16:15:18 +02:00
parent 512f6ca0ec
commit b4e6c20bbd
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
6 changed files with 87 additions and 13 deletions

View file

@ -8,13 +8,15 @@ module DossierCorrectableConcern
scope :with_pending_corrections, -> { joins(:corrections).where(corrections: { resolved_at: nil }) }
def flag_as_pending_correction!(commentaire, kind)
def flag_as_pending_correction!(commentaire, kind = nil)
return unless may_flag_as_pending_correction?
kind ||= :correction
corrections.create!(commentaire:, kind:)
log_pending_correction_operation(commentaire, kind) if procedure.sva_svr_enabled?
return if en_construction?
repasser_en_construction_with_pending_correction!(instructeur: commentaire.instructeur)
@ -43,5 +45,18 @@ module DossierCorrectableConcern
pending_corrections.update!(resolved_at: Time.current)
pending_corrections.reset
end
private
def log_pending_correction_operation(commentaire, kind)
operation = case kind.to_sym
when :correction
"demander_une_correction"
when :incomplete
"demander_a_completer"
end
log_dossier_operation(commentaire.instructeur, operation, commentaire)
end
end
end

View file

@ -928,7 +928,13 @@ class Dossier < ApplicationRecord
save!
NotificationMailer.send_en_instruction_notification(self).deliver_later
log_automatic_dossier_operation(:passer_en_instruction)
if procedure.sva_svr_enabled?
# TODO: handle serialization errors when SIRET demandeur was not completed
log_automatic_dossier_operation(:passer_en_instruction, self)
else
log_automatic_dossier_operation(:passer_en_instruction)
end
end
def after_repasser_en_construction(h)

View file

@ -19,6 +19,8 @@ class DossierOperationLog < ApplicationRecord
changer_groupe_instructeur: 'changer_groupe_instructeur',
passer_en_instruction: 'passer_en_instruction',
repasser_en_construction: 'repasser_en_construction',
demander_une_correction: 'demander_une_correction',
demander_a_completer: 'demander_a_completer',
repasser_en_instruction: 'repasser_en_instruction',
accepter: 'accepter',
refuser: 'refuser',
@ -134,6 +136,8 @@ class DossierOperationLog < ApplicationRecord
SerializerService.champ(subject)
when Avis
SerializerService.avis(subject)
when Commentaire
SerializerService.message(subject)
end
end
end

View file

@ -41,6 +41,15 @@ class SerializerService
end
end
def self.message(commentaire)
Sentry.with_scope do |scope|
scope.set_tags(dossier_id: commentaire.dossier_id)
data = execute_query('serializeMessage', { number: commentaire.dossier_id, id: commentaire.to_typed_id })
data && data['dossier']["messages"].first
end
end
def self.execute_query(operation_name, variables)
result = API::V2::Schema.execute(QUERY,
variables: variables,
@ -113,6 +122,14 @@ class SerializerService
}
}
query serializeMessage($number: Int!, $id: ID!) {
dossier(number: $number) {
messages(id: $id) {
...MessageFragment
}
}
}
fragment DossierFragment on Dossier {
id
number
@ -359,5 +376,15 @@ class SerializerService
}
}
}
fragment MessageFragment on Message {
id
email
body
createdAt
attachments {
...FileFragment
}
}
GRAPHQL
end

View file

@ -31,9 +31,11 @@ describe DossierCorrectableConcern do
let(:instructeur) { create(:instructeur) }
let(:commentaire) { create(:commentaire, dossier:, instructeur:) }
subject(:flag) { dossier.flag_as_pending_correction!(commentaire) }
context 'when dossier is en_construction' do
it 'creates a correction' do
expect { dossier.flag_as_pending_correction!(commentaire) }.to change { dossier.corrections.pending.count }.by(1)
expect { flag }.to change { dossier.corrections.pending.count }.by(1)
expect(dossier.corrections.last).to be_correction
end
@ -43,7 +45,7 @@ describe DossierCorrectableConcern do
end
it 'does not change dossier state' do
expect { dossier.flag_as_pending_correction!(commentaire) }.not_to change { dossier.state }
expect { flag }.not_to change { dossier.state }
end
end
@ -51,11 +53,11 @@ describe DossierCorrectableConcern do
let(:dossier) { create(:dossier, :en_instruction) }
it 'creates a correction' do
expect { dossier.flag_as_pending_correction!(commentaire) }.to change { dossier.corrections.pending.count }.by(1)
expect { flag }.to change { dossier.corrections.pending.count }.by(1)
end
it 'repasse dossier en_construction' do
expect { dossier.flag_as_pending_correction!(commentaire) }.to change { dossier.state }.to('en_construction')
expect { flag }.to change { dossier.state }.to('en_construction')
end
end
@ -63,7 +65,7 @@ describe DossierCorrectableConcern do
before { create(:dossier_correction, dossier:) }
it 'does not create a correction' do
expect { dossier.flag_as_pending_correction!(commentaire) }.not_to change { dossier.corrections.pending.count }
expect { flag }.not_to change { dossier.corrections.pending.count }
end
end
@ -71,7 +73,7 @@ describe DossierCorrectableConcern do
before { create(:dossier_correction, :resolved, dossier:) }
it 'creates a correction' do
expect { dossier.flag_as_pending_correction!(commentaire) }.to change { dossier.corrections.pending.count }.by(1)
expect { flag }.to change { dossier.corrections.pending.count }.by(1)
end
end
@ -79,7 +81,7 @@ describe DossierCorrectableConcern do
let(:dossier) { create(:dossier, :accepte) }
it 'does not create a correction' do
expect { dossier.flag_as_pending_correction!(commentaire) }.not_to change { dossier.corrections.pending.count }
expect { flag }.not_to change { dossier.corrections.pending.count }
end
end
@ -87,11 +89,31 @@ describe DossierCorrectableConcern do
let(:dossier) { create(:dossier, :en_instruction, procedure: create(:procedure, :published, :sva)) }
it 'creates a correction' do
expect { dossier.flag_as_pending_correction!(commentaire) }.to change { dossier.corrections.pending.count }.by(1)
expect { flag }.to change { dossier.corrections.pending.count }.by(1)
end
it 'repasse dossier en_construction' do
expect { dossier.flag_as_pending_correction!(commentaire) }.to change { dossier.state }.to('en_construction')
expect { flag }.to change { dossier.state }.to('en_construction')
end
it 'creates a log operation' do
expect { flag }.to change { dossier.dossier_operation_logs.count }.by(2)
log_correction, log_construction = dossier.dossier_operation_logs.last(2)
expect(log_correction.operation).to eq("demander_une_correction")
expect(log_construction.operation).to eq("repasser_en_construction")
expect(log_correction.data["subject"]["body"]).to eq(commentaire.body)
expect(log_correction.data["subject"]["email"]).to eq(commentaire.instructeur.email)
end
it 'creates a log operation of incomplete dossier' do
expect { dossier.flag_as_pending_correction!(commentaire, "incomplete") }.to change { dossier.dossier_operation_logs.count }.by(2)
log_correction, _ = dossier.dossier_operation_logs.last(2)
expect(log_correction.operation).to eq("demander_a_completer")
expect(log_correction.data["subject"]["body"]).to eq(commentaire.body)
expect(log_correction.data["subject"]["email"]).to eq(commentaire.instructeur.email)
end
end
end

View file

@ -1115,8 +1115,8 @@ describe Dossier, type: :model do
end
context "via procedure sva" do
let(:procedure) { create(:procedure, :sva, :published) }
let(:dossier) { create(:dossier, :en_construction, procedure:) }
let(:procedure) { create(:procedure, :sva, :published, :for_individual) }
let(:dossier) { create(:dossier, :en_construction, :with_individual, procedure:) }
subject do
dossier.process_sva_svr!