diff --git a/app/integrations/kafka.py b/app/integrations/kafka.py index a974343..5cd4ea6 100644 --- a/app/integrations/kafka.py +++ b/app/integrations/kafka.py @@ -131,7 +131,7 @@ class KafkaIntegration: try: response = SimbrellaClient.disbursement(message) logger.info( - f"Successfully sent message to disbursement service: {response.status_code}" + f"Successfully sent message to disbursement service: {response}" ) except Exception as e: logger.info(f"Failed to call disbursement service: {e}") @@ -145,7 +145,7 @@ class KafkaIntegration: try: response = SimbrellaClient.collect_loan(message) logger.info( - f"Successfully sent message to collect_loan service: {response.status_code}" + f"Successfully sent message to collect_loan service: {response}" ) except Exception as e: logger.info(f"Failed to call collect_loan service: {e}") diff --git a/app/integrations/simbrella.py b/app/integrations/simbrella.py index 55b1855..b40f21c 100644 --- a/app/integrations/simbrella.py +++ b/app/integrations/simbrella.py @@ -4,6 +4,7 @@ from app.utils.auth import get_headers from app.utils.logger import logger from flask import jsonify, current_app from app.services.transactions import TransactionService +from app.services.repayment import RepaymentService class SimbrellaClient: @@ -58,6 +59,16 @@ class SimbrellaClient: api_url = f"{SimbrellaClient.BANK_CALL_BASE_URL}/CollectLoan" logger.info(f"Calling CollectLoan endpoint with data: {data}") + # Check if the repayment exists + logger.info(f"Checking if repayment exists") + repayment = RepaymentService.get_repayment_by_transaction_id(transaction_id=data['transactionId']) + logger.info(f"Response from database: {repayment}") + + # If repayment is not found + if not repayment: + logger.info(f"Repayment id: {data['transactionId']}, was not found") + return 0 + collect_loan_data = { "transactionId": "T002", "fbnTransactionId": "FBN20231123", @@ -119,8 +130,4 @@ class SimbrellaClient: except Exception as e: logger.info(f"Failed to call Payment Callback endpoint: {e}") - raise - - @staticmethod - def check_transaction(txn_id): - return run_in_app_context(Transaction.get_transaction_by_id(txn_id)) \ No newline at end of file + raise \ No newline at end of file diff --git a/app/models/__init__.py b/app/models/__init__.py index 0f0d015..7621c0f 100644 --- a/app/models/__init__.py +++ b/app/models/__init__.py @@ -1,3 +1,4 @@ from .transactions import Transaction +from .repayment import Repayment -__all__ = ['Transaction'] \ No newline at end of file +__all__ = ['Transaction', 'Repayment'] \ No newline at end of file diff --git a/app/models/repayment.py b/app/models/repayment.py new file mode 100644 index 0000000..bc18452 --- /dev/null +++ b/app/models/repayment.py @@ -0,0 +1,24 @@ +from app.extensions import db +from datetime import datetime, timezone + +class Repayment(db.Model): + __tablename__ = "repayments" + + id = db.Column( + db.Integer, + primary_key=True, + autoincrement=True, + ) + loan_id = db.Column(db.String(50), nullable=False) + customer_id = db.Column(db.String(50), nullable=False) + product_id = db.Column(db.String(20), nullable=True) + transaction_id = db.Column(db.String(50), nullable=False) + 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)) + + def __repr__(self): + return f'' + + @classmethod + def get_repayment_by_transaction_id(cls, transaction_id): + return cls.query.filter_by(transaction_id=transaction_id).first() \ No newline at end of file diff --git a/app/services/repayment.py b/app/services/repayment.py new file mode 100644 index 0000000..b2249ff --- /dev/null +++ b/app/services/repayment.py @@ -0,0 +1,10 @@ +from app.models import Repayment + +class RepaymentService: + + @staticmethod + def get_repayment_by_transaction_id(transaction_id): + """ + Get the repayment by transaction ID + """ + return Repayment.get_repayment_by_transaction_id(transaction_id) \ No newline at end of file