mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-19 05:09:38 +02:00
Add ability to delete Maybe account (#698)
* Build out user deactivation and purging workflows * Add i18n translations for user deletion * Add tests for user deletion * Fix lint issue
This commit is contained in:
parent
55cb1ae5bd
commit
19ee773d9b
15 changed files with 128 additions and 8 deletions
|
@ -38,4 +38,40 @@ class User < ApplicationRecord
|
|||
def has_seen_upgrade_alert?(upgrade)
|
||||
last_alerted_upgrade_commit_sha == upgrade.commit_sha
|
||||
end
|
||||
|
||||
# Deactivation
|
||||
validate :can_deactivate, if: -> { active_changed? && !active }
|
||||
after_update_commit :purge_later, if: -> { saved_change_to_active?(from: true, to: false) }
|
||||
|
||||
def deactivate
|
||||
update active: false, email: deactivated_email
|
||||
end
|
||||
|
||||
def can_deactivate
|
||||
if admin? && family.users.count > 1
|
||||
errors.add(:base, I18n.t("activerecord.errors.user.cannot_deactivate_admin_with_other_users"))
|
||||
end
|
||||
end
|
||||
|
||||
def purge_later
|
||||
UserPurgeJob.perform_later(self)
|
||||
end
|
||||
|
||||
def purge
|
||||
if last_user_in_family?
|
||||
family.destroy
|
||||
else
|
||||
destroy
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def last_user_in_family?
|
||||
family.users.count == 1
|
||||
end
|
||||
|
||||
def deactivated_email
|
||||
email.gsub(/@/, "-deactivated-#{SecureRandom.uuid}@")
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue