feat: add ticket solution closure loop
This commit is contained in:
@@ -7,6 +7,7 @@ from apps.solutions.schemas import (
|
||||
SolutionOut,
|
||||
TicketSolutionLinkIn,
|
||||
TicketSolutionLinkOut,
|
||||
TicketSolutionOut,
|
||||
)
|
||||
from apps.tickets.models import Ticket
|
||||
|
||||
@@ -38,6 +39,25 @@ def list_solutions(request, status: str | None = None):
|
||||
return queryset[:100]
|
||||
|
||||
|
||||
@router.get("/tickets/{ticket_id}", response=list[TicketSolutionOut])
|
||||
def list_ticket_solutions(request, ticket_id: int):
|
||||
require_permission(request, "solution.view")
|
||||
queryset = TicketSolutionLink.objects.select_related("solution", "ticket").filter(
|
||||
ticket_id=ticket_id
|
||||
)
|
||||
return [
|
||||
{
|
||||
"id": link.id,
|
||||
"ticket_id": link.ticket_id,
|
||||
"solution_id": link.solution_id,
|
||||
"link_type": link.link_type,
|
||||
"solution": link.solution,
|
||||
"created_at": link.created_at,
|
||||
}
|
||||
for link in queryset
|
||||
]
|
||||
|
||||
|
||||
@router.get("/{solution_id}", response=SolutionOut)
|
||||
def get_solution(request, solution_id: int):
|
||||
require_permission(request, "solution.view")
|
||||
|
||||
@@ -42,3 +42,12 @@ class TicketSolutionLinkOut(Schema):
|
||||
link_type: str
|
||||
solution_usage_count: int
|
||||
created_at: datetime
|
||||
|
||||
|
||||
class TicketSolutionOut(Schema):
|
||||
id: int
|
||||
ticket_id: int
|
||||
solution_id: int
|
||||
link_type: str
|
||||
solution: SolutionOut
|
||||
created_at: datetime
|
||||
|
||||
@@ -39,3 +39,30 @@ def test_link_solution_to_ticket_increments_usage(admin_client, db):
|
||||
assert response.status_code == 200
|
||||
assert response.json()["link_type"] == "adopted"
|
||||
assert response.json()["solution_usage_count"] == 1
|
||||
|
||||
|
||||
def test_list_ticket_solutions_returns_linked_solution_details(admin_client, db):
|
||||
ticket = Ticket.objects.create(ticket_no="TK202607020001", title="A", description="A")
|
||||
solution = admin_client.post(
|
||||
"/api/solutions",
|
||||
data={
|
||||
"title": "Restart callback worker",
|
||||
"problem_signature": "Callback timeout",
|
||||
"root_cause": "Worker queue blocked",
|
||||
"solution": "Restart worker and verify queue depth.",
|
||||
"status": "published",
|
||||
},
|
||||
content_type="application/json",
|
||||
).json()
|
||||
admin_client.post(
|
||||
f"/api/solutions/{solution['id']}/link-ticket",
|
||||
data={"ticket_id": ticket.id, "link_type": "adopted"},
|
||||
content_type="application/json",
|
||||
)
|
||||
|
||||
response = admin_client.get(f"/api/solutions/tickets/{ticket.id}")
|
||||
|
||||
assert response.status_code == 200
|
||||
payload = response.json()
|
||||
assert payload[0]["link_type"] == "adopted"
|
||||
assert payload[0]["solution"]["title"] == "Restart callback worker"
|
||||
|
||||
@@ -11,7 +11,7 @@ class TicketStatusError(ValueError):
|
||||
ALLOWED_TRANSITIONS: dict[str, set[str]] = {
|
||||
"pending_info": {"pending", "closed"},
|
||||
"pending": {"in_progress", "closed"},
|
||||
"in_progress": {"pending_confirm", "resolved", "closed"},
|
||||
"in_progress": {"pending_confirm"},
|
||||
"pending_confirm": {"resolved", "in_progress", "closed"},
|
||||
"resolved": {"closed"},
|
||||
"closed": set(),
|
||||
|
||||
@@ -7,6 +7,16 @@ def test_pending_ticket_can_move_to_in_progress():
|
||||
validate_status_transition("pending", "in_progress")
|
||||
|
||||
|
||||
def test_in_progress_ticket_must_not_close_directly():
|
||||
with pytest.raises(TicketStatusError, match="in_progress -> closed"):
|
||||
validate_status_transition("in_progress", "closed")
|
||||
|
||||
|
||||
def test_in_progress_ticket_must_not_resolve_directly():
|
||||
with pytest.raises(TicketStatusError, match="in_progress -> resolved"):
|
||||
validate_status_transition("in_progress", "resolved")
|
||||
|
||||
|
||||
def test_resolved_ticket_cannot_move_back_to_in_progress():
|
||||
with pytest.raises(TicketStatusError, match="resolved -> in_progress"):
|
||||
validate_status_transition("resolved", "in_progress")
|
||||
|
||||
Reference in New Issue
Block a user