diff --git a/app/api/services/loan_status.py b/app/api/services/loan_status.py index 6a2c01a..55de9b8 100644 --- a/app/api/services/loan_status.py +++ b/app/api/services/loan_status.py @@ -32,17 +32,24 @@ class LoanStatusService(BaseService): customer_id = validated_data.get('customerId') customer = Customer.get_customer(customer_id) transactionId = validated_data.get('transactionId') + account_id = validated_data.get('accountId') - # Get loans - loans = [loan.to_dict() for loan in customer.loans if loan.status == LoanStatus.ACTIVE] + if(LoanStatusService.validate_account_ownership(account_id = account_id, customer_id = customer_id)): - transaction = LoanStatusService.log_transaction(validated_data = validated_data) + # Get loans + loans = [loan.to_dict() for loan in customer.loans if loan.status == LoanStatus.ACTIVE] - if not transaction: - logger.error(f"Failed to log transaction") + 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": "Failed to log transaction." - }), 400 + "message": "Invalid Customer or Account" + }), 400 # loans = [ diff --git a/app/api/services/repayment.py b/app/api/services/repayment.py index 4d400db..dac4fb0 100644 --- a/app/api/services/repayment.py +++ b/app/api/services/repayment.py @@ -2,6 +2,7 @@ from flask import request, jsonify from marshmallow import ValidationError from app.api.enums.loan_status import LoanStatus from app.models import Repayment +from app.models.customer import Customer from app.models.loan import Loan from app.utils.logger import logger from app.api.schemas.repayment import RepaymentSchema @@ -31,34 +32,41 @@ class RepaymentService(BaseService): request_id = validated_data.get('requestId') loan_id = validated_data.get('debtId') product_id = validated_data.get('productId') - - + account_id = validated_data.get('accountId') + customer = Customer.get_customer(customer_id) - # Save the repayment details - repayment = Repayment.create_repayment( - customer_id = customer_id, - loan_id = loan_id, - product_id = product_id + if(RepaymentService.validate_account_ownership(account_id = account_id, customer_id = customer_id)): - ) + # Save the repayment details + repayment = Repayment.create_repayment( + customer_id = customer_id, + loan_id = loan_id, + product_id = product_id - if not repayment: - logger.error(f"Failed to save repayment details") - return jsonify({ - "message": "Failed to save repayment details." - }), 400 - + ) + + if not repayment: + logger.error(f"Failed to save repayment details") + return jsonify({ + "message": "Failed to save repayment details." + }), 400 - #Update Loan status - Loan.update_status(loan_id = loan_id, status = LoanStatus.REPAID) - - transaction = RepaymentService.log_transaction(validated_data = validated_data) + + #Update Loan status + Loan.update_status(loan_id = loan_id, status = LoanStatus.REPAID) + + transaction = RepaymentService.log_transaction(validated_data = validated_data) - if not transaction: - logger.error(f"Failed to log transaction") + if not transaction: + logger.error(f"Failed to log transaction") + return jsonify({ + "message": "Failed to log transaction." + }), 400 + else: return jsonify({ - "message": "Failed to log transaction." - }), 400 + "message": "Invalid Customer or Account" + }), 400 + # Simulated processing logic diff --git a/app/swagger/schemas/LoanStatusRequest.json b/app/swagger/schemas/LoanStatusRequest.json index 0252f26..43adf6b 100644 --- a/app/swagger/schemas/LoanStatusRequest.json +++ b/app/swagger/schemas/LoanStatusRequest.json @@ -16,6 +16,10 @@ "channel": { "type": "string", "example": "USSD" + }, + "accountId": { + "type": "string", + "example": "ACN8263457" } }, "xml": { diff --git a/app/swagger/schemas/RepaymentRequest.json b/app/swagger/schemas/RepaymentRequest.json index c31d585..9543f74 100644 --- a/app/swagger/schemas/RepaymentRequest.json +++ b/app/swagger/schemas/RepaymentRequest.json @@ -24,6 +24,10 @@ "channel": { "type": "string", "example": "USSD" + }, + "accountId": { + "type": "string", + "example": "ACN8263457" } }, "xml": {