Merge branch 'loan_count_fix' of DigiFi/digifi-BankToProductCore into master

This commit is contained in:
2025-06-13 18:53:52 +00:00
committed by Gogs
4 changed files with 24 additions and 21 deletions
+2 -3
View File
@@ -1,6 +1,5 @@
from flask import session, jsonify from flask import session, jsonify
from app.models.loan import Loan from app.models.loan import Loan
from app.models.transaction_offers import TransactionOffer
from app.utils.logger import logger from app.utils.logger import logger
from app.api.services.base_service import BaseService from app.api.services.base_service import BaseService
from app.api.schemas.eligibility_check import EligibilityCheckSchema from app.api.schemas.eligibility_check import EligibilityCheckSchema
@@ -187,10 +186,10 @@ class EligibilityCheckService(BaseService):
logger.error(f"Offer not found for offer_id: {offer_id} (customer_id: {customer_id})") logger.error(f"Offer not found for offer_id: {offer_id} (customer_id: {customer_id})")
return False return False
daily_count = TransactionOffer.get_daily_loan_count(customer_id, offer_id) daily_count = Loan.get_daily_loan_count(customer_id, offer.product_id)
logger.error(f"daily_count: {daily_count}, Max: {offer.max_daily_loans}") logger.info(f"daily_count: {daily_count}, Max: {offer.max_daily_loans}")
if offer.max_daily_loans is not None and daily_count >= offer.max_daily_loans: if offer.max_daily_loans is not None and daily_count >= offer.max_daily_loans:
return False return False
+2
View File
@@ -60,8 +60,10 @@ class SelectOfferService(BaseService):
db.session.flush() db.session.flush()
if amount < offer.min_amount: if amount < offer.min_amount:
logger.error(f"The amount {amount} is less than the minimum allowed offer amount {offer.min_amount}.")
return ResponseHelper.error(result_description="The amount is less than the minimum allowed offer amount.") return ResponseHelper.error(result_description="The amount is less than the minimum allowed offer amount.")
elif amount > offer.max_amount: elif amount > offer.max_amount:
logger.error(f"The amount {amount} is greater than the minimum allowed offer amount {offer.min_amount}.")
return ResponseHelper.error(result_description="The amount is greater than the maximum allowed offer amount.") return ResponseHelper.error(result_description="The amount is greater than the maximum allowed offer amount.")
+20 -1
View File
@@ -1,4 +1,4 @@
from datetime import datetime, timezone from datetime import datetime, timezone, timedelta
from itertools import product from itertools import product
from app.extensions import db from app.extensions import db
from app.models.customer import Customer from app.models.customer import Customer
@@ -215,6 +215,25 @@ class Loan(db.Model):
# Update loan status and the updated_at timestamp # Update loan status and the updated_at timestamp
loan.status = status loan.status = status
@classmethod
def get_daily_loan_count(cls, customer_id, product_id):
"""
Returns the count of loans created today for a customer.
"""
start_of_day = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0)
end_of_day = start_of_day + timedelta(days=1)
return cls.query.filter_by(
customer_id=customer_id,
product_id=product_id,
).filter(
cls.created_at >= start_of_day,
cls.created_at < end_of_day
).count()
def to_dict(self): def to_dict(self):
""" """
Convert the Loan object to a dictionary format for JSON serialization. Convert the Loan object to a dictionary format for JSON serialization.
-17
View File
@@ -76,23 +76,6 @@ class TransactionOffer(db.Model):
""" """
return cls.query.filter_by(customer_id=customer_id).count() return cls.query.filter_by(customer_id=customer_id).count()
@classmethod
def get_daily_loan_count(cls, customer_id, offer_id):
"""
Returns the count of loans created today for a customer.
"""
start_of_day = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0)
end_of_day = start_of_day + timedelta(days=1)
return cls.query.filter_by(
customer_id=customer_id,
offer_id=offer_id
).filter(
cls.created_at >= start_of_day,
cls.created_at < end_of_day
).count()
@classmethod @classmethod
def get_latest_transaction_offer(cls, customer_id): def get_latest_transaction_offer(cls, customer_id):