forked from DigiFi/digifi-BankToProductCore
[add]: account_id to loan_status and repayment
This commit is contained in:
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"accountId": {
|
||||
"type": "string",
|
||||
"example": "ACN8263457"
|
||||
},
|
||||
"transactionId": {
|
||||
"type": "string",
|
||||
"example": "Tr201712RK9232P115"
|
||||
|
||||
@@ -21,6 +21,10 @@
|
||||
"type": "string",
|
||||
"example": "CID0000025585"
|
||||
},
|
||||
"accountId": {
|
||||
"type": "string",
|
||||
"example": "ACN8263457"
|
||||
},
|
||||
"channel": {
|
||||
"type": "string",
|
||||
"example": "USSD"
|
||||
|
||||
Reference in New Issue
Block a user