from flask import request, jsonify from marshmallow import ValidationError from app.utils.logger import logger from app.api.schemas.loan_status import LoanStatusSchema, LoanStatusResponseSchema from datetime import datetime, timedelta class LoanStatusService: @staticmethod def process_request(data): """ Process the LoanStatus request. Args: data (dict): The request data. Returns: dict: A standardized response. """ try: logger.info("Processing LoanStatus request") # Validate input data using LoanStatusSchema schema = LoanStatusSchema() validated_data = schema.load(data) # Raises ValidationError if invalid # Simulated processing logic # In a real implementation, this would interact with your business logic # to retrieve the customer's loan status # For demonstration, we'll create a sample response with one loan # In a real implementation, you would query your database for actual loan data current_date = datetime.now() loan_date = (current_date - timedelta(days=30)).strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] due_date = (current_date + timedelta(days=30)).strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] # Sample loan data loan_data = { "debtId": "123456789", "loanDate": loan_date, "dueDate": due_date, "currentLoanAmount": 8500.0, "initialLoanAmount": 10000.0, "defaultPenaltyFee": 0.0, "continuousFee": 0.0, "productId": "101" } # Create response with the loan data response_data = { "customerId": validated_data.get('customerId'), "transactionId": validated_data.get('transactionId'), "loans": [loan_data], # Array with one loan "totalDebtAmount": 8500.0, # Same as currentLoanAmount for this example "resultCode": "00", "resultDescription": "Successful" } # Validate the response using the response schema response_schema = LoanStatusResponseSchema() validated_response = response_schema.dump(response_data) return jsonify(validated_response) except ValidationError as err: logger.error(f"Validation Error: {err.messages}") return jsonify({ "resultCode": "01", "resultDescription": f"Validation error: {err.messages}" }), 422 except Exception as e: logger.error(f"An error occurred: {str(e)}", exc_info=True) return jsonify({ "resultCode": "08", "resultDescription": f"Error occurred: {str(e)}" }), 500