2022-11-18 15:26:31 +01:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: batch_operations
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
|
|
|
# failed_dossier_ids :bigint default([]), not null, is an Array
|
|
|
|
# finished_at :datetime
|
|
|
|
# operation :string not null
|
|
|
|
# payload :jsonb not null
|
|
|
|
# run_at :datetime
|
|
|
|
# success_dossier_ids :bigint default([]), not null, is an Array
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# instructeur_id :bigint not null
|
|
|
|
#
|
2022-11-18 16:59:46 +01:00
|
|
|
|
2022-11-18 15:26:31 +01:00
|
|
|
class BatchOperation < ApplicationRecord
|
|
|
|
enum operation: {
|
|
|
|
archiver: 'archiver'
|
|
|
|
}
|
|
|
|
|
|
|
|
has_many :dossiers, dependent: :nullify
|
|
|
|
belongs_to :instructeur
|
2022-11-19 06:03:59 +01:00
|
|
|
|
2022-11-18 15:26:31 +01:00
|
|
|
validates :operation, presence: true
|
|
|
|
|
2022-11-18 16:59:46 +01:00
|
|
|
def enqueue_all
|
|
|
|
Dossier.joins(:procedure)
|
|
|
|
.where(procedure: { id: instructeur.procedures.ids })
|
|
|
|
.where(id: dossiers.ids)
|
|
|
|
.map { |dossier| BatchOperationProcessOneJob.perform_later(self, dossier) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_one(dossier)
|
2022-11-18 15:26:31 +01:00
|
|
|
case operation
|
|
|
|
when BatchOperation.operations.fetch(:archiver)
|
2022-11-18 16:59:46 +01:00
|
|
|
dossier.archiver!(instructeur)
|
2022-11-18 15:26:31 +01:00
|
|
|
end
|
2022-11-18 16:59:46 +01:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def called_for_first_time?
|
|
|
|
run_at.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def called_for_last_time? # beware, must be reloaded first
|
|
|
|
dossiers.count.zero?
|
2022-11-18 15:26:31 +01:00
|
|
|
end
|
|
|
|
end
|