diff --git a/app/config.py b/app/config.py index abfd996..db761e8 100644 --- a/app/config.py +++ b/app/config.py @@ -57,6 +57,7 @@ class Config: BANK_CALL_DISBURSE_LOAN_ENDPOINT = os.getenv("BANK_CALL_DISBURSE_LOAN_ENDPOINT","/DisburseLoan") BANK_CALL_COLLECT_LOAN_ENDPOINT = os.getenv("BANK_CALL_COLLECT_LOAN_ENDPOINT","/CollectLoan") BANK_CALL_TRANSACTION_VERIFY = os.getenv("BANK_CALL_TRANSACTION_VERIFY", "/TransactionVerify") + BANK_HEALTH_CHECK_ENDPOINT = os.getenv("BANK_HEALTH_CHECK_ENDPOINT", "/system-health-check") TEST_NO = os.getenv("TEST_NO", "2347038224367") settings = Config() diff --git a/app/integrations/__init__.py b/app/integrations/__init__.py index 7e9a8e2..5327474 100644 --- a/app/integrations/__init__.py +++ b/app/integrations/__init__.py @@ -1,2 +1,3 @@ from .kafka import KafkaIntegration -from .simbrella import SimbrellaClient \ No newline at end of file +from .simbrella import SimbrellaClient +from .bank_service import BankService \ No newline at end of file diff --git a/app/integrations/bank_service.py b/app/integrations/bank_service.py new file mode 100644 index 0000000..f9e8ad3 --- /dev/null +++ b/app/integrations/bank_service.py @@ -0,0 +1,25 @@ +import requests +from app.config import settings +from app.utils.auth import get_headers +from app.utils.logger import logger + + +class BankService: + BANK_CALL_BASE_URL = settings.BANK_CALL_BASE_URL + BANK_HEALTH_CHECK_ENDPOINT = settings.BANK_HEALTH_CHECK_ENDPOINT + BANK_CALL_APP_ID = settings.BANK_CALL_APP_ID + + @staticmethod + def health_check(): + api_url = f"{BankService.BANK_CALL_BASE_URL}{BankService.BANK_HEALTH_CHECK_ENDPOINT}" + logger.info(f"Calling Health Check endpoint: {api_url}") + + try: + response = requests.get(api_url, timeout=5, headers=get_headers()) + logger.info(f"Health Check response status code: {response.status_code}") + + return response.json() + + except Exception as e: + logger.error(f"Health Check API call failed: {str(e)}", exc_info=True) + raise \ No newline at end of file diff --git a/app/integrations/simbrella.py b/app/integrations/simbrella.py index 4a12dc3..6d701bb 100644 --- a/app/integrations/simbrella.py +++ b/app/integrations/simbrella.py @@ -32,6 +32,7 @@ class SimbrellaClient: BANK_CALL_DISBURSE_LOAN_ENDPOINT = settings.BANK_CALL_DISBURSE_LOAN_ENDPOINT BANK_CALL_COLLECT_LOAN_ENDPOINT = settings.BANK_CALL_COLLECT_LOAN_ENDPOINT BANK_CALL_TRANSACTION_VERIFY = settings.BANK_CALL_TRANSACTION_VERIFY + BANK_HEALTH_CHECK_ENDPOINT = settings.BANK_HEALTH_CHECK_ENDPOINT @staticmethod def disburse_loan(data): @@ -399,4 +400,6 @@ class SimbrellaClient: except Exception as e: logger.info(f"Failed to call Penal Charge endpoint: {e}") - raise \ No newline at end of file + raise + + \ No newline at end of file diff --git a/app/routes/authentication.py b/app/routes/authentication.py index e050368..3b364b2 100644 --- a/app/routes/authentication.py +++ b/app/routes/authentication.py @@ -5,6 +5,7 @@ from sqlalchemy import text from app.utils.auth import get_headers from app.config import settings from app.utils.logger import logger +from app.integrations.bank_service import BankService auth_bp = Blueprint("auth", __name__) @@ -14,30 +15,60 @@ BASE_URL = settings.BANK_CALL_BASE_URL @auth_bp.route("/health", methods=["GET"]) def health(): logger.info("Health check endpoint called") + errors = [] # collect all errors + try: # Detect database type dialect = db.engine.dialect.name.lower() - logger.info(f"the database dialect {dialect}") + logger.info(f"Database dialect detected: {dialect}") + # Build correct query based on DB type if "oracle" in dialect: query = text("SELECT 1 FROM dual") - else: # Postgres, MySQL, SQLite, etc. + else: query = text("SELECT 1") - db.session.execute(query) + # Test database connection + try: + db.session.execute(query) + logger.info("Database connection successful.") + except Exception as db_err: + logger.error(f"Database connection failed: {str(db_err)}") + errors.append(f"Database connection failed: {str(db_err)}") + + # Check Bank Service health + try: + bank_response = BankService.health_check() + logger.info(f"Bank Service health check response: {bank_response}") + except Exception as bank_err: + logger.error(f"Bank Service health check failed: {str(bank_err)}") + errors.append(f"Bank Service health check failed: {str(bank_err)}") + + # Build final response + if errors: + return jsonify({ + "status": "error", + "database": dialect, + "errors": errors + }), 500 + return jsonify({ "status": "success", "database": dialect, - "message": "Database connection successful" + "db_status": "connected", + "bank_service_status": "operational", + "message": "All systems operational" }), 200 except Exception as e: + logger.exception("Unexpected error during health check") return jsonify({ "status": "error", - "message": str(e) + "errors": [str(e)] }), 500 + @auth_bp.route("/login", methods=["POST"]) def login(): data = request.get_json()