2026-01-01 18:36:34 -05:00
|
|
|
<?php
|
2026-02-11 14:50:06 -05:00
|
|
|
require_once __DIR__ . '/bootstrap.php';
|
2026-01-01 19:07:56 -05:00
|
|
|
require_once dirname(__DIR__) . '/models/TicketModel.php';
|
|
|
|
|
require_once dirname(__DIR__) . '/models/AuditLogModel.php';
|
2026-01-20 09:55:01 -05:00
|
|
|
require_once dirname(__DIR__) . '/models/UserModel.php';
|
2026-03-29 21:34:16 -04:00
|
|
|
require_once dirname(__DIR__) . '/helpers/NotificationHelper.php';
|
|
|
|
|
require_once dirname(__DIR__) . '/helpers/SynapseHelper.php';
|
2026-01-01 18:36:34 -05:00
|
|
|
|
|
|
|
|
// Get request data
|
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
2026-03-19 22:20:43 -04:00
|
|
|
if (!is_array($data)) {
|
|
|
|
|
http_response_code(400);
|
|
|
|
|
echo json_encode(['success' => false, 'error' => 'Invalid JSON body']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 13:31:10 -04:00
|
|
|
$ticketIdRaw = isset($data['ticket_id']) ? trim((string)$data['ticket_id']) : '';
|
2026-01-01 18:36:34 -05:00
|
|
|
$assignedTo = $data['assigned_to'] ?? null;
|
|
|
|
|
|
2026-04-11 13:31:10 -04:00
|
|
|
if (!ctype_digit($ticketIdRaw) || (int)$ticketIdRaw <= 0) {
|
2026-03-19 22:20:43 -04:00
|
|
|
http_response_code(400);
|
2026-01-01 18:36:34 -05:00
|
|
|
echo json_encode(['success' => false, 'error' => 'Ticket ID required']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
2026-04-11 13:31:10 -04:00
|
|
|
$ticketId = $ticketIdRaw;
|
2026-01-01 18:36:34 -05:00
|
|
|
|
|
|
|
|
$ticketModel = new TicketModel($conn);
|
|
|
|
|
$auditLogModel = new AuditLogModel($conn);
|
2026-01-20 09:55:01 -05:00
|
|
|
$userModel = new UserModel($conn);
|
2026-01-01 18:36:34 -05:00
|
|
|
|
2026-03-20 21:39:02 -04:00
|
|
|
// Verify ticket exists and user can access it
|
2026-03-19 22:20:43 -04:00
|
|
|
$ticket = $ticketModel->getTicketById($ticketId);
|
2026-03-20 21:39:02 -04:00
|
|
|
if (!$ticket || !$ticketModel->canUserAccessTicket($ticket, $currentUser)) {
|
2026-03-19 22:20:43 -04:00
|
|
|
http_response_code(404);
|
|
|
|
|
echo json_encode(['success' => false, 'error' => 'Ticket not found']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Authorization: only admins or the ticket creator/assignee can reassign
|
2026-03-29 17:39:46 -04:00
|
|
|
if (!$isAdmin && (int)$ticket['created_by'] !== (int)$userId && (int)$ticket['assigned_to'] !== (int)$userId) {
|
2026-03-19 22:20:43 -04:00
|
|
|
http_response_code(403);
|
|
|
|
|
echo json_encode(['success' => false, 'error' => 'Permission denied']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 18:36:34 -05:00
|
|
|
if ($assignedTo === null || $assignedTo === '') {
|
|
|
|
|
// Unassign ticket
|
2026-01-01 19:07:56 -05:00
|
|
|
$success = $ticketModel->unassignTicket($ticketId, $userId);
|
2026-01-01 18:36:34 -05:00
|
|
|
if ($success) {
|
2026-01-01 19:07:56 -05:00
|
|
|
$auditLogModel->log($userId, 'unassign', 'ticket', $ticketId);
|
2026-01-01 18:36:34 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-01-20 09:55:01 -05:00
|
|
|
// Validate assigned_to is a valid user ID
|
|
|
|
|
$assignedTo = (int)$assignedTo;
|
|
|
|
|
$targetUser = $userModel->getUserById($assignedTo);
|
|
|
|
|
if (!$targetUser) {
|
2026-03-29 17:39:46 -04:00
|
|
|
http_response_code(400);
|
2026-01-20 09:55:01 -05:00
|
|
|
echo json_encode(['success' => false, 'error' => 'Invalid user ID']);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 18:36:34 -05:00
|
|
|
// Assign ticket
|
2026-01-01 19:07:56 -05:00
|
|
|
$success = $ticketModel->assignTicket($ticketId, $assignedTo, $userId);
|
2026-01-01 18:36:34 -05:00
|
|
|
if ($success) {
|
2026-01-01 19:07:56 -05:00
|
|
|
$auditLogModel->log($userId, 'assign', 'ticket', $ticketId, ['assigned_to' => $assignedTo]);
|
2026-03-29 21:34:16 -04:00
|
|
|
|
|
|
|
|
if (!empty($GLOBALS['config']['MATRIX_NOTIFY_ASSIGNMENTS'])) {
|
|
|
|
|
$changedByDisplay = $currentUser['display_name'] ?? $currentUser['username'] ?? null;
|
|
|
|
|
$assigneeName = $targetUser['display_name'] ?? $targetUser['username'] ?? null;
|
|
|
|
|
$assigneeMatrix = isset($targetUser['username'])
|
|
|
|
|
? SynapseHelper::resolveUsername($targetUser['username'])
|
|
|
|
|
: null;
|
|
|
|
|
NotificationHelper::sendAssignmentNotification(
|
|
|
|
|
$ticketId,
|
|
|
|
|
$ticket['title'] ?? "Ticket #{$ticketId}",
|
|
|
|
|
$assigneeName,
|
|
|
|
|
$assigneeMatrix,
|
|
|
|
|
$changedByDisplay
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-01-01 18:36:34 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-19 22:20:43 -04:00
|
|
|
if (!$success) {
|
|
|
|
|
http_response_code(500);
|
2026-03-30 19:01:18 -04:00
|
|
|
apiRespond(['success' => false, 'error' => 'Failed to update ticket assignment']);
|
2026-03-19 22:20:43 -04:00
|
|
|
} else {
|
2026-04-05 12:39:24 -04:00
|
|
|
require_once dirname(__DIR__) . '/models/StatsModel.php';
|
|
|
|
|
(new StatsModel($conn))->invalidateCache();
|
2026-03-30 19:01:18 -04:00
|
|
|
apiRespond(['success' => true]);
|
2026-03-19 22:20:43 -04:00
|
|
|
}
|