90 lines
No EOL
3.1 KiB
Python
90 lines
No EOL
3.1 KiB
Python
from flask import Blueprint
|
|
|
|
from config import Config
|
|
from interbend.db import db, get_user
|
|
from interbend.auth import *
|
|
import mysql.connector
|
|
|
|
transactions_bp = Blueprint('transactions_bp', __name__)
|
|
|
|
@transactions_bp.route('/balance', methods=['GET'])
|
|
def get_balance():
|
|
bid = request.args.get('bid')
|
|
user = get_user(bid)
|
|
if not user:
|
|
return jsonify({"error": "User not found."}), 404
|
|
return jsonify({"balance": user["balance"]})
|
|
|
|
@transactions_bp.route('/collect', methods=['POST'])
|
|
@jwt_required
|
|
def collect():
|
|
user_bid = request.bid
|
|
data = request.get_json()
|
|
cooldown = Config.COLLECT_COOLDOWN
|
|
try:
|
|
db.start_transaction()
|
|
with db.cursor(dictionary=True) as cur:
|
|
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
|
|
def transfer():
|
|
# Ignore warning because it's dynamically added via jwt required.
|
|
user_bid = request.bid
|
|
data = request.get_json()
|
|
fbid = data.get('from')
|
|
tbid = data.get('to')
|
|
amount = data.get('amount')
|
|
if not tbid or not amount:
|
|
return jsonify({"error": "To and amount are required"}), 400
|
|
if not fbid:
|
|
fbid = user_bid
|
|
if fbid != user_bid:
|
|
return jsonify({"error": "Unauthorized transfer from another account"}), 401
|
|
try:
|
|
amount = float(amount)
|
|
if amount <= 0:
|
|
raise ValueError
|
|
except ValueError:
|
|
return jsonify({"error": "Invalid amount","message":"Try to request Money instead"}), 400
|
|
sender = get_user(fbid)
|
|
receiver = get_user(tbid)
|
|
if not sender or not receiver:
|
|
return jsonify({"error":"User not found"}), 404
|
|
if sender["balance"] < amount:
|
|
return jsonify({"error": "Insufficient funds"}), 400
|
|
try:
|
|
db.start_transaction()
|
|
with db.cursor(dictionary=True) as cur:
|
|
cur.execute("UPDATE users SET balance = balance - %s WHERE bid = %s", (amount, fbid))
|
|
cur.execute("UPDATE users SET balance = balance + %s WHERE bid = %s", (amount, tbid))
|
|
db.commit()
|
|
return jsonify({"message": "Transfer successful"}), 200
|
|
except mysql.connector.Error as err:
|
|
db.rollback()
|
|
print(f"Transactional Error: {err}")
|
|
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 |