Added Transaction offer
This commit is contained in:
@@ -6,6 +6,7 @@ from app.api.services.repayment_service import RepaymentService
|
||||
from app.api.services.loan_charge_service import LoanChargeService
|
||||
from app.api.services.loan_service import LoanService
|
||||
from app.api.services.transaction_service import TransactionService
|
||||
from app.api.services.transaction_offers_service import TransactionOfferService
|
||||
from app.api.services.auth_service import AuthService
|
||||
from app.api.services.dashboard_service import DashboardService
|
||||
from app.api.services.offer_service import OfferService
|
||||
@@ -144,6 +145,24 @@ def get_transactions():
|
||||
response = TransactionService.process_request(filters)
|
||||
return response
|
||||
|
||||
@api.route('/transaction-offers', methods=['GET'])
|
||||
# @token_required
|
||||
def get_transaction_offers():
|
||||
# Extract query parameters for filtering
|
||||
filters = {
|
||||
'customer_id': request.args.get('customer_id'),
|
||||
'transaction_id': request.args.get('transaction_id'),
|
||||
'offer_id': request.args.get('offer_id'),
|
||||
'product_id': request.args.get('product_id'),
|
||||
'original_transaction': request.args.get('original_transaction'),
|
||||
'start_date': request.args.get('start_date'),
|
||||
'end_date': request.args.get('end_date'),
|
||||
'page': request.args.get('page', 1),
|
||||
'limit': request.args.get('limit', 20)
|
||||
}
|
||||
response = TransactionOfferService.process_request(filters)
|
||||
return response
|
||||
|
||||
@api.route('/repayments', methods=['GET'])
|
||||
# @token_required
|
||||
def get_all_repayments():
|
||||
|
||||
@@ -8,3 +8,4 @@ from app.api.services.dashboard_service import DashboardService
|
||||
from app.api.services.repayment_service import RepaymentService
|
||||
from app.api.services.loan_charge_service import LoanChargeService
|
||||
from app.api.services.loan_repayment_schedule_service import LoanRepaymentScheduleService
|
||||
from app.api.services.transaction_offers_service import TransactionOfferService
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
from flask import jsonify
|
||||
|
||||
from app.models.transaction_offers import TransactionOffer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TransactionOfferService:
|
||||
"""
|
||||
Service class for handling transaction offer-related operations.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def process_request(filters=None):
|
||||
"""
|
||||
Process the get transaction offers request.
|
||||
|
||||
Args:
|
||||
filters (dict, optional): Filters for the transaction offers query.
|
||||
|
||||
Returns:
|
||||
dict: A standardized response with transaction offers data.
|
||||
"""
|
||||
try:
|
||||
if filters is None:
|
||||
filters = {}
|
||||
|
||||
# Extract filters
|
||||
customer_id = filters.get('customer_id')
|
||||
transaction_id = filters.get('transaction_id')
|
||||
offer_id = filters.get('offer_id')
|
||||
product_id = filters.get('product_id')
|
||||
original_transaction = filters.get('original_transaction')
|
||||
start_date = filters.get('start_date')
|
||||
end_date = filters.get('end_date')
|
||||
|
||||
# Extract pagination parameters
|
||||
page = int(filters.get('page', 1))
|
||||
limit = int(filters.get('limit', 20))
|
||||
|
||||
# Ensure page and limit are valid
|
||||
if page < 1:
|
||||
page = 1
|
||||
if limit < 1 or limit > 100:
|
||||
limit = 20
|
||||
|
||||
# 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 transaction offers with optional filters and pagination
|
||||
transaction_offers, total_count = TransactionOffer.get_all_transaction_offers(
|
||||
customer_id=customer_id,
|
||||
transaction_id=transaction_id,
|
||||
offer_id=offer_id,
|
||||
product_id=product_id,
|
||||
original_transaction=original_transaction,
|
||||
start_date=start_date,
|
||||
end_date=end_date,
|
||||
page=page,
|
||||
limit=limit
|
||||
)
|
||||
|
||||
# Convert transaction offers to dictionary format
|
||||
transaction_offers_data = []
|
||||
for offer in transaction_offers:
|
||||
transaction_offers_data.append({
|
||||
'id': offer.id,
|
||||
'customer_id': offer.customer_id,
|
||||
'transaction_id': offer.transaction_id,
|
||||
'original_transaction': offer.original_transaction,
|
||||
'offer_id': offer.offer_id,
|
||||
'product_id': offer.product_id,
|
||||
'min_amount': offer.min_amount,
|
||||
'max_amount': offer.max_amount,
|
||||
'eligible_amount': offer.eligible_amount,
|
||||
'tenor': offer.tenor,
|
||||
'created_at': offer.created_at.isoformat(),
|
||||
'updated_at': offer.updated_at.isoformat() if offer.updated_at else None
|
||||
})
|
||||
|
||||
# Calculate total pages
|
||||
total_pages = (total_count + limit - 1) // limit
|
||||
|
||||
response_data = {
|
||||
'transaction_offers': transaction_offers_data,
|
||||
'count': len(transaction_offers_data),
|
||||
'pagination': {
|
||||
'total_count': total_count,
|
||||
'total_pages': total_pages,
|
||||
'current_page': page,
|
||||
'limit': limit,
|
||||
'has_next': page < total_pages,
|
||||
'has_prev': page > 1
|
||||
}
|
||||
}
|
||||
|
||||
return response_data
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
||||
return jsonify({
|
||||
"message": "Internal Server Error"
|
||||
}), 500
|
||||
Reference in New Issue
Block a user