[add]: account_id to loan_status and repayment

This commit is contained in:
VivianDee
2025-04-14 15:17:08 +01:00
parent 5e49b4bb35
commit 3de0c6a980
4 changed files with 74 additions and 44 deletions
+29 -18
View File
@@ -1,5 +1,6 @@
from flask import request, jsonify from flask import request, jsonify
from marshmallow import ValidationError from marshmallow import ValidationError
from app.api.enums.loan_status import LoanStatus
from app.models import Customer from app.models import Customer
from app.utils.logger import logger from app.utils.logger import logger
from app.api.schemas.loan_status import LoanStatusSchema from app.api.schemas.loan_status import LoanStatusSchema
@@ -26,28 +27,33 @@ class LoanStatusService(BaseService):
with db.session.begin(): with db.session.begin():
# Validate data # Validate data
validated_data = LoanStatusService.validate_data(data, LoanStatusSchema()) validated_data = LoanStatusService.validate_data(data, LoanStatusSchema())
account_id = validated_data.get('accountId')
customer_id = validated_data.get('customerId') customer_id = validated_data.get('customerId')
customer = Customer.get_customer(customer_id) customer = Customer.get_customer(customer_id)
transactionId = validated_data.get('transactionId') transactionId = validated_data.get('transactionId')
# Get loans if not customer:
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")
return jsonify({ return jsonify({
"message": "Failed to log transaction." "message": "Customer not found."
}), 400 }), 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 = [ # loans = [
# { # {
@@ -62,12 +68,17 @@ class LoanStatusService(BaseService):
# } # }
# ] # ]
total_debt_amount = sum(
loan.get("currentLoanAmount") or 0
for loan in loans
)
# Simulated processing logic # Simulated processing logic
response_data = { response_data = {
"customerId": customer_id, "customerId": customer_id,
"transactionId": transactionId, "transactionId": transactionId,
"loans": loans, "loans": loans,
"totalDebtAmount": 8500, "totalDebtAmount": total_debt_amount,
"resultCode": "00", "resultCode": "00",
"resultDescription": "Successful" "resultDescription": "Successful"
} }
+37 -26
View File
@@ -2,6 +2,7 @@ from flask import request, jsonify
from marshmallow import ValidationError from marshmallow import ValidationError
from app.api.enums.loan_status import LoanStatus from app.api.enums.loan_status import LoanStatus
from app.models import Repayment from app.models import Repayment
from app.models.customer import Customer
from app.models.loan import Loan from app.models.loan import Loan
from app.utils.logger import logger from app.utils.logger import logger
from app.api.schemas.repayment import RepaymentSchema from app.api.schemas.repayment import RepaymentSchema
@@ -27,42 +28,52 @@ class RepaymentService(BaseService):
try: try:
with db.session.begin(): with db.session.begin():
validated_data = RepaymentService.validate_data(data, RepaymentSchema()) validated_data = RepaymentService.validate_data(data, RepaymentSchema())
account_id = validated_data.get('accountId')
customer_id = validated_data.get('customerId') customer_id = validated_data.get('customerId')
request_id = validated_data.get('requestId') request_id = validated_data.get('requestId')
loan_id = validated_data.get('debtId') loan_id = validated_data.get('debtId')
product_id = validated_data.get('productId') product_id = validated_data.get('productId')
customer = Customer.get_customer(customer_id)
if not customer:
return jsonify({
"message": "Customer not found."
}), 404
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
) # 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({ return jsonify({
"message": "Failed to save repayment details." "message": "Invalid Customer or Account"
}), 400 }), 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 # Simulated processing logic
@@ -1,6 +1,10 @@
{ {
"type": "object", "type": "object",
"properties": { "properties": {
"accountId": {
"type": "string",
"example": "ACN8263457"
},
"transactionId": { "transactionId": {
"type": "string", "type": "string",
"example": "Tr201712RK9232P115" "example": "Tr201712RK9232P115"
@@ -21,6 +21,10 @@
"type": "string", "type": "string",
"example": "CID0000025585" "example": "CID0000025585"
}, },
"accountId": {
"type": "string",
"example": "ACN8263457"
},
"channel": { "channel": {
"type": "string", "type": "string",
"example": "USSD" "example": "USSD"