108 lines
3.7 KiB
Python
108 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 Customer
|
|
from app.utils.logger import logger
|
|
from app.api.schemas.loan_status_schema import LoanStatusSchema
|
|
from app.api.services.base_service import BaseService
|
|
from app.api.enums import TransactionType
|
|
from app.extensions import db
|
|
|
|
|
|
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:
|
|
with db.session.begin():
|
|
# Validate data
|
|
validated_data = LoanStatusService.validate_data(data, LoanStatusSchema())
|
|
|
|
|
|
customer_id = validated_data.get('customerId')
|
|
customer = Customer.get_customer(customer_id)
|
|
transactionId = validated_data.get('transactionId')
|
|
account_id = validated_data.get('accountId')
|
|
|
|
if(LoanStatusService.validate_account_ownership(account_id = account_id, customer_id = customer_id)):
|
|
|
|
# Get loans
|
|
loans = [loan.to_dict() for loan in customer.loans if loan.status == LoanStatus.ACTIVE]
|
|
|
|
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"
|
|
# }
|
|
# ]
|
|
|
|
total_debt_amount = sum(
|
|
loan.get("currentLoanAmount") or 0
|
|
for loan in loans
|
|
)
|
|
|
|
# Simulated processing logic
|
|
response_data = {
|
|
"customerId": customer_id,
|
|
"transactionId": transactionId,
|
|
"loans": loans,
|
|
"totalDebtAmount": total_debt_amount,
|
|
"resultCode": "00",
|
|
"resultDescription": "Successful"
|
|
}
|
|
|
|
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 |