116 lines
4.0 KiB
Python
116 lines
4.0 KiB
Python
from datetime import datetime, timezone, timedelta
|
|
from app.api.enums.loan_status import LoanStatus
|
|
from app.extensions import db
|
|
from sqlalchemy.orm import relationship
|
|
from sqlalchemy.sql import func
|
|
import logging
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
class TransactionOffer(db.Model):
|
|
__tablename__ = 'transaction_offers'
|
|
|
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
|
customer_id = db.Column(db.String(50), nullable=False)
|
|
transaction_id = db.Column(db.String(50), nullable=False)
|
|
original_transaction = db.Column(db.String(50), nullable=True)
|
|
offer_id = db.Column(db.String(20), nullable=False)
|
|
product_id = db.Column(db.String(20), nullable=True)
|
|
min_amount = db.Column(db.Float, nullable=False)
|
|
max_amount = db.Column(db.Float, nullable=False)
|
|
eligible_amount = db.Column(db.Float, nullable=True)
|
|
tenor = db.Column(db.Integer, nullable=True) # tenor in months, typically
|
|
|
|
created_at = db.Column(db.DateTime(timezone=True), server_default=func.now())
|
|
updated_at = db.Column(db.DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
|
|
# customer = relationship(
|
|
# "Customer",
|
|
# primaryjoin="Customer.id == TransactionOffer.customer_id",
|
|
# foreign_keys=[customer_id],
|
|
# back_populates="transaction_offers",
|
|
# )
|
|
|
|
@classmethod
|
|
def is_valid_transaction_offer(cls, transaction_offer, customer_id, product_id):
|
|
transaction_offer = cls.query.filter_by(
|
|
id = transaction_offer,
|
|
customer_id = customer_id,
|
|
# product_id = product_id
|
|
# transaction_id = transaction_id,
|
|
).first()
|
|
|
|
|
|
if not transaction_offer:
|
|
return False
|
|
return transaction_offer
|
|
|
|
@classmethod
|
|
def create_transaction_offer(cls, customer_id, transaction_id, original_transaction, offer_id, min_amount, max_amount, eligible_amount=None, product_id=None, tenor=None):
|
|
"""
|
|
Class method to create and save a TransactionOffer.
|
|
"""
|
|
transaction_offer = cls(
|
|
customer_id=customer_id,
|
|
transaction_id=transaction_id,
|
|
original_transaction=original_transaction,
|
|
offer_id=offer_id,
|
|
min_amount=min_amount,
|
|
max_amount=max_amount,
|
|
eligible_amount=eligible_amount,
|
|
product_id=product_id,
|
|
tenor=tenor,
|
|
created_at=datetime.now(timezone.utc),
|
|
updated_at=datetime.now(timezone.utc)
|
|
)
|
|
|
|
db.session.add(transaction_offer)
|
|
db.session.flush()
|
|
|
|
return transaction_offer
|
|
|
|
@classmethod
|
|
def get_lifetime_loan_count(cls, customer_id):
|
|
"""
|
|
Returns the total number of loans ever created for a customer.
|
|
"""
|
|
return cls.query.filter_by(customer_id=customer_id).count()
|
|
|
|
|
|
@classmethod
|
|
def get_latest_transaction_offer(cls, customer_id):
|
|
"""
|
|
Returns the most recent transaction offer for the given customer based on creation time.
|
|
"""
|
|
return cls.query.filter_by(customer_id=customer_id) \
|
|
.order_by(cls.created_at.desc()) \
|
|
.first()
|
|
|
|
@classmethod
|
|
def get_transaction_offer(cls, transaction_offer_id):
|
|
"""
|
|
Returns a transaction offer by its ID.
|
|
"""
|
|
return cls.query.get(transaction_offer_id)
|
|
|
|
|
|
|
|
|
|
def to_dict(self):
|
|
return {
|
|
'id': self.id,
|
|
'customerId': self.customer_id,
|
|
'transactionId': self.transaction_id,
|
|
'offerId': self.offer_id,
|
|
'productId': self.product_id,
|
|
'minAmount': self.min_amount,
|
|
'maxAmount': self.max_amount,
|
|
'eligibleAmount': self.eligible_amount,
|
|
'tenor': self.tenor,
|
|
'createdAt': self.created_at.isoformat() if self.created_at else None,
|
|
'updatedAt': self.updated_at.isoformat() if self.updated_at else None,
|
|
}
|
|
|
|
def __repr__(self):
|
|
return f'<TransactionOffer {self.id}>'
|