Refactor transaction data handling in LamassuTransactionProcessor: Improved consistency in processing None and empty values during data ingestion. Defaulted numeric fields to 0 and percentage fields to 0.0 for better error handling. Ensured clean extraction of transaction details, enhancing reliability in transaction processing.
This commit is contained in:
parent
4843b43147
commit
d1242e5cd2
1 changed files with 26 additions and 14 deletions
|
|
@ -493,14 +493,26 @@ class LamassuTransactionProcessor:
|
|||
# Convert string values to appropriate types
|
||||
processed_row = {}
|
||||
for key, value in row.items():
|
||||
if value == '':
|
||||
processed_row[key] = None
|
||||
# Handle None/empty values consistently at data ingestion boundary
|
||||
if value == '' or value is None:
|
||||
if key in ['fiat_amount', 'crypto_amount']:
|
||||
processed_row[key] = 0 # Default numeric fields to 0
|
||||
elif key in ['commission_percentage', 'discount']:
|
||||
processed_row[key] = 0.0 # Default percentage fields to 0.0
|
||||
else:
|
||||
processed_row[key] = None # Keep None for non-numeric fields
|
||||
elif key in ['transaction_id', 'device_id', 'crypto_code', 'fiat_code']:
|
||||
processed_row[key] = str(value)
|
||||
elif key in ['fiat_amount', 'crypto_amount']:
|
||||
processed_row[key] = int(float(value)) if value else 0
|
||||
try:
|
||||
processed_row[key] = int(float(value))
|
||||
except (ValueError, TypeError):
|
||||
processed_row[key] = 0 # Fallback to 0 for invalid values
|
||||
elif key in ['commission_percentage', 'discount']:
|
||||
processed_row[key] = float(value) if value else 0.0
|
||||
try:
|
||||
processed_row[key] = float(value)
|
||||
except (ValueError, TypeError):
|
||||
processed_row[key] = 0.0 # Fallback to 0.0 for invalid values
|
||||
elif key == 'transaction_time':
|
||||
from datetime import datetime
|
||||
# Parse PostgreSQL timestamp format and ensure it's in UTC for consistency
|
||||
|
|
@ -628,11 +640,11 @@ class LamassuTransactionProcessor:
|
|||
logger.info("No Flow Mode clients found - skipping distribution")
|
||||
return {}
|
||||
|
||||
# Extract transaction details with None-safe defaults
|
||||
crypto_atoms = transaction.get("crypto_amount") # Total sats with commission baked in
|
||||
fiat_amount = transaction.get("fiat_amount") # Actual fiat dispensed (principal only)
|
||||
commission_percentage = transaction.get("commission_percentage") # Already stored as decimal (e.g., 0.045)
|
||||
discount = transaction.get("discount") # Discount percentage
|
||||
# Extract transaction details - guaranteed clean from data ingestion
|
||||
crypto_atoms = transaction.get("crypto_amount", 0) # Total sats with commission baked in
|
||||
fiat_amount = transaction.get("fiat_amount", 0) # Actual fiat dispensed (principal only)
|
||||
commission_percentage = transaction.get("commission_percentage", 0.0) # Already stored as decimal (e.g., 0.045)
|
||||
discount = transaction.get("discount", 0.0) # Discount percentage
|
||||
transaction_time = transaction.get("transaction_time") # ATM transaction timestamp for temporal accuracy
|
||||
|
||||
# Normalize transaction_time to UTC if present
|
||||
|
|
@ -995,11 +1007,11 @@ class LamassuTransactionProcessor:
|
|||
async def store_lamassu_transaction(self, transaction: Dict[str, Any]) -> Optional[str]:
|
||||
"""Store the Lamassu transaction in our database for audit and UI"""
|
||||
try:
|
||||
# Extract and validate transaction data
|
||||
# Extract transaction data - guaranteed clean from data ingestion boundary
|
||||
crypto_atoms = transaction.get("crypto_amount", 0)
|
||||
fiat_amount = transaction.get("fiat_amount", 0)
|
||||
commission_percentage = transaction.get("commission_percentage") or 0.0
|
||||
discount = transaction.get("discount") or 0.0
|
||||
commission_percentage = transaction.get("commission_percentage", 0.0)
|
||||
discount = transaction.get("discount", 0.0)
|
||||
transaction_time = transaction.get("transaction_time")
|
||||
|
||||
# Normalize transaction_time to UTC if present
|
||||
|
|
@ -1145,8 +1157,8 @@ class LamassuTransactionProcessor:
|
|||
|
||||
# Calculate commission amount for sending to commission wallet
|
||||
crypto_atoms = transaction.get("crypto_amount", 0)
|
||||
commission_percentage = transaction.get("commission_percentage") or 0.0
|
||||
discount = transaction.get("discount") or 0.0
|
||||
commission_percentage = transaction.get("commission_percentage", 0.0)
|
||||
discount = transaction.get("discount", 0.0)
|
||||
|
||||
if commission_percentage and commission_percentage > 0:
|
||||
effective_commission = commission_percentage * (100 - discount) / 100
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue