Skip to content
Lesson 08 of 148 min

The status workflow ​

The status workflow decides which moves an issue can make. In this lesson an issue moves from To Do to Done one step at a time, and every skipped step is refused.

1. Write the Workflow Down ​

The workflow from lesson 1 is a map from each status to the statuses it can move to. Put it next to the enum in app/enums.py, and give IssueStatus two methods that read it:

python
from enum import StrEnum


class IssueStatus(StrEnum):
    TODO = "todo"
    IN_PROGRESS = "in_progress"
    IN_REVIEW = "in_review"
    DONE = "done"

    def transitions(self) -> list["IssueStatus"]: 
        return WORKFLOW[self] 
    def can_move_to(self, status: "IssueStatus") -> bool: 
        return status in self.transitions() 
WORKFLOW = { 
    IssueStatus.TODO: [IssueStatus.IN_PROGRESS], 
    IssueStatus.IN_PROGRESS: [IssueStatus.TODO, IssueStatus.IN_REVIEW], 
    IssueStatus.IN_REVIEW: [IssueStatus.IN_PROGRESS, IssueStatus.DONE], 
    IssueStatus.DONE: [IssueStatus.TODO], 
} 

The rules live in one place. To add a "Blocked" status later, you add it to the enum and to this map.

2. Check the Move While Validating ​

Field rules check one value at a time. Whether a move is allowed depends on the issue's current status in the database, so it needs an after() hook. It runs once the field rules have passed and can add its own errors. Add a request to app/http/requests/issue.py:

python
from typing import Annotated

from fastapi_startkit.validation import Field, FormRequest 
from fastapi_startkit.validation import Field, FormRequest, Validator 

from app.enums import IssuePriority 
from app.enums import IssuePriority, IssueStatus 
from app.models.issue import Issue 


class StoreIssueRequest(FormRequest):
    ...


class TransitionIssueRequest(FormRequest): 
    status: IssueStatus 
    @classmethod
    async def after(cls, validator: Validator) -> None: 
        issue = await Issue.find(validator.context["issue"]) 
        status = validator.data.get("status") 
        if issue is None or validator.errors.has("status"): 
            return
        if not issue.status.can_move_to(status): 
            validator.errors.add("status", f"An issue in {issue.status} can't move to {status}.") 

The validator gives the hook what it needs:

  • validator.context holds the path parameters, so context["issue"] is the {issue} in the URL, already turned into an int.
  • validator.data is the body that passed the field rules.
  • validator.errors collects errors. Anything you add() comes back in the same 422 shape as the field rules.

The hook returns early when the issue doesn't exist, so the controller's find_or_fail() can answer with a 404. It also returns early when status already failed, such as "closed", so the client sees one clear error.

3. Show the Next Steps ​

Clients need to know which buttons to show. A computed field adds transitions to every issue the API returns. In app/http/resources/issue.py:

python
from datetime import datetime

from pydantic import BaseModel, ConfigDict 
from pydantic import BaseModel, ConfigDict, computed_field 

from app.enums import IssuePriority, IssueStatus
from app.http.resources.user import UserResource


class IssueResource(BaseModel):
    ...
    assignee: UserResource | None
    created_at: datetime

    @computed_field
    @property
    def transitions(self) -> list[IssueStatus]: 
        return self.status.transitions() 

4. Add the Endpoint ​

A move is an action on an issue, so it gets its own route instead of being a field you can edit. Add a method to IssueController:

python
from app.http.requests.issue import StoreIssueRequest 
from app.http.requests.issue import StoreIssueRequest, TransitionIssueRequest 

class IssueController:
    ...

    async def transition(self, issue: int, request: Validated[TransitionIssueRequest]) -> IssueResource: 
        found = await Issue.find_or_fail(issue) 
        await found.update({"status": request.status}) 
        await found.load("reporter", "assignee") 
        return IssueResource.model_validate(found) 

And its route in routes/api.py:

python
api.get("/issues/{issue}", issues.show)
api.post("/issues/{issue}/transitions", issues.transition) 

Check ​

Start from a clean database with one new issue:

bash
uv run python artisan db:migrate:fresh
uv run python artisan db:seed
curl -X POST http://127.0.0.1:8000/api/projects/1/issues \
  -H "Content-Type: application/json" \
  -d '{"title": "Add a pricing page", "reporter_id": 2}'

The response now ends with "transitions": ["in_progress"]. Try to skip ahead to Done:

bash
curl -X POST http://127.0.0.1:8000/api/issues/1/transitions \
  -H "Content-Type: application/json" \
  -d '{"status": "done"}'
json
{"message": "An issue in todo can't move to done.", "errors": {"status": ["An issue in todo can't move to done."]}}

Now take the allowed path. Each response shows the new status and the next moves:

bash
curl -X POST http://127.0.0.1:8000/api/issues/1/transitions \
  -H "Content-Type: application/json" \
  -d '{"status": "in_progress"}'
json
{"id": 1, "key": "WEB-1", "status": "in_progress", "transitions": ["todo", "in_review"], ...}

Send in_review and then done, and the issue ends with "status": "done" and "transitions": ["todo"].

A status that isn't in the enum is caught by the field rule:

json
{"message": "Input should be 'todo', 'in_progress', 'in_review' or 'done'", "errors": {"status": ["Input should be 'todo', 'in_progress', 'in_review' or 'done'"]}}

Compare your code

This lesson's code is tagged lesson-08. See the changes in this lesson.