Compare commits
2 commits
bc4beb825a
...
7c9656e4d9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c9656e4d9 | ||
|
|
ed256b8e6e |
1 changed files with 52 additions and 29 deletions
|
|
@ -18,33 +18,60 @@ def get_balance():
|
||||||
@transactions_bp.route('/collect', methods=['POST'])
|
@transactions_bp.route('/collect', methods=['POST'])
|
||||||
@jwt_required
|
@jwt_required
|
||||||
def collect():
|
def collect():
|
||||||
user_bid = request.bid
|
bid = request.bid
|
||||||
data = request.get_json()
|
|
||||||
cooldown = Config.COLLECT_COOLDOWN
|
cooldown = Config.COLLECT_COOLDOWN
|
||||||
try:
|
try:
|
||||||
db.start_transaction()
|
|
||||||
with db.cursor(dictionary=True) as cur:
|
with db.cursor(dictionary=True) as cur:
|
||||||
query = """
|
cur.execute("SELECT * FROM user_jobs WHERE bid = %s", (bid,))
|
||||||
SELECT uj.job_id, \
|
user_jt = cur.fetchone()
|
||||||
uj.collected, \
|
if not user_jt:
|
||||||
j.salary_class, \
|
return jsonify({"error": "You dont have any Jobs"}), 404
|
||||||
s.money AS salary_amount
|
active_cooldown = user_jt["collected"]
|
||||||
FROM user_jobs uj \
|
except mysql.connector.Error as err:
|
||||||
JOIN jobs j ON uj.job_id = j.job_id \
|
current_app.logger.error(f"Database error in collect, salary: {err}")
|
||||||
JOIN salary s ON j.salary_class = s.class
|
return jsonify({"error": "A database error occurred, please try again later."}), 500
|
||||||
WHERE uj.user_bid = %s \
|
if active_cooldown + timedelta(hours=cooldown) > datetime.now(timezone.utc):
|
||||||
"""
|
remaining_time = (active_cooldown + timedelta(hours=cooldown)) - datetime.now(timezone.utc)
|
||||||
cur.execute(query, (user_bid,))
|
hours = int(remaining_time.total_seconds() // 3600)
|
||||||
user_jobs = cur.fetchall()
|
minutes = int(remaining_time.total_seconds() % 3600 // 60)
|
||||||
if not user_jobs:
|
return jsonify({"error": f"You can only collect your salary every {cooldown} hours. Please wait {hours}h {minutes}m."}), 429
|
||||||
|
job = user_jt["job_id"]
|
||||||
|
try:
|
||||||
|
with db.cursor(dictionary=True) as cur:
|
||||||
|
cur.execute("SELECT * FROM jobs WHERE job_id = %i", (job,))
|
||||||
|
job_data = cur.fetchone()
|
||||||
|
except mysql.connector.Error as err:
|
||||||
|
current_app.logger.error(f"Database error in collect, salary: {err}")
|
||||||
|
return jsonify({"error": "A database error occurred, please try again later."}), 500
|
||||||
|
if not job_data:
|
||||||
|
return jsonify({"error": "Invalid Job","message":"If you believe this is an error, contact a "
|
||||||
|
"Administrator"}), 404
|
||||||
|
salary_class = job_data["salary_class"]
|
||||||
|
try:
|
||||||
|
with db.cursor(dictionary=True) as cur:
|
||||||
|
cur.execute("SELECT * FROM salary WHERE class = %i", (salary_class,))
|
||||||
|
salary_data = cur.fetchone()
|
||||||
|
except mysql.connector.Error as err:
|
||||||
|
current_app.logger.error(f"Database error in collect, salary: {err}")
|
||||||
|
return jsonify({"error": "A database error occurred, please try again later."}), 500
|
||||||
|
if not salary_data:
|
||||||
|
return jsonify({"error": "Invalid Salary Class"}), 500
|
||||||
|
amount = salary_data["money"]
|
||||||
|
try:
|
||||||
|
with db.cursor(dictionary=True) as cur:
|
||||||
|
cur.execute("UPDATE users SET balance = balance + %s WHERE bid = %s", (amount, bid,))
|
||||||
|
cur.execute("UPDATE user_jobs SET collected = %s WHERE bid = %s", (datetime.now(timezone.utc), bid,))
|
||||||
|
cur.execute("INSERT INTO transactions (source, target, amount, type, timestamp, status) VALUES (%s, %s, "
|
||||||
|
"%s, %s, %s, %s)", "NULL", bid, amount, "salary", datetime.now(timezone.utc), "completed",)
|
||||||
|
cur.execute("SELECT balance FROM users WHERE bid = %s", (bid,))
|
||||||
|
new_bal2 = cur.fetchone()
|
||||||
|
new_bal = new_bal2["balance"]
|
||||||
|
db.commit()
|
||||||
|
except mysql.connector.Error as err:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
return jsonify({"error": "You do not have a job to collect a salary from."}), 400
|
current_app.logger.error(f"Database error in collect, salary: {err}")
|
||||||
total_payout = 0
|
return jsonify({"error": "A database error occurred, please try again later."}), 500
|
||||||
jobs_collected_count = 0
|
return jsonify({"message":"Salary Collected","New Balance":new_bal}), 200
|
||||||
now = datetime.now(timezone.utc)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@transactions_bp.route('/transfer', methods=['POST'])
|
@transactions_bp.route('/transfer', methods=['POST'])
|
||||||
@jwt_required
|
@jwt_required
|
||||||
|
|
@ -84,7 +111,3 @@ def transfer():
|
||||||
db.rollback()
|
db.rollback()
|
||||||
print(f"Transactional Error: {err}")
|
print(f"Transactional Error: {err}")
|
||||||
return jsonify({"error": "A database error occurred during the transfer."}), 500
|
return jsonify({"error": "A database error occurred during the transfer."}), 500
|
||||||
|
|
||||||
@transactions_bp.route('/collect', methods=['POST'])
|
|
||||||
def collect_salary():
|
|
||||||
return jsonify({"message":"no implementation"}), 501
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue