diff --git a/app/api/services/eligibility_check.py b/app/api/services/eligibility_check.py index 5670e1c..d9a7dd8 100644 --- a/app/api/services/eligibility_check.py +++ b/app/api/services/eligibility_check.py @@ -1,6 +1,5 @@ from flask import session, jsonify from app.models.loan import Loan -from app.models.transaction_offers import TransactionOffer from app.utils.logger import logger from app.api.services.base_service import BaseService 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})") 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: return False diff --git a/app/api/services/select_offer.py b/app/api/services/select_offer.py index c73be44..15e5c10 100644 --- a/app/api/services/select_offer.py +++ b/app/api/services/select_offer.py @@ -60,8 +60,10 @@ class SelectOfferService(BaseService): db.session.flush() 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.") 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.") diff --git a/app/models/loan.py b/app/models/loan.py index edf56f4..6c22a51 100644 --- a/app/models/loan.py +++ b/app/models/loan.py @@ -1,4 +1,4 @@ -from datetime import datetime, timezone +from datetime import datetime, timezone, timedelta from itertools import product from app.extensions import db from app.models.customer import Customer @@ -215,6 +215,25 @@ class Loan(db.Model): # Update loan status and the updated_at timestamp 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): """ Convert the Loan object to a dictionary format for JSON serialization. diff --git a/app/models/transaction_offers.py b/app/models/transaction_offers.py index d40fa4f..a14943f 100644 --- a/app/models/transaction_offers.py +++ b/app/models/transaction_offers.py @@ -76,23 +76,6 @@ class TransactionOffer(db.Model): """ 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 def get_latest_transaction_offer(cls, customer_id):