demarches-normaliennes/lib/tasks/support.rake

158 lines
4.5 KiB
Ruby
Raw Normal View History

require Rails.root.join("lib", "tasks", "task_helper")
namespace :support do
desc <<~EOD
Give procedure #PROCEDURE_ID a new owner.
2018-10-04 11:59:27 +02:00
The new owner can be specified with NEW_OWNER_ID or NEW_OWNER_EMAIL.
EOD
task transfer_procedure_ownership: :environment do
new_owner_id = ENV['NEW_OWNER_ID']
2018-10-04 11:59:27 +02:00
new_owner_email = ENV['NEW_OWNER_EMAIL']
new_owner = nil
if new_owner_id.present?
2018-10-04 12:00:40 +02:00
rake_puts("Looking for new owner by id\n")
new_owner = Administrateur.find(new_owner_id)
2018-10-04 11:59:27 +02:00
elsif new_owner_email.present?
2018-10-04 12:00:40 +02:00
rake_puts("Looking for new owner by email\n")
2018-10-04 11:59:27 +02:00
new_owner = Administrateur.find_by('LOWER(email) = LOWER(?)', new_owner_email)
end
if new_owner.blank?
fail "Must specify a new owner"
end
procedure_id = ENV['PROCEDURE_ID']
procedure = Procedure.find(procedure_id)
rake_puts("Changing owner of procedure ##{procedure_id} from ##{procedure.administrateur_id} to ##{new_owner.id}")
procedure.update(administrateur: new_owner)
end
2018-06-27 12:38:38 +02:00
desc <<~EOD
Give all procedures owned by OLD_OWNER_ID or OLD_OWNER_EMAIL a new owner.
The new owner can be specified with NEW_OWNER_ID or NEW_OWNER_EMAIL.
EOD
task transfer_all_procedures_ownership: :environment do
old_owner_id = ENV['OLD_OWNER_ID']
old_owner_email = ENV['OLD_OWNER_EMAIL']
new_owner_id = ENV['NEW_OWNER_ID']
new_owner_email = ENV['NEW_OWNER_EMAIL']
old_owner = nil
if old_owner_id.present?
rake_puts("Looking for old owner by id\n")
old_owner = Administrateur.find(old_owner_id)
elsif old_owner_email.present?
rake_puts("Looking for old owner by email\n")
old_owner = Administrateur.find_by('LOWER(email) = LOWER(?)', old_owner_email)
end
if old_owner.blank?
fail "Must specify an old owner"
end
procedures = old_owner.procedures
new_owner = nil
if new_owner_id.present?
rake_puts("Looking for new owner by id\n")
new_owner = Administrateur.find(new_owner_id)
elsif new_owner_email.present?
rake_puts("Looking for new owner by email\n")
new_owner = Administrateur.find_by('LOWER(email) = LOWER(?)', new_owner_email)
end
if new_owner.blank?
fail "Must specify a new owner"
end
procedures.update_all(administrateur_id: new_owner.id)
end
2018-06-27 12:38:38 +02:00
desc <<~EOD
2018-10-04 11:59:27 +02:00
Delete the user account for a given USER_EMAIL.
2018-06-27 12:38:38 +02:00
Only works if the user has no dossier where the instruction has started.
EOD
task delete_user_account: :environment do
2018-10-04 11:59:27 +02:00
user_email = ENV['USER_EMAIL']
if user_email.nil?
fail "Must specify a USER_EMAIL"
2018-06-27 12:38:38 +02:00
end
2018-10-04 11:59:27 +02:00
user = User.find_by(email: user_email)
2018-06-27 12:38:38 +02:00
if user.dossiers.state_instruction_commencee.any?
fail "Cannot delete this user because instruction has started for some dossiers"
end
2018-10-01 14:20:14 +02:00
user.dossiers.each(&:delete_and_keep_track)
2018-06-27 12:38:38 +02:00
user.destroy
end
2018-10-05 19:05:55 +02:00
desc <<~EOD
Change the SIRET for a given dossier (specified by DOSSIER_ID)
EOD
task update_dossier_siret: :environment do
siret_number = ENV['SIRET']
dossier_id = ENV['DOSSIER_ID']
if siret_number.nil?
fail "Must specify a SIRET"
end
siret_number = siret_number.dup # Unfreeze the string
siret = Siret.new(siret: siret_number)
if siret.invalid?
fail siret.errors.full_messages.to_sentence
end
dossier = Dossier.find(dossier_id)
EtablissementUpdateJob.perform_now(dossier, siret_number)
end
2018-10-05 19:11:22 +02:00
desc <<~EOD
Change a users mail from OLD_EMAIL to NEW_EMAIL.
Also works for administrateurs and instructeurs.
EOD
task change_user_email: :environment do
old_email = ENV['OLD_EMAIL']
new_email = ENV['NEW_EMAIL']
if User.find_by(email: new_email).present?
fail "There is an existing account for #{new_email}, not overwriting"
end
user = User.find_by(email: old_email)
if user.nil?
fail "Couldnt find existing account for #{old_email}"
end
user.update(email: new_email)
end
desc <<~EOD
Activate feature publish draft
EOD
task activate_publish_draft: :environment do
start_with = ENV['START_WITH']
administrateurs = Administrateur.where("email like ?", "#{start_with}%")
rake_puts("Activating publish draft for #{administrateurs.count} administrateurs...")
administrateurs.each do |a|
rake_puts("Activating publish draft for #{a.email}")
a.features["publish_draft"] = true
a.save
a.procedures.brouillon.each do |p|
if p.path.nil?
p.path = SecureRandom.uuid
p.save
end
end
end
end
end