diff --git a/app/tasks/maintenance/verify_confirmed_users_task.rb b/app/tasks/maintenance/verify_confirmed_users_task.rb new file mode 100644 index 000000000..e77d9bd93 --- /dev/null +++ b/app/tasks/maintenance/verify_confirmed_users_task.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module Maintenance + # This task backfills the email_verified_at field for confirmed users + # as a bug fixed by https://github.com/demarches-simplifiees/demarches-simplifiees.fr/pull/11074 + # produced unverified confirmed users. + class VerifyConfirmedUsersTask < MaintenanceTasks::Task + include RunnableOnDeployConcern + + run_on_first_deploy + + no_collection + + attribute :verifying_date, :string + attribute :correction_period, :integer + + def process + email_verified_at = if verifying_date.present? + Time.zone.parse(verifying_date) + else + Time.zone.parse('25/11/2024') + end + + created_at = if correction_period.present? + correction_period.months.ago.. + else + 4.months.ago.. + end + + User + .where.not(confirmed_at: nil) + .where(email_verified_at: nil) + .where(created_at:) + .where(instructeur: { id: nil }) # instructeur is eager loaded + .where.missing(:expert) + .update_all(email_verified_at:) + end + end +end diff --git a/spec/tasks/maintenance/verify_confirmed_users_task_spec.rb b/spec/tasks/maintenance/verify_confirmed_users_task_spec.rb new file mode 100644 index 000000000..a8be9d577 --- /dev/null +++ b/spec/tasks/maintenance/verify_confirmed_users_task_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +module Maintenance + RSpec.describe VerifyConfirmedUsersTask do + describe "#process" do + subject(:process) { described_class.process } + + let!(:unverified_confirmed_user) { create(:user, confirmed_at: Time.zone.now) } + let!(:unverified_unconfirmed_user) { create(:user, confirmed_at: nil) } + let!(:unverified_confirmed_instructeur) do + user = create(:instructeur).user + user.update!(confirmed_at: Time.zone.now) + user + end + + let!(:unverified_confirmed_expert) do + user = create(:expert).user + user.update!(confirmed_at: Time.zone.now) + user + end + + it 'verifies only confirmed user' do + process + + expect(unverified_confirmed_user.reload.email_verified_at).to be_present + + expect(unverified_unconfirmed_user.reload.email_verified_at).to be_nil + expect(unverified_confirmed_instructeur.reload.email_verified_at).to be_nil + expect(unverified_confirmed_expert.reload.email_verified_at).to be_nil + end + end + end +end