38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
from fastapi import APIRouter, HTTPException
|
|
from fastapi.responses import FileResponse
|
|
|
|
from backend.app.schemas import FinanceSyncResponse, FinanceSyncResult
|
|
from backend.app.services.email_service import create_monthly_zip, sync_finance_emails
|
|
|
|
|
|
router = APIRouter(prefix="/finance", tags=["finance"])
|
|
|
|
|
|
@router.post("/sync", response_model=FinanceSyncResponse)
|
|
async def sync_finance():
|
|
try:
|
|
items_raw = await sync_finance_emails()
|
|
except RuntimeError as exc:
|
|
raise HTTPException(status_code=500, detail=str(exc)) from exc
|
|
|
|
items = [FinanceSyncResult(**item) for item in items_raw]
|
|
return FinanceSyncResponse(items=items)
|
|
|
|
|
|
@router.get("/download/{month}")
|
|
async def download_finance_month(month: str):
|
|
"""
|
|
Download a zipped archive for a given month (YYYY-MM).
|
|
"""
|
|
try:
|
|
zip_path = await create_monthly_zip(month)
|
|
except FileNotFoundError as exc:
|
|
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
|
|
return FileResponse(
|
|
path=zip_path,
|
|
media_type="application/zip",
|
|
filename=f"finance_{month}.zip",
|
|
)
|
|
|