diff --git a/README.md b/README.md index 05ef30d..78677a4 100644 --- a/README.md +++ b/README.md @@ -25,13 +25,14 @@ Interbend is a Flask-based web application that provides a backend API for manag Create a `.env` file in the root directory of the project and add the following variables: ``` JWT_KEY=your_secret_jwt_key - JWT_EXPIRATION=30 + JWT_EXPIRATION=30 // jwt experation duration in days DB_HOST=your_database_host DB_USER=your_database_user DB_PASSWORD=your_database_password DB_NAME=your_database_name ADMIN_KEY=your_secret_admin_key - COLLECT_COOLDOWN=30 + COLLECT_COOLDOWN=24 // collect cooldown in hours + TAX_ACCOUNT_BID=BUSINESS_BID_HERE // tax account bid here ``` ## Usage diff --git a/To-Do.md b/To-Do.md index 238ab43..cfa7b53 100644 --- a/To-Do.md +++ b/To-Do.md @@ -14,4 +14,8 @@ This file tracks potential new features and improvements for the Interbend banki * **Description:** Create a new API endpoint (e.g., `GET /transactions`) that allows an authenticated user to retrieve a paginated list of their own transaction history. * **Benefits:** Provides users with transparency and a way to track their finances, which is a core feature of any banking application. - * FINISHED: PLEASE CHECK IF WORKING! \ No newline at end of file + * FINISHED: PLEASE CHECK IF WORKING! + +Frontend? + +idfk? \ No newline at end of file diff --git a/config.py b/config.py index 62a2adf..9659b73 100644 --- a/config.py +++ b/config.py @@ -19,3 +19,4 @@ class Config: # Admin ADMIN_KEY = os.getenv('ADMIN_KEY') COLLECT_COOLDOWN = int(os.getenv('COLLECT_COOLDOWN', 24)) + TAX_ACCOUNT_BID = os.getenv('TAX_ACCOUNT_BID') \ No newline at end of file diff --git a/interbend/routes/transaction_routes.py b/interbend/routes/transaction_routes.py index c8a2155..6d90d7b 100644 --- a/interbend/routes/transaction_routes.py +++ b/interbend/routes/transaction_routes.py @@ -94,23 +94,17 @@ def get_transactions(): return jsonify({"error": "An unexpected server error occurred."}), 500 -# this should be fine -@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') - note = data.get('note') - 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 +# this should be fine (not) + +def transfer_boilerplate(user_bid, fbid, tbid, amount, note, type): + if not user_bid or not fbid or not tbid or not amount: + return jsonify({"error": "From, To, and amount are required"}), 400 + if not note: + print(f"{type} failed to provide note. No trace available because I am lazy to program such things.") + return jsonify ({"error": "Note shouldve been passed by internal method."}), 500 + if not type in [0, 1, 2, 3, 4, 5, 6, 7, 8]: # 0 = personal, 1 = business, 2 = fine, 3 = salary, 4 = bill, 5 = admin adjustment, 6 = placeholder, 7 = placeholder2, 8 = other + print(f"type was {type}, which is invalid. This is a bug. Internal Method failed to provide a valid type. No trace available because I am lazy to program such things.") + return jsonify({"error": "Internal method failed to provide a valid type."}), 500 try: amount = float(amount) if amount <= 0: @@ -121,6 +115,15 @@ def transfer(): receiver = get_user(tbid) if not sender or not receiver: return jsonify({"error":"User not found"}), 404 + # here is space to add tax if its a business transaction. + if type == 1: # Business Transfer + gtax = 0.3 # 30% tax for business transactions - configurable later TO DO + tax = amount * gtax + note += f" (Tax applied: {tax})" + tax_account_bid = Config.TAX_ACCOUNT_BID + if sender["balance"] < amount + tax: + return jsonify({"error": "Insufficient funds"}), 400 + if sender["balance"] < amount: return jsonify({"error": "Insufficient funds"}), 400 try: @@ -128,6 +131,9 @@ def transfer(): 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)) + if type == 1: # Business Transfer + cur.execute("UPDATE users SET balance = balance - %s WHERE bid = %s", (tax, fbid)) + cur.execute("UPDATE users SET balance = balance + %s WHERE bid = %s", (tax, tax_account_bid)) cur.execute("INSERT INTO transactions (source, target, amount, note, type, timestamp, status) VALUES (%s, %s, " "%s, %s, %s, %s)", fbid, tbid, amount, note, "transfer", datetime.now(timezone.utc), "completed", ) @@ -136,4 +142,44 @@ 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 \ No newline at end of file + return jsonify({"error": "A database error occurred during the transfer."}), 500 + +# PERSONAL TRANSFERS +@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') + note = data.get('note') + type = int(0) # Personal Transfer + if not fbid: + user_bid = fbid + if fbid != user_bid: + return jsonify({"error": "Unauthorized transfer from another account"}), 401 + if not note: + note = "No note provided, Personal Transfer from " + fbid + return transfer_boilerplate(user_bid, fbid, tbid, amount, note, type) + +# BUSINESS TRANSFERS +@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') + note = data.get('note') + type = int(1) # Business Transfer + if not fbid: + user_bid = fbid + if fbid != user_bid: + return jsonify({"error": "Unauthorized transfer from another account"}), 401 + if not note: + note = "No note provided, Business Transfer from " + fbid + return transfer_boilerplate(user_bid, fbid, tbid, amount, note, type) \ No newline at end of file