[add]: loan status

This commit is contained in:
VivianDee
2025-04-11 00:02:35 +01:00
parent d397c834f4
commit 9f9512b060
3 changed files with 50 additions and 14 deletions
+24 -14
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.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
from app.api.services.base_service import BaseService from app.api.services.base_service import BaseService
@@ -25,6 +26,15 @@ class LoanStatusService(BaseService):
with db.session.begin(): with db.session.begin():
validated_data = LoanStatusService.validate_data(data, LoanStatusSchema()) validated_data = LoanStatusService.validate_data(data, LoanStatusSchema())
customer_id = validated_data.get('customerId') customer_id = validated_data.get('customerId')
customer = Customer.get_customer(customer_id)
transactionId = validated_data.get('transactionId')
loans = customer.loans
db.session.flush()
validated_data['refId'] = customer.id
validated_data['refModel'] = "customer"
transaction = LoanStatusService.log_transaction(validated_data = validated_data) transaction = LoanStatusService.log_transaction(validated_data = validated_data)
@@ -35,23 +45,23 @@ class LoanStatusService(BaseService):
}), 400 }), 400
loans = [ # loans = [
{ # {
"debtId": "123456789", # "debtId": "123456789",
"loanDate": "2019-10-18 14:26:21.063", # "loanDate": "2019-10-18 14:26:21.063",
"dueDate": "2019-11-20 14:26:21.063", # "dueDate": "2019-11-20 14:26:21.063",
"currentLoanAmount": 8500, # "currentLoanAmount": 8500,
"initialLoanAmount": 10000, # "initialLoanAmount": 10000,
"defaultPenaltyFee": 0, # "defaultPenaltyFee": 0,
"continuousFee": 0, # "continuousFee": 0,
"productId": "101" # "productId": "101"
} # }
] # ]
# Simulated processing logic # Simulated processing logic
response_data = { response_data = {
"customerId": "CN621868", "customerId": customer_id,
"transactionId": "Tr201712RK9232P115", "transactionId": transactionId,
"loans": loans, "loans": loans,
"totalDebtAmount": 8500, "totalDebtAmount": 8500,
"resultCode": "00", "resultCode": "00",
+18
View File
@@ -20,6 +20,13 @@ class Customer(db.Model):
back_populates="customer", back_populates="customer",
) )
loans = relationship(
"Loan",
primaryjoin="Customer.id == Loan.customer_id",
foreign_keys="Loan.customer_id",
back_populates="customer",
)
@classmethod @classmethod
def is_valid_customer(cls, customer_id): def is_valid_customer(cls, customer_id):
customer = cls.query.filter_by(id=customer_id).first() customer = cls.query.filter_by(id=customer_id).first()
@@ -47,6 +54,17 @@ class Customer(db.Model):
except IntegrityError as err: except IntegrityError as err:
raise ValueError(f"Database integrity error: {err}") raise ValueError(f"Database integrity error: {err}")
return customer return customer
@classmethod
def get_customer(cls, customer_id):
"""
Get customer by ID.
"""
customer = cls.query.filter_by(id=customer_id).first()
if not customer:
raise ValueError(f"Customer does not exist")
return customer
def __repr__(self): def __repr__(self):
return f'<Customer {self.id}>' return f'<Customer {self.id}>'
+8
View File
@@ -3,6 +3,8 @@ from app.extensions import db
from app.models.customer import Customer from app.models.customer import Customer
from app.models.account import Account from app.models.account import Account
from sqlalchemy.exc import IntegrityError from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import relationship
from app.models import Customer
class Loan(db.Model): class Loan(db.Model):
@@ -21,6 +23,12 @@ class Loan(db.Model):
created_at = db.Column(db.DateTime, default=datetime.now(timezone.utc)) created_at = db.Column(db.DateTime, default=datetime.now(timezone.utc))
updated_at = db.Column(db.DateTime, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc)) updated_at = db.Column(db.DateTime, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc))
customer = relationship(
"Customer",
primaryjoin="Customer.id == Loan.customer_id",
foreign_keys=[customer_id],
back_populates="loans",
)
@classmethod @classmethod
def create_loan(cls, customer_id, account_id, offer_id, principal_amount, status='pending'): def create_loan(cls, customer_id, account_id, offer_id, principal_amount, status='pending'):