diff --git a/app/api/services/loan_status.py b/app/api/services/loan_status.py index bfe8b68..08d2c2a 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.models import Customer from app.utils.logger import logger from app.api.schemas.loan_status import LoanStatusSchema from app.api.services.base_service import BaseService @@ -25,6 +26,15 @@ class LoanStatusService(BaseService): with db.session.begin(): validated_data = LoanStatusService.validate_data(data, LoanStatusSchema()) 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) @@ -35,23 +45,23 @@ class LoanStatusService(BaseService): }), 400 - loans = [ - { - "debtId": "123456789", - "loanDate": "2019-10-18 14:26:21.063", - "dueDate": "2019-11-20 14:26:21.063", - "currentLoanAmount": 8500, - "initialLoanAmount": 10000, - "defaultPenaltyFee": 0, - "continuousFee": 0, - "productId": "101" - } - ] + # loans = [ + # { + # "debtId": "123456789", + # "loanDate": "2019-10-18 14:26:21.063", + # "dueDate": "2019-11-20 14:26:21.063", + # "currentLoanAmount": 8500, + # "initialLoanAmount": 10000, + # "defaultPenaltyFee": 0, + # "continuousFee": 0, + # "productId": "101" + # } + # ] # Simulated processing logic response_data = { - "customerId": "CN621868", - "transactionId": "Tr201712RK9232P115", + "customerId": customer_id, + "transactionId": transactionId, "loans": loans, "totalDebtAmount": 8500, "resultCode": "00", diff --git a/app/models/customer.py b/app/models/customer.py index c36a11a..7692f5a 100644 --- a/app/models/customer.py +++ b/app/models/customer.py @@ -20,6 +20,13 @@ class Customer(db.Model): back_populates="customer", ) + loans = relationship( + "Loan", + primaryjoin="Customer.id == Loan.customer_id", + foreign_keys="Loan.customer_id", + back_populates="customer", + ) + @classmethod def is_valid_customer(cls, customer_id): customer = cls.query.filter_by(id=customer_id).first() @@ -47,6 +54,17 @@ class Customer(db.Model): except IntegrityError as err: raise ValueError(f"Database integrity error: {err}") 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): return f'' diff --git a/app/models/loan.py b/app/models/loan.py index 252fe74..5f8f1fc 100644 --- a/app/models/loan.py +++ b/app/models/loan.py @@ -3,6 +3,8 @@ from app.extensions import db from app.models.customer import Customer from app.models.account import Account from sqlalchemy.exc import IntegrityError +from sqlalchemy.orm import relationship +from app.models import Customer class Loan(db.Model): @@ -21,6 +23,12 @@ class Loan(db.Model): 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)) + customer = relationship( + "Customer", + primaryjoin="Customer.id == Loan.customer_id", + foreign_keys=[customer_id], + back_populates="loans", + ) @classmethod def create_loan(cls, customer_id, account_id, offer_id, principal_amount, status='pending'):