112 lines
3.7 KiB
Python
112 lines
3.7 KiB
Python
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
|
|
from app.extensions import db
|
|
|
|
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:
|
|
with db.session.begin():
|
|
validated_data = RepaymentService.validate_data(data, RepaymentSchema())
|
|
customer_id = validated_data.get('customerId')
|
|
request_id = validated_data.get('requestId')
|
|
loan_id = validated_data.get('debtId')
|
|
product_id = validated_data.get('productId')
|
|
|
|
|
|
|
|
# Save the repayment details
|
|
repayment = Repayment.create_repayment(
|
|
customer_id = customer_id,
|
|
loan_id = loan_id,
|
|
product_id = product_id
|
|
|
|
)
|
|
|
|
if not repayment:
|
|
logger.error(f"Failed to save repayment details")
|
|
return jsonify({
|
|
"message": "Failed to save repayment details."
|
|
}), 400
|
|
|
|
db.session.flush()
|
|
|
|
validated_data['refId'] = repayment.id
|
|
validated_data['refModel'] = "repayment"
|
|
|
|
#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
|
|
|
|
|
|
# Simulated processing logic
|
|
response_data = {
|
|
"customerId": customer_id,
|
|
"productId": product_id,
|
|
"debtId": loan_id,
|
|
"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()
|
|
|
|
db.session.commit()
|
|
return response_data
|
|
|
|
except ValidationError as err:
|
|
|
|
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
|
db.session.rollback()
|
|
|
|
return jsonify({
|
|
"message": "Validation exception"
|
|
}) , 422
|
|
|
|
except ValueError as err:
|
|
logger.error(f"{getattr(err, 'messages', str(err))}")
|
|
db.session.rollback()
|
|
|
|
return jsonify({
|
|
"message": str(err)
|
|
}) , 400
|
|
|
|
except Exception as e:
|
|
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
|
db.session.rollback()
|
|
return jsonify({
|
|
"message": "Internal Server Error"
|
|
}) , 500
|