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

Files touched

--- 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 {

Review this PR

Python practice