Files
FirstCore/app/api/routes/routes.py
T
Azeez Muibi 3c88e53bab update
2025-04-16 12:03:44 +01:00

118 lines
3.3 KiB
Python

from flask import Blueprint, request, jsonify
from app.api.services.loan import LoanService
from app.api.services.transaction import TransactionService
from app.api.services.auth_service import AuthService
from app.api.services.dashboard_service import DashboardService
from functools import wraps
api = Blueprint('api', __name__)
# JWT Authentication decorator
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
token = None
# Get token from header
auth_header = request.headers.get('Authorization')
if auth_header:
if auth_header.startswith('Bearer '):
token = auth_header.split(' ')[1]
if not token:
return jsonify({'message': 'Token is missing!'}), 401
# Verify token
payload = AuthService.verify_token(token)
if not payload:
return jsonify({'message': 'Token is invalid or expired!'}), 401
# Add user info to request
request.user = payload
return f(*args, **kwargs)
return decorated
@api.route('/login', methods=['POST'])
def login():
data = request.get_json()
# Check if username and password are provided
if not data or 'username' not in data or 'password' not in data:
return jsonify({
'error': 'Missing credentials',
'message': 'Username and password are required'
}), 400
username = data.get('username', '')
password = data.get('password', '')
# Call the login method from AuthService
result = AuthService.login(username, password)
# Check if result is a tuple (error response)
if isinstance(result, tuple):
return jsonify(result[0]), result[1]
return jsonify(result)
@api.route('/dashboard', methods=['GET'])
@token_required
def get_dashboard():
# Call the dashboard service
result = DashboardService.get_dashboard_data()
return jsonify(result)
@api.route('/loans', methods=['GET'])
@token_required
def get_loans():
# Extract query parameters
customer_id = request.args.get('customer_id')
loan_id = request.args.get('loan_id')
status = request.args.get('status')
offer_id = request.args.get('offer_id')
product_id = request.args.get('product_id')
start_date = request.args.get('start_date')
end_date = request.args.get('end_date')
# Call the loan service
result = LoanService.process_request(
customer_id=customer_id,
loan_id=loan_id,
status=status,
offer_id=offer_id,
product_id=product_id,
start_date=start_date,
end_date=end_date
)
return jsonify(result)
@api.route('/transactions', methods=['GET'])
@token_required
def get_transactions():
# Extract query parameters
account_id = request.args.get('account_id')
transaction_id = request.args.get('transaction_id')
type = request.args.get('type')
channel = request.args.get('channel')
start_date = request.args.get('start_date')
end_date = request.args.get('end_date')
# Call the transaction service
result = TransactionService.process_request(
account_id=account_id,
transaction_id=transaction_id,
type=type,
channel=channel,
start_date=start_date,
end_date=end_date
)
return jsonify(result)