Added Transaction offer

This commit is contained in:
Azeez Muibi
2025-05-19 13:24:17 +01:00
parent 636f0f1a95
commit 3ae29b69b5
7 changed files with 446 additions and 22 deletions
+76 -22
View File
@@ -2,9 +2,10 @@ from datetime import datetime, timezone
from app.extensions import db
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from sqlalchemy import and_, or_, not_
import logging
logger = logging.getLogger(__name__)
logger = logging.getLogger(__name__)
class TransactionOffer(db.Model):
@@ -31,36 +32,89 @@ class TransactionOffer(db.Model):
)
@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,
id=transaction_offer,
customer_id=customer_id,
# product_id = product_id
# transaction_id = transaction_id,
).first()
).first()
if not transaction_offer:
return False
return transaction_offer
@classmethod
def get_all_transaction_offers(cls, customer_id=None, transaction_id=None, offer_id=None,
product_id=None, original_transaction=None,
start_date=None, end_date=None, page=1, limit=20):
"""
Get all transaction offers with optional filtering
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,
}
Args:
customer_id (str, optional): Filter by customer ID
transaction_id (str, optional): Filter by transaction ID
offer_id (str, optional): Filter by offer ID
product_id (str, optional): Filter by product ID
original_transaction (str, optional): Filter by original transaction
start_date (datetime, optional): Filter by start date
end_date (datetime, optional): Filter by end date
page (int, optional): Page number for pagination
limit (int, optional): Number of items per page
Returns:
tuple: (List of TransactionOffer objects, total count)
"""
query = cls.query
# Apply filters if provided
if customer_id:
query = query.filter(cls.customer_id == customer_id)
if transaction_id:
query = query.filter(cls.transaction_id == transaction_id)
if offer_id:
query = query.filter(cls.offer_id == offer_id)
if product_id:
query = query.filter(cls.product_id == product_id)
if original_transaction:
query = query.filter(cls.original_transaction == original_transaction)
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())
# Get total count before pagination
total_count = query.count()
# Apply pagination
offset = (page - 1) * limit
query = query.limit(limit).offset(offset)
return query.all(), total_count
# 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}>'
return f'<TransactionOffer {self.id}>'