fix: backfill user email_verified_at

This commit is contained in:
simon lehericey 2024-11-25 10:17:50 +01:00
parent 0c6dbc86f0
commit 58f1608d2f
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
2 changed files with 72 additions and 0 deletions

View file

@ -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

View file

@ -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