Added offer and charge

This commit is contained in:
Azeez Muibi
2025-04-30 12:48:34 +01:00
parent d1b9d80c84
commit 9bec2b2e9f
15 changed files with 775 additions and 28 deletions
+88
View File
@@ -0,0 +1,88 @@
import logging
from datetime import datetime
from flask import jsonify
from app.models.charge import Charge
# Configure logging
logger = logging.getLogger(__name__)
class ChargeService:
"""
Service class for handling charge-related operations.
"""
@staticmethod
def get_all_charges(filters=None):
"""
Get all charges with optional filtering.
Args:
filters (dict, optional): Filters for the charges query.
Returns:
dict: A standardized response with charges data.
"""
try:
if filters is None:
filters = {}
# Extract filters
offer_id = filters.get('offer_id')
code = filters.get('code')
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 charges with optional filters and pagination
charges, total_count = Charge.get_all_charges(
offer_id=offer_id,
code=code,
start_date=start_date,
end_date=end_date,
page=page,
limit=limit
)
# Convert charges to dictionary format
charges_data = []
for charge in charges:
charges_data.append(charge.to_dict())
# Calculate total pages
total_pages = (total_count + limit - 1) // limit
response_data = {
'charges': charges_data,
'count': len(charges_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
@@ -28,6 +28,7 @@ class LoanRepaymentScheduleService:
# Extract filters
loan_id = filters.get('loan_id')
transaction_id = filters.get('transaction_id')
product_id = filters.get('product_id')
paid = filters.get('paid')
due_before = filters.get('due_before')
@@ -57,6 +58,7 @@ class LoanRepaymentScheduleService:
# Get loan repayment schedules with optional filters and pagination
schedules, total_count = LoanRepaymentSchedule.get_all_repayment_schedules(
loan_id=loan_id,
transaction_id=transaction_id,
product_id=product_id,
paid=paid,
due_before=due_before,
@@ -73,6 +75,7 @@ class LoanRepaymentScheduleService:
'id': schedule.id,
'loan_id': schedule.loan_id,
'product_id': schedule.product_id,
'transaction_id': schedule.transaction_id,
'installment_number': schedule.installment_number,
'due_date': schedule.due_date.isoformat() if schedule.due_date else None,
'installment_amount': schedule.installment_amount,
+88
View File
@@ -0,0 +1,88 @@
import logging
from datetime import datetime
from flask import jsonify
from app.models.offer import Offer
# Configure logging
logger = logging.getLogger(__name__)
class OfferService:
"""
Service class for handling offer-related operations.
"""
@staticmethod
def get_all_offers(filters=None):
"""
Get all offers with optional filtering.
Args:
filters (dict, optional): Filters for the offers query.
Returns:
dict: A standardized response with offers data.
"""
try:
if filters is None:
filters = {}
# Extract filters
id = filters.get('id')
product_id = filters.get('product_id')
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 offers with optional filters and pagination
offers, total_count = Offer.get_all_offers(
id=id,
product_id=product_id,
start_date=start_date,
end_date=end_date,
page=page,
limit=limit
)
# Convert offers to dictionary format
offers_data = []
for offer in offers:
offers_data.append(offer.to_dict())
# Calculate total pages
total_pages = (total_count + limit - 1) // limit
response_data = {
'offers': offers_data,
'count': len(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