Merge pull request #10125 from demarches-simplifiees/destroy-procedure-without-administrateur-and-without-dossier

Correctif : supprime les démarches sans administrateur ni dossier
This commit is contained in:
Eric Leroy-Terquem 2024-03-18 08:48:52 +00:00 committed by GitHub
commit 94f37b6708
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 40 additions and 0 deletions

View file

@ -82,6 +82,10 @@ class Administrateur < ApplicationRecord
next_administrateur = procedure.administrateurs.where.not(id: self.id).first
procedure.service.update(administrateur: next_administrateur)
if (procedure.administrateurs.count == 1 && procedure.dossiers.empty?)
procedure.destroy
end
end
services.each do |service|

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
module Maintenance
class DestroyProcedureWithoutAdministrateurAndWithoutDossierTask < MaintenanceTasks::Task
def collection
Procedure.with_discarded.where.missing(:administrateurs, :dossiers)
end
def process(procedure)
procedure.destroy!
end
end
end

View file

@ -0,0 +1,23 @@
# frozen_string_literal: true
require "rails_helper"
module Maintenance
RSpec.describe DestroyProcedureWithoutAdministrateurAndWithoutDossierTask do
describe "#process" do
subject(:process) { described_class.process(procedure) }
let(:procedure) { create(:procedure) }
before do
administrateur = procedure.administrateurs.first
AdministrateursProcedure.where(administrateur_id: administrateur.id).delete_all
administrateur.destroy
end
it "destroys procedure" do
subject
expect(Procedure.count).to eq 0
end
end
end
end