Files
plantillas-proyectos/backend/api/v1/modules/a76/exchange_rate/services.py
acazares 962f43fe62 Refactor and enhance CRUD operations for Seal, Trailer, Transporter, Vehicle, and Customs Broker modules
- Updated SealService to support tenant and company filtering with pagination and enhanced CRUD methods.
- Refactored TrailerService to include tenant and company support, added filtering capabilities, and improved CRUD methods.
- Introduced TenantCRUDRoutes for Trailer and Transporter routes to streamline API endpoint creation and management.
- Enhanced TransporterService with tenant and company filtering, pagination, and improved CRUD operations.
- Added Customs Broker module with DTOs, models, services, and routes for managing customs broker data.
- Implemented CRUD operations for Customs Broker, including personnel and VU management.
- Improved data validation and descriptions in DTOs for better API documentation.
2025-11-11 18:20:39 -06:00

113 lines
3.5 KiB
Python

from typing import Optional, Tuple, List, Dict, Any
from sqlalchemy.orm import Session
from . import dto, models
class ExchangeRateService:
"""Service for ExchangeRate CRUD operations with tenant support"""
@staticmethod
def get_all(
db: Session,
tenant_id: int,
company_id: int,
skip: int = 0,
limit: int = 50,
filters: Optional[Dict[str, Any]] = None,
) -> Tuple[List[models.ExchangeRate], int]:
"""Get all exchange rates for a tenant/company with pagination"""
query = db.query(models.ExchangeRate).filter(
models.ExchangeRate.tenant_id == tenant_id,
models.ExchangeRate.company_id == company_id,
)
# Apply filters if provided
if filters:
if filters.get("date"):
query = query.filter(models.ExchangeRate.date == filters["date"])
if filters.get("local_currency"):
query = query.filter(
models.ExchangeRate.local_currency == filters["local_currency"]
)
if filters.get("foreign_currency"):
query = query.filter(
models.ExchangeRate.foreign_currency == filters["foreign_currency"]
)
total = query.count()
exchange_rates = query.order_by(models.ExchangeRate.date.desc()).offset(skip).limit(limit).all()
return exchange_rates, total
@staticmethod
def get_by_id(
db: Session, exchange_rate_id: int, tenant_id: int, company_id: int
) -> Optional[models.ExchangeRate]:
"""Get exchange rate by ID"""
return (
db.query(models.ExchangeRate)
.filter(
models.ExchangeRate.id == exchange_rate_id,
models.ExchangeRate.tenant_id == tenant_id,
models.ExchangeRate.company_id == company_id,
)
.first()
)
@staticmethod
def create(
db: Session,
exchange_rate_data: dto.ExchangeRateCreateDTO,
tenant_id: int,
company_id: int,
) -> models.ExchangeRate:
"""Create a new exchange rate"""
new_exchange_rate = models.ExchangeRate(
**exchange_rate_data.model_dump(), tenant_id=tenant_id, company_id=company_id
)
db.add(new_exchange_rate)
db.commit()
db.refresh(new_exchange_rate)
return new_exchange_rate
@staticmethod
def update(
db: Session,
exchange_rate_id: int,
tenant_id: int,
company_id: int,
exchange_rate_data: dto.ExchangeRateUpdateDTO,
) -> Optional[models.ExchangeRate]:
"""Update an exchange rate"""
exchange_rate = ExchangeRateService.get_by_id(
db, exchange_rate_id, tenant_id, company_id
)
if not exchange_rate:
return None
# Update fields
update_data = exchange_rate_data.model_dump(exclude_unset=True)
for field, value in update_data.items():
setattr(exchange_rate, field, value)
db.commit()
db.refresh(exchange_rate)
return exchange_rate
@staticmethod
def delete(
db: Session, exchange_rate_id: int, tenant_id: int, company_id: int
) -> bool:
"""Delete an exchange rate"""
exchange_rate = ExchangeRateService.get_by_id(
db, exchange_rate_id, tenant_id, company_id
)
if not exchange_rate:
return False
db.delete(exchange_rate)
db.commit()
return True