104 lines
4.6 KiB
Python
104 lines
4.6 KiB
Python
import random
|
|
from flask import request, jsonify
|
|
from marshmallow import ValidationError
|
|
from app.utils.logger import logger
|
|
from app.api.helpers.response_helper import ResponseHelper
|
|
from app.api.schemas.collect_loan import CollectLoanSchema, CollectLoanResponseSchema
|
|
from app.config import Config
|
|
|
|
"""
|
|
Process the CollectLoan request.
|
|
|
|
Args:
|
|
data (dict): The request data.
|
|
|
|
Returns:
|
|
tuple: JSON response and status code.
|
|
"""
|
|
class CollectLoanService:
|
|
@staticmethod
|
|
def process_request(data):
|
|
try:
|
|
logger.info("Processing CollectLoan request")
|
|
|
|
# Validate input data using CollectLoanSchema
|
|
schema = CollectLoanSchema()
|
|
validated_data = schema.load(data)
|
|
|
|
amountForCollection = validated_data.get("collectAmount")
|
|
productId = validated_data.get("productId")
|
|
customerId = validated_data.get("customerId")
|
|
amountCollected = amountForCollection
|
|
responseDescr= "Loan Collection Successful EMULATOR"
|
|
fullDescription= "Loan collection completed successfully EMULATOR"
|
|
responseMessage= "Loan collection completed successfully EMULATOR"
|
|
interestCollected = amountForCollection * 0.03
|
|
lienAmount = validated_data.get("lienAmount", 0)
|
|
|
|
isValid = not (
|
|
int(customerId[-1]) in [2, 7, 9]
|
|
)
|
|
if Config.MIN_AMOUNT_FOR_COLLECTION <= amountForCollection <= Config.MAX_AMOUNT_FOR_COLLECTION:
|
|
amountCollected = amountForCollection if lienAmount >= amountForCollection else 0
|
|
responseDescr = "Partial Loan Collection Successful EMULATOR"
|
|
fullDescription = "Partial Loan collection completed successfully EMULATOR"
|
|
responseMessage = "Partial Loan collection completed successfully EMULATOR"
|
|
|
|
response_data = {
|
|
"transactionId": validated_data.get("transactionId"),
|
|
"fbnTransactionId": validated_data.get("fbnTransactionId"),
|
|
"debtId": validated_data.get("debtId"),
|
|
"customerId": validated_data.get("customerId"),
|
|
"accountId": validated_data.get("accountId"),
|
|
"productId": validated_data.get("productId"),
|
|
"amountCollected": amountCollected,
|
|
"interestCollected": interestCollected,
|
|
"penalChargeCollected": 0,
|
|
"lienAmount": lienAmount if amountCollected == 0 else 0,
|
|
"countryId": validated_data.get("countryId"),
|
|
"comment": validated_data.get("comment", "Testing CollectionLoanRequest EMULATOR"),
|
|
"responseCode": "00",
|
|
"responseDescr": responseDescr,
|
|
"fullDescription": fullDescription,
|
|
"responseMessage": responseMessage
|
|
}
|
|
|
|
else:
|
|
response_data = {
|
|
"transactionId": validated_data.get("transactionId"),
|
|
"fbnTransactionId": validated_data.get("fbnTransactionId"),
|
|
"debtId": validated_data.get("debtId"),
|
|
"customerId": validated_data.get("customerId"),
|
|
"accountId": validated_data.get("accountId"),
|
|
"productId": validated_data.get("productId"),
|
|
"amountCollected": amountCollected,
|
|
"interestCollected": interestCollected,
|
|
"penalChargeCollected": 0,
|
|
"amountCollected": amountCollected,
|
|
"countryId": validated_data.get("countryId"),
|
|
"comment": validated_data.get("comment", "Testing CollectionLoanRequest EMULATOR"),
|
|
"responseCode": "00",
|
|
"responseDescr": responseDescr,
|
|
"fullDescription": fullDescription,
|
|
"responseMessage": responseMessage
|
|
}
|
|
|
|
# Validate and serialize the response data
|
|
response_schema = CollectLoanResponseSchema()
|
|
result = response_schema.dump(response_data)
|
|
|
|
return jsonify(result), 200
|
|
|
|
except ValidationError as err:
|
|
logger.error(f"Validation Error: {err.messages}")
|
|
return jsonify({
|
|
"message": "Validation exception",
|
|
"errors": err.messages
|
|
}) , 422
|
|
|
|
except Exception as e:
|
|
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
|
return jsonify({
|
|
"message": "Internal Server Error"
|
|
}) , 500
|