40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from pydantic import BaseModel
|
|
from typing import Optional
|
|
from app.database import get_db
|
|
from app.api.v1.deps import get_current_user_id
|
|
from app.services.invoice import InvoiceService
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
class InvoiceApplyRequest(BaseModel):
|
|
invoice_type: str
|
|
title: str
|
|
tax_id: Optional[str] = None
|
|
amount: float
|
|
|
|
|
|
@router.post("/apply")
|
|
async def apply_invoice(
|
|
data: InvoiceApplyRequest,
|
|
user_id: str = Depends(get_current_user_id),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
service = InvoiceService(db)
|
|
result = await service.apply(user_id, data.model_dump())
|
|
if "error" in result:
|
|
raise HTTPException(status_code=400, detail=result["error"])
|
|
return {"success": True, "data": result}
|
|
|
|
|
|
@router.get("/list")
|
|
async def list_invoices(
|
|
user_id: str = Depends(get_current_user_id),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
service = InvoiceService(db)
|
|
items = await service.list_user(user_id)
|
|
return {"success": True, "data": items}
|