no-mistakes(review): Guard photo cleanup; add transitions helper and assign auto-advance
This commit is contained in:
+24
-1
@@ -2,6 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
@@ -32,6 +33,8 @@ from app.schemas.ticket import (
|
||||
from app.services import ticket as ticket_service
|
||||
from app.services.sla import get_sla_status
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/api/tickets", tags=["tickets"])
|
||||
|
||||
# Ensure uploads directory exists
|
||||
@@ -224,6 +227,19 @@ async def list_tickets(
|
||||
return TicketListResponse(items=items, total=total, page=page, page_size=page_size)
|
||||
|
||||
|
||||
@router.get("/{ticket_id}/transitions")
|
||||
async def get_ticket_transitions(
|
||||
ticket_id: int,
|
||||
db: Annotated[AsyncSession, Depends(get_db)],
|
||||
) -> dict:
|
||||
"""Return the valid next statuses for a ticket's current status."""
|
||||
ticket = await ticket_service.get_ticket(db, ticket_id)
|
||||
return {
|
||||
"current_status": ticket.status,
|
||||
"transitions": ticket_service.VALID_TRANSITIONS.get(ticket.status, []),
|
||||
}
|
||||
|
||||
|
||||
@router.get("/{ticket_id}", response_model=TicketOut)
|
||||
async def get_ticket(
|
||||
ticket_id: int,
|
||||
@@ -266,7 +282,14 @@ async def delete_ticket(
|
||||
for photo in ticket.photos:
|
||||
if photo.photo_url:
|
||||
name = photo.photo_url.rsplit("/", 1)[-1]
|
||||
(UPLOADS_DIR / name).unlink(missing_ok=True)
|
||||
try:
|
||||
(UPLOADS_DIR / name).unlink(missing_ok=True)
|
||||
except OSError:
|
||||
logger.warning(
|
||||
"Could not remove orphaned photo file %s for ticket %s",
|
||||
name,
|
||||
ticket_id,
|
||||
)
|
||||
|
||||
|
||||
# ── Status Transitions (convenience endpoints) ───────────────────────
|
||||
|
||||
+18
-1
@@ -236,6 +236,7 @@ async def update_ticket(
|
||||
# Handle status transitions separately
|
||||
new_status = data.get("status")
|
||||
old_status = ticket.status
|
||||
status_changed = new_status is not None and old_status != new_status
|
||||
if new_status is not None:
|
||||
if old_status != new_status:
|
||||
valid_targets = VALID_TRANSITIONS.get(old_status, [])
|
||||
@@ -296,9 +297,25 @@ async def update_ticket(
|
||||
|
||||
ticket.status = new_status
|
||||
|
||||
# Assigning a technician advances pre-Assigned tickets to Assigned with a
|
||||
# timeline entry; tickets already past Assigned keep their current status.
|
||||
advanced_to_assigned = False
|
||||
if "assigned_to" in data and data["assigned_to"] != ticket.assigned_to:
|
||||
if ticket.status in {"New", "Logged", "Triage"}:
|
||||
await _log_status_change(
|
||||
db,
|
||||
ticket.id,
|
||||
from_status=ticket.status,
|
||||
to_status="Assigned",
|
||||
note=data.get("note") if not status_changed else None,
|
||||
user_id=user.id if user else None,
|
||||
)
|
||||
ticket.status = "Assigned"
|
||||
advanced_to_assigned = True
|
||||
|
||||
# Handle standalone note (no status change)
|
||||
note_only = data.get("note")
|
||||
if note_only and not (new_status is not None and old_status != new_status):
|
||||
if note_only and not status_changed and not advanced_to_assigned:
|
||||
await _log_status_change(
|
||||
db,
|
||||
ticket.id,
|
||||
|
||||
@@ -204,21 +204,9 @@
|
||||
<label class="block text-sm font-medium text-gray-700 mb-1">Current: <span class="font-bold" x-text="ticket.status"></span></label>
|
||||
<select x-model="statusForm.newStatus" class="w-full px-4 py-2.5 rounded-lg border border-gray-300 focus:ring-2 focus:ring-denya-500 outline-none">
|
||||
<option value="">Select new status</option>
|
||||
<option value="Logged">Logged</option>
|
||||
<option value="Triage">Triage</option>
|
||||
<option value="Assigned">Assigned</option>
|
||||
<option value="Accepted">Accepted</option>
|
||||
<option value="Travelling">Travelling</option>
|
||||
<option value="On Site">On Site</option>
|
||||
<option value="In Progress">In Progress</option>
|
||||
<option value="Waiting Parts">Waiting Parts</option>
|
||||
<option value="Escalated">Escalated</option>
|
||||
<option value="Completed">Completed</option>
|
||||
<option value="On-Field Verification">On-Field Verification</option>
|
||||
<option value="Wahab Review">Wahab Review</option>
|
||||
<option value="Closed">Closed</option>
|
||||
<option value="Reopened">Reopened</option>
|
||||
<option value="Cancelled">Cancelled</option>
|
||||
<template x-for="target in availableTransitions" :key="target">
|
||||
<option :value="target" x-text="target"></option>
|
||||
</template>
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
@@ -303,6 +291,7 @@
|
||||
statusForm: { newStatus: '', note: '' },
|
||||
statusSubmitting: false,
|
||||
statusError: '',
|
||||
availableTransitions: [],
|
||||
|
||||
// Assign form
|
||||
assignForm: { technicianId: '' },
|
||||
@@ -316,6 +305,7 @@
|
||||
|
||||
async init() {
|
||||
await this.loadTicket();
|
||||
await this.loadTransitions();
|
||||
if (app().isFM || app().isAdmin) {
|
||||
await this.loadTechnicians();
|
||||
}
|
||||
@@ -332,6 +322,16 @@
|
||||
}
|
||||
},
|
||||
|
||||
async loadTransitions() {
|
||||
try {
|
||||
const data = await app().apiGet(`/api/tickets/${this.ticketId}/transitions`);
|
||||
this.availableTransitions = (data && data.transitions) || [];
|
||||
} catch (e) {
|
||||
console.error('Transitions load error', e);
|
||||
this.availableTransitions = [];
|
||||
}
|
||||
},
|
||||
|
||||
async loadTechnicians() {
|
||||
try {
|
||||
const users = await app().apiGet('/api/auth/users');
|
||||
@@ -356,6 +356,7 @@
|
||||
|
||||
const updated = await app().apiPatch(`/api/tickets/${this.ticketId}`, payload);
|
||||
this.ticket = updated;
|
||||
await this.loadTransitions();
|
||||
this.showStatusModal = false;
|
||||
this.statusForm = { newStatus: '', note: '' };
|
||||
app().showToast('Status updated', 'success');
|
||||
@@ -378,6 +379,7 @@
|
||||
assigned_to: parseInt(this.assignForm.technicianId)
|
||||
});
|
||||
this.ticket = updated;
|
||||
await this.loadTransitions();
|
||||
this.showAssignModal = false;
|
||||
app().showToast('Technician assigned', 'success');
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user