feat: Add transaction routes and update configurations

This commit introduces new transaction routes for handling various transaction-related operations. It also includes updates to the `config.py` file to support these new features.

The following files were modified:
- `README.md`: Updated documentation to clarify .env variables and their purposes
- `To-Do.md`: Updated task meaningless
- `config.py`: Added COLLECT_COOLDOWN and TAX_ACCOUNT_BID configurations.

    Main Thing:
- `interbend/routes/transaction_routes.py`:
Added boilerplate transaction to handle all transfers and separated business transfers with tax application.
/transfer for personal transfers
/transfer-business for business transfers with tax deduction

TODO: add route to change tax rate and add non hardcoded tax rate.
Type 6 and 7 are reserved for future use. currently implemented are 0 and 1.
This commit is contained in:
MrEisbear 2025-09-20 20:59:19 -05:00
parent 3c5550beed
commit dcee6058cb
4 changed files with 73 additions and 21 deletions

View file

@ -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

View file

@ -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!
* FINISHED: PLEASE CHECK IF WORKING!
Frontend?
idfk?

View file

@ -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')

View file

@ -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
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)