forked from DigiFi/digifi-BankToProductCore
78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
from flask import request, jsonify
|
|
from marshmallow import ValidationError
|
|
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
|
|
|
|
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:
|
|
validated_data = RepaymentService.validate_data(data, RepaymentSchema())
|
|
customer_id = validated_data.get('customerId')
|
|
customer = RepaymentService.get_or_create_customer(validated_data)
|
|
account = customer.accounts[0]
|
|
|
|
|
|
if (RepaymentService.validate_account_ownership(account_id = account.id, customer_id = customer_id)):
|
|
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
|
|
else:
|
|
return jsonify({
|
|
"message": "Invalid Customer or Account"
|
|
}), 400
|
|
|
|
# Simulated processing logic
|
|
response_data = {
|
|
"customerId": "CN621868",
|
|
"productId": "101",
|
|
"debtId": "273194670",
|
|
"resultCode": "00",
|
|
"resultDescription": "Successful"
|
|
}
|
|
|
|
# return ResponseHelper.success(
|
|
# data=response_data,
|
|
# message="Repayment processed successfully"
|
|
# )
|
|
|
|
return response_data
|
|
|
|
except ValidationError as err:
|
|
|
|
logger.error(f"Validation Error: {getattr(err, 'messages', str(err))}")
|
|
|
|
return jsonify({
|
|
"message": "Validation exception"
|
|
}) , 422
|
|
|
|
except ValueError as err:
|
|
logger.error(f"{getattr(err, 'messages', str(err))}")
|
|
|
|
return jsonify({
|
|
"message": str(err)
|
|
}) , 400
|
|
|
|
except Exception as e:
|
|
logger.error(f"An error occurred: {str(e)}", exc_info=True)
|
|
return jsonify({
|
|
"message": "Internal Server Error"
|
|
}) , 500
|