diff --git a/app/config.py b/app/config.py index 55e027bd..4dc11beb 100644 --- a/app/config.py +++ b/app/config.py @@ -246,6 +246,7 @@ JOB_ONBOARDING_2 = "onboarding-2" JOB_ONBOARDING_3 = "onboarding-3" JOB_ONBOARDING_4 = "onboarding-4" JOB_BATCH_IMPORT = "batch-import" +JOB_DELETE_ACCOUNT = "delete-account" # for pagination PAGE_LIMIT = 20 diff --git a/app/dashboard/views/setting.py b/app/dashboard/views/setting.py index a2248b04..a50cdfb7 100644 --- a/app/dashboard/views/setting.py +++ b/app/dashboard/views/setting.py @@ -15,7 +15,7 @@ from wtforms import StringField, validators from wtforms.fields.html5 import EmailField from app import s3, email_utils -from app.config import URL, FIRST_ALIAS_DOMAIN +from app.config import URL, FIRST_ALIAS_DOMAIN, JOB_DELETE_ACCOUNT from app.dashboard.base import dashboard_bp from app.email_utils import ( email_can_be_used_as_mailbox, @@ -37,6 +37,7 @@ from app.models import ( SLDomain, CoinbaseSubscription, AppleSubscription, + Job, ) from app.utils import random_string, sanitize_email @@ -178,12 +179,21 @@ def setting(): return redirect(url_for("dashboard.setting")) elif request.form.get("form-name") == "delete-account": - LOG.warning("Delete account %s", current_user) - User.delete(current_user.id) - db.session.commit() - flash("Your account has been deleted", "success") - logout_user() - return redirect(url_for("auth.register")) + # Schedule delete account job + LOG.warning("schedule delete account job for %s", current_user) + Job.create( + name=JOB_DELETE_ACCOUNT, + payload={"user_id": current_user.id}, + run_at=arrow.now(), + commit=True, + ) + + flash( + "Your account deletion has been scheduled. " + "You'll receive an email when the deletion is finished", + "success", + ) + return redirect(url_for("dashboard.setting")) elif request.form.get("form-name") == "change-alias-generator": scheme = int(request.form.get("alias-generator-scheme")) diff --git a/job_runner.py b/job_runner.py index 3ad9652f..b24c052e 100644 --- a/job_runner.py +++ b/job_runner.py @@ -11,6 +11,7 @@ from app.config import ( JOB_ONBOARDING_2, JOB_ONBOARDING_4, JOB_BATCH_IMPORT, + JOB_DELETE_ACCOUNT, ) from app.email_utils import ( send_email, @@ -149,7 +150,25 @@ if __name__ == "__main__": batch_import_id = job.payload.get("batch_import_id") batch_import = BatchImport.get(batch_import_id) handle_batch_import(batch_import) + elif job.name == JOB_DELETE_ACCOUNT: + user_id = job.payload.get("user_id") + user = User.get(user_id) + if not user: + LOG.exception("No user found for %s", user_id) + continue + + user_email = user.email + LOG.warning("Delete user %s", user) + User.delete(user.id) + db.session.commit() + + send_email( + user_email, + "Your SimpleLogin account has been deleted", + render("transactional/account-delete.txt"), + render("transactional/account-delete.html"), + ) else: LOG.exception("Unknown job name %s", job.name) diff --git a/templates/emails/transactional/account-delete.html b/templates/emails/transactional/account-delete.html new file mode 100644 index 00000000..191ba769 --- /dev/null +++ b/templates/emails/transactional/account-delete.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block content %} + {% call text() %} +

+ Your SimpleLogin account has been deleted successfully. +

+ {% endcall %} + + {% call text() %} + Thank you for having used SimpleLogin. + {% endcall %} + + {{ render_text('Best,
SimpleLogin Team.') }} +{% endblock %} diff --git a/templates/emails/transactional/account-delete.txt b/templates/emails/transactional/account-delete.txt new file mode 100644 index 00000000..42e8f510 --- /dev/null +++ b/templates/emails/transactional/account-delete.txt @@ -0,0 +1,6 @@ +Your SimpleLogin account has been deleted successfully. + +Thank you for having used SimpleLogin. + +Best, +SimpleLogin team.