Refactor: reduce nesting
We can reduce cyclomatic complexity (how far we indented) by checking for a negative condition and exiting early, instead of checking for a positive condition and continuing
This commit is contained in:
parent
5838f15b1c
commit
420d954568
|
@ -12,30 +12,33 @@ def handle_redeem(message, user_id):
|
|||
else:
|
||||
note = split_message[1]
|
||||
|
||||
if redeem in current_app.config['REDEEMS']:
|
||||
db = get_db()
|
||||
price = current_app.config['REDEEMS'][redeem]["price"]
|
||||
redeem_type = current_app.config['REDEEMS'][redeem]["type"]
|
||||
points = read_users_points(db, user_id)
|
||||
if points is not None and points >= price:
|
||||
if redeem_type == "counter":
|
||||
add_to_counter(db, redeem)
|
||||
use_points(db, user_id, price)
|
||||
send_chat(f"{redeem} redeemed for {price} points.")
|
||||
elif redeem_type == "list":
|
||||
add_to_redeem_queue(db, user_id, redeem)
|
||||
use_points(db, user_id, price)
|
||||
send_chat(f"{redeem} redeemed for {price} points.")
|
||||
elif redeem_type == "note":
|
||||
if note is not None:
|
||||
add_to_redeem_queue(db, user_id, redeem, note)
|
||||
use_points(db, user_id, price)
|
||||
send_chat(f"{redeem} redeemed for {price} points.")
|
||||
else:
|
||||
send_chat(f"Cannot redeem {redeem}, no note included.")
|
||||
else:
|
||||
send_chat(f"{redeem} not redeemed because of an error.")
|
||||
else:
|
||||
send_chat(f"Can't redeem {redeem}, you don't have enough points.")
|
||||
else:
|
||||
if redeem not in current_app.config['REDEEMS']:
|
||||
send_chat("Can't redeem, redeem not found.")
|
||||
return
|
||||
|
||||
db = get_db()
|
||||
price = current_app.config['REDEEMS'][redeem]["price"]
|
||||
redeem_type = current_app.config['REDEEMS'][redeem]["type"]
|
||||
points = read_users_points(db, user_id)
|
||||
|
||||
if not points or points < price:
|
||||
send_chat(f"Can't redeem {redeem}, you don't have enough points.")
|
||||
return
|
||||
|
||||
if redeem_type == "counter":
|
||||
add_to_counter(db, redeem)
|
||||
use_points(db, user_id, price)
|
||||
send_chat(f"{redeem} redeemed for {price} points.")
|
||||
elif redeem_type == "list":
|
||||
add_to_redeem_queue(db, user_id, redeem)
|
||||
use_points(db, user_id, price)
|
||||
send_chat(f"{redeem} redeemed for {price} points.")
|
||||
elif redeem_type == "note":
|
||||
if not note:
|
||||
send_chat(f"Cannot redeem {redeem}, no note included.")
|
||||
return
|
||||
add_to_redeem_queue(db, user_id, redeem, note)
|
||||
use_points(db, user_id, price)
|
||||
send_chat(f"{redeem} redeemed for {price} points.")
|
||||
else:
|
||||
send_chat(f"{redeem} not redeemed because of an error.")
|
||||
|
|
Loading…
Reference in New Issue