81 lines
3.0 KiB
Python
81 lines
3.0 KiB
Python
from datetime import datetime, timezone
|
|
from app.extensions import db
|
|
from sqlalchemy.orm import relationship
|
|
|
|
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, 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 == TransactionOffer.customer_id",
|
|
foreign_keys=[customer_id],
|
|
back_populates="transaction_offers",
|
|
)
|
|
|
|
@classmethod
|
|
def is_valid_transaction_offer(cls, offer_id, customer_id, product_id):
|
|
transaction_offer = cls.query.filter_by(
|
|
id = str(offer_id),
|
|
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
|
|
)
|
|
|
|
db.session.add(transaction_offer)
|
|
db.session.flush()
|
|
|
|
return transaction_offer
|
|
|
|
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}>'
|