81 lines
3.2 KiB
Python
81 lines
3.2 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 import LoanStatusSchema
|
|
from app.api.services.base_service import BaseService
|
|
from app.api.enums import TransactionType
|
|
from app.extensions import db
|
|
from app.api.helpers.response_helper import ResponseHelper
|
|
|
|
|
|
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')
|
|
|
|
logger.info(f"Looking for customer *** {customer_id}")
|
|
customer = Customer.get_customer_with_loan_list(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 ResponseHelper.error(result_description="Failed to log transaction.")
|
|
else:
|
|
return ResponseHelper.error(result_description="Invalid Customer or Account")
|
|
|
|
total_debt_amount = sum(
|
|
loan.get("currentLoanAmount") or 0
|
|
for loan in loans
|
|
)
|
|
|
|
# Simulated processing logic
|
|
response_data = {
|
|
"customerId": customer_id,
|
|
"accountId": account_id,
|
|
"transactionId": transactionId,
|
|
"loans": loans,
|
|
"totalDebtAmount": total_debt_amount,
|
|
}
|
|
|
|
db.session.commit()
|
|
return ResponseHelper.success(data=response_data)
|
|
|
|
except ValidationError as err:
|
|
|
|
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
|
db.session.rollback()
|
|
return ResponseHelper.unprocessable_entity(result_description="Validation exception")
|
|
|
|
except ValueError as err:
|
|
logger.error(f"{getattr(err, 'messages', str(err))}")
|
|
db.session.rollback()
|
|
return ResponseHelper.error(result_description=str(err))
|
|
|
|
except Exception as e:
|
|
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
|
db.session.rollback()
|
|
return ResponseHelper.internal_server_error() |