diff --git a/app/api/services/offer_analysis.py b/app/api/services/offer_analysis.py index 962d568..11ec0a1 100644 --- a/app/api/services/offer_analysis.py +++ b/app/api/services/offer_analysis.py @@ -1,3 +1,4 @@ +from decimal import Decimal from app.models import Offer, TransactionOffer from app.models.loan import Loan import random @@ -7,6 +8,7 @@ from app.config import Config RAC_TRUE_CHECK_RULES = Config.rac_true_rules RAC_FALSE_CHECK_RULES = Config.rac_false_rules +RAC_SALARY_PAYMENTS = Config.rac_salary_payments logger = logging.getLogger(__name__) @@ -39,11 +41,67 @@ class OfferAnalysis: return transaction_offer, offer, eligible_amount, original_transaction @staticmethod - def _analyze_rack_checks(rack_response): + def _analyze_rack_checks(rack_response, offer): logger.info(f"This is PayLoad for ANALYSYS ***** : {str(rack_response)}", exc_info=True) logger.info(f"RACk TRUE RULES {str(RAC_TRUE_CHECK_RULES)}", exc_info=True) logger.info(f"RACk FALSE RULES {str(RAC_FALSE_CHECK_RULES)}", exc_info=True) + logger.info(f"RACk SALARY PAYMENTS {str(RAC_SALARY_PAYMENTS)}", exc_info=True) + if not isinstance(rack_response, dict) or not offer : + raise ValueError("Invalid RAC response format.") + + + + failed_true_rules = [] + failed_false_rules = [] + salaries = [] + + # Expects true + for rule in RAC_TRUE_CHECK_RULES: + if not rack_response.get(rule, False): + failed_true_rules.append(rule) + + # Expects false + for rule in RAC_FALSE_CHECK_RULES: + if rack_response.get(rule, True): + failed_false_rules.append(rule) + + + # Expects false + for key in RAC_SALARY_PAYMENTS: + value = rack_response.get(key) + if isinstance(value, Decimal): + salaries.append(value) + elif isinstance(value, (int, float, str)): + try: + salaries.append(Decimal(str(value))) + except: + logger.warning(f"Could not convert value of {key} to Decimal: {value}") + + + if failed_true_rules or failed_false_rules or not salaries: + logger.warning(f"Failed TRUE rules: {failed_true_rules}") + logger.warning(f"Failed FALSE rules: {failed_false_rules}") + logger.warning("No salary records found in RAC response.") + raise ValueError(f"RAC analysis failed due to: {failed_true_rules + failed_false_rules} and {salaries}") + + + + logger.info(f"These are the salarie amounts ***** : {str(salaries)}", exc_info=True) + + #Least salary in the last 6 months + min_salary = min(salaries) + + # Check consistency rule + consistent_income = rack_response.get("rule7-consistent-salary-amount", False) + + if consistent_income: + eligible_amount = min_salary * Decimal("0.5") + else: + eligible_amount = min_salary * Decimal("0.75") + + logger.info(f"Calculated eligible amount from RAC: {eligible_amount} based on {'stable' if consistent_income else 'unstable'} income.") + return eligible_amount.quantize(Decimal("1.00")) # "racResponse": { # "accountStatus": true, # "bvnValidated": true, @@ -76,15 +134,13 @@ class OfferAnalysis: # if we have active offers - we have to feed off it logger.info(f"**RACK ANALYSIS** {customer_id}") # Analyze Rack Checks - OfferAnalysis._analyze_rack_checks(rack_checks_response) #--> We need detail analysis + # new_eligible_amount = OfferAnalysis._analyze_rack_checks(rack_checks_response) #--> We need detail analysis # we can now find the origin transactions # Find the last loan - it will have original_transaction last_customer_loan = Loan.get_customer_last_loan(customer_id) # logger.info(f"{last_customer_loan}") - new_eligible_amount = 0 - if last_customer_loan: original_transaction = last_customer_loan.original_transaction or last_customer_loan.transaction_id logger.info(f"transaction_id |-| original_transaction === > {transaction_id} {original_transaction}") @@ -136,10 +192,11 @@ class OfferAnalysis: for offer in offers: - # Get approved amount - random_float = random.random() # temporary to play data + + new_eligible_amount = OfferAnalysis._analyze_rack_checks(rack_checks_response, offer) - approved_amount = new_eligible_amount if new_eligible_amount > 0 else min(offer.max_amount, offer.max_amount * random_float) + + approved_amount = new_eligible_amount approved_amount = round(approved_amount, 2) transaction_offer = TransactionOffer.create_transaction_offer( diff --git a/app/config.py b/app/config.py index 1124629..32ee3b0 100644 --- a/app/config.py +++ b/app/config.py @@ -65,10 +65,20 @@ class Config: "rule14-no-lien", "rule15-null-ignore" ] + rac_false_rules = [ ] + rac_salary_payments = [ + "SALARYPAYMENT_1", + "SALARYPAYMENT_2", + "SALARYPAYMENT_3", + "SALARYPAYMENT_4", + "SALARYPAYMENT_5", + "SALARYPAYMENT_6" + ] +