first commit
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
from .transaction_type import TransactionType
|
||||
from .loan_status import LoanStatus
|
||||
@@ -0,0 +1,6 @@
|
||||
from enum import Enum
|
||||
|
||||
class LoanStatus(str, Enum):
|
||||
PENDING = "pending"
|
||||
ACTIVE = "active"
|
||||
REPAID = "repaid"
|
||||
@@ -0,0 +1,10 @@
|
||||
from enum import Enum
|
||||
|
||||
class TransactionType(str, Enum):
|
||||
ELIGIBILITY_CHECK = "eligibility_check"
|
||||
CUSTOMER_CONSENT = "customer_consent"
|
||||
LOAN_STATUS = "loan_status"
|
||||
NOTIFICATION_CALLBACK = "notification_callback"
|
||||
PROVIDE_LOAN = "provide_loan"
|
||||
REPAYMENT = "repayment"
|
||||
SELECT_OFFER = "select_offer"
|
||||
@@ -0,0 +1,251 @@
|
||||
from flask import jsonify
|
||||
from typing import List, Dict, Union, Optional, Any
|
||||
|
||||
|
||||
class ResponseHelper:
|
||||
"""
|
||||
A helper class for building standardized JSON responses in Flask.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def build_response(
|
||||
status: bool,
|
||||
message: str,
|
||||
data: Optional[Union[Dict, List, str]] = None,
|
||||
status_code: int = 200,
|
||||
error: Optional[Union[Dict, str]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Build a standardized JSON response.
|
||||
|
||||
Args:
|
||||
status (bool): Indicates whether the request was successful.
|
||||
message (str): A message describing the result of the request.
|
||||
data (Optional[Union[Dict, List, str]]): The data to return in the response.
|
||||
status_code (int): The HTTP status code for the response.
|
||||
error (Optional[Union[Dict, str]]): Any error details to include in the response.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary representing the JSON response.
|
||||
"""
|
||||
response = {
|
||||
"status": status,
|
||||
"statusCode": status_code,
|
||||
"message": message,
|
||||
"data": data if data is not None else {},
|
||||
"error": error if error is not None else {},
|
||||
}
|
||||
return jsonify(response), status_code
|
||||
|
||||
@staticmethod
|
||||
def success(
|
||||
data: Optional[Union[Dict, List, str]] = None,
|
||||
message: str = "Successful",
|
||||
status_code: int = 200,
|
||||
error: Optional[Union[Dict, str]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Return a success response.
|
||||
|
||||
Args:
|
||||
data (Optional[Union[Dict, List, str]]): The data to return in the response.
|
||||
message (str): A message describing the result of the request.
|
||||
status_code (int): The HTTP status code for the response.
|
||||
error (Optional[Union[Dict, str]]): Any error details to include in the response.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary representing the JSON response.
|
||||
"""
|
||||
return ResponseHelper.build_response(True, message, data, status_code, error)
|
||||
|
||||
@staticmethod
|
||||
def error(
|
||||
message: str = "An error occurred",
|
||||
status_code: int = 400,
|
||||
data: Optional[Union[Dict, List, str]] = None,
|
||||
error: Optional[Union[Dict, str]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Return an error response.
|
||||
|
||||
Args:
|
||||
message (str): A message describing the error.
|
||||
status_code (int): The HTTP status code for the response.
|
||||
data (Optional[Union[Dict, List, str]]): The data to return in the response.
|
||||
error (Optional[Union[Dict, str]]): Any error details to include in the response.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary representing the JSON response.
|
||||
"""
|
||||
return ResponseHelper.build_response(False, message, data, status_code, error)
|
||||
|
||||
@staticmethod
|
||||
def created(
|
||||
data: Optional[Union[Dict, List, str]] = None,
|
||||
message: str = "Resource created successfully",
|
||||
error: Optional[Union[Dict, str]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Return a response for a created resource.
|
||||
|
||||
Args:
|
||||
data (Optional[Union[Dict, List, str]]): The data to return in the response.
|
||||
message (str): A message describing the result of the request.
|
||||
error (Optional[Union[Dict, str]]): Any error details to include in the response.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary representing the JSON response.
|
||||
"""
|
||||
return ResponseHelper.build_response(True, message, data, 201, error)
|
||||
|
||||
@staticmethod
|
||||
def updated(
|
||||
data: Optional[Union[Dict, List, str]] = None,
|
||||
message: str = "Resource updated successfully",
|
||||
error: Optional[Union[Dict, str]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Return a response for an updated resource.
|
||||
|
||||
Args:
|
||||
data (Optional[Union[Dict, List, str]]): The data to return in the response.
|
||||
message (str): A message describing the result of the request.
|
||||
error (Optional[Union[Dict, str]]): Any error details to include in the response.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary representing the JSON response.
|
||||
"""
|
||||
return ResponseHelper.build_response(True, message, data, 200, error)
|
||||
|
||||
@staticmethod
|
||||
def internal_server_error(
|
||||
message: str = "Internal Server Error",
|
||||
data: Optional[Union[Dict, List, str]] = None,
|
||||
error: Optional[Union[Dict, str]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Return a response for an internal server error.
|
||||
|
||||
Args:
|
||||
message (str): A message describing the error.
|
||||
data (Optional[Union[Dict, List, str]]): The data to return in the response.
|
||||
error (Optional[Union[Dict, str]]): Any error details to include in the response.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary representing the JSON response.
|
||||
"""
|
||||
return ResponseHelper.build_response(False, message, data, 500, error)
|
||||
|
||||
@staticmethod
|
||||
def unauthorized(
|
||||
message: str = "Unauthorized",
|
||||
data: Optional[Union[Dict, List, str]] = None,
|
||||
error: Optional[Union[Dict, str]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Return a response for an unauthorized request.
|
||||
|
||||
Args:
|
||||
message (str): A message describing the error.
|
||||
data (Optional[Union[Dict, List, str]]): The data to return in the response.
|
||||
error (Optional[Union[Dict, str]]): Any error details to include in the response.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary representing the JSON response.
|
||||
"""
|
||||
return ResponseHelper.build_response(False, message, data, 401, error)
|
||||
|
||||
@staticmethod
|
||||
def forbidden(
|
||||
message: str = "Forbidden",
|
||||
data: Optional[Union[Dict, List, str]] = None,
|
||||
error: Optional[Union[Dict, str]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Return a response for a forbidden request.
|
||||
|
||||
Args:
|
||||
message (str): A message describing the error.
|
||||
data (Optional[Union[Dict, List, str]]): The data to return in the response.
|
||||
error (Optional[Union[Dict, str]]): Any error details to include in the response.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary representing the JSON response.
|
||||
"""
|
||||
return ResponseHelper.build_response(False, message, data, 403, error)
|
||||
|
||||
@staticmethod
|
||||
def not_found(
|
||||
message: str = "Resource not found",
|
||||
data: Optional[Union[Dict, List, str]] = None,
|
||||
error: Optional[Union[Dict, str]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Return a response for a not found resource.
|
||||
|
||||
Args:
|
||||
message (str): A message describing the error.
|
||||
data (Optional[Union[Dict, List, str]]): The data to return in the response.
|
||||
error (Optional[Union[Dict, str]]): Any error details to include in the response.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary representing the JSON response.
|
||||
"""
|
||||
return ResponseHelper.build_response(False, message, data, 404, error)
|
||||
|
||||
@staticmethod
|
||||
def unprocessable_entity(
|
||||
message: str = "Unprocessable entity",
|
||||
data: Optional[Union[Dict, List, str]] = None,
|
||||
error: Optional[Union[Dict, str]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Return a response for an unprocessable entity.
|
||||
|
||||
Args:
|
||||
message (str): A message describing the error.
|
||||
data (Optional[Union[Dict, List, str]]): The data to return in the response.
|
||||
error (Optional[Union[Dict, str]]): Any error details to include in the response.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary representing the JSON response.
|
||||
"""
|
||||
return ResponseHelper.build_response(False, message, data, 422, error)
|
||||
|
||||
@staticmethod
|
||||
def method_not_allowed(
|
||||
message: str = "Method Not Allowed",
|
||||
data: Optional[Union[Dict, List, str]] = None,
|
||||
error: Optional[Union[Dict, str]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Return a response for a method not allowed error.
|
||||
|
||||
Args:
|
||||
message (str): A message describing the error.
|
||||
data (Optional[Union[Dict, List, str]]): The data to return in the response.
|
||||
error (Optional[Union[Dict, str]]): Any error details to include in the response.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary representing the JSON response.
|
||||
"""
|
||||
return ResponseHelper.build_response(False, message, data, 405, error)
|
||||
|
||||
@staticmethod
|
||||
def bad_request(
|
||||
message: str = "Bad Request",
|
||||
data: Optional[Union[Dict, List, str]] = None,
|
||||
error: Optional[Union[Dict, str]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Return a response for a bad request error.
|
||||
|
||||
Args:
|
||||
message (str): A message describing the error.
|
||||
data (Optional[Union[Dict, List, str]]): The data to return in the response.
|
||||
error (Optional[Union[Dict, str]]): Any error details to include in the response.
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: A dictionary representing the JSON response.
|
||||
"""
|
||||
return ResponseHelper.build_response(False, message, data, 400, error)
|
||||
@@ -0,0 +1,2 @@
|
||||
from .simbrella import SimbrellaIntegration
|
||||
from .kafka import KafkaIntegration
|
||||
@@ -0,0 +1,82 @@
|
||||
from confluent_kafka import Producer
|
||||
import json
|
||||
import logging
|
||||
from app.config import settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class KafkaIntegration:
|
||||
_producer = None
|
||||
_config = {
|
||||
"bootstrap.servers": settings.KAFKA_BROKER,
|
||||
"client.id": "loan-service-producer",
|
||||
"acks": "all",
|
||||
"retries": 3,
|
||||
"debug": "broker,topic,msg",
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _get_producer():
|
||||
"""Kafka producer"""
|
||||
if not KafkaIntegration._producer:
|
||||
KafkaIntegration._producer = Producer(KafkaIntegration._config)
|
||||
|
||||
logger.info(
|
||||
f"Connected to Kafka broker at {KafkaIntegration._config['bootstrap.servers']}"
|
||||
)
|
||||
|
||||
return KafkaIntegration._producer
|
||||
|
||||
|
||||
@staticmethod
|
||||
def delivery_report(err, msg):
|
||||
"""Called once for each message produced"""
|
||||
if err is not None:
|
||||
|
||||
logger.error(f'Message delivery failed: {err}')
|
||||
raise RuntimeError(f"Message delivery failed: {err}")
|
||||
|
||||
else:
|
||||
logger.debug(f'Message delivered to {msg.topic()} [{msg.partition()}] @ offset {msg.offset()}')
|
||||
|
||||
|
||||
|
||||
@staticmethod
|
||||
def send_loan_request(loan_data, request_id, topic):
|
||||
"""
|
||||
Send loan request to topic
|
||||
|
||||
Args:
|
||||
loan_data: Loan request payload as dict
|
||||
request_id: Unique request identifier (used as Kafka key)
|
||||
"""
|
||||
try:
|
||||
|
||||
# Proceed to send loan request to Kafka
|
||||
producer = KafkaIntegration._get_producer()
|
||||
|
||||
# Sending loan request message to Kafka
|
||||
producer.produce(
|
||||
topic=topic,
|
||||
key=str(request_id),
|
||||
value=json.dumps(loan_data).encode("utf-8"),
|
||||
callback=KafkaIntegration.delivery_report,
|
||||
|
||||
)
|
||||
|
||||
producer.poll(0)
|
||||
|
||||
logger.info(f"Loan request {request_id} queued for processing")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Failed to send loan request to Kafka: {str(e)}", exc_info=True
|
||||
)
|
||||
raise Exception(f"Failed to send loan request to Kafka: {str(e)}")
|
||||
|
||||
@staticmethod
|
||||
def flush():
|
||||
"""Shutdown"""
|
||||
producer = KafkaIntegration._get_producer()
|
||||
producer.flush()
|
||||
@@ -0,0 +1,55 @@
|
||||
import requests
|
||||
import json
|
||||
from requests.auth import HTTPBasicAuth
|
||||
from app.utils.logger import logger
|
||||
from app.config import settings
|
||||
|
||||
class SimbrellaIntegration:
|
||||
BASE_URL = settings.SIMBRELLA_BASE_URL
|
||||
|
||||
@staticmethod
|
||||
def rac_check(customer_id, account_id, transaction_id):
|
||||
"""
|
||||
Calls the RACCheck endpoit
|
||||
"""
|
||||
url = f"{SimbrellaIntegration.BASE_URL}/RACCheck"
|
||||
|
||||
payload = {
|
||||
"customerId": customer_id,
|
||||
"accountId": account_id,
|
||||
"transactionId": transaction_id,
|
||||
"RAC_Array": [
|
||||
{
|
||||
"salaryAccount": True,
|
||||
"bvn": "12345678901",
|
||||
"crc": False,
|
||||
"crms": True,
|
||||
"accountStatus": "active",
|
||||
"lien": False,
|
||||
"noBouncedCheck": True,
|
||||
"existingLoan": False,
|
||||
"whitelist": True,
|
||||
"noPastDueSalaryLoan": True,
|
||||
"noPastDueOtherLoans": False
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
logger.error(f"This is PayLoad: {str(payload)}",exc_info=True)
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'x-api-key': f'{settings.VALID_API_KEY}',
|
||||
'App-Id': f'{settings.VALID_APP_ID}'
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(url, json=payload, timeout=10, headers=headers)
|
||||
logger.error(f"This is Response: {str(response)}", exc_info=True)
|
||||
# Raise an error for non-200 responses
|
||||
if response.status_code != 200:
|
||||
response.raise_for_status()
|
||||
|
||||
return response.json()
|
||||
except requests.exceptions.RequestException as err:
|
||||
logger.error(f"RACCheck API call failed: {str(err)}", exc_info=True)
|
||||
return {"error": "RACCheck API error"}
|
||||
@@ -0,0 +1,4 @@
|
||||
from .verify_api_key import require_api_key
|
||||
from .app_id_checker import require_app_id
|
||||
from .cors import enforce_json
|
||||
from .basic_auth import require_auth
|
||||
@@ -0,0 +1,27 @@
|
||||
from functools import wraps
|
||||
from flask import request, jsonify
|
||||
from app.utils.logger import logger
|
||||
from app.config import Config
|
||||
|
||||
|
||||
|
||||
VALID_APP_ID = Config.VALID_APP_ID
|
||||
|
||||
def require_app_id(f):
|
||||
"""Decorator to enforce App-ID validation."""
|
||||
@wraps(f)
|
||||
def decorated_function(*args, **kwargs):
|
||||
app_id = request.headers.get("App-ID")
|
||||
|
||||
if not app_id:
|
||||
logger.error("Unauthorized access: Missing App-ID.")
|
||||
return jsonify({"message": "Invalid request"}), 400
|
||||
|
||||
|
||||
if app_id != VALID_APP_ID:
|
||||
logger.error(f"Unauthorized access: Invalid App-ID {app_id}.")
|
||||
return jsonify({"message": "Invalid request"}), 400
|
||||
|
||||
return f(*args, **kwargs)
|
||||
|
||||
return decorated_function
|
||||
@@ -0,0 +1,30 @@
|
||||
from functools import wraps
|
||||
from flask import request, jsonify
|
||||
import base64
|
||||
from app.config import Config
|
||||
|
||||
USERNAME = Config.BASIC_AUTH_USERNAME
|
||||
PASSWORD = Config.BASIC_AUTH_PASSWORD
|
||||
|
||||
def require_auth(f):
|
||||
@wraps(f)
|
||||
def decorated(*args, **kwargs):
|
||||
auth = request.headers.get('Authorization')
|
||||
if not auth or not check_auth(auth):
|
||||
return jsonify({"message": "Invalid request"}), 401
|
||||
return f(*args, **kwargs)
|
||||
return decorated
|
||||
|
||||
def check_auth(auth_header):
|
||||
if not auth_header:
|
||||
return False
|
||||
try:
|
||||
auth_type, credentials = auth_header.split()
|
||||
if auth_type.lower() != "basic":
|
||||
return False
|
||||
|
||||
decoded_credentials = base64.b64decode(credentials).decode("utf-8")
|
||||
user, pwd = decoded_credentials.split(":", 1)
|
||||
return user == USERNAME and pwd == PASSWORD
|
||||
except Exception:
|
||||
return False
|
||||
@@ -0,0 +1,7 @@
|
||||
from flask import request, jsonify
|
||||
|
||||
|
||||
def enforce_json():
|
||||
"""Middleware to enforce JSON Content-Type for incoming requests"""
|
||||
if request.method in ["POST", "PUT", "PATCH"] and request.content_type != "application/json":
|
||||
return jsonify({"message": "Invalid request"}), 400
|
||||
@@ -0,0 +1,25 @@
|
||||
from functools import wraps
|
||||
from flask import request, jsonify
|
||||
from app.utils.logger import logger
|
||||
from app.config import Config
|
||||
|
||||
# Load valid API key from environment variables (fallback for testing)
|
||||
VALID_API_KEY = Config.VALID_API_KEY
|
||||
|
||||
def require_api_key(f):
|
||||
"""Decorator to enforce API key authentication."""
|
||||
@wraps(f)
|
||||
def decorated_function(*args, **kwargs):
|
||||
api_key = request.headers.get("X-API-KEY")
|
||||
|
||||
if not api_key:
|
||||
logger.error("Unauthorized access: Missing API key.")
|
||||
return jsonify({"message": "Invalid request"}), 400
|
||||
|
||||
if api_key != VALID_API_KEY:
|
||||
logger.error("Unauthorized access: Invalid API key.")
|
||||
return jsonify({"message": "Invalid request"}), 400
|
||||
|
||||
return f(*args, **kwargs)
|
||||
|
||||
return decorated_function
|
||||
@@ -0,0 +1 @@
|
||||
from .routes import api
|
||||
@@ -0,0 +1,138 @@
|
||||
from flask import Blueprint, request, jsonify, send_from_directory
|
||||
from app.api.services import (
|
||||
EligibilityCheckService,
|
||||
SelectOfferService,
|
||||
ProvideLoanService,
|
||||
LoanStatusService,
|
||||
RepaymentService,
|
||||
CustomerConsentService,
|
||||
NotificationCallbackService,
|
||||
AuthorizationService,
|
||||
)
|
||||
from app.utils.logger import logger
|
||||
from app.api.middlewares import enforce_json, require_auth
|
||||
import os
|
||||
from flask_jwt_extended import (
|
||||
JWTManager,
|
||||
jwt_required,
|
||||
create_access_token,
|
||||
get_jwt_identity,
|
||||
create_refresh_token,
|
||||
)
|
||||
|
||||
|
||||
api = Blueprint("api", __name__)
|
||||
|
||||
|
||||
@api.before_request
|
||||
def cors_middleware():
|
||||
"""Middleware applied globally to all API routes in this blueprint"""
|
||||
return enforce_json()
|
||||
|
||||
|
||||
# Swagger JSON file
|
||||
@api.route("/swagger.json", methods=["GET"])
|
||||
def swagger_json():
|
||||
swagger_dir = os.path.join("swagger")
|
||||
return send_from_directory(swagger_dir, "digifi_swagger.json")
|
||||
|
||||
|
||||
@api.route("/swagger/<path:filename>")
|
||||
def serve_paths(filename):
|
||||
swagger_dir = os.path.join("swagger")
|
||||
return send_from_directory(swagger_dir, filename)
|
||||
|
||||
|
||||
# EligibilityCheck Endpoint
|
||||
@api.route("/EligibilityCheck", methods=["POST"])
|
||||
@jwt_required()
|
||||
def eligibility_check():
|
||||
data = request.get_json()
|
||||
# logger.info(f"EligibilityCheck request received: {data}")
|
||||
response = EligibilityCheckService.process_request(data)
|
||||
return response
|
||||
|
||||
|
||||
# SelectOffer Endpoint
|
||||
@api.route("/SelectOffer", methods=["POST"])
|
||||
@jwt_required()
|
||||
def select_offer():
|
||||
data = request.get_json()
|
||||
# logger.info(f"SelectOffer request received: {data}")
|
||||
response = SelectOfferService.process_request(data)
|
||||
return response
|
||||
|
||||
|
||||
# ProvideLoan Endpoint
|
||||
@api.route("/ProvideLoan", methods=["POST"])
|
||||
@jwt_required()
|
||||
def provide_loan():
|
||||
data = request.get_json()
|
||||
# logger.info(f"ProvideLoan request received: {data}")
|
||||
response = ProvideLoanService.process_request(data)
|
||||
return response
|
||||
|
||||
|
||||
# LoanStatus Endpoint
|
||||
@api.route("/LoanStatus", methods=["POST"])
|
||||
@jwt_required()
|
||||
def loan_status():
|
||||
data = request.get_json()
|
||||
# logger.info(f"LoanStatus request received: {data}")
|
||||
response = LoanStatusService.process_request(data)
|
||||
return response
|
||||
|
||||
|
||||
# Repayment Endpoint
|
||||
@api.route("/Repayment", methods=["POST"])
|
||||
@jwt_required()
|
||||
def repayment():
|
||||
data = request.get_json()
|
||||
# logger.info(f"Repayment request received: {data}")
|
||||
response = RepaymentService.process_request(data)
|
||||
return response
|
||||
|
||||
|
||||
# CustomerConsent Endpoint
|
||||
@api.route("/CustomerConsent", methods=["POST"])
|
||||
@jwt_required()
|
||||
def customer_consent():
|
||||
data = request.get_json()
|
||||
# logger.info(f"CustomerConsent request received: {data}")
|
||||
response = CustomerConsentService.process_request(data)
|
||||
return response
|
||||
|
||||
|
||||
# NotificationCallback Endpoint
|
||||
@api.route("/NotificationCallback", methods=["POST"])
|
||||
@jwt_required()
|
||||
def notification_callback():
|
||||
data = request.get_json()
|
||||
# logger.info(f"NotificationCallback request received: {data}")
|
||||
response = NotificationCallbackService.process_request(data)
|
||||
return response
|
||||
|
||||
|
||||
# Health Check Endpoint
|
||||
@api.route("/health", methods=["GET"])
|
||||
def health_check():
|
||||
return {"status": "ok"}, 200
|
||||
|
||||
|
||||
# Authorize endpoint
|
||||
@api.route("/Authorize", methods=["POST"])
|
||||
def authorize():
|
||||
data = request.get_json()
|
||||
# logger.info(f"Authorize request received: {data}")
|
||||
response = AuthorizationService.process_request(data)
|
||||
return response
|
||||
|
||||
|
||||
# Authorize refresh endpoint
|
||||
@api.route("/AuthorizeRefresh", methods=["POST"])
|
||||
@jwt_required(refresh=True)
|
||||
def refresh():
|
||||
data = request.get_json()
|
||||
# logger.info(f"Authorize refresh request received: {data}")
|
||||
response = AuthorizationService.process_refresh_request()
|
||||
return response
|
||||
@@ -0,0 +1,6 @@
|
||||
from marshmallow import Schema, fields
|
||||
|
||||
|
||||
class AuthorizeRequestSchema(Schema):
|
||||
username = fields.Str(required=True)
|
||||
password = fields.Str(required=True)
|
||||
@@ -0,0 +1,11 @@
|
||||
from marshmallow import Schema, fields
|
||||
|
||||
# Customer Consent Schema
|
||||
class CustomerConsentSchema(Schema):
|
||||
type = fields.Str(required=True)
|
||||
transactionId = fields.Str(required=True)
|
||||
customerId = fields.Str(required=True)
|
||||
accountId = fields.Str(required=True)
|
||||
requestTime = fields.DateTime(required=True, format="%Y-%m-%d %H:%M:%S.%f")
|
||||
consentType = fields.Str(required=True)
|
||||
channel = fields.Str(required=True)
|
||||
@@ -0,0 +1,10 @@
|
||||
from marshmallow import Schema, fields
|
||||
|
||||
class EligibilityCheckSchema(Schema):
|
||||
transactionId = fields.Str(required=True)
|
||||
countryCode = fields.Str(required=True)
|
||||
customerId = fields.Str(required=True)
|
||||
accountId = fields.Str(required=True)
|
||||
msisdn = fields.Str(required=True)
|
||||
accountId = fields.Str(required=True)
|
||||
channel = fields.Str(required=True)
|
||||
@@ -0,0 +1,8 @@
|
||||
from marshmallow import Schema, fields
|
||||
|
||||
# Loan Information Schema
|
||||
class LoanStatusSchema(Schema):
|
||||
transactionId = fields.Str(required=True)
|
||||
customerId = fields.Str(required=True)
|
||||
msisdn = fields.Str(required=False)
|
||||
channel = fields.Str(required=True)
|
||||
@@ -0,0 +1,14 @@
|
||||
from marshmallow import Schema, fields
|
||||
|
||||
# Notification Callback Schema
|
||||
class NotificationCallbackSchema(Schema):
|
||||
fbnTransactionId = fields.Str(required=True)
|
||||
transactionId = fields.Str(required=True)
|
||||
customerId = fields.Str(required=True)
|
||||
accountId = fields.Str(required=True)
|
||||
debtId = fields.Str(required=True)
|
||||
transactionType = fields.Str(required=True)
|
||||
amountProvided = fields.Float(required=True)
|
||||
amountCollected = fields.Float(required=True)
|
||||
responseCode = fields.Str(required=True)
|
||||
responseDescription = fields.Str(required=True)
|
||||
@@ -0,0 +1,16 @@
|
||||
from marshmallow import Schema, fields
|
||||
|
||||
# Provide Loan Schema
|
||||
class ProvideLoanSchema(Schema):
|
||||
type = fields.Str(required=False)
|
||||
requestId = fields.Str(required=True)
|
||||
transactionId = fields.Str(required=True)
|
||||
customerId = fields.Str(required=True)
|
||||
accountId = fields.Str(required=True)
|
||||
msisdn = fields.Str(required=False)
|
||||
# productId = fields.Str(required=True)
|
||||
# lienAmount = fields.Float(required=True)
|
||||
requestedAmount = fields.Float(required=True)
|
||||
collectionType = fields.Int(required=True)
|
||||
offerId = fields.Int(required=True)
|
||||
channel = fields.Str(required=True)
|
||||
@@ -0,0 +1,11 @@
|
||||
from marshmallow import Schema, fields
|
||||
|
||||
# Repayment Schema
|
||||
class RepaymentSchema(Schema):
|
||||
type = fields.Str(required=False)
|
||||
msisdn = fields.Str(required=False) #optional
|
||||
debtId = fields.Str(required=True)
|
||||
productId = fields.Str(required=True)
|
||||
transactionId = fields.Str(required=True)
|
||||
customerId = fields.Str(required=True)
|
||||
channel = fields.Str(required=True)
|
||||
@@ -0,0 +1,13 @@
|
||||
from marshmallow import Schema, fields
|
||||
|
||||
# Select Offer Schema
|
||||
class SelectOfferSchema(Schema):
|
||||
requestId = fields.Str(required=True)
|
||||
transactionId = fields.Str(required=True)
|
||||
customerId = fields.Str(required=True)
|
||||
accountId = fields.Str(required=True)
|
||||
msisdn = fields.Str(required=True)
|
||||
requestedAmount = fields.Float(required=True)
|
||||
productId = fields.Str(required=True)
|
||||
channel = fields.Str(required=True)
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
from app.api.services.eligibility_check import EligibilityCheckService
|
||||
from app.api.services.select_offer import SelectOfferService
|
||||
from app.api.services.provide_loan import ProvideLoanService
|
||||
from app.api.services.loan_status import LoanStatusService
|
||||
from app.api.services.repayment import RepaymentService
|
||||
from app.api.services.customer_consent import CustomerConsentService
|
||||
from app.api.services.notification_callback import NotificationCallbackService
|
||||
from app.api.services.authorization import AuthorizationService
|
||||
@@ -0,0 +1,102 @@
|
||||
from flask import request, jsonify
|
||||
from marshmallow import ValidationError
|
||||
from app.api.services.base_service import BaseService
|
||||
from app.utils.logger import logger
|
||||
from app.api.schemas.authorization import AuthorizeRequestSchema
|
||||
from app.api.helpers.response_helper import ResponseHelper
|
||||
from flask_jwt_extended import (
|
||||
JWTManager,
|
||||
jwt_required,
|
||||
create_access_token,
|
||||
create_refresh_token,
|
||||
get_jwt_identity,
|
||||
)
|
||||
from app.config import Config
|
||||
|
||||
USERNAME = Config.BASIC_AUTH_USERNAME
|
||||
PASSWORD = Config.BASIC_AUTH_PASSWORD
|
||||
|
||||
|
||||
class AuthorizationService(BaseService):
|
||||
|
||||
@staticmethod
|
||||
def process_request(data):
|
||||
"""
|
||||
Process the Authorization request.
|
||||
|
||||
Args:
|
||||
data (dict): The request data.
|
||||
|
||||
Returns:
|
||||
dict: A standardized response.
|
||||
"""
|
||||
try:
|
||||
logger.info("Processing Authorization request")
|
||||
|
||||
if not data:
|
||||
return ResponseHelper.bad_request(message="Missing JSON in request")
|
||||
|
||||
# Validate input data using the Authorization schema
|
||||
schema = AuthorizeRequestSchema()
|
||||
validated_data = schema.load(data) # Raises ValidationError if invalid
|
||||
|
||||
if (
|
||||
validated_data["username"] != USERNAME
|
||||
or validated_data["password"] != PASSWORD
|
||||
):
|
||||
return ResponseHelper.unauthorized(message="Invalid credentials")
|
||||
|
||||
access_token = create_access_token(identity=validated_data["username"])
|
||||
refresh_token = create_refresh_token(identity=validated_data["username"])
|
||||
|
||||
# Simulated processing logic
|
||||
response_data = {
|
||||
"access_token": access_token,
|
||||
"refresh_token": refresh_token,
|
||||
}
|
||||
|
||||
return ResponseHelper.success(
|
||||
data=response_data, message="Authorization processed successfully"
|
||||
)
|
||||
|
||||
except ValidationError as e:
|
||||
logger.error(f"Validation error: {e}")
|
||||
return ResponseHelper.bad_request(message=f"Validation error: {e}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing Authorization request: {e}")
|
||||
return ResponseHelper.internal_server_error(
|
||||
message=f"Error processing Authorization request: {e}"
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def process_refresh_request():
|
||||
"""
|
||||
Process the RefreshToken request.
|
||||
|
||||
Args:
|
||||
data (dict): The request data.
|
||||
|
||||
Returns:
|
||||
dict: A standardized response.
|
||||
"""
|
||||
try:
|
||||
logger.info("Processing RefreshToken request")
|
||||
|
||||
identity = get_jwt_identity()
|
||||
access_token = create_access_token(identity=identity)
|
||||
|
||||
# Simulated processing logic
|
||||
response_data = {
|
||||
"access_token": access_token,
|
||||
}
|
||||
|
||||
return ResponseHelper.success(
|
||||
data=response_data, message="RefreshToken processed successfully"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing RefreshToken request: {e}")
|
||||
return ResponseHelper.internal_server_error(
|
||||
message=f"Error processing RefreshToken request: {e}"
|
||||
)
|
||||
@@ -0,0 +1,61 @@
|
||||
from app.models import Customer, Account, Transaction
|
||||
from app.api.enums import TransactionType
|
||||
from flask import jsonify
|
||||
from marshmallow import ValidationError
|
||||
import logging
|
||||
from app.api.integrations import KafkaIntegration
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class BaseService:
|
||||
TRANSACTION_TYPE = None
|
||||
|
||||
@classmethod
|
||||
def validate_data(cls, data, schema):
|
||||
"""
|
||||
Validate input data based on the provided schema.
|
||||
"""
|
||||
logger.info(f"Processing {cls.TRANSACTION_TYPE} request")
|
||||
return schema.load(data)
|
||||
|
||||
@classmethod
|
||||
def get_or_create_customer(cls, validated_data):
|
||||
|
||||
"""
|
||||
Check if a customer exists; if not, create one.
|
||||
"""
|
||||
|
||||
customer = Customer.query.filter_by(id=validated_data.get("customerId")).first()
|
||||
if not customer:
|
||||
customer = Customer.create_customer(
|
||||
id=validated_data.get("customerId"),
|
||||
msisdn=validated_data.get("msisdn"),
|
||||
country_code=validated_data.get("countryCode"),
|
||||
account_id=validated_data.get("accountId"),
|
||||
)
|
||||
return customer
|
||||
|
||||
@classmethod
|
||||
def validate_account_ownership(cls, account_id, customer_id):
|
||||
"""
|
||||
Check if the provided account belongs to the customer.
|
||||
"""
|
||||
is_valid = Account.is_valid_account(account_id, customer_id)
|
||||
return is_valid
|
||||
|
||||
@classmethod
|
||||
def log_transaction(cls, validated_data):
|
||||
"""
|
||||
Create a new transaction.
|
||||
"""
|
||||
return Transaction.create_transaction(
|
||||
transaction_id =validated_data.get("transactionId"),
|
||||
account_id=validated_data.get("accountId"),
|
||||
type=cls.TRANSACTION_TYPE,
|
||||
channel=validated_data.get("channel"),
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def async_send_to_kafka(cls, loan_data, request_id, topic):
|
||||
KafkaIntegration.send_loan_request(loan_data = loan_data, request_id = request_id, topic = topic)
|
||||
KafkaIntegration.flush()
|
||||
@@ -0,0 +1,71 @@
|
||||
from flask import request, jsonify
|
||||
from app.api.services.base_service import BaseService
|
||||
from marshmallow import ValidationError
|
||||
from app.utils.logger import logger
|
||||
from app.api.schemas.customer_consent import CustomerConsentSchema
|
||||
from app.api.services.base_service import BaseService
|
||||
from app.api.enums import TransactionType
|
||||
|
||||
|
||||
class CustomerConsentService(BaseService):
|
||||
TRANSACTION_TYPE = TransactionType.CUSTOMER_CONSENT
|
||||
|
||||
@staticmethod
|
||||
def process_request(data):
|
||||
"""
|
||||
Process the CustomerConsent request.
|
||||
|
||||
Args:
|
||||
data (dict): The request data.
|
||||
|
||||
Returns:
|
||||
dict: A standardized response.
|
||||
"""
|
||||
try:
|
||||
|
||||
validated_data = CustomerConsentService.validate_data(data, CustomerConsentSchema())
|
||||
account_id = validated_data.get('accountId')
|
||||
customer_id = validated_data.get('customerId')
|
||||
|
||||
if(CustomerConsentService.validate_account_ownership(account_id = account_id, customer_id = customer_id)):
|
||||
transaction = CustomerConsentService.log_transaction(validated_data = validated_data)
|
||||
|
||||
if not transaction:
|
||||
logger.error(f"Failed to log transaction")
|
||||
return jsonify({
|
||||
"message": "Failed to log transaction."
|
||||
}), 400
|
||||
else:
|
||||
return jsonify({
|
||||
"message": "Invalid Customer or Account"
|
||||
}), 400
|
||||
|
||||
|
||||
# Simulated processing logic
|
||||
response_data = {
|
||||
"resultCode": "00",
|
||||
"resultDescription": "Request is received"
|
||||
}
|
||||
|
||||
return response_data
|
||||
|
||||
except ValidationError as err:
|
||||
|
||||
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
||||
|
||||
return jsonify({
|
||||
"message": "Validation exception"
|
||||
}) , 422
|
||||
|
||||
except ValueError as err:
|
||||
logger.error(f"{getattr(err, 'messages', str(err))}")
|
||||
|
||||
return jsonify({
|
||||
"message": str(err)
|
||||
}) , 400
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
||||
return jsonify({
|
||||
"message": "Internal Server Error"
|
||||
}) , 500
|
||||
@@ -0,0 +1,108 @@
|
||||
from flask import session, jsonify
|
||||
from app.utils.logger import logger
|
||||
from app.api.services.base_service import BaseService
|
||||
from app.api.schemas.eligibility_check import EligibilityCheckSchema
|
||||
from marshmallow import ValidationError
|
||||
from app.api.enums import TransactionType
|
||||
from app.api.integrations import SimbrellaIntegration
|
||||
|
||||
class EligibilityCheckService(BaseService):
|
||||
TRANSACTION_TYPE = TransactionType.ELIGIBILITY_CHECK
|
||||
|
||||
@staticmethod
|
||||
def process_request(data):
|
||||
"""
|
||||
Process the EligibilityCheck request.
|
||||
|
||||
Args:
|
||||
data (dict): The request data.
|
||||
|
||||
Returns:
|
||||
dict: A standardized response.
|
||||
"""
|
||||
try:
|
||||
|
||||
validated_data = EligibilityCheckService.validate_data(data, EligibilityCheckSchema())
|
||||
account_id = validated_data.get('accountId')
|
||||
customer_id = validated_data.get('customerId')
|
||||
transactionId = validated_data.get('transactionId')
|
||||
msisdn = validated_data.get('msisdn')
|
||||
|
||||
customer = EligibilityCheckService.get_or_create_customer(validated_data = validated_data)
|
||||
|
||||
if (EligibilityCheckService.validate_account_ownership(account_id = account_id, customer_id = customer_id)):
|
||||
transaction = EligibilityCheckService.log_transaction(validated_data = validated_data)
|
||||
|
||||
if not transaction:
|
||||
logger.error(f"Failed to log transaction")
|
||||
return jsonify({
|
||||
"message": "Failed to log transaction."
|
||||
}), 400
|
||||
else:
|
||||
return jsonify({
|
||||
"message": "Invalid Customer or Account"
|
||||
}), 400
|
||||
|
||||
# Call RACCheck
|
||||
response = SimbrellaIntegration.rac_check(
|
||||
customer_id = customer_id,
|
||||
account_id = account_id,
|
||||
transaction_id = transaction.id,
|
||||
)
|
||||
logger.error(f"This is Response Returned ****** : {str(response)}")
|
||||
|
||||
# this chck for error is not valid
|
||||
logger.error(f"Check for ERROR is not valid ****** FIX THIS !!!!!")
|
||||
#if "error" in response or response.get("status") != 200:
|
||||
# return jsonify({"message": "RACCheck failed"}), 400
|
||||
|
||||
offers = [
|
||||
{
|
||||
"offerId": "SAL90",
|
||||
"productId": "2030",
|
||||
"minAmount": 5000,
|
||||
"maxAmount": 100000,
|
||||
"tenor": 30
|
||||
},
|
||||
{
|
||||
"offerId": "SAL30",
|
||||
"productId": "2090",
|
||||
"minAmount": 3000,
|
||||
"maxAmount": 500000,
|
||||
"tenor": 90
|
||||
}
|
||||
]
|
||||
|
||||
# Simulate processing
|
||||
response_data = {
|
||||
"customerId": customer_id,
|
||||
"transactionId": transactionId,
|
||||
"countryCode": "NG",
|
||||
"msisdn": msisdn,
|
||||
"eligibleOffers": offers,
|
||||
"resultDescription": "Successful",
|
||||
"resultCode": "00",
|
||||
"accountId": account_id
|
||||
}
|
||||
|
||||
return response_data
|
||||
except ValidationError as err:
|
||||
|
||||
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
||||
|
||||
return jsonify({
|
||||
"message": "Validation exception"
|
||||
}) , 422
|
||||
|
||||
except ValueError as err:
|
||||
logger.error(f"{getattr(err, 'messages', str(err))}")
|
||||
|
||||
return jsonify({
|
||||
"message": str(err)
|
||||
}) , 400
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
||||
return jsonify({
|
||||
"message": "Internal Server Error"
|
||||
}) , 500
|
||||
@@ -0,0 +1,88 @@
|
||||
from flask import request, jsonify
|
||||
from marshmallow import ValidationError
|
||||
from app.utils.logger import logger
|
||||
from app.api.schemas.loan_status import LoanStatusSchema
|
||||
from app.api.services.base_service import BaseService
|
||||
from app.api.enums import TransactionType
|
||||
|
||||
|
||||
class LoanStatusService(BaseService):
|
||||
TRANSACTION_TYPE = TransactionType.LOAN_STATUS
|
||||
|
||||
@staticmethod
|
||||
def process_request(data):
|
||||
"""
|
||||
Process the Loan Information request.
|
||||
|
||||
Args:
|
||||
data (dict): The request data.
|
||||
|
||||
Returns:
|
||||
dict: A standardized response.
|
||||
"""
|
||||
try:
|
||||
validated_data = LoanStatusService.validate_data(data, LoanStatusSchema())
|
||||
customer_id = validated_data.get('customerId')
|
||||
customer = LoanStatusService.get_or_create_customer(validated_data)
|
||||
account = customer.accounts[0]
|
||||
|
||||
if (LoanStatusService.validate_account_ownership(account_id = account.id, customer_id = customer_id)):
|
||||
transaction = LoanStatusService.log_transaction(validated_data = validated_data)
|
||||
|
||||
if not transaction:
|
||||
logger.error(f"Failed to log transaction")
|
||||
return jsonify({
|
||||
"message": "Failed to log transaction."
|
||||
}), 400
|
||||
else:
|
||||
return jsonify({
|
||||
"message": "Invalid Customer or Account"
|
||||
}), 400
|
||||
|
||||
|
||||
loans = [
|
||||
{
|
||||
"debtId": "123456789",
|
||||
"loanDate": "2019-10-18 14:26:21.063",
|
||||
"dueDate": "2019-11-20 14:26:21.063",
|
||||
"currentLoanAmount": 8500,
|
||||
"initialLoanAmount": 10000,
|
||||
"defaultPenaltyFee": 0,
|
||||
"continuousFee": 0,
|
||||
"productId": "101"
|
||||
}
|
||||
]
|
||||
|
||||
# Simulated processing logic
|
||||
response_data = {
|
||||
"customerId": "CN621868",
|
||||
"transactionId": "Tr201712RK9232P115",
|
||||
"loans": loans,
|
||||
"totalDebtAmount": 8500,
|
||||
"resultCode": "00",
|
||||
"resultDescription": "Successful"
|
||||
}
|
||||
|
||||
|
||||
return response_data
|
||||
|
||||
except ValidationError as err:
|
||||
|
||||
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
||||
|
||||
return jsonify({
|
||||
"message": "Validation exception"
|
||||
}) , 422
|
||||
|
||||
except ValueError as err:
|
||||
logger.error(f"{getattr(err, 'messages', str(err))}")
|
||||
|
||||
return jsonify({
|
||||
"message": str(err)
|
||||
}) , 400
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
||||
return jsonify({
|
||||
"message": "Internal Server Error"
|
||||
}) , 500
|
||||
@@ -0,0 +1,62 @@
|
||||
from flask import request, jsonify
|
||||
from marshmallow import ValidationError
|
||||
from app.api.services.base_service import BaseService
|
||||
from app.api.enums import TransactionType
|
||||
from app.utils.logger import logger
|
||||
from app.api.schemas.notification_callback import NotificationCallbackSchema
|
||||
|
||||
class NotificationCallbackService(BaseService):
|
||||
TRANSACTION_TYPE = TransactionType.NOTIFICATION_CALLBACK
|
||||
|
||||
@staticmethod
|
||||
def process_request(data):
|
||||
"""
|
||||
Process the NotificationCallback request.
|
||||
|
||||
Args:
|
||||
data (dict): The request data.
|
||||
|
||||
Returns:
|
||||
dict: A standardized response.
|
||||
"""
|
||||
try:
|
||||
logger.info("Processing NotificationCallback request")
|
||||
|
||||
# Validate input data using the NotificationCallback schema
|
||||
schema = NotificationCallbackSchema()
|
||||
validated_data = schema.load(data) # Raises ValidationError if invalid
|
||||
|
||||
# Simulated processing logic
|
||||
response_data = {
|
||||
"resultCode": "00",
|
||||
"resultDescription": "Successful"
|
||||
}
|
||||
|
||||
|
||||
# return ResponseHelper.success(
|
||||
# data=response_data,
|
||||
# message="Notification callback processed successfully"
|
||||
# )
|
||||
|
||||
return response_data
|
||||
|
||||
except ValidationError as err:
|
||||
|
||||
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
||||
|
||||
return jsonify({
|
||||
"message": "Validation exception"
|
||||
}) , 422
|
||||
|
||||
except ValueError as err:
|
||||
logger.error(f"{getattr(err, 'messages', str(err))}")
|
||||
|
||||
return jsonify({
|
||||
"message": str(err)
|
||||
}) , 400
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
||||
return jsonify({
|
||||
"message": "Internal Server Error"
|
||||
}) , 500
|
||||
@@ -0,0 +1,109 @@
|
||||
from flask import request, jsonify
|
||||
from marshmallow import ValidationError
|
||||
from app.api.integrations.kafka import KafkaIntegration
|
||||
from app.api.services.base_service import BaseService
|
||||
from app.api.enums import TransactionType
|
||||
from app.utils.logger import logger
|
||||
from app.api.schemas.provide_loan import ProvideLoanSchema
|
||||
from threading import Thread
|
||||
from app.models.loan import Loan
|
||||
from app.api.enums import LoanStatus
|
||||
|
||||
class ProvideLoanService(BaseService):
|
||||
TRANSACTION_TYPE = TransactionType.PROVIDE_LOAN
|
||||
|
||||
|
||||
@staticmethod
|
||||
def process_request(data):
|
||||
"""
|
||||
Process the ProvideLoan request.
|
||||
|
||||
Args:
|
||||
data (dict): The request data.
|
||||
|
||||
Returns:
|
||||
dict: A standardized response.
|
||||
"""
|
||||
try:
|
||||
validated_data = ProvideLoanService.validate_data(data, ProvideLoanSchema())
|
||||
account_id = validated_data.get('accountId')
|
||||
customer_id = validated_data.get('customerId')
|
||||
request_id = validated_data.get('requestId')
|
||||
transaction_id = validated_data.get('transactionId')
|
||||
|
||||
if (ProvideLoanService.validate_account_ownership(account_id = account_id, customer_id = customer_id)):
|
||||
|
||||
# Save the loan details
|
||||
loan = Loan.create_loan(
|
||||
customer_id=customer_id,
|
||||
account_id=account_id,
|
||||
offer_id=validated_data.get('offerId'),
|
||||
principal_amount=validated_data.get('requestedAmount'),
|
||||
status=LoanStatus.ACTIVE
|
||||
)
|
||||
|
||||
if not loan:
|
||||
logger.error(f"Failed to save loan details")
|
||||
return jsonify({
|
||||
"message": "Failed to save loan details."
|
||||
}), 400
|
||||
|
||||
# Log Transaction
|
||||
transaction = ProvideLoanService.log_transaction(validated_data = validated_data)
|
||||
|
||||
if not transaction:
|
||||
logger.error(f"Failed to log transaction")
|
||||
return jsonify({
|
||||
"message": "Failed to log transaction."
|
||||
}), 400
|
||||
|
||||
|
||||
|
||||
else:
|
||||
return jsonify({
|
||||
"message": "Invalid Customer or Account"
|
||||
}), 400
|
||||
|
||||
|
||||
response_data = {
|
||||
"requestId": request_id,
|
||||
"transactionId": transaction_id,
|
||||
"customerId": customer_id,
|
||||
"accountId": account_id,
|
||||
"msisdn": "3451342",
|
||||
"resultCode": "00",
|
||||
"resultDescription": "Successful"
|
||||
}
|
||||
|
||||
|
||||
# KafkaIntegration.send_loan_request(loan_data = response_data, request_id = request_id)
|
||||
# Call Kafka in a background thread
|
||||
thread = Thread(target=ProvideLoanService.async_send_to_kafka, args=(response_data, request_id, "PROCESS_PAYMENT"))
|
||||
thread.start()
|
||||
|
||||
return response_data
|
||||
|
||||
except ValidationError as err:
|
||||
|
||||
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
||||
|
||||
return jsonify({
|
||||
"message": "Validation exception"
|
||||
}) , 422
|
||||
|
||||
except ValueError as err:
|
||||
logger.error(f"{getattr(err, 'messages', str(err))}")
|
||||
|
||||
return jsonify({
|
||||
"message": str(err)
|
||||
}) , 400
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
||||
return jsonify({
|
||||
"message": "Internal Server Error"
|
||||
}) , 500
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
from flask import request, jsonify
|
||||
from marshmallow import ValidationError
|
||||
from app.api.enums.loan_status import LoanStatus
|
||||
from app.models import Repayment
|
||||
from app.models.loan import Loan
|
||||
from app.utils.logger import logger
|
||||
from app.api.schemas.repayment import RepaymentSchema
|
||||
from app.api.services.base_service import BaseService
|
||||
from app.api.enums import TransactionType
|
||||
from threading import Thread
|
||||
|
||||
class RepaymentService(BaseService):
|
||||
TRANSACTION_TYPE = TransactionType.REPAYMENT
|
||||
|
||||
@staticmethod
|
||||
def process_request(data):
|
||||
"""
|
||||
Process the Repayment request.
|
||||
|
||||
Args:
|
||||
data (dict): The request data.
|
||||
|
||||
Returns:
|
||||
dict: A standardized response.
|
||||
"""
|
||||
try:
|
||||
validated_data = RepaymentService.validate_data(data, RepaymentSchema())
|
||||
customer_id = validated_data.get('customerId')
|
||||
customer = RepaymentService.get_or_create_customer(validated_data)
|
||||
account = customer.accounts[0]
|
||||
validated_data['accountId'] = account.id
|
||||
request_id = validated_data.get('requestId')
|
||||
loan_id = validated_data.get('debtId')
|
||||
|
||||
|
||||
if (RepaymentService.validate_account_ownership(account_id = account.id, customer_id = customer_id)):
|
||||
|
||||
# Save the repayment details
|
||||
repayment = Repayment.create_repayment(
|
||||
customer_id = customer_id,
|
||||
loan_id = loan_id,
|
||||
product_id = validated_data.get('productId')
|
||||
|
||||
)
|
||||
|
||||
if not repayment:
|
||||
logger.error(f"Failed to save repayment details")
|
||||
return jsonify({
|
||||
"message": "Failed to save repayment details."
|
||||
}), 400
|
||||
|
||||
#Update Loan status
|
||||
Loan.update_status(loan_id = loan_id, status = LoanStatus.REPAID)
|
||||
|
||||
transaction = RepaymentService.log_transaction(validated_data = validated_data)
|
||||
|
||||
if not transaction:
|
||||
logger.error(f"Failed to log transaction")
|
||||
return jsonify({
|
||||
"message": "Failed to log transaction."
|
||||
}), 400
|
||||
else:
|
||||
return jsonify({
|
||||
"message": "Invalid Customer or Account"
|
||||
}), 400
|
||||
|
||||
# Simulated processing logic
|
||||
response_data = {
|
||||
"customerId": "CN621868",
|
||||
"productId": "101",
|
||||
"debtId": "273194670",
|
||||
"resultCode": "00",
|
||||
"resultDescription": "Successful"
|
||||
}
|
||||
|
||||
# return ResponseHelper.success(
|
||||
# data=response_data,
|
||||
# message="Repayment processed successfully"
|
||||
# )
|
||||
|
||||
# Call Kafka in a background thread
|
||||
thread = Thread(target=RepaymentService.async_send_to_kafka, args=(response_data, request_id, "LOAN_REPAYMENT"))
|
||||
thread.start()
|
||||
|
||||
return response_data
|
||||
|
||||
except ValidationError as err:
|
||||
|
||||
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
||||
|
||||
return jsonify({
|
||||
"message": "Validation exception"
|
||||
}) , 422
|
||||
|
||||
except ValueError as err:
|
||||
logger.error(f"{getattr(err, 'messages', str(err))}")
|
||||
|
||||
return jsonify({
|
||||
"message": str(err)
|
||||
}) , 400
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
||||
return jsonify({
|
||||
"message": "Internal Server Error"
|
||||
}) , 500
|
||||
@@ -0,0 +1,93 @@
|
||||
from flask import request, jsonify
|
||||
from marshmallow import ValidationError
|
||||
from app.api.services.base_service import BaseService
|
||||
from app.api.enums import TransactionType
|
||||
from app.utils.logger import logger
|
||||
from app.api.schemas.select_offer import SelectOfferSchema
|
||||
|
||||
class SelectOfferService(BaseService):
|
||||
TRANSACTION_TYPE = TransactionType.SELECT_OFFER
|
||||
|
||||
@staticmethod
|
||||
def process_request(data):
|
||||
"""
|
||||
Process the SelectOffer request.
|
||||
|
||||
Args:
|
||||
data (dict): The request data.
|
||||
|
||||
Returns:
|
||||
dict: A standardized response.
|
||||
"""
|
||||
try:
|
||||
validated_data = SelectOfferService.validate_data(data, SelectOfferSchema())
|
||||
account_id = validated_data.get('accountId')
|
||||
customer_id = validated_data.get('customerId')
|
||||
|
||||
if (SelectOfferService.validate_account_ownership(account_id = account_id, customer_id = customer_id)):
|
||||
transaction = SelectOfferService.log_transaction(validated_data = validated_data)
|
||||
|
||||
if not transaction:
|
||||
logger.error(f"Failed to log transaction")
|
||||
return jsonify({
|
||||
"message": "Failed to log transaction."
|
||||
}), 400
|
||||
else:
|
||||
return jsonify({
|
||||
"message": "Invalid Customer or Account"
|
||||
}), 400
|
||||
|
||||
offers = [
|
||||
{
|
||||
"offerId": "14451",
|
||||
"productId": "2030",
|
||||
"amount": 10000.0,
|
||||
"upfrontPayment": 1000.0,
|
||||
"interestRate": 3.0,
|
||||
"managementRate": 1.0,
|
||||
"managementFee": 1.0,
|
||||
"insuranceRate": 1.0,
|
||||
"insuranceFee": 100.0,
|
||||
"VATRate": 7.5,
|
||||
"VATAmount": 100.0,
|
||||
"recommendedRepaymentDates": ["2022-11-30"],
|
||||
"installmentAmount": 11000.0,
|
||||
"totalRepaymentAmount": 11000.0
|
||||
}
|
||||
]
|
||||
|
||||
# Business logic - selecting an offer
|
||||
response_data = {
|
||||
"outstandingDebtAmount": 0,
|
||||
"requestId": "202111170001371256908",
|
||||
"transactionId": transaction.id,
|
||||
"customerId": customer_id,
|
||||
"accountId": account_id,
|
||||
"loan": offers,
|
||||
"resultCode": "00",
|
||||
"resultDescription": "Successful"
|
||||
}
|
||||
|
||||
|
||||
return response_data
|
||||
|
||||
except ValidationError as err:
|
||||
|
||||
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
||||
|
||||
return jsonify({
|
||||
"message": "Validation exception"
|
||||
}) , 422
|
||||
|
||||
except ValueError as err:
|
||||
logger.error(f"{getattr(err, 'messages', str(err))}")
|
||||
|
||||
return jsonify({
|
||||
"message": str(err)
|
||||
}) , 400
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
||||
return jsonify({
|
||||
"message": "Internal Server Error"
|
||||
}) , 500
|
||||
Reference in New Issue
Block a user