Added Transaction
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
from datetime import datetime, timezone
|
||||
from app.extensions import db
|
||||
|
||||
class Offer(db.Model):
|
||||
__tablename__ = 'offers'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
product_id = db.Column(db.String, nullable=False)
|
||||
min_amount = db.Column(db.Float, nullable=False)
|
||||
max_amount = db.Column(db.Float, nullable=False)
|
||||
tenor = db.Column(db.Integer, nullable=False)
|
||||
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))
|
||||
|
||||
def __repr__(self):
|
||||
return f'<LoanOffer {self.id}>'
|
||||
@@ -3,6 +3,7 @@ from app.extensions import db
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy import and_, or_, not_
|
||||
|
||||
|
||||
class Transaction(db.Model):
|
||||
__tablename__ = 'transactions'
|
||||
id = db.Column(
|
||||
@@ -10,7 +11,7 @@ class Transaction(db.Model):
|
||||
primary_key=True,
|
||||
autoincrement=True,
|
||||
)
|
||||
#id = db.Column(db.Int, primary_key=True)
|
||||
# id = db.Column(db.Int, primary_key=True)
|
||||
transaction_id = db.Column(db.String(50), nullable=False)
|
||||
account_id = db.Column(db.String(50), nullable=False)
|
||||
type = db.Column(db.String(50), nullable=False)
|
||||
@@ -27,11 +28,9 @@ class Transaction(db.Model):
|
||||
# if cls.query.filter_by(transaction_id=transaction_id).first():
|
||||
# raise ValueError("Duplicate Transaction")
|
||||
|
||||
if cls.query.filter( and_( cls.transaction_id ==transaction_id, cls.type==type) ).first():
|
||||
if cls.query.filter(and_(cls.transaction_id == transaction_id, cls.type == type)).first():
|
||||
raise ValueError("Duplicate Transaction")
|
||||
|
||||
|
||||
|
||||
transaction = cls(
|
||||
transaction_id=transaction_id,
|
||||
account_id=account_id,
|
||||
@@ -50,4 +49,42 @@ class Transaction(db.Model):
|
||||
|
||||
@classmethod
|
||||
def get_transaction_by_id(cls, transaction_id):
|
||||
return cls.query.get(transaction_id)
|
||||
return cls.query.get(transaction_id)
|
||||
|
||||
@classmethod
|
||||
def get_all_transactions(cls, account_id=None, transaction_type=None, channel=None, start_date=None, end_date=None):
|
||||
"""
|
||||
Get all transactions with optional filtering
|
||||
|
||||
Args:
|
||||
account_id (str, optional): Filter by account ID
|
||||
transaction_type (str, optional): Filter by transaction type
|
||||
channel (str, optional): Filter by channel
|
||||
start_date (datetime, optional): Filter by start date
|
||||
end_date (datetime, optional): Filter by end date
|
||||
|
||||
Returns:
|
||||
list: List of Transaction objects
|
||||
"""
|
||||
query = cls.query
|
||||
|
||||
# Apply filters if provided
|
||||
if account_id:
|
||||
query = query.filter(cls.account_id == account_id)
|
||||
|
||||
if transaction_type:
|
||||
query = query.filter(cls.type == transaction_type)
|
||||
|
||||
if channel:
|
||||
query = query.filter(cls.channel == channel)
|
||||
|
||||
if start_date:
|
||||
query = query.filter(cls.created_at >= start_date)
|
||||
|
||||
if end_date:
|
||||
query = query.filter(cls.created_at <= end_date)
|
||||
|
||||
# Order by created_at descending (newest first)
|
||||
query = query.order_by(cls.created_at.desc())
|
||||
|
||||
return query.all()
|
||||
|
||||
Reference in New Issue
Block a user