From 6e6bd81872865022a5bfcbb29a59dc5909613b58 Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Thu, 1 Dec 2022 03:24:38 +0200 Subject: [PATCH] better kofi claiming --- files/routes/users.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/files/routes/users.py b/files/routes/users.py index f20f83dd6..81caed7c4 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -1139,27 +1139,33 @@ kofi_tiers={ @auth_required def settings_kofi(v:User): if not KOFI_TOKEN or KOFI_TOKEN == DEFAULT_CONFIG_VALUE: abort(404) + if not (v.email and v.is_activated): abort(400, f"You must have a verified email to verify {patron} status and claim your rewards!") - transaction = g.db.query(Transaction).filter_by(email=v.email).order_by(Transaction.created_utc.desc()).first() - if not transaction: - abort(404, "Email not found") - if transaction.claimed: + + transactions = g.db.query(Transaction).filter_by(email=v.email, claimed=None).order_by(Transaction.created_utc.desc()).all() + + if not transactions: abort(400, f"{patron} rewards already claimed") - tier = kofi_tiers[transaction.amount] + highest_tier = 0 + marseybux = 0 + + for transaction in transactions: + tier = kofi_tiers[transaction.amount] + marseybux += marseybux_li[tier] + if tier > highest_tier: highest_tier = tier + transaction.claimed = True + g.db.add(transaction) - marseybux = marseybux_li[tier] v.pay_account('marseybux', marseybux) send_repeatable_notification(v.id, f"You have received {marseybux} Marseybux! You can use them to buy awards in the [shop](/shop).") g.db.add(v) - if tier > v.patron: - v.patron = tier + if highest_tier > v.patron: + v.patron = highest_tier for badge in g.db.query(Badge).filter(Badge.user_id == v.id, Badge.badge_id > 20, Badge.badge_id < 28).all(): g.db.delete(badge) - badge_grant(badge_id=20+tier, user=v) + badge_grant(badge_id=20+highest_tier, user=v) - transaction.claimed = True - g.db.add(transaction) return {"message": f"{patron} rewards claimed!"}