[add]: offer analysis

This commit is contained in:
VivianDee
2025-05-06 07:09:36 +01:00
parent 4c4ef909c2
commit 74066bae56
3 changed files with 41 additions and 10 deletions
+9 -2
View File
@@ -7,7 +7,7 @@ from marshmallow import ValidationError
from app.api.enums import TransactionType from app.api.enums import TransactionType
from app.api.integrations import SimbrellaIntegration from app.api.integrations import SimbrellaIntegration
from app.extensions import db from app.extensions import db
from app.models import Offer from app.models import Offer, RACCheck
import random import random
@@ -57,13 +57,20 @@ class EligibilityCheckService(BaseService):
response = SimbrellaIntegration.rac_check( response = SimbrellaIntegration.rac_check(
customer_id = customer_id, customer_id = customer_id,
account_id = account_id, account_id = account_id,
transaction_id = transaction.id, transaction_id = transaction.transaction_id,
) )
# this chck for error is not valid # this chck for error is not valid
if response.status_code != 200: if response.status_code != 200:
return jsonify({"message": "RACCheck failed"}), 400 return jsonify({"message": "RACCheck failed"}), 400
RACCheck.add_rac_check(
customer_id = customer_id,
account_id = account_id,
transaction_id = transaction.transaction_id,
data = response.data
)
offers = Offer.get_all_offers() offers = Offer.get_all_offers()
eligible_offers = [] eligible_offers = []
+5 -2
View File
@@ -8,7 +8,7 @@ from app.models.loan_charge import LoanCharge
from app.utils.logger import logger from app.utils.logger import logger
from app.api.schemas.provide_loan import ProvideLoanSchema from app.api.schemas.provide_loan import ProvideLoanSchema
from threading import Thread from threading import Thread
from app.models import Loan, Offer, Charge , TransactionOffer from app.models import Loan, Offer, Charge , TransactionOffer, RACCheck
from app.api.enums import LoanStatus from app.api.enums import LoanStatus
from app.extensions import db from app.extensions import db
from datetime import datetime, timezone from datetime import datetime, timezone
@@ -47,10 +47,13 @@ class ProvideLoanService(BaseService):
customer = Customer.is_valid_customer(customer_id) customer = Customer.is_valid_customer(customer_id)
if (ProvideLoanService.validate_account_ownership(account_id = account_id, customer_id = customer_id)): if (ProvideLoanService.validate_account_ownership(account_id = account_id, customer_id = customer_id)):
rac_response = RACCheck.get_rac_check(customer_id = customer_id, account_id = account_id)
try: try:
transaction_offer, offer, eligible_amount = OfferAnalysis.get_offer( transaction_offer, offer, eligible_amount = OfferAnalysis.get_offer(
transaction_id=transaction_id, transaction_id=transaction_id,
rac_response=None, rac_response=rac_response,
validated_data=validated_data validated_data=validated_data
) )
except ValueError as ve: except ValueError as ve:
+26 -5
View File
@@ -1,7 +1,7 @@
from datetime import datetime, timezone from datetime import datetime, timezone
from app.extensions import db from app.extensions import db
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.exc import IntegrityError
from uuid import uuid4 from uuid import uuid4
from sqlalchemy.types import JSON from sqlalchemy.types import JSON
@@ -16,6 +16,26 @@ class RACCheck(db.Model):
created_at = db.Column(db.DateTime, default=datetime.now(timezone.utc)) created_at = db.Column(db.DateTime, default=datetime.now(timezone.utc))
updated_at = db.Column(db.DateTime, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc)) updated_at = db.Column(db.DateTime, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc))
@classmethod
def add_rac_check(cls, customer_id, account_id, transaction_id, data = None):
# Save the response
rac_check = cls(
customer_id = customer_id,
account_id = account_id,
transaction_id = transaction_id,
original_transaction = transaction_id,
rac_response = data
)
try:
db.session.add(rac_check)
except IntegrityError as err:
raise ValueError(f"Database integrity error: {err}")
return rac_check
@classmethod @classmethod
def get_all_rac_checks(cls): def get_all_rac_checks(cls):
""" """
@@ -24,18 +44,19 @@ class RACCheck(db.Model):
rac_checks = cls.query.all() rac_checks = cls.query.all()
if not rac_checks: if not rac_checks:
raise ValueError("No available RAC checks") return None
return rac_checks return rac_checks
@classmethod @classmethod
def get_rac_check_by_id(cls, check_id): def get_rac_check(cls, customer_id, account_id):
""" """
Return a RAC check by its ID. Return a RAC check by its ID.
""" """
rac_check = cls.query.filter_by(id=check_id).first() rac_check = cls.query.filter_by( customer_id = customer_id,
account_id = account_id,).first()
if not rac_check: if not rac_check:
raise ValueError(f"RAC Check with ID {check_id} not found") raise ValueError(f"RAC Check for customer not found")
return rac_check return rac_check
def to_dict(self): def to_dict(self):