diff --git a/app/api/services/offer_analysis.py b/app/api/services/offer_analysis.py index 9a897a2..b1a021a 100644 --- a/app/api/services/offer_analysis.py +++ b/app/api/services/offer_analysis.py @@ -1,4 +1,6 @@ from app.models import Offer, TransactionOffer +from app.models.loan import Loan + import logging logger = logging.getLogger(__name__) @@ -27,5 +29,9 @@ class OfferAnalysis: if not offer: raise ValueError("Invalid Offer.") + original_transaction = transaction_id + # we can now find the origin transactions + customer_loan = Loan.get_customer_current_active_loan(customer_id) - return transaction_offer, offer, eligible_amount + + return transaction_offer, offer, eligible_amount, original_transaction diff --git a/app/api/services/provide_loan.py b/app/api/services/provide_loan.py index f32021c..fb9178b 100644 --- a/app/api/services/provide_loan.py +++ b/app/api/services/provide_loan.py @@ -50,7 +50,7 @@ class ProvideLoanService(BaseService): rac_response = RACCheck.get_rac_check(customer_id = customer_id, account_id = account_id) try: - transaction_offer, offer, eligible_amount = OfferAnalysis.get_offer( + transaction_offer, offer, eligible_amount, original_transaction = OfferAnalysis.get_offer( transaction_id=transaction_id, rac_response=rac_response, validated_data=validated_data @@ -116,6 +116,7 @@ class ProvideLoanService(BaseService): product_id = offer.product_id, collection_type = collection_type, transaction_id = validated_data.get('transactionId'), + original_transaction = validated_data.get('transactionId'), initial_loan_amount = validated_data.get('requestedAmount'), upfront_fee = upfront_fee, repayment_amount = repayment_amount, @@ -145,7 +146,7 @@ class ProvideLoanService(BaseService): # charges = Charge.get_offer_charges(offer.id) - logger.error(f"{charges}") + logger.info(f"{charges}") loan_id = loan.id diff --git a/app/models/loan.py b/app/models/loan.py index 1e60b8c..f24af7e 100644 --- a/app/models/loan.py +++ b/app/models/loan.py @@ -69,6 +69,7 @@ class Loan(db.Model): initial_loan_amount, collection_type, transaction_id, + original_transaction, upfront_fee, repayment_amount, installment_amount, @@ -92,7 +93,7 @@ class Loan(db.Model): product_id = product_id, collection_type = collection_type, transaction_id = transaction_id, - original_transaction = transaction_id, + original_transaction = original_transaction, initial_loan_amount = initial_loan_amount, current_loan_amount = initial_loan_amount, upfront_fee = upfront_fee, @@ -125,13 +126,32 @@ class Loan(db.Model): @classmethod def get_customer_loan(cls, loan_id, customer_id): """ - Get customer's active loans. + Get customer's active loans by loan_id. """ loan = cls.query.filter_by(id = loan_id, customer_id = customer_id).first() if not loan: raise ValueError(f"Loan with ID {loan_id} does not exist or does not belong to customer {customer_id}.") return loan - + + @classmethod + def get_customer_current_active_loan(cls, customer_id): + """ + Get customer's active loans. + """ + loan = cls.query.filter_by( customer_id = customer_id).first() + if not loan: + loan = { + "eligible_amount": 0, + "loan_amount": 0, + "customer_id": customer_id, + "transaction_id": "", + "resultDescription": "No Active Loan" + } + + logger.info(f"{loan}") + + return loan + @classmethod def update_status(cls, loan_id, status): """