- Rearranged imports in multiple files for consistency and clarity. - Updated logging middleware to exclude specific paths from logging. - Enhanced security module by cleaning up token handling and improving tenant validation. - Added tenant and company scoped mixins for better database model management. - Implemented generic CRUD routes for tenant-scoped resources. - Improved error handling and response management in API routes. - Cleaned up login and logout processes to ensure proper session management. - Introduced mechanisms to clear local storage and cookies on tenant change. - Enhanced company store to detect tenant changes and clear data accordingly. - Added new DTO mixins for currency and value affect flags.
101 lines
3.1 KiB
Python
101 lines
3.1 KiB
Python
from typing import List
|
|
|
|
from core.database import get_core_db
|
|
from core.security import get_current_user
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from .dto import ExchangeRateCreateDTO, ExchangeRateResponseDTO
|
|
from .services import ExchangeRateService
|
|
|
|
router = APIRouter(prefix="/exchange-rate", tags=["ExchangeRate"])
|
|
|
|
|
|
@router.get("/", response_model=List[ExchangeRateResponseDTO])
|
|
async def list_exchange_rates(
|
|
db: Session = Depends(get_core_db), current_user: dict = Depends(get_current_user)
|
|
):
|
|
"""
|
|
List all ExchangeRate entries.
|
|
"""
|
|
# Validate access to the tenant and company
|
|
tenant_id = current_user.get("tenant_id")
|
|
company_id = current_user.get("company_id")
|
|
|
|
if not tenant_id or not company_id:
|
|
raise HTTPException(
|
|
status_code=403, detail="Access denied: Tenant or Company not found"
|
|
)
|
|
|
|
return db.query(ExchangeRateService).all()
|
|
|
|
|
|
@router.get("/{date}", response_model=ExchangeRateResponseDTO)
|
|
async def read_exchange_rate(
|
|
date: int,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Get a specific ExchangeRate by its date.
|
|
"""
|
|
# Validate access to the tenant and company
|
|
tenant_id = current_user.get("tenant_id")
|
|
company_id = current_user.get("company_id")
|
|
|
|
if not tenant_id or not company_id:
|
|
raise HTTPException(
|
|
status_code=403, detail="Access denied: Tenant or Company not found"
|
|
)
|
|
|
|
exchange_rate = ExchangeRateService.get_exchange_rate_by_date(db, date)
|
|
if not exchange_rate:
|
|
raise HTTPException(status_code=404, detail="ExchangeRate not found")
|
|
return exchange_rate
|
|
|
|
|
|
@router.post(
|
|
"/", response_model=ExchangeRateResponseDTO, status_code=status.HTTP_201_CREATED
|
|
)
|
|
async def create_exchange_rate(
|
|
exchange_rate_data: ExchangeRateCreateDTO,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Create a new ExchangeRate entry.
|
|
"""
|
|
# Validate access to the tenant and company
|
|
tenant_id = current_user.get("tenant_id")
|
|
company_id = current_user.get("company_id")
|
|
|
|
if not tenant_id or not company_id:
|
|
raise HTTPException(
|
|
status_code=403, detail="Access denied: Tenant or Company not found"
|
|
)
|
|
|
|
return ExchangeRateService.create_exchange_rate(db, exchange_rate_data)
|
|
|
|
|
|
@router.delete("/{date}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_exchange_rate(
|
|
date: int,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user),
|
|
):
|
|
"""
|
|
Delete an ExchangeRate by its date.
|
|
"""
|
|
# Validate access to the tenant and company
|
|
tenant_id = current_user.get("tenant_id")
|
|
company_id = current_user.get("company_id")
|
|
|
|
if not tenant_id or not company_id:
|
|
raise HTTPException(
|
|
status_code=403, detail="Access denied: Tenant or Company not found"
|
|
)
|
|
|
|
exchange_rate = ExchangeRateService.delete_exchange_rate(db, date)
|
|
if not exchange_rate:
|
|
raise HTTPException(status_code=404, detail="ExchangeRate not found")
|