Merge pull request #11075 from demarches-simplifiees/backfill_verified_at_for_user
ETQ Usager ayant confirmé mon mail, je veux recevoir mes notifications
This commit is contained in:
commit
7b4c21b431
2 changed files with 72 additions and 0 deletions
39
app/tasks/maintenance/verify_confirmed_users_task.rb
Normal file
39
app/tasks/maintenance/verify_confirmed_users_task.rb
Normal 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
|
33
spec/tasks/maintenance/verify_confirmed_users_task_spec.rb
Normal file
33
spec/tasks/maintenance/verify_confirmed_users_task_spec.rb
Normal 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
|
Loading…
Reference in a new issue