Compare commits
No commits in common. "7c9656e4d97ff5642ec830f70d28e6fc139fca2b" and "bc4beb825a8d484f4ef0ad22a69824c00e9e6a87" have entirely different histories.
7c9656e4d9
...
bc4beb825a
1 changed files with 29 additions and 52 deletions
|
|
@ -18,60 +18,33 @@ def get_balance():
|
|||
@transactions_bp.route('/collect', methods=['POST'])
|
||||
@jwt_required
|
||||
def collect():
|
||||
bid = request.bid
|
||||
user_bid = request.bid
|
||||
data = request.get_json()
|
||||
cooldown = Config.COLLECT_COOLDOWN
|
||||
try:
|
||||
db.start_transaction()
|
||||
with db.cursor(dictionary=True) as cur:
|
||||
cur.execute("SELECT * FROM user_jobs WHERE bid = %s", (bid,))
|
||||
user_jt = cur.fetchone()
|
||||
if not user_jt:
|
||||
return jsonify({"error": "You dont have any Jobs"}), 404
|
||||
active_cooldown = user_jt["collected"]
|
||||
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 active_cooldown + timedelta(hours=cooldown) > datetime.now(timezone.utc):
|
||||
remaining_time = (active_cooldown + timedelta(hours=cooldown)) - datetime.now(timezone.utc)
|
||||
hours = int(remaining_time.total_seconds() // 3600)
|
||||
minutes = int(remaining_time.total_seconds() % 3600 // 60)
|
||||
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()
|
||||
current_app.logger.error(f"Database error in collect, salary: {err}")
|
||||
return jsonify({"error": "A database error occurred, please try again later."}), 500
|
||||
return jsonify({"message":"Salary Collected","New Balance":new_bal}), 200
|
||||
query = """
|
||||
SELECT uj.job_id, \
|
||||
uj.collected, \
|
||||
j.salary_class, \
|
||||
s.money AS salary_amount
|
||||
FROM user_jobs uj \
|
||||
JOIN jobs j ON uj.job_id = j.job_id \
|
||||
JOIN salary s ON j.salary_class = s.class
|
||||
WHERE uj.user_bid = %s \
|
||||
"""
|
||||
cur.execute(query, (user_bid,))
|
||||
user_jobs = cur.fetchall()
|
||||
if not user_jobs:
|
||||
db.rollback()
|
||||
return jsonify({"error": "You do not have a job to collect a salary from."}), 400
|
||||
total_payout = 0
|
||||
jobs_collected_count = 0
|
||||
now = datetime.now(timezone.utc)
|
||||
|
||||
|
||||
|
||||
|
||||
@transactions_bp.route('/transfer', methods=['POST'])
|
||||
@jwt_required
|
||||
|
|
@ -110,4 +83,8 @@ def transfer():
|
|||
except mysql.connector.Error as err:
|
||||
db.rollback()
|
||||
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