Files
FirstCore/app/api/services/transaction_offers_service.py
2025-05-19 13:43:49 +01:00

110 lines
3.9 KiB
Python

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() if offer.created_at else None,
'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