From 3602599f4a47405cfef11b598f29f1e660bac0c9 Mon Sep 17 00:00:00 2001 From: "CHIEFSOFT\\ameye" Date: Sun, 2 Nov 2025 17:42:56 -0500 Subject: [PATCH] Cfeate transactions --- app/integrations/simbrella.py | 8 ++++++++ app/models/transactions.py | 26 ++++++++++++++++++++++++++ app/services/transactions.py | 8 +++++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/app/integrations/simbrella.py b/app/integrations/simbrella.py index eb9de23..6aa6f4e 100644 --- a/app/integrations/simbrella.py +++ b/app/integrations/simbrella.py @@ -233,7 +233,15 @@ class SimbrellaClient: "text": f"Transaction {loan_data.get('transactionId')} verified successfully", "unicode": True } + try: + TransactionService.create_transaction(loan_data['transactionId'], loan_data['accountId'], loan_data['customerId'], "send_sms", "USSD") + except Exception as e: + logger.info(f"Failed to LOG SMS Transaction Record: {e}") + + + try: + sms_response = requests.post(sms_url, json=sms_data, timeout=10, headers=get_headers()) sms_response.raise_for_status() # Raise an exception for 4xx or 5xx status codes diff --git a/app/models/transactions.py b/app/models/transactions.py index 6d352af..0cc463f 100644 --- a/app/models/transactions.py +++ b/app/models/transactions.py @@ -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'' diff --git a/app/services/transactions.py b/app/services/transactions.py index 36dd3a3..ff42c33 100644 --- a/app/services/transactions.py +++ b/app/services/transactions.py @@ -8,4 +8,10 @@ class TransactionService: Get the transaction by ID """ return Transaction.get_transaction_by_transaction_id(transaction_id) - \ No newline at end of file + + @staticmethod + def create_transaction(transaction_id, account_id, customer_id, type, channel): + """ + Create Transaction Entry + """ + return Transaction.create_transaction(transaction_id, account_id, customer_id, type, channel)