add notification email for accounts with extreme negative balances

This commit is contained in:
forest 2022-03-30 12:10:55 -05:00
parent 51da38e176
commit 6741eba404
1 changed files with 28 additions and 0 deletions

View File

@ -235,6 +235,9 @@ def get_warnings_list():
def notify_users_about_account_balance():
accounts = get_model().all_accounts()
out_of_bounds_accounts = dict()
for account in accounts:
vms = get_model().list_vms_for_account(account['email'])
payments = get_model().list_payments_for_account(account['email'])
@ -243,6 +246,9 @@ def notify_users_about_account_balance():
balance_now = get_account_balance(vms, payments, datetime.utcnow())
current_warning = account['account_balance_warning']
if balance_now < -11:
out_of_bounds_accounts[account['email']] = balance_now
longterm_vms = list(filter(lambda vm: vm['shortterm'] == False, vms))
if len(longterm_vms) == 0:
@ -280,6 +286,28 @@ def notify_users_about_account_balance():
current_app.logger.warning(f"cron_task: deleting {vm['id']} ( {account['email']} ) due to negative account balance.")
current_app.config["HUB_MODEL"].destroy(email=account["email"], id=vm['id'])
get_model().delete_vm(email=account["email"], id=vm['id'])
if len(out_of_bounds_accounts) > 0:
lines = ["The following accounts have out-of-bounds account balances:", ""]
for email, balance in out_of_bounds_accounts.items():
lines.append(f"{email}: ${format(balance, '.2f')}")
email_addresses_raw = current_app.config['ADMIN_EMAIL_ADDRESSES'].split(",")
email_addresses = list(filter(lambda x: len(x) > 6, map(lambda x: x.strip(), email_addresses_raw ) ))
current_app.logger.info(f"notify_users_about_account_balance: sending out of bounds account balances email to {','.join(email_addresses)}:")
for line in lines:
current_app.logger.info(f"notify_users_about_account_balance: {line}.")
current_app.config["FLASK_MAIL_INSTANCE"].send(
Message(
"Capsul Out Of Bounds Account Balance Notification",
sender=current_app.config["MAIL_DEFAULT_SENDER"],
body="\n".join(lines),
recipients=email_addresses
)
)
def delete_shortterm_vms_if_account_is_empty():