feat(sva): log operation when instructeur requests a correction
This commit is contained in:
parent
512f6ca0ec
commit
b4e6c20bbd
6 changed files with 87 additions and 13 deletions
|
@ -8,13 +8,15 @@ module DossierCorrectableConcern
|
||||||
|
|
||||||
scope :with_pending_corrections, -> { joins(:corrections).where(corrections: { resolved_at: nil }) }
|
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?
|
return unless may_flag_as_pending_correction?
|
||||||
|
|
||||||
kind ||= :correction
|
kind ||= :correction
|
||||||
|
|
||||||
corrections.create!(commentaire:, kind:)
|
corrections.create!(commentaire:, kind:)
|
||||||
|
|
||||||
|
log_pending_correction_operation(commentaire, kind) if procedure.sva_svr_enabled?
|
||||||
|
|
||||||
return if en_construction?
|
return if en_construction?
|
||||||
|
|
||||||
repasser_en_construction_with_pending_correction!(instructeur: commentaire.instructeur)
|
repasser_en_construction_with_pending_correction!(instructeur: commentaire.instructeur)
|
||||||
|
@ -43,5 +45,18 @@ module DossierCorrectableConcern
|
||||||
pending_corrections.update!(resolved_at: Time.current)
|
pending_corrections.update!(resolved_at: Time.current)
|
||||||
pending_corrections.reset
|
pending_corrections.reset
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -928,7 +928,13 @@ class Dossier < ApplicationRecord
|
||||||
save!
|
save!
|
||||||
|
|
||||||
NotificationMailer.send_en_instruction_notification(self).deliver_later
|
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
|
end
|
||||||
|
|
||||||
def after_repasser_en_construction(h)
|
def after_repasser_en_construction(h)
|
||||||
|
|
|
@ -19,6 +19,8 @@ class DossierOperationLog < ApplicationRecord
|
||||||
changer_groupe_instructeur: 'changer_groupe_instructeur',
|
changer_groupe_instructeur: 'changer_groupe_instructeur',
|
||||||
passer_en_instruction: 'passer_en_instruction',
|
passer_en_instruction: 'passer_en_instruction',
|
||||||
repasser_en_construction: 'repasser_en_construction',
|
repasser_en_construction: 'repasser_en_construction',
|
||||||
|
demander_une_correction: 'demander_une_correction',
|
||||||
|
demander_a_completer: 'demander_a_completer',
|
||||||
repasser_en_instruction: 'repasser_en_instruction',
|
repasser_en_instruction: 'repasser_en_instruction',
|
||||||
accepter: 'accepter',
|
accepter: 'accepter',
|
||||||
refuser: 'refuser',
|
refuser: 'refuser',
|
||||||
|
@ -134,6 +136,8 @@ class DossierOperationLog < ApplicationRecord
|
||||||
SerializerService.champ(subject)
|
SerializerService.champ(subject)
|
||||||
when Avis
|
when Avis
|
||||||
SerializerService.avis(subject)
|
SerializerService.avis(subject)
|
||||||
|
when Commentaire
|
||||||
|
SerializerService.message(subject)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -41,6 +41,15 @@ class SerializerService
|
||||||
end
|
end
|
||||||
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)
|
def self.execute_query(operation_name, variables)
|
||||||
result = API::V2::Schema.execute(QUERY,
|
result = API::V2::Schema.execute(QUERY,
|
||||||
variables: variables,
|
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 {
|
fragment DossierFragment on Dossier {
|
||||||
id
|
id
|
||||||
number
|
number
|
||||||
|
@ -359,5 +376,15 @@ class SerializerService
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fragment MessageFragment on Message {
|
||||||
|
id
|
||||||
|
email
|
||||||
|
body
|
||||||
|
createdAt
|
||||||
|
attachments {
|
||||||
|
...FileFragment
|
||||||
|
}
|
||||||
|
}
|
||||||
GRAPHQL
|
GRAPHQL
|
||||||
end
|
end
|
||||||
|
|
|
@ -31,9 +31,11 @@ describe DossierCorrectableConcern do
|
||||||
let(:instructeur) { create(:instructeur) }
|
let(:instructeur) { create(:instructeur) }
|
||||||
let(:commentaire) { create(:commentaire, dossier:, instructeur:) }
|
let(:commentaire) { create(:commentaire, dossier:, instructeur:) }
|
||||||
|
|
||||||
|
subject(:flag) { dossier.flag_as_pending_correction!(commentaire) }
|
||||||
|
|
||||||
context 'when dossier is en_construction' do
|
context 'when dossier is en_construction' do
|
||||||
it 'creates a correction' 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
|
expect(dossier.corrections.last).to be_correction
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -43,7 +45,7 @@ describe DossierCorrectableConcern do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'does not change dossier state' do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -51,11 +53,11 @@ describe DossierCorrectableConcern do
|
||||||
let(:dossier) { create(:dossier, :en_instruction) }
|
let(:dossier) { create(:dossier, :en_instruction) }
|
||||||
|
|
||||||
it 'creates a correction' 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)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'repasse dossier en_construction' do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -63,7 +65,7 @@ describe DossierCorrectableConcern do
|
||||||
before { create(:dossier_correction, dossier:) }
|
before { create(:dossier_correction, dossier:) }
|
||||||
|
|
||||||
it 'does not create a correction' do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -71,7 +73,7 @@ describe DossierCorrectableConcern do
|
||||||
before { create(:dossier_correction, :resolved, dossier:) }
|
before { create(:dossier_correction, :resolved, dossier:) }
|
||||||
|
|
||||||
it 'creates a correction' 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)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -79,7 +81,7 @@ describe DossierCorrectableConcern do
|
||||||
let(:dossier) { create(:dossier, :accepte) }
|
let(:dossier) { create(:dossier, :accepte) }
|
||||||
|
|
||||||
it 'does not create a correction' do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -87,11 +89,31 @@ describe DossierCorrectableConcern do
|
||||||
let(:dossier) { create(:dossier, :en_instruction, procedure: create(:procedure, :published, :sva)) }
|
let(:dossier) { create(:dossier, :en_instruction, procedure: create(:procedure, :published, :sva)) }
|
||||||
|
|
||||||
it 'creates a correction' 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)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'repasse dossier en_construction' do
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1115,8 +1115,8 @@ describe Dossier, type: :model do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "via procedure sva" do
|
context "via procedure sva" do
|
||||||
let(:procedure) { create(:procedure, :sva, :published) }
|
let(:procedure) { create(:procedure, :sva, :published, :for_individual) }
|
||||||
let(:dossier) { create(:dossier, :en_construction, procedure:) }
|
let(:dossier) { create(:dossier, :en_construction, :with_individual, procedure:) }
|
||||||
|
|
||||||
subject do
|
subject do
|
||||||
dossier.process_sva_svr!
|
dossier.process_sva_svr!
|
||||||
|
|
Loading…
Reference in a new issue