From b4e6c20bbd5f363a557fea09cc494cba5f413c54 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Tue, 20 Jun 2023 16:15:18 +0200 Subject: [PATCH] feat(sva): log operation when instructeur requests a correction --- .../concerns/dossier_correctable_concern.rb | 17 +++++++- app/models/dossier.rb | 8 +++- app/models/dossier_operation_log.rb | 4 ++ app/services/serializer_service.rb | 27 +++++++++++++ .../dossier_correctable_concern_spec.rb | 40 ++++++++++++++----- spec/models/dossier_spec.rb | 4 +- 6 files changed, 87 insertions(+), 13 deletions(-) diff --git a/app/models/concerns/dossier_correctable_concern.rb b/app/models/concerns/dossier_correctable_concern.rb index 0c4475d4c..7e43a6e7d 100644 --- a/app/models/concerns/dossier_correctable_concern.rb +++ b/app/models/concerns/dossier_correctable_concern.rb @@ -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 diff --git a/app/models/dossier.rb b/app/models/dossier.rb index d6ee4cae7..b2bb25821 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -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) diff --git a/app/models/dossier_operation_log.rb b/app/models/dossier_operation_log.rb index 7ac45bd9f..3fa2072d6 100644 --- a/app/models/dossier_operation_log.rb +++ b/app/models/dossier_operation_log.rb @@ -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 diff --git a/app/services/serializer_service.rb b/app/services/serializer_service.rb index c569038da..6f97841c1 100644 --- a/app/services/serializer_service.rb +++ b/app/services/serializer_service.rb @@ -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 diff --git a/spec/models/concern/dossier_correctable_concern_spec.rb b/spec/models/concern/dossier_correctable_concern_spec.rb index 23e5d50d5..a18d58fc3 100644 --- a/spec/models/concern/dossier_correctable_concern_spec.rb +++ b/spec/models/concern/dossier_correctable_concern_spec.rb @@ -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 diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index dd68f40d1..a1c3ec698 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -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!