diff --git a/app/models/loan.py b/app/models/loan.py index d26b5cc..38483f2 100644 --- a/app/models/loan.py +++ b/app/models/loan.py @@ -223,10 +223,18 @@ class Loan(db.Model): @classmethod def get_customer_loans(cls, customer_id): """ - Get customer's active loans by customer_id. + Get customer's active loans and sum by customer_id. """ customer_loans = cls.query.filter_by( customer_id = customer_id).all() if not customer_loans: raise ValueError(f"Customer with Id {customer_id} does not have any loan.") - logger.info(f"Found {len(customer_loans)} loans for customer ID: {customer_id}") - return customer_loans \ No newline at end of file + + total_amount = ( + db.session.query(func.coalesce(func.sum(cls.current_loan_amount), 0.0)) + .filter_by(customer_id=customer_id) + .scalar() + ) + + logger.info(f"Found {len(customer_loans)} loans for customer ID: {customer_id} with total amount: {total_amount}") + + return customer_loans, total_amount \ No newline at end of file diff --git a/app/routes/autocall.py b/app/routes/autocall.py index 4f06455..3656f87 100644 --- a/app/routes/autocall.py +++ b/app/routes/autocall.py @@ -154,7 +154,7 @@ def salary_detect(): # Step 3.2: Get loans try: - loans = LoanService.get_customer_loans(pending_salary.customer_id) + loans, total_amount = LoanService.get_customer_loans(pending_salary.customer_id) if not loans: logger.warning(f"No loans found for customer ID: {pending_salary.customer_id}") continue