68 lines
2.4 KiB
Python
68 lines
2.4 KiB
Python
from app.extensions import db
|
|
from datetime import datetime, timezone
|
|
|
|
from app.utils.logger import logger
|
|
from sqlalchemy import and_, or_, not_
|
|
|
|
class Transaction(db.Model):
|
|
__tablename__ = "transactions"
|
|
|
|
id = db.Column(
|
|
db.Integer,
|
|
primary_key=True,
|
|
autoincrement=True,
|
|
)
|
|
transaction_id = db.Column(db.String(50), nullable=False)
|
|
account_id = db.Column(db.String(50), nullable=True)
|
|
customer_id = db.Column(db.String(50), nullable=True)
|
|
type = db.Column(db.String(50), nullable=False)
|
|
channel = db.Column(db.String(50), nullable=False)
|
|
phone_number = db.Column(db.String(50), nullable=True)
|
|
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))
|
|
|
|
@classmethod
|
|
def create_transaction(cls, transaction_id, account_id, customer_id, type, channel):
|
|
logger.error(f"**Setting Transaction {transaction_id} for Type {type}")
|
|
if cls.query.filter( and_( cls.transaction_id ==transaction_id, cls.type==type) ).first():
|
|
logger.error(f"Transaction already exists for {type}")
|
|
return '' # dont raise - do not crash beacause of this
|
|
|
|
transaction = cls(
|
|
transaction_id = transaction_id,
|
|
customer_id = customer_id,
|
|
account_id = account_id,
|
|
type = type,
|
|
channel = channel,
|
|
created_at=datetime.now(timezone.utc),
|
|
updated_at=datetime.now(timezone.utc)
|
|
)
|
|
|
|
try:
|
|
db.session.add(transaction)
|
|
db.session.commit()
|
|
except IntegrityError as err:
|
|
raise ValueError(f"Database integrity error: {err}")
|
|
|
|
return transaction
|
|
|
|
def __repr__(self):
|
|
return f'<Transaction {self.id}>'
|
|
|
|
def to_dict(self):
|
|
"""
|
|
Convert the Transaction object to a dictionary format for JSON serialization.
|
|
"""
|
|
return {
|
|
'id': self.id,
|
|
'transaction_id': self.transaction_id,
|
|
'account_id': self.account_id,
|
|
'customer_id': self.customer_id,
|
|
'phone_number':self.phone_number,
|
|
'type': self.type,
|
|
'channel': self.channel,
|
|
}
|
|
|
|
@classmethod
|
|
def get_transaction_by_transaction_id(cls, transaction_id):
|
|
return cls.query.filter_by(transaction_id=transaction_id).first() |