Show only changed fields (delta) in ticket activity timeline

Before: entire ticket data was logged and shown in the activity tab.
After: compare old vs new values before saving; log only fields that
actually changed as { field: { from: '...', to: '...' } } pairs.

- TicketController.php: fetch old ticket before update, compute delta
- api/update_ticket.php: same fix for the API endpoint (currentTicket
  already fetched for auth, reuse it for delta comparison)
- TicketView.php: render delta format as "Field: old → new" with color;
  truncate long values (description) at 60 chars; keep legacy flat format
  as fallback for older log entries

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-28 13:35:01 -04:00
parent 3bb4792635
commit 170bd86aa6
3 changed files with 47 additions and 7 deletions
+17 -3
View File
@@ -161,14 +161,28 @@ class TicketController {
return;
}
// Fetch current ticket values before updating (for delta logging)
$oldTicket = $this->ticketModel->getTicketById((int)$id);
// Update ticket with user tracking
// Pass expected_updated_at for optimistic locking if provided
$expectedUpdatedAt = $data['expected_updated_at'] ?? null;
$result = $this->ticketModel->updateTicket($data, $userId, $expectedUpdatedAt);
// Log ticket update to audit log
if ($result['success'] && isset($GLOBALS['auditLog']) && $userId) {
$GLOBALS['auditLog']->logTicketUpdate($userId, $id, $data);
// Log ticket update to audit log — only the changed fields (delta)
if ($result['success'] && isset($GLOBALS['auditLog']) && $userId && $oldTicket) {
$trackFields = ['title', 'priority', 'status', 'description', 'category', 'type'];
$delta = [];
foreach ($trackFields as $field) {
$oldVal = (string)($oldTicket[$field] ?? '');
$newVal = (string)($data[$field] ?? '');
if ($oldVal !== $newVal) {
$delta[$field] = ['from' => $oldVal, 'to' => $newVal];
}
}
if (!empty($delta)) {
$GLOBALS['auditLog']->logTicketUpdate($userId, $id, $delta);
}
}
// Return JSON response