from flask import request, jsonify from marshmallow import ValidationError from app.utils.logger import logger from app.api.schemas.repayment import RepaymentSchema, RepaymentResponseSchema class RepaymentService: @staticmethod def process_request(data): """ Process the Repayment request. Args: data (dict): The request data. Returns: dict: A standardized response. """ try: logger.info("Processing Repayment request") # Validate input data using RepaymentSchema schema = RepaymentSchema() validated_data = schema.load(data) # Raises ValidationError if invalid # Simulated processing logic # In a real implementation, this would interact with your business logic # to process the repayment # For demonstration, we'll simulate a partial repayment collected_amount = validated_data.get('collectedAmount') * 0.75 # 75% of requested amount lien_amount = validated_data.get('lienAmount') * 0.25 # 25% of requested amount as lien response_data = { "requestId": validated_data.get('requestId'), "countryCode": validated_data.get('countryCode'), "transactionId": validated_data.get('transactionId'), "debtId": validated_data.get('debtId'), "customerId": validated_data.get('customerId'), "accountId": validated_data.get('accountId'), "productId": validated_data.get('productId'), "collectedAmount": collected_amount, "penalCharge": validated_data.get('penalCharge', 0), "lienAmount": lien_amount, "comment": validated_data.get('comment', ""), "resultCode": "00", "resultDescription": "Loan Collection Successful" } # Validate the response using the response schema response_schema = RepaymentResponseSchema() 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