Compare commits
2 commits
5aae713469
...
bc4beb825a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc4beb825a | ||
|
|
bcc089e13b |
3 changed files with 73 additions and 6 deletions
|
|
@ -18,3 +18,4 @@ class Config:
|
||||||
|
|
||||||
# Admin
|
# Admin
|
||||||
ADMIN_KEY = os.getenv('ADMIN_KEY')
|
ADMIN_KEY = os.getenv('ADMIN_KEY')
|
||||||
|
COLLECT_COOLDOWN = os.getenv('COLLECT_COOLDOWN')
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
|
import mysql.connector
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
from interbend.db import db, get_user
|
from interbend.db import db, get_user
|
||||||
from interbend.auth import *
|
from interbend.auth import *
|
||||||
import hmac
|
import hmac
|
||||||
|
import decimal
|
||||||
from werkzeug.security import generate_password_hash
|
from werkzeug.security import generate_password_hash
|
||||||
|
|
||||||
admin_bp = Blueprint('admin_bp', __name__)
|
admin_bp = Blueprint('admin_bp', __name__)
|
||||||
|
|
@ -13,13 +15,13 @@ def _keychecker(key):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@admin_bp.route('/salary', methods=['POST'])
|
@admin_bp.route('/set-job', methods=['POST'])
|
||||||
def set_salary():
|
def set_job():
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
bid = data.get('bid')
|
bid = data.get('bid')
|
||||||
salary_class = data.get('class')
|
job_id = data.get('job')
|
||||||
key = data.get('key')
|
key = data.get('key')
|
||||||
if not bid or not salary_class or not key:
|
if not bid or not job_id or not key:
|
||||||
return jsonify({"error": "BID, salary class, and key are required"}), 400
|
return jsonify({"error": "BID, salary class, and key are required"}), 400
|
||||||
if not _keychecker(key):
|
if not _keychecker(key):
|
||||||
return jsonify({"error":"Admin Key required"}), 403
|
return jsonify({"error":"Admin Key required"}), 403
|
||||||
|
|
@ -27,10 +29,41 @@ def set_salary():
|
||||||
if not user:
|
if not user:
|
||||||
return jsonify({"error": "User not found"}), 404
|
return jsonify({"error": "User not found"}), 404
|
||||||
with db.cursor(dictionary=True) as cur:
|
with db.cursor(dictionary=True) as cur:
|
||||||
cur.execute("UPDATE users SET salary_class = %s WHERE bid = %s", (salary_class, bid))
|
cur.execute("UPDATE user_jobs SET job_id = %s WHERE bid = %s", (job_id, bid))
|
||||||
db.commit()
|
db.commit()
|
||||||
return jsonify({"message": "Salary class updated successfully"}), 200
|
return jsonify({"message": "Salary class updated successfully"}), 200
|
||||||
|
|
||||||
|
@admin_bp.route('/add-money', methods=['POST', 'PATCH'])
|
||||||
|
def add_money():
|
||||||
|
data = request.get_json()
|
||||||
|
if not data:
|
||||||
|
return jsonify({"error": "No data provided"}), 400
|
||||||
|
bid = data.get('bid')
|
||||||
|
amount = data.get('amount')
|
||||||
|
key = data.get('key')
|
||||||
|
if not bid or not money or not key:
|
||||||
|
return jsonify({"error": "BID, Amount and AdminKey are required"}), 400
|
||||||
|
if not _keychecker(key):
|
||||||
|
return jsonify({"error":"Admin Key required"}), 403
|
||||||
|
try:
|
||||||
|
amount_dec = decimal.Decimal(amount)
|
||||||
|
except (ValueError, decimal.InvalidOperation):
|
||||||
|
return jsonify({"error": "Invalid amount"}), 400
|
||||||
|
user = get_user(bid)
|
||||||
|
if not user:
|
||||||
|
return jsonify({"error": "User not found"}), 404
|
||||||
|
balance = user["balance"]
|
||||||
|
new_balance = balance + amount_dec
|
||||||
|
try:
|
||||||
|
with db.cursor(dictionary=True) as cur:
|
||||||
|
cur.execute("UPDATE users SET balance = %s WHERE bid = %s", (new_balance, bid))
|
||||||
|
db.commit()
|
||||||
|
return jsonify({"message": "Money successfully updated!"}), 200
|
||||||
|
except mysql.connector.Error as err:
|
||||||
|
db.rollback()
|
||||||
|
current_app.logger.error(f"Database error in add_money: {err}")
|
||||||
|
return jsonify({"error":"A database error occurred, please try again later."}), 500
|
||||||
|
|
||||||
@admin_bp.route('/change-password', methods=['POST', 'PATCH'])
|
@admin_bp.route('/change-password', methods=['POST', 'PATCH'])
|
||||||
def change_password():
|
def change_password():
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
|
|
||||||
|
from config import Config
|
||||||
from interbend.db import db, get_user
|
from interbend.db import db, get_user
|
||||||
from interbend.auth import *
|
from interbend.auth import *
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
|
|
@ -13,10 +15,41 @@ def get_balance():
|
||||||
return jsonify({"error": "User not found."}), 404
|
return jsonify({"error": "User not found."}), 404
|
||||||
return jsonify({"balance": user["balance"]})
|
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'])
|
@transactions_bp.route('/transfer', methods=['POST'])
|
||||||
@jwt_required
|
@jwt_required
|
||||||
def transfer():
|
def transfer():
|
||||||
# Ignore warning because its dynamically added via jwt required.
|
# Ignore warning because it's dynamically added via jwt required.
|
||||||
user_bid = request.bid
|
user_bid = request.bid
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
fbid = data.get('from')
|
fbid = data.get('from')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue