- Consolidate all 20 API files to use centralized Database helper - Add optimistic locking to ticket updates to prevent concurrent conflicts - Add caching to StatsModel (60s TTL) for dashboard performance - Add health check endpoint (api/health.php) for monitoring - Improve rate limit cleanup with cron script and efficient DirectoryIterator - Enable rate limit response headers (X-RateLimit-*) - Add audit logging for workflow transitions - Log Discord webhook failures instead of silencing - Fix visibility check on export_tickets.php - Add database migration system with performance indexes - Fix cron recurring tickets to use assignTicket method Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
108 lines
2.9 KiB
PHP
108 lines
2.9 KiB
PHP
<?php
|
|
/**
|
|
* Check for duplicate tickets API
|
|
*
|
|
* Searches for tickets with similar titles using LIKE and SOUNDEX
|
|
*/
|
|
|
|
// Apply rate limiting
|
|
require_once dirname(__DIR__) . '/middleware/RateLimitMiddleware.php';
|
|
RateLimitMiddleware::apply('api');
|
|
|
|
session_start();
|
|
require_once dirname(__DIR__) . '/config/config.php';
|
|
require_once dirname(__DIR__) . '/helpers/Database.php';
|
|
require_once dirname(__DIR__) . '/helpers/ResponseHelper.php';
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
// Check authentication
|
|
if (!isset($_SESSION['user']) || !isset($_SESSION['user']['user_id'])) {
|
|
ResponseHelper::unauthorized();
|
|
}
|
|
|
|
// Only accept GET requests
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
|
|
ResponseHelper::error('Method not allowed', 405);
|
|
}
|
|
|
|
// Get title parameter
|
|
$title = isset($_GET['title']) ? trim($_GET['title']) : '';
|
|
|
|
if (strlen($title) < 5) {
|
|
ResponseHelper::success(['duplicates' => []]);
|
|
}
|
|
|
|
// Use centralized database connection
|
|
$conn = Database::getConnection();
|
|
|
|
// Search for similar titles
|
|
// Use both LIKE for substring matching and SOUNDEX for phonetic matching
|
|
$duplicates = [];
|
|
|
|
// Prepare search term for LIKE
|
|
$searchTerm = '%' . $conn->real_escape_string($title) . '%';
|
|
|
|
// Get SOUNDEX of title
|
|
$soundexTitle = soundex($title);
|
|
|
|
// First, search for exact substring matches (case-insensitive)
|
|
$sql = "SELECT ticket_id, title, status, priority, created_at
|
|
FROM tickets
|
|
WHERE (
|
|
title LIKE ?
|
|
OR SOUNDEX(title) = ?
|
|
)
|
|
AND status != 'Closed'
|
|
ORDER BY created_at DESC
|
|
LIMIT 10";
|
|
|
|
$stmt = $conn->prepare($sql);
|
|
$stmt->bind_param("ss", $searchTerm, $soundexTitle);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
|
|
while ($row = $result->fetch_assoc()) {
|
|
// Calculate similarity score
|
|
$similarity = 0;
|
|
|
|
// Check for exact substring match
|
|
if (stripos($row['title'], $title) !== false) {
|
|
$similarity = 90;
|
|
}
|
|
// Check SOUNDEX match
|
|
elseif (soundex($row['title']) === $soundexTitle) {
|
|
$similarity = 70;
|
|
}
|
|
// Check word overlap
|
|
else {
|
|
$titleWords = array_map('strtolower', preg_split('/\s+/', $title));
|
|
$rowWords = array_map('strtolower', preg_split('/\s+/', $row['title']));
|
|
$matchingWords = array_intersect($titleWords, $rowWords);
|
|
$similarity = (count($matchingWords) / max(count($titleWords), 1)) * 60;
|
|
}
|
|
|
|
if ($similarity >= 30) {
|
|
$duplicates[] = [
|
|
'ticket_id' => $row['ticket_id'],
|
|
'title' => $row['title'],
|
|
'status' => $row['status'],
|
|
'priority' => $row['priority'],
|
|
'created_at' => $row['created_at'],
|
|
'similarity' => round($similarity)
|
|
];
|
|
}
|
|
}
|
|
|
|
$stmt->close();
|
|
|
|
// Sort by similarity descending
|
|
usort($duplicates, function($a, $b) {
|
|
return $b['similarity'] - $a['similarity'];
|
|
});
|
|
|
|
// Limit to top 5
|
|
$duplicates = array_slice($duplicates, 0, 5);
|
|
|
|
ResponseHelper::success(['duplicates' => $duplicates]);
|