2022-11-18 15:26:31 +01:00
|
|
|
class BatchOperation < ApplicationRecord
|
|
|
|
enum operation: {
|
2023-01-04 11:51:22 +01:00
|
|
|
accepter: 'accepter',
|
2023-07-03 16:05:48 +02:00
|
|
|
refuser: 'refuser',
|
|
|
|
classer_sans_suite: 'classer_sans_suite',
|
2022-12-12 10:02:33 +01:00
|
|
|
archiver: 'archiver',
|
2023-11-29 22:36:55 +01:00
|
|
|
desarchiver: 'desarchiver',
|
2023-01-04 11:51:22 +01:00
|
|
|
follow: 'follow',
|
2023-01-11 11:05:19 +01:00
|
|
|
passer_en_instruction: 'passer_en_instruction',
|
2023-11-30 16:11:04 +01:00
|
|
|
repousser_expiration: 'repousser_expiration',
|
2023-01-13 15:09:24 +01:00
|
|
|
repasser_en_construction: 'repasser_en_construction',
|
2023-11-29 19:40:51 +01:00
|
|
|
restaurer: 'restaurer',
|
2023-11-16 10:30:16 +01:00
|
|
|
unfollow: 'unfollow',
|
|
|
|
supprimer: 'supprimer'
|
2022-11-18 15:26:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
has_many :dossiers, dependent: :nullify
|
2022-12-22 22:23:47 +01:00
|
|
|
has_many :dossier_operations, class_name: 'DossierBatchOperation', dependent: :destroy
|
|
|
|
has_many :groupe_instructeurs, through: :dossier_operations
|
2022-11-18 15:26:31 +01:00
|
|
|
belongs_to :instructeur
|
2022-11-19 06:03:59 +01:00
|
|
|
|
2022-12-19 15:03:39 +01:00
|
|
|
store_accessor :payload, :motivation, :justificatif_motivation
|
2022-12-16 17:38:32 +01:00
|
|
|
|
2022-11-18 15:26:31 +01:00
|
|
|
validates :operation, presence: true
|
|
|
|
|
2022-12-22 22:23:47 +01:00
|
|
|
before_create :build_operations
|
|
|
|
|
2022-12-05 13:36:38 +01:00
|
|
|
RETENTION_DURATION = 4.hours
|
|
|
|
MAX_DUREE_GENERATION = 24.hours
|
|
|
|
|
|
|
|
scope :stale, lambda {
|
|
|
|
where.not(finished_at: nil)
|
|
|
|
.where('updated_at < ?', (Time.zone.now - RETENTION_DURATION))
|
|
|
|
}
|
|
|
|
|
|
|
|
scope :stuck, lambda {
|
|
|
|
where(finished_at: nil)
|
|
|
|
.where('updated_at < ?', (Time.zone.now - MAX_DUREE_GENERATION))
|
|
|
|
}
|
|
|
|
|
2022-11-25 15:43:00 +01:00
|
|
|
def dossiers_safe_scope(dossier_ids = self.dossier_ids)
|
2022-12-13 15:52:32 +01:00
|
|
|
query = instructeur
|
|
|
|
.dossiers
|
|
|
|
.where(id: dossier_ids)
|
2022-11-25 15:43:00 +01:00
|
|
|
case operation
|
|
|
|
when BatchOperation.operations.fetch(:archiver) then
|
2023-11-29 19:40:51 +01:00
|
|
|
query.visible_by_administration.not_archived.state_termine
|
2023-11-29 22:36:55 +01:00
|
|
|
when BatchOperation.operations.fetch(:desarchiver) then
|
|
|
|
query.visible_by_administration.archived.state_termine
|
2022-12-12 10:02:33 +01:00
|
|
|
when BatchOperation.operations.fetch(:passer_en_instruction) then
|
2023-11-29 19:40:51 +01:00
|
|
|
query.visible_by_administration.state_en_construction
|
2022-12-15 17:35:50 +01:00
|
|
|
when BatchOperation.operations.fetch(:accepter) then
|
2023-11-29 19:40:51 +01:00
|
|
|
query.visible_by_administration.state_en_instruction
|
2023-07-03 16:05:48 +02:00
|
|
|
when BatchOperation.operations.fetch(:refuser) then
|
2023-11-29 19:40:51 +01:00
|
|
|
query.visible_by_administration.state_en_instruction
|
2023-07-03 16:05:48 +02:00
|
|
|
when BatchOperation.operations.fetch(:classer_sans_suite) then
|
2023-11-29 19:40:51 +01:00
|
|
|
query.visible_by_administration.state_en_instruction
|
2023-01-04 11:51:22 +01:00
|
|
|
when BatchOperation.operations.fetch(:follow) then
|
2023-11-29 19:40:51 +01:00
|
|
|
query.visible_by_administration.without_followers.en_cours
|
2023-11-30 16:11:04 +01:00
|
|
|
when BatchOperation.operations.fetch(:repousser_expiration) then
|
2023-12-06 14:55:54 +01:00
|
|
|
query.visible_by_administration.termine_or_en_construction_close_to_expiration
|
2023-01-11 11:05:19 +01:00
|
|
|
when BatchOperation.operations.fetch(:repasser_en_construction) then
|
2023-11-29 19:40:51 +01:00
|
|
|
query.visible_by_administration.state_en_instruction
|
2023-01-13 15:09:24 +01:00
|
|
|
when BatchOperation.operations.fetch(:unfollow) then
|
2023-11-29 19:40:51 +01:00
|
|
|
query.visible_by_administration.with_followers.en_cours
|
2023-11-16 10:30:16 +01:00
|
|
|
when BatchOperation.operations.fetch(:supprimer) then
|
2023-11-29 19:40:51 +01:00
|
|
|
query.visible_by_administration.state_termine
|
|
|
|
when BatchOperation.operations.fetch(:restaurer) then
|
|
|
|
query.hidden_by_administration
|
2022-11-25 15:43:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def enqueue_all
|
|
|
|
dossiers_safe_scope # later in batch .
|
2022-11-18 16:59:46 +01:00
|
|
|
.map { |dossier| BatchOperationProcessOneJob.perform_later(self, dossier) }
|
|
|
|
end
|
|
|
|
|
2022-11-25 15:43:00 +01:00
|
|
|
def process_one(dossier)
|
|
|
|
case operation
|
|
|
|
when BatchOperation.operations.fetch(:archiver)
|
|
|
|
dossier.archiver!(instructeur)
|
2023-11-29 22:36:55 +01:00
|
|
|
when BatchOperation.operations.fetch(:desarchiver)
|
|
|
|
dossier.desarchiver!
|
2022-12-12 10:02:33 +01:00
|
|
|
when BatchOperation.operations.fetch(:passer_en_instruction)
|
2024-05-29 11:31:58 +02:00
|
|
|
dossier.passer_en_instruction!(instructeur: instructeur)
|
2022-12-15 17:35:50 +01:00
|
|
|
when BatchOperation.operations.fetch(:accepter)
|
2024-05-29 11:31:58 +02:00
|
|
|
dossier.accepter!(instructeur: instructeur, motivation: motivation, justificatif: justificatif_motivation)
|
2023-07-03 16:05:48 +02:00
|
|
|
when BatchOperation.operations.fetch(:refuser)
|
2024-05-29 11:31:58 +02:00
|
|
|
dossier.refuser!(instructeur: instructeur, motivation: motivation, justificatif: justificatif_motivation)
|
2023-07-03 16:05:48 +02:00
|
|
|
when BatchOperation.operations.fetch(:classer_sans_suite)
|
2024-05-29 11:31:58 +02:00
|
|
|
dossier.classer_sans_suite!(instructeur: instructeur, motivation: motivation, justificatif: justificatif_motivation)
|
2023-01-04 11:51:22 +01:00
|
|
|
when BatchOperation.operations.fetch(:follow)
|
|
|
|
instructeur.follow(dossier)
|
2023-11-30 16:11:04 +01:00
|
|
|
when BatchOperation.operations.fetch(:repousser_expiration)
|
2024-07-15 18:02:54 +02:00
|
|
|
dossier.extend_conservation(1.month, instructeur)
|
2023-01-11 11:05:19 +01:00
|
|
|
when BatchOperation.operations.fetch(:repasser_en_construction)
|
|
|
|
dossier.repasser_en_construction!(instructeur: instructeur)
|
2023-01-13 15:09:24 +01:00
|
|
|
when BatchOperation.operations.fetch(:unfollow)
|
|
|
|
instructeur.unfollow(dossier)
|
2023-11-16 10:30:16 +01:00
|
|
|
when BatchOperation.operations.fetch(:supprimer)
|
|
|
|
dossier.hide_and_keep_track!(instructeur, :instructeur_request)
|
2023-11-29 19:40:51 +01:00
|
|
|
when BatchOperation.operations.fetch(:restaurer)
|
|
|
|
dossier.restore(instructeur)
|
2022-11-25 15:43:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def track_processed_dossier(success, dossier)
|
2023-01-05 15:10:07 +01:00
|
|
|
dossiers.delete(dossier)
|
|
|
|
touch(:run_at) if called_for_first_time?
|
2023-02-01 14:59:37 +01:00
|
|
|
touch(:finished_at)
|
2022-12-22 22:23:47 +01:00
|
|
|
|
2023-01-05 15:10:07 +01:00
|
|
|
if success
|
|
|
|
dossier_operation(dossier).done!
|
|
|
|
else
|
|
|
|
dossier_operation(dossier).fail!
|
2022-11-25 12:30:06 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-25 15:43:00 +01:00
|
|
|
# when an instructeur want to create a batch from his interface,
|
|
|
|
# another one might have run something on one of the dossier
|
|
|
|
# we use this approach to create a batch with given dossiers safely
|
|
|
|
def self.safe_create!(params)
|
|
|
|
transaction do
|
|
|
|
instance = new(params)
|
|
|
|
instance.dossiers = instance.dossiers_safe_scope(params[:dossier_ids])
|
2022-12-01 11:22:20 +01:00
|
|
|
.not_having_batch_operation
|
2022-12-12 10:02:33 +01:00
|
|
|
if instance.dossiers.present?
|
|
|
|
instance.save!
|
|
|
|
BatchOperationEnqueueAllJob.perform_later(instance)
|
|
|
|
instance
|
|
|
|
end
|
2022-11-18 15:26:31 +01:00
|
|
|
end
|
2022-11-18 16:59:46 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def called_for_first_time?
|
|
|
|
run_at.nil?
|
|
|
|
end
|
|
|
|
|
2022-12-05 17:07:59 +01:00
|
|
|
def total_count
|
2022-12-22 22:23:47 +01:00
|
|
|
dossier_operations.size
|
2022-12-01 11:22:20 +01:00
|
|
|
end
|
|
|
|
|
2022-12-22 22:23:47 +01:00
|
|
|
def success_count
|
|
|
|
dossier_operations.success.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def errors?
|
|
|
|
dossier_operations.error.present?
|
2022-12-01 11:22:20 +01:00
|
|
|
end
|
|
|
|
|
2023-02-01 14:59:37 +01:00
|
|
|
def finished_at
|
2023-02-01 16:23:07 +01:00
|
|
|
dossiers.empty? ? super : nil
|
2023-02-01 14:59:37 +01:00
|
|
|
end
|
|
|
|
|
2022-11-21 16:32:17 +01:00
|
|
|
private
|
|
|
|
|
2022-12-22 22:23:47 +01:00
|
|
|
def dossier_operation(dossier)
|
|
|
|
dossier_operations.find_by!(dossier:)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_operations
|
|
|
|
dossier_operations.build(dossiers.map { { dossier: _1 } })
|
2022-11-21 16:32:17 +01:00
|
|
|
end
|
2022-11-18 15:26:31 +01:00
|
|
|
end
|