Cfeate transactions
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
from app.extensions import db
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from app.utils.logger import logger
|
||||
|
||||
class Transaction(db.Model):
|
||||
__tablename__ = "transactions"
|
||||
|
||||
@@ -18,6 +20,30 @@ class Transaction(db.Model):
|
||||
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):
|
||||
|
||||
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)
|
||||
except IntegrityError as err:
|
||||
raise ValueError(f"Database integrity error: {err}")
|
||||
|
||||
return transaction
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Transaction {self.id}>'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user