diff --git a/Gemfile b/Gemfile index 7c905fb2f..b8b8e1e10 100644 --- a/Gemfile +++ b/Gemfile @@ -10,6 +10,7 @@ gem 'active_storage_validations' gem 'addressable' gem 'administrate' gem 'administrate-field-enum' # Allow using Field::Enum in administrate +gem 'after_commit_everywhere' gem 'after_party' gem 'ancestry' gem 'anchored' diff --git a/Gemfile.lock b/Gemfile.lock index 2ab1bda90..4189299fa 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -116,6 +116,9 @@ GEM administrate-field-enum (0.0.9) administrate (~> 0.12) aes_key_wrap (1.1.0) + after_commit_everywhere (1.4.0) + activerecord (>= 4.2) + activesupport after_party (1.11.2) ancestry (4.3.3) activerecord (>= 5.2.6) @@ -859,6 +862,7 @@ DEPENDENCIES addressable administrate administrate-field-enum + after_commit_everywhere after_party ancestry anchored diff --git a/app/models/concerns/dossier_state_concern.rb b/app/models/concerns/dossier_state_concern.rb index 672a662dc..908f2bbd6 100644 --- a/app/models/concerns/dossier_state_concern.rb +++ b/app/models/concerns/dossier_state_concern.rb @@ -6,18 +6,22 @@ module DossierStateConcern self.depose_at = self.en_construction_at = self.traitements .passer_en_construction .processed_at + save! + RoutingEngine.compute(self) + MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:en_construction)) + procedure.compute_dossiers_count + end + + def after_commit_passer_en_construction NotificationMailer.send_en_construction_notification(self).deliver_later NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? - procedure.compute_dossiers_count end def after_passer_en_instruction(h) instructeur = h[:instructeur] - disable_notification = h.fetch(:disable_notification, false) - instructeur.follow(self) self.en_construction_close_to_expiration_notice_sent_at = nil @@ -27,14 +31,19 @@ module DossierStateConcern .processed_at save! + MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:en_instruction)) resolve_pending_correction! - MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:en_instruction)) + log_dossier_operation(instructeur, :passer_en_instruction) + end + + def after_commit_passer_en_instruction(h) + disable_notification = h.fetch(:disable_notification, false) + if !disable_notification NotificationMailer.send_en_instruction_notification(self).deliver_later NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? end - log_dossier_operation(instructeur, :passer_en_instruction) end def after_passer_automatiquement_en_instruction @@ -47,9 +56,8 @@ module DossierStateConcern end save! + MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:en_instruction)) - NotificationMailer.send_en_instruction_notification(self).deliver_later - NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? if procedure.sva_svr_enabled? # TODO: handle serialization errors when SIRET demandeur was not completed @@ -59,6 +67,11 @@ module DossierStateConcern end end + def after_commit_passer_automatiquement_en_instruction + NotificationMailer.send_en_instruction_notification(self).deliver_later + NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? + end + def after_repasser_en_construction(h) instructeur = h[:instructeur] @@ -69,15 +82,19 @@ module DossierStateConcern self.en_construction_at = self.traitements .passer_en_construction(instructeur: instructeur) .processed_at + save! + log_dossier_operation(instructeur, :repasser_en_construction) end + def after_commit_repasser_en_construction + end + def after_accepter(h) instructeur = h[:instructeur] motivation = h[:motivation] justificatif = h[:justificatif] - disable_notification = h.fetch(:disable_notification, false) self.processed_at = self.traitements .accepter(motivation: motivation, instructeur: instructeur) @@ -93,15 +110,22 @@ module DossierStateConcern end save! - remove_titres_identite! MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:accepte)) + + log_dossier_operation(instructeur, :accepter, self) + end + + def after_commit_accepter(h) + disable_notification = h.fetch(:disable_notification, false) + if !disable_notification NotificationMailer.send_accepte_notification(self).deliver_later NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? end + send_dossier_decision_to_experts(self) - log_dossier_operation(instructeur, :accepter, self) + remove_titres_identite! end def after_accepter_automatiquement @@ -121,18 +145,24 @@ module DossierStateConcern end save! - remove_titres_identite! + MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:accepte)) + + log_automatic_dossier_operation(:accepter, self) + end + + def after_commit_accepter_automatiquement NotificationMailer.send_accepte_notification(self).deliver_later NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? - log_automatic_dossier_operation(:accepter, self) + + send_dossier_decision_to_experts(self) + remove_titres_identite! end def after_refuser(h) instructeur = h[:instructeur] motivation = h[:motivation] justificatif = h[:justificatif] - disable_notification = h.fetch(:disable_notification, false) self.processed_at = self.traitements .refuser(motivation: motivation, instructeur: instructeur) @@ -144,16 +174,22 @@ module DossierStateConcern end save! - remove_titres_identite! MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:refuse)) + log_dossier_operation(instructeur, :refuser, self) + end + + def after_commit_refuser(h) + disable_notification = h.fetch(:disable_notification, false) + if !disable_notification NotificationMailer.send_refuse_notification(self).deliver_later NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? end + send_dossier_decision_to_experts(self) - log_dossier_operation(instructeur, :refuser, self) + remove_titres_identite! end def after_refuser_automatiquement @@ -167,18 +203,23 @@ module DossierStateConcern save! - remove_titres_identite! MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:refuse)) + + log_automatic_dossier_operation(:refuser, self) + end + + def after_commit_refuser_automatiquement NotificationMailer.send_refuse_notification(self).deliver_later NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? - log_automatic_dossier_operation(:refuser, self) + + send_dossier_decision_to_experts(self) + remove_titres_identite! end def after_classer_sans_suite(h) instructeur = h[:instructeur] motivation = h[:motivation] justificatif = h[:justificatif] - disable_notification = h.fetch(:disable_notification, false) self.processed_at = self.traitements .classer_sans_suite(motivation: motivation, instructeur: instructeur) @@ -190,21 +231,26 @@ module DossierStateConcern end save! - remove_titres_identite! MailTemplatePresenterService.create_commentaire_for_state(self, Dossier.states.fetch(:sans_suite)) + log_dossier_operation(instructeur, :classer_sans_suite, self) + end + + def after_commit_classer_sans_suite(h) + disable_notification = h.fetch(:disable_notification, false) + if !disable_notification NotificationMailer.send_sans_suite_notification(self).deliver_later NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? end + send_dossier_decision_to_experts(self) - log_dossier_operation(instructeur, :classer_sans_suite, self) + remove_titres_identite! end def after_repasser_en_instruction(h) instructeur = h[:instructeur] - disable_notification = h.fetch(:disable_notification, false) create_missing_traitemets @@ -222,13 +268,20 @@ module DossierStateConcern self.justificatif_motivation.purge_later save! - rebase_later MailTemplatePresenterService.create_commentaire_for_state(self, DossierOperationLog.operations.fetch(:repasser_en_instruction)) + + log_dossier_operation(instructeur, :repasser_en_instruction) + end + + def after_commit_repasser_en_instruction(h) + disable_notification = h.fetch(:disable_notification, false) + if !disable_notification NotificationMailer.send_repasser_en_instruction_notification(self).deliver_later NotificationMailer.send_notification_for_tiers(self, repasser_en_instruction: true).deliver_later if self.for_tiers? end - log_dossier_operation(instructeur, :repasser_en_instruction) + + rebase_later end end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 23d5c7fc4..44a5b8941 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -158,48 +158,48 @@ class Dossier < ApplicationRecord state :refuse state :sans_suite - event :passer_en_construction, after: :after_passer_en_construction do + event :passer_en_construction, after: :after_passer_en_construction, after_commit: :after_commit_passer_en_construction do transitions from: :brouillon, to: :en_construction end - event :passer_en_instruction, after: :after_passer_en_instruction do + event :passer_en_instruction, after: :after_passer_en_instruction, after_commit: :after_commit_passer_en_instruction do transitions from: :en_construction, to: :en_instruction, guard: :can_passer_en_instruction? end - event :passer_automatiquement_en_instruction, after: :after_passer_automatiquement_en_instruction do + event :passer_automatiquement_en_instruction, after: :after_passer_automatiquement_en_instruction, after_commit: :after_commit_passer_automatiquement_en_instruction do transitions from: :en_construction, to: :en_instruction, guard: :can_passer_automatiquement_en_instruction? end - event :repasser_en_construction, after: :after_repasser_en_construction do + event :repasser_en_construction, after: :after_repasser_en_construction, after_commit: :after_commit_repasser_en_construction do transitions from: :en_instruction, to: :en_construction, guard: :can_repasser_en_construction? end - event :repasser_en_construction_with_pending_correction, after: :after_repasser_en_construction do + event :repasser_en_construction_with_pending_correction, after: :after_repasser_en_construction, after_commit: :after_commit_repasser_en_construction do transitions from: :en_instruction, to: :en_construction end - event :accepter, after: :after_accepter do + event :accepter, after: :after_accepter, after_commit: :after_commit_accepter do transitions from: :en_instruction, to: :accepte, guard: :can_terminer? end - event :accepter_automatiquement, after: :after_accepter_automatiquement do + event :accepter_automatiquement, after: :after_accepter_automatiquement, after_commit: :after_commit_accepter_automatiquement do transitions from: :en_construction, to: :accepte, guard: :can_accepter_automatiquement? transitions from: :en_instruction, to: :accepte, guard: :can_accepter_automatiquement? end - event :refuser, after: :after_refuser do + event :refuser, after: :after_refuser, after_commit: :after_commit_refuser do transitions from: :en_instruction, to: :refuse, guard: :can_terminer? end - event :refuser_automatiquement, after: :after_refuser_automatiquement do + event :refuser_automatiquement, after: :after_refuser_automatiquement, after_commit: :after_commit_refuser_automatiquement do transitions from: :en_instruction, to: :refuse, guard: :can_refuser_automatiquement? end - event :classer_sans_suite, after: :after_classer_sans_suite do + event :classer_sans_suite, after: :after_classer_sans_suite, after_commit: :after_commit_classer_sans_suite do transitions from: :en_instruction, to: :sans_suite, guard: :can_terminer? end - event :repasser_en_instruction, after: :after_repasser_en_instruction do + event :repasser_en_instruction, after: :after_repasser_en_instruction, after_commit: :after_commit_repasser_en_instruction do transitions from: :refuse, to: :en_instruction, guard: :can_repasser_en_instruction? transitions from: :sans_suite, to: :en_instruction, guard: :can_repasser_en_instruction? transitions from: :accepte, to: :en_instruction, guard: :can_repasser_en_instruction?