Added Transaction
This commit is contained in:
@@ -8,6 +8,7 @@ from app.api.services import (
|
||||
CustomerConsentService,
|
||||
NotificationCallbackService,
|
||||
AuthorizationService,
|
||||
TransactionService,
|
||||
)
|
||||
from app.utils.logger import logger
|
||||
from app.api.middlewares import enforce_json, require_auth
|
||||
@@ -20,7 +21,6 @@ from flask_jwt_extended import (
|
||||
create_refresh_token,
|
||||
)
|
||||
|
||||
|
||||
api = Blueprint("api", __name__)
|
||||
|
||||
|
||||
@@ -119,6 +119,24 @@ def health_check():
|
||||
return {"status": "ok"}, 200
|
||||
|
||||
|
||||
# Get All Transactions Endpoint
|
||||
@api.route("/transactions", methods=["GET"])
|
||||
@jwt_required()
|
||||
def get_transactions():
|
||||
# Extract query parameters for filtering
|
||||
filters = {
|
||||
'account_id': request.args.get('account_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')
|
||||
}
|
||||
|
||||
# logger.info(f"Get transactions request received with filters: {filters}")
|
||||
response = TransactionService.process_request(filters)
|
||||
return response
|
||||
|
||||
|
||||
# Authorize endpoint
|
||||
@api.route("/Authorize", methods=["POST"])
|
||||
def authorize():
|
||||
|
||||
@@ -6,3 +6,4 @@ from app.api.services.repayment import RepaymentService
|
||||
from app.api.services.customer_consent import CustomerConsentService
|
||||
from app.api.services.notification_callback import NotificationCallbackService
|
||||
from app.api.services.authorization import AuthorizationService
|
||||
from app.api.services.transaction import TransactionService
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
from flask import jsonify
|
||||
from app.utils.logger import logger
|
||||
from app.api.services.base_service import BaseService
|
||||
from app.models.transaction import Transaction
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class TransactionService(BaseService):
|
||||
@staticmethod
|
||||
def process_request(filters=None):
|
||||
"""
|
||||
Process the get transactions request.
|
||||
|
||||
Args:
|
||||
filters (dict, optional): Filters for the transactions query.
|
||||
|
||||
Returns:
|
||||
dict: A standardized response with transactions data.
|
||||
"""
|
||||
try:
|
||||
if filters is None:
|
||||
filters = {}
|
||||
|
||||
# Extract filters
|
||||
account_id = filters.get('account_id')
|
||||
transaction_type = filters.get('type')
|
||||
channel = filters.get('channel')
|
||||
start_date = filters.get('start_date')
|
||||
end_date = filters.get('end_date')
|
||||
|
||||
# Convert string dates to datetime objects if provided
|
||||
if start_date and isinstance(start_date, str):
|
||||
start_date = datetime.fromisoformat(start_date.replace('Z', '+00:00'))
|
||||
if end_date and isinstance(end_date, str):
|
||||
end_date = datetime.fromisoformat(end_date.replace('Z', '+00:00'))
|
||||
|
||||
# Get transactions with optional filters
|
||||
transactions = Transaction.get_all_transactions(
|
||||
account_id=account_id,
|
||||
transaction_type=transaction_type,
|
||||
channel=channel,
|
||||
start_date=start_date,
|
||||
end_date=end_date
|
||||
)
|
||||
|
||||
# Convert transactions to dictionary format
|
||||
transactions_data = []
|
||||
for transaction in transactions:
|
||||
transactions_data.append({
|
||||
'id': transaction.id,
|
||||
'transaction_id': transaction.transaction_id,
|
||||
'account_id': transaction.account_id,
|
||||
'type': transaction.type,
|
||||
'channel': transaction.channel,
|
||||
'created_at': transaction.created_at.isoformat(),
|
||||
'updated_at': transaction.updated_at.isoformat()
|
||||
})
|
||||
|
||||
response_data = {
|
||||
'status': 'success',
|
||||
'message': 'Transactions retrieved successfully',
|
||||
'data': {
|
||||
'transactions': transactions_data,
|
||||
'count': len(transactions_data)
|
||||
}
|
||||
}
|
||||
|
||||
return jsonify(response_data), 200
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error retrieving transactions: {str(e)}", exc_info=True)
|
||||
return jsonify({
|
||||
'status': 'error',
|
||||
'message': f'Failed to retrieve transactions: {str(e)}'
|
||||
}), 500
|
||||
Reference in New Issue
Block a user