2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-11-07 07:33:12 +01:00
|
|
|
class Expired::UsersDeletionService < Expired::MailRateLimiter
|
2023-11-03 13:52:37 +01:00
|
|
|
def process_expired
|
2023-11-09 17:46:22 +01:00
|
|
|
# we are working on two dataset because we apply two incompatible join on the same query
|
|
|
|
# inner join on users not having dossier.en_instruction [so we do not destroy users with dossiers.en_instruction]
|
|
|
|
# outer join on users not having dossier at all [so we destroy users without dossiers]
|
2023-11-16 14:12:18 +01:00
|
|
|
[expired_users_without_dossiers, expired_users_with_dossiers].each do |expired_segment|
|
|
|
|
delete_notified_users(expired_segment)
|
|
|
|
send_inactive_close_to_expiration_notice(expired_segment)
|
2023-11-03 12:13:41 +01:00
|
|
|
end
|
2023-11-03 10:11:08 +01:00
|
|
|
end
|
|
|
|
|
2023-11-06 09:29:47 +01:00
|
|
|
private
|
|
|
|
|
2023-12-14 10:30:03 +01:00
|
|
|
# in case of perf downside :
|
|
|
|
# consider using perform_all_later
|
|
|
|
# consider changing notify_inactive_close_to_deletion method, taking a user_id, and updating inactive_close_to_expiration_notice_sent_at
|
2023-11-03 13:52:37 +01:00
|
|
|
def send_inactive_close_to_expiration_notice(users)
|
2023-12-14 10:30:03 +01:00
|
|
|
user_ids = to_notify_only(users).pluck(:id)
|
|
|
|
user_ids.each do |user_id|
|
|
|
|
send_with_delay(UserMailer.notify_inactive_close_to_deletion(User.find(user_id)))
|
2023-11-03 10:11:08 +01:00
|
|
|
end
|
2023-12-14 10:30:03 +01:00
|
|
|
User.where(id: user_ids).update_all(inactive_close_to_expiration_notice_sent_at: Time.zone.now.utc)
|
2023-11-03 10:11:08 +01:00
|
|
|
end
|
|
|
|
|
2023-11-16 14:12:18 +01:00
|
|
|
def delete_notified_users(users)
|
2023-12-14 10:30:03 +01:00
|
|
|
user_ids = only_notified(users).pluck(:id)
|
|
|
|
user_ids.each do |user_id|
|
|
|
|
user = User.find(user_id)
|
2023-11-04 08:45:40 +01:00
|
|
|
begin
|
2023-11-17 10:22:34 +01:00
|
|
|
user.delete_and_keep_track_dossiers_also_delete_user(nil, reason: :user_expired)
|
2023-11-04 08:45:40 +01:00
|
|
|
rescue => e
|
|
|
|
Sentry.capture_exception(e, extra: { user_id: user.id })
|
|
|
|
end
|
2023-11-03 09:24:38 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop:disable DS/Unscoped
|
2023-11-16 14:12:18 +01:00
|
|
|
def expired_users_with_dossiers
|
2023-11-21 11:18:02 +01:00
|
|
|
dossiers = Dossier.arel_table
|
|
|
|
users = User.arel_table
|
|
|
|
|
2023-11-16 14:12:18 +01:00
|
|
|
expired_users
|
2023-11-21 11:18:02 +01:00
|
|
|
.joins(
|
|
|
|
users.join(dossiers, Arel::Nodes::OuterJoin)
|
|
|
|
.on(users[:id].eq(dossiers[:user_id])
|
|
|
|
.and(dossiers[:state].eq(Dossier.states.fetch(:en_instruction))))
|
|
|
|
.join_sources
|
|
|
|
)
|
|
|
|
.where(dossiers[:id].eq(nil))
|
2023-11-17 09:41:18 +01:00
|
|
|
.group("users.id")
|
2023-11-03 09:24:38 +01:00
|
|
|
end
|
2023-11-03 12:13:41 +01:00
|
|
|
|
2023-11-16 14:12:18 +01:00
|
|
|
def expired_users_without_dossiers
|
|
|
|
expired_users.where.missing(:dossiers)
|
2023-11-09 17:46:22 +01:00
|
|
|
end
|
|
|
|
|
2023-11-16 14:12:18 +01:00
|
|
|
def expired_users
|
2023-11-03 12:13:41 +01:00
|
|
|
User.unscoped
|
2023-11-09 17:46:22 +01:00
|
|
|
.where.missing(:expert, :instructeur, :administrateur)
|
2023-11-07 10:45:40 +01:00
|
|
|
.where(last_sign_in_at: ..Expired::INACTIVE_USER_RETATION_IN_YEAR.years.ago)
|
2023-11-03 12:13:41 +01:00
|
|
|
end
|
2023-11-03 09:24:38 +01:00
|
|
|
# rubocop:enable DS/Unscoped
|
2023-11-03 10:11:08 +01:00
|
|
|
|
2023-11-03 13:52:37 +01:00
|
|
|
def to_notify_only(users)
|
2023-11-03 12:13:41 +01:00
|
|
|
users.where(inactive_close_to_expiration_notice_sent_at: nil)
|
2023-11-07 10:51:55 +01:00
|
|
|
.limit(daily_limit) # ensure to not send too much email
|
2023-11-03 10:11:08 +01:00
|
|
|
end
|
|
|
|
|
2023-11-16 14:12:18 +01:00
|
|
|
def only_notified(users)
|
2023-11-07 07:47:40 +01:00
|
|
|
users.where.not(inactive_close_to_expiration_notice_sent_at: Expired::REMAINING_WEEKS_BEFORE_EXPIRATION.weeks.ago..)
|
2023-11-07 10:51:55 +01:00
|
|
|
.limit(daily_limit) # event if we do not send email, avoid to destroy 800k user in one batch
|
2023-11-03 10:11:08 +01:00
|
|
|
end
|
2023-11-04 08:45:40 +01:00
|
|
|
|
2023-11-07 10:51:55 +01:00
|
|
|
def daily_limit
|
2023-11-04 08:45:40 +01:00
|
|
|
(ENV['EXPIRE_USER_DELETION_JOB_LIMIT'] || 10_000).to_i
|
|
|
|
end
|
2023-11-03 09:24:38 +01:00
|
|
|
end
|