diff --git a/backend/core/api/public/endpoints/Invoices/edit.py b/backend/core/api/public/endpoints/Invoices/edit.py index 145666bd..94c76452 100644 --- a/backend/core/api/public/endpoints/Invoices/edit.py +++ b/backend/core/api/public/endpoints/Invoices/edit.py @@ -76,7 +76,7 @@ def edit_invoice_endpoint(request: APIRequest): @api_view(["POST"]) def change_status_endpoint(request, invoice_id: int, invoice_status: str): - invoice_status: Literal["paid", "draft", "pending"] = invoice_status.lower() if invoice_status else "" + new_status = invoice_status.lower() if invoice_status else "" try: invoice = Invoice.objects.get(id=invoice_id) @@ -86,15 +86,13 @@ def change_status_endpoint(request, invoice_id: int, invoice_status: str): if request.user.logged_in_as_team and request.user.logged_in_as_team != invoice.organization or request.user != invoice.user: return APIResponse(False, {"error": "You do not have permission to edit this invoice"}, status=status.HTTP_403_FORBIDDEN) - if invoice_status not in ["paid", "draft", "pending"]: - return APIResponse(False, {"error": "Invalid status. Please choose from: pending, paid, draft"}, status=status.HTTP_400_BAD_REQUEST) - - if invoice.status == invoice_status: - return APIResponse(False, {"error": f"Invoice status is already {invoice_status}"}, status=status.HTTP_400_BAD_REQUEST) + if invoice.status == new_status: + return APIResponse(False, {"error": f"Invoice status is already {new_status}"}, status=status.HTTP_400_BAD_REQUEST) - invoice.set_status(invoice_status) + if not invoice.set_status(new_status, save=True): + return APIResponse(False, {"error": "Invalid status. Please choose from: pending, paid, draft"}, status=status.HTTP_400_BAD_REQUEST) - return APIResponse(True, {"message": f"Invoice status been changed to {invoice_status}"}, status=status.HTTP_200_OK) + return APIResponse(True, {"message": f"Invoice status been changed to {new_status}"}, status=status.HTTP_200_OK) @api_view(["POST"]) diff --git a/backend/finance/api/invoices/edit.py b/backend/finance/api/invoices/edit.py index 1c9be2ec..9ed35fe2 100644 --- a/backend/finance/api/invoices/edit.py +++ b/backend/finance/api/invoices/edit.py @@ -77,7 +77,7 @@ def edit_invoice(request: HtmxHttpRequest): @require_POST @web_require_scopes("invoices:write", True, True) def change_status(request: HtmxHttpRequest, invoice_id: int, status: str) -> HttpResponse: - status: Literal["paid", "draft", "pending"] = status.lower() if status else "" + status = status.lower() if status else "" if not request.htmx: return redirect("finance:invoices:single:dashboard") diff --git a/backend/finance/api/invoices/recurring/update_status.py b/backend/finance/api/invoices/recurring/update_status.py index 276d2ddd..49cf302b 100644 --- a/backend/finance/api/invoices/recurring/update_status.py +++ b/backend/finance/api/invoices/recurring/update_status.py @@ -1,3 +1,4 @@ +from typing import Literal from django.conf import settings from django.contrib import messages from django.http import HttpRequest, HttpResponse diff --git a/backend/finance/models.py b/backend/finance/models.py index 87c6c236..10519695 100644 --- a/backend/finance/models.py +++ b/backend/finance/models.py @@ -169,7 +169,9 @@ def __str__(self): return f"Invoice #{invoice_id} for {client}" - def set_status(self, status: Literal["draft", "pending", "paid"], save=True): + def set_status(self, status: str, save=True): + if status not in ["draft", "pending", "paid"]: + return False self.status = status self.status_updated_at = timezone.now() if save: