feat(task/support:delete_adminstrateurs_procedures): add task to remove all AdministrateursProcedure from a user admin

This commit is contained in:
Martin 2022-07-06 14:26:51 +02:00 committed by mfo
parent 83f92b6b5e
commit 308bedf28a

View file

@ -19,6 +19,30 @@ namespace :support do
user.destroy user.destroy
end end
desc <<~EOD
Destroy all AdministrateursProcedures for a given USER_EMAIL
Only works if the AdministrateursProcedures is not the last of the Procedure.
EOD
task delete_adminstrateurs_procedures: :environment do
user_email = ENV['USER_EMAIL']
fail "Must specify a USER_EMAIL" if user_email.nil?
administrateur = Administrateur.joins(:user).where(user: { email: user_email }).first
AdministrateursProcedure.where(administrateur: administrateur).each do |administrateur_procedure|
procedure = administrateur_procedure.procedure
if procedure.administrateurs.count > 1
begin
procedure.administrateurs.delete(administrateur)
puts "Deleted #{user_email} from #{procedure.libelle}"
rescue ActiveRecord::RecordInvalid
puts "Can't unlink #{user_email} from <#{procedure.libelle}> due to error"
end
else
puts "Can't unlink #{user_email} from <#{procedure.libelle}> because last admin"
end
end
end
desc <<~EOD desc <<~EOD
Change the SIRET for a given dossier (specified by DOSSIER_ID) Change the SIRET for a given dossier (specified by DOSSIER_ID)
EOD EOD