138 lines
4.1 KiB
Python
138 lines
4.1 KiB
Python
from flask import Blueprint, request, jsonify, current_app
|
|
import requests
|
|
from app.extensions import db
|
|
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__)
|
|
|
|
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"Database dialect detected: {dialect}")
|
|
|
|
# Build correct query based on DB type
|
|
if "oracle" in dialect:
|
|
query = text("SELECT 1 FROM dual")
|
|
else:
|
|
query = text("SELECT 1")
|
|
|
|
# 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,
|
|
"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",
|
|
"errors": [str(e)]
|
|
}), 500
|
|
|
|
|
|
|
|
@auth_bp.route("/login", methods=["POST"])
|
|
def login():
|
|
data = request.get_json()
|
|
api_url = f"{BASE_URL}/login"
|
|
|
|
response = requests.post(api_url, json=data)
|
|
if response.status_code == 200:
|
|
return jsonify(response.json()), 200
|
|
return jsonify({"error": "Invalid credentials"}), response.status_code
|
|
|
|
|
|
@auth_bp.route("/status-call", methods=["POST"])
|
|
def status_call():
|
|
data = request.get_json()
|
|
api_url = f"{BASE_URL}/StatusCall"
|
|
|
|
# response = requests.post(api_url, json=data, headers=get_headers())
|
|
# return jsonify(response.json()), response.status_code
|
|
response = {
|
|
"transactionId": "24110114545374721",
|
|
"data": {
|
|
"transactionId": "241101",
|
|
"providedAmount": 1000,
|
|
"collectedAmount": 0,
|
|
"resultCode": "00",
|
|
"resultDescription": "Loan Provision is successful",
|
|
},
|
|
"resultCode": "00",
|
|
"resultDescription": "SUCCESS",
|
|
}
|
|
|
|
return jsonify(response), 200
|
|
|
|
|
|
@auth_bp.route("/sms", methods=["POST"])
|
|
def sms():
|
|
data = request.get_json()
|
|
api_url = f"{BASE_URL}/SMS"
|
|
|
|
# response = requests.post(api_url, json=data, headers=get_headers())
|
|
# return jsonify(response.json()), response.status_code
|
|
response = {
|
|
"data": "",
|
|
"statusCode": 200,
|
|
"IsSuccessful": True,
|
|
"errorMessage": None,
|
|
}
|
|
|
|
return jsonify(response), 200
|
|
|
|
|
|
@auth_bp.route("/bulk-sms", methods=["POST"])
|
|
def bulk_sms():
|
|
data = request.get_json()
|
|
api_url = f"{BASE_URL}/BulkSMS"
|
|
|
|
# response = requests.post(api_url, json=data, headers=get_headers())
|
|
# return jsonify(response.json()), response.status_code
|
|
response = {
|
|
"data": "",
|
|
"statusCode": 200,
|
|
"IsSuccessful": True,
|
|
"errorMessage": None,
|
|
}
|
|
|
|
return jsonify(response), 200
|