feat(file retrieval): add recovery_service
This commit is contained in:
parent
cd7fd5832c
commit
e91eeadccb
2 changed files with 103 additions and 0 deletions
27
app/services/recovery_service.rb
Normal file
27
app/services/recovery_service.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
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] }
|
||||
|
||||
procedure_ids
|
||||
.select { |id| id.in?(recoverable_procedure_ids) }
|
||||
.then do |p_ids|
|
||||
previous_user.dossiers.joins(:procedure)
|
||||
.where(procedure: { id: p_ids })
|
||||
.update_all(user_id: next_user.id)
|
||||
end
|
||||
end
|
||||
end
|
76
spec/services/recovery_service_spec.rb
Normal file
76
spec/services/recovery_service_spec.rb
Normal file
|
@ -0,0 +1,76 @@
|
|||
RSpec.describe RecoveryService, type: :service do
|
||||
describe '.recoverable_procedures' do
|
||||
subject { described_class.recoverable_procedures(previous_user:, siret:) }
|
||||
|
||||
context 'when the previous_user is nil' do
|
||||
let(:previous_user) { nil }
|
||||
let(:siret) { '123' }
|
||||
|
||||
it 'returns []' do
|
||||
expect(subject).to eq([])
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the previous_user has some dossiers' do
|
||||
let(:previous_user) { create(:user) }
|
||||
|
||||
let(:procedure_1) { create(:procedure) }
|
||||
let(:siret) { '123' }
|
||||
|
||||
let(:procedure_2) { create(:procedure) }
|
||||
let(:another_siret) { 'another_123' }
|
||||
|
||||
before do
|
||||
3.times do
|
||||
create(:dossier, procedure: procedure_1,
|
||||
etablissement: create(:etablissement, siret:),
|
||||
user: previous_user)
|
||||
end
|
||||
|
||||
create(:dossier, procedure: procedure_2,
|
||||
etablissement: create(:etablissement, siret: another_siret),
|
||||
user: previous_user)
|
||||
end
|
||||
|
||||
it 'returns the procedures with their count' do
|
||||
expect(subject).to eq([{ procedure_id: procedure_1.id, libelle: procedure_1.libelle, count: 3 }])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '.recover_procedure!' do
|
||||
subject { described_class.recover_procedure!(previous_user:, next_user:, siret:, procedure_ids:) }
|
||||
|
||||
context 'when the previous_user has some dossiers' do
|
||||
let!(:previous_user) { create(:user) }
|
||||
let!(:next_user) { create(:user) }
|
||||
|
||||
let!(:procedure_1) { create(:procedure) }
|
||||
let!(:siret) { '123' }
|
||||
|
||||
let!(:procedure_2) { create(:procedure) }
|
||||
let!(:another_siret) { 'another_123' }
|
||||
|
||||
let!(:dossiers_to_recover) do
|
||||
3.times do
|
||||
create(:dossier, procedure: procedure_1,
|
||||
etablissement: create(:etablissement, siret:),
|
||||
user: previous_user)
|
||||
end
|
||||
end
|
||||
|
||||
let!(:dossiers_not_to_recover) do
|
||||
create(:dossier, procedure: procedure_2,
|
||||
etablissement: create(:etablissement, siret: another_siret),
|
||||
user: previous_user)
|
||||
end
|
||||
|
||||
let(:procedure_ids) { [procedure_1.id] }
|
||||
|
||||
it 'moves the files to the next user' do
|
||||
subject
|
||||
expect(next_user.dossiers.count).to eq(3)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue