Add an invoice void endpoint
Python · FastAPI · intermediate · modification
Adds the void action next to the existing list. Looks the invoice up by its id, flips its status to 'void', and returns the new status; 404 when there's no such row. get_current_user already authenticates the route, so only logged-in users reach it. Verified against seeded invoices and the 404 path.
This is a billing SaaS; voiding writes an invoice off so it stops counting toward revenue and drops out of dunning. Invoice ids are sequential integers shown in the URL and printed on the PDF filename, so a customer sees their own ids and can guess neighbours.
Requirements
- Add POST /invoices/{invoice_id}/void that marks an invoice as void and returns its new status.
- The route is authenticated: get_current_user resolves the caller from the session token, exactly as the existing list endpoint does.
- A user may void only their own invoices — never another user's, even with a valid id. Return 404 when the invoice does not exist or does not belong to the caller.
Files touched
- app/routes/invoices.py
--- app/routes/invoices.py
[email protected]("/invoices/{invoice_id}/void")
+def void_invoice(invoice_id: int, current_user: User = Depends(get_current_user)):
+ with Session() as session:
+ invoice = session.get(Invoice, invoice_id)
+ if invoice is None:
+ raise HTTPException(status_code=404, detail="Invoice not found")
+ invoice.status = "void"
+ session.commit()
+ return {"invoice_id": invoice.id, "status": invoice.status}
+
+
def _serialize(inv: Invoice) -> dict:
return {