diff --git a/api/assign_ticket.php b/api/assign_ticket.php index a142ed4..f68d2fe 100644 --- a/api/assign_ticket.php +++ b/api/assign_ticket.php @@ -1,17 +1,19 @@ false, 'error' => 'Not authenticated']); exit; } +$userId = $_SESSION['user']['user_id']; + // Get request data $data = json_decode(file_get_contents('php://input'), true); $ticketId = $data['ticket_id'] ?? null; @@ -22,21 +24,36 @@ if (!$ticketId) { exit; } +// Create database connection +$conn = new mysqli( + $GLOBALS['config']['DB_HOST'], + $GLOBALS['config']['DB_USER'], + $GLOBALS['config']['DB_PASS'], + $GLOBALS['config']['DB_NAME'] +); + +if ($conn->connect_error) { + echo json_encode(['success' => false, 'error' => 'Database connection failed']); + exit; +} + $ticketModel = new TicketModel($conn); $auditLogModel = new AuditLogModel($conn); if ($assignedTo === null || $assignedTo === '') { // Unassign ticket - $success = $ticketModel->unassignTicket($ticketId, $_SESSION['user_id']); + $success = $ticketModel->unassignTicket($ticketId, $userId); if ($success) { - $auditLogModel->log($_SESSION['user_id'], 'unassign', 'ticket', $ticketId); + $auditLogModel->log($userId, 'unassign', 'ticket', $ticketId); } } else { // Assign ticket - $success = $ticketModel->assignTicket($ticketId, $assignedTo, $_SESSION['user_id']); + $success = $ticketModel->assignTicket($ticketId, $assignedTo, $userId); if ($success) { - $auditLogModel->log($_SESSION['user_id'], 'assign', 'ticket', $ticketId, ['assigned_to' => $assignedTo]); + $auditLogModel->log($userId, 'assign', 'ticket', $ticketId, ['assigned_to' => $assignedTo]); } } +$conn->close(); + echo json_encode(['success' => $success]);