2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-12-19 11:16:06 +01:00
|
|
|
class RecoveryService
|
|
|
|
def self.recoverable_procedures(previous_user:, siret:)
|
|
|
|
return [] if previous_user.nil?
|
|
|
|
|
|
|
|
previous_user.dossiers
|
|
|
|
.includes(:procedure)
|
|
|
|
.joins(:etablissement)
|
|
|
|
.where(etablissements: { siret: })
|
|
|
|
.pluck('procedures.id, procedures.libelle')
|
|
|
|
.tally
|
|
|
|
.map { |(procedure_id, libelle), count| { procedure_id:, libelle:, count: } }
|
|
|
|
.sort_by { |h| [-h[:count], h[:libelle]] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.recover_procedure!(previous_user:, next_user:, siret:, procedure_ids:)
|
|
|
|
recoverable_procedure_ids = recoverable_procedures(previous_user: previous_user, siret: siret)
|
|
|
|
.map { _1[:procedure_id] }
|
|
|
|
|
2023-12-19 16:17:21 +01:00
|
|
|
dossiers = procedure_ids
|
|
|
|
.filter { |id| id.in?(recoverable_procedure_ids) }
|
2023-12-19 11:16:06 +01:00
|
|
|
.then do |p_ids|
|
|
|
|
previous_user.dossiers.joins(:procedure)
|
|
|
|
.where(procedure: { id: p_ids })
|
|
|
|
end
|
2023-12-19 16:17:21 +01:00
|
|
|
|
|
|
|
dossiers.pluck(:id).map do |id|
|
|
|
|
{
|
|
|
|
dossier_id: id,
|
|
|
|
from: previous_user.email,
|
|
|
|
from_support: false,
|
|
|
|
to: next_user.email
|
|
|
|
}
|
|
|
|
end.then { |array| DossierTransferLog.create(array) }
|
|
|
|
|
|
|
|
dossiers.update_all(user_id: next_user.id)
|
2023-12-19 11:16:06 +01:00
|
|
|
end
|
|
|
|
end
|