Merge pull request #6006 from betagouv/fix-avis-concern

backfill experts_procedure_id on avis table
This commit is contained in:
Kara Diaby 2021-03-23 15:27:59 +01:00 committed by GitHub
commit b2a0bc1e6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,7 +1,7 @@
namespace :after_party do
desc 'Deployment task: backfill_experts_procedure_id_missing_on_avis'
task backfill_experts_procedure_id_missing_on_avis: :environment do
puts "Running deploy task 'backfill_experts_procedure_id_missing_on_avis'"
desc 'Deployment task: backfill_experts_procedure_on_avis_2'
task backfill_experts_procedure_on_avis_2: :environment do
puts "Running deploy task 'backfill_experts_procedure_on_avis_2'"
without_instructeur = Avis.where(experts_procedure_id: nil, answer: nil, instructeur_id: nil).where.not(email: nil)
with_instructeur = Avis.where(experts_procedure_id: nil, answer: nil, email: nil).where.not(instructeur_id: nil)
@ -10,10 +10,11 @@ namespace :after_party do
without_instructeur.find_each do |avis|
# if the avis email is valid then we create the associated expert
if Devise.email_regexp.match?(avis.email)
user = User.create_or_promote_to_expert(avis.email, SecureRandom.hex)
email = avis.email.strip
if Devise.email_regexp.match?(email)
user = User.create_or_promote_to_expert(email, SecureRandom.hex)
user.reload
experts_procedure = ExpertsProcedure.find_or_create_by(procedure: avis.dossier.procedure, expert: user.expert)
experts_procedure = ExpertsProcedure.find_or_create_by!(procedure: avis.dossier.procedure, expert: user.expert)
avis.update_column(:experts_procedure_id, experts_procedure.id)
end
progress.inc
@ -25,13 +26,15 @@ namespace :after_party do
if instructeur && instructeur.user
user = User.create_or_promote_to_expert(instructeur.user.email, SecureRandom.hex)
user.reload
experts_procedure = ExpertsProcedure.find_or_create_by(procedure: avis.dossier.procedure, expert: user.expert)
experts_procedure = ExpertsProcedure.find_or_create_by!(procedure: avis.dossier.procedure, expert: user.expert)
avis.update_column(:experts_procedure_id, experts_procedure.id)
end
progress2.inc
end
progress2.finish
# Update task as completed. If you remove the line below, the task will
# run with every deploy (or every time you call after_party:run).
AfterParty::TaskRecord
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
end