00 Add currency conversion utilities and update models for GTQ handling: Introduced currency conversion functions for GTQ and centavos, updated API and database models to handle GTQ amounts directly, and modified API endpoints to streamline deposit and balance summary responses. Adjusted frontend to reflect changes in currency representation, ensuring consistency across the application.
This commit is contained in:
parent
7b40fcef21
commit
aa71321c84
5 changed files with 127 additions and 30 deletions
45
currency_utils.py
Normal file
45
currency_utils.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# Currency conversion utilities for API boundary
|
||||
from decimal import Decimal
|
||||
from typing import Union
|
||||
|
||||
|
||||
def gtq_to_centavos(gtq_amount: Union[float, int, str]) -> int:
|
||||
"""Convert GTQ to centavos for database storage"""
|
||||
return int(Decimal(str(gtq_amount)) * 100)
|
||||
|
||||
|
||||
def centavos_to_gtq(centavos: int) -> float:
|
||||
"""Convert centavos to GTQ for API responses"""
|
||||
return float(centavos) / 100
|
||||
|
||||
|
||||
def format_gtq_currency(centavos: int) -> str:
|
||||
"""Format centavos as GTQ currency string"""
|
||||
gtq_amount = centavos_to_gtq(centavos)
|
||||
return f"Q{gtq_amount:.2f}"
|
||||
|
||||
|
||||
# Conversion helpers for API responses
|
||||
def deposit_db_to_api(deposit_db) -> dict:
|
||||
"""Convert database deposit model to API response"""
|
||||
return {
|
||||
"id": deposit_db.id,
|
||||
"client_id": deposit_db.client_id,
|
||||
"amount_gtq": centavos_to_gtq(deposit_db.amount),
|
||||
"currency": deposit_db.currency,
|
||||
"status": deposit_db.status,
|
||||
"notes": deposit_db.notes,
|
||||
"created_at": deposit_db.created_at,
|
||||
"confirmed_at": deposit_db.confirmed_at
|
||||
}
|
||||
|
||||
|
||||
def balance_summary_db_to_api(balance_db) -> dict:
|
||||
"""Convert database balance summary to API response"""
|
||||
return {
|
||||
"client_id": balance_db.client_id,
|
||||
"total_deposits_gtq": centavos_to_gtq(balance_db.total_deposits),
|
||||
"total_payments_gtq": centavos_to_gtq(balance_db.total_payments),
|
||||
"remaining_balance_gtq": centavos_to_gtq(balance_db.remaining_balance),
|
||||
"currency": balance_db.currency
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue