This commit is contained in:
2026-05-29 07:44:27 +02:00
parent d374723fd6
commit 583021915a
3 changed files with 147 additions and 17 deletions

View File

@@ -7,9 +7,15 @@ require_once __DIR__ . '/resolve_table_operator.php';
$kdsSecret = 'karczma_kuchnia';
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
function verifyKdsSecret(): bool
{
global $kdsSecret;
$secret = isset($_GET['kds_secret']) ? trim((string) $_GET['kds_secret']) : '';
if ($secret !== $kdsSecret) {
return $secret === $kdsSecret;
}
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if (!verifyKdsSecret()) {
http_response_code(403);
echo json_encode([
'status' => 'error',
@@ -28,20 +34,25 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
message_text,
otwierajacy_imie,
otwierajacy_nazwisko,
api_sent,
status_kds,
created_at
created_at,
updated_at
FROM guest_action_queue
WHERE status_kds = 0
AND created_at >= DATE_SUB(NOW(), INTERVAL 12 HOUR)
ORDER BY created_at ASC
LIMIT 50
LIMIT 100
");
$rows = $stmt->fetchAll();
foreach ($rows as &$row) {
$row['id'] = (int) $row['id'];
}
unset($row);
echo json_encode([
'status' => 'success',
'count' => count($rows),
'polled_at' => date('Y-m-d H:i:s'),
'poll_interval_seconds' => 30,
'data' => $rows,
], JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
@@ -54,6 +65,70 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'PATCH') {
if (!verifyKdsSecret()) {
http_response_code(403);
echo json_encode([
'status' => 'error',
'message' => 'Forbidden',
], JSON_UNESCAPED_UNICODE);
exit;
}
$rawBody = file_get_contents('php://input');
$data = json_decode($rawBody, true);
if (!is_array($data)) {
http_response_code(400);
echo json_encode([
'status' => 'error',
'message' => 'Invalid JSON payload',
], JSON_UNESCAPED_UNICODE);
exit;
}
$id = isset($data['id']) ? (int) $data['id'] : 0;
if ($id < 1) {
http_response_code(422);
echo json_encode([
'status' => 'error',
'message' => 'id is required',
], JSON_UNESCAPED_UNICODE);
exit;
}
try {
$pdo = getAnalyticsPdo();
$stmt = $pdo->prepare("
UPDATE guest_action_queue
SET status_kds = 1
WHERE id = :id
AND status_kds = 0
");
$stmt->execute([':id' => $id]);
if ($stmt->rowCount() === 0) {
http_response_code(404);
echo json_encode([
'status' => 'error',
'message' => 'Queue item not found or already dismissed',
], JSON_UNESCAPED_UNICODE);
exit;
}
echo json_encode([
'status' => 'success',
'id' => $id,
], JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
http_response_code(500);
echo json_encode([
'status' => 'error',
'message' => 'Queue dismiss failed',
], JSON_UNESCAPED_UNICODE);
}
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode([