From 3de0c6a9802d2a29ea36955a866a2f4e4d98ba57 Mon Sep 17 00:00:00 2001 From: VivianDee <115420678+VivianDee@users.noreply.github.com> Date: Mon, 14 Apr 2025 15:17:08 +0100 Subject: [PATCH] [add]: account_id to loan_status and repayment --- app/api/services/loan_status.py | 47 +++++++++------- app/api/services/repayment.py | 63 +++++++++++++--------- app/swagger/schemas/LoanStatusRequest.json | 4 ++ app/swagger/schemas/RepaymentRequest.json | 4 ++ 4 files changed, 74 insertions(+), 44 deletions(-) diff --git a/app/api/services/loan_status.py b/app/api/services/loan_status.py index f80b7f2..afafbc9 100644 --- a/app/api/services/loan_status.py +++ b/app/api/services/loan_status.py @@ -1,5 +1,6 @@ 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 @@ -26,28 +27,33 @@ class LoanStatusService(BaseService): with db.session.begin(): # Validate data validated_data = LoanStatusService.validate_data(data, LoanStatusSchema()) - - + account_id = validated_data.get('accountId') customer_id = validated_data.get('customerId') customer = Customer.get_customer(customer_id) transactionId = validated_data.get('transactionId') - # Get loans - loans = [loan.to_dict() for loan in customer.loans] - - - validated_data['refId'] = customer.id - validated_data['refModel'] = "customer" - - - transaction = LoanStatusService.log_transaction(validated_data = validated_data) - - if not transaction: - logger.error(f"Failed to log transaction") + if not customer: return jsonify({ - "message": "Failed to log transaction." - }), 400 - + "message": "Customer not found." + }), 404 + + 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 jsonify({ + "message": "Failed to log transaction." + }), 400 + else: + return jsonify({ + "message": "Invalid Customer or Account" + }), 400 + # loans = [ # { @@ -62,12 +68,17 @@ class LoanStatusService(BaseService): # } # ] + total_debt_amount = sum( + loan.get("currentLoanAmount") or 0 + for loan in loans + ) + # Simulated processing logic response_data = { "customerId": customer_id, "transactionId": transactionId, "loans": loans, - "totalDebtAmount": 8500, + "totalDebtAmount": total_debt_amount, "resultCode": "00", "resultDescription": "Successful" } diff --git a/app/api/services/repayment.py b/app/api/services/repayment.py index 33be586..cf5736d 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 @@ -27,42 +28,52 @@ class RepaymentService(BaseService): try: with db.session.begin(): validated_data = RepaymentService.validate_data(data, RepaymentSchema()) + account_id = validated_data.get('accountId') customer_id = validated_data.get('customerId') request_id = validated_data.get('requestId') loan_id = validated_data.get('debtId') product_id = validated_data.get('productId') + customer = Customer.get_customer(customer_id) + + if not customer: + return jsonify({ + "message": "Customer not found." + }), 404 - - # 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") + ) + + 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) + + if not transaction: + logger.error(f"Failed to log transaction") + return jsonify({ + "message": "Failed to log transaction." + }), 400 + + else: return jsonify({ - "message": "Failed to save repayment details." - }), 400 + "message": "Invalid Customer or Account" + }), 400 - db.session.flush() - - validated_data['refId'] = repayment.id - validated_data['refModel'] = "repayment" - - #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") - return jsonify({ - "message": "Failed to log transaction." - }), 400 # Simulated processing logic diff --git a/app/swagger/schemas/LoanStatusRequest.json b/app/swagger/schemas/LoanStatusRequest.json index 0252f26..cb62a35 100644 --- a/app/swagger/schemas/LoanStatusRequest.json +++ b/app/swagger/schemas/LoanStatusRequest.json @@ -1,6 +1,10 @@ { "type": "object", "properties": { + "accountId": { + "type": "string", + "example": "ACN8263457" + }, "transactionId": { "type": "string", "example": "Tr201712RK9232P115" diff --git a/app/swagger/schemas/RepaymentRequest.json b/app/swagger/schemas/RepaymentRequest.json index c31d585..c2ef8bd 100644 --- a/app/swagger/schemas/RepaymentRequest.json +++ b/app/swagger/schemas/RepaymentRequest.json @@ -21,6 +21,10 @@ "type": "string", "example": "CID0000025585" }, + "accountId": { + "type": "string", + "example": "ACN8263457" + }, "channel": { "type": "string", "example": "USSD"