Poprawki API i panelu admina

This commit is contained in:
2026-05-29 16:27:23 +02:00
parent 583021915a
commit 9b15131461
12 changed files with 390 additions and 58 deletions

View File

@@ -4,6 +4,7 @@ header('Content-Type: application/json; charset=utf-8');
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/get_table_name.php';
require_once __DIR__ . '/resolve_table_operator.php';
require_once __DIR__ . '/message_text_helper.php';
$kdsSecret = 'karczma_kuchnia';
@@ -14,19 +15,69 @@ function verifyKdsSecret(): bool
return $secret === $kdsSecret;
}
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if (!verifyKdsSecret()) {
http_response_code(403);
echo json_encode([
'status' => 'error',
'message' => 'Forbidden',
], JSON_UNESCAPED_UNICODE);
exit;
function resolveGuestQueueTableId(string $tableId, string $qrHash): string
{
global $conn;
if ($tableId === '' && $qrHash !== '' && isset($conn)) {
$resolved = getTableNameByHash($conn, $qrHash);
if ($resolved !== '') {
$tableId = $resolved;
}
}
try {
$pdo = getAnalyticsPdo();
$stmt = $pdo->query("
return trim($tableId);
}
function hasPendingGuestAction(PDO $pdo, string $tableId, string $messageType): bool
{
$stmt = $pdo->prepare("
SELECT 1
FROM guest_action_queue
WHERE table_id = :table_id
AND message_type = :message_type
AND status_kds = 0
LIMIT 1
");
$stmt->execute([
':table_id' => $tableId,
':message_type' => $messageType,
]);
return (bool) $stmt->fetchColumn();
}
function fetchPendingGuestActions(PDO $pdo, string $tableId): array
{
$stmt = $pdo->prepare("
SELECT message_type
FROM guest_action_queue
WHERE table_id = :table_id
AND status_kds = 0
AND message_type IN ('waiter_call', 'bill_request')
");
$stmt->execute([':table_id' => $tableId]);
$pending = [
'waiter_call' => false,
'bill_request' => false,
];
while ($row = $stmt->fetch()) {
$type = (string) ($row['message_type'] ?? '');
if (isset($pending[$type])) {
$pending[$type] = true;
}
}
return $pending;
}
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if (verifyKdsSecret()) {
try {
$pdo = getAnalyticsPdo();
$stmt = $pdo->query("
SELECT
id,
table_id,
@@ -45,6 +96,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
foreach ($rows as &$row) {
$row['id'] = (int) $row['id'];
$row['message_text'] = normalizeQueueMessageText((string) ($row['message_text'] ?? ''));
}
unset($row);
@@ -61,6 +113,44 @@ if ($_SERVER['REQUEST_METHOD'] === 'GET') {
'status' => 'error',
'message' => 'Queue fetch failed',
], JSON_UNESCAPED_UNICODE);
}
exit;
}
$qrHash = isset($_GET['h']) ? trim((string) $_GET['h']) : '';
$tableId = resolveGuestQueueTableId(
isset($_GET['tableId']) ? trim((string) $_GET['tableId']) : '',
$qrHash
);
if ($tableId === '') {
http_response_code(400);
echo json_encode([
'status' => 'error',
'message' => 'tableId or h is required',
], JSON_UNESCAPED_UNICODE);
exit;
}
if (strlen($tableId) > 32) {
$tableId = substr($tableId, 0, 32);
}
try {
$pdo = getAnalyticsPdo();
$pending = fetchPendingGuestActions($pdo, $tableId);
echo json_encode([
'status' => 'success',
'table_id' => $tableId,
'pending' => $pending,
], JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
http_response_code(500);
echo json_encode([
'status' => 'error',
'message' => 'Pending status fetch failed',
], JSON_UNESCAPED_UNICODE);
}
exit;
}
@@ -151,7 +241,7 @@ if (!is_array($data)) {
$tableId = isset($data['tableId']) ? trim((string) $data['tableId']) : '';
$messageType = isset($data['messageType']) ? trim((string) $data['messageType']) : '';
$messageText = isset($data['messageText']) ? trim((string) $data['messageText']) : '';
$messageText = isset($data['messageText']) ? (string) $data['messageText'] : '';
$qrHash = isset($data['qrHash']) ? trim((string) $data['qrHash']) : '';
$otwierajacyImie = isset($data['otwierajacyImie']) ? trim((string) $data['otwierajacyImie']) : '';
$otwierajacyNazwisko = isset($data['otwierajacyNazwisko']) ? trim((string) $data['otwierajacyNazwisko']) : '';
@@ -166,12 +256,7 @@ if ($messageType === '' || !in_array($messageType, $allowedTypes, true)) {
exit;
}
if ($tableId === '' && $qrHash !== '' && isset($conn)) {
$resolved = getTableNameByHash($conn, $qrHash);
if ($resolved !== '') {
$tableId = $resolved;
}
}
$tableId = resolveGuestQueueTableId($tableId, $qrHash);
if ($tableId === '') {
http_response_code(422);
@@ -182,6 +267,8 @@ if ($tableId === '') {
exit;
}
$messageText = normalizeQueueMessageText($messageText);
if ($messageText === '') {
http_response_code(422);
echo json_encode([
@@ -216,6 +303,19 @@ if (strlen($otwierajacyNazwisko) > 100) {
try {
$pdo = getAnalyticsPdo();
if (hasPendingGuestAction($pdo, $tableId, $messageType)) {
http_response_code(409);
echo json_encode([
'status' => 'error',
'code' => 'pending_on_kds',
'message' => $messageType === 'waiter_call'
? 'Kelner został już wezwany i czeka na obsłudze w KDS.'
: 'Prośba o rachunek jest już aktywna w KDS.',
], JSON_UNESCAPED_UNICODE);
exit;
}
$stmt = $pdo->prepare("
INSERT INTO guest_action_queue (
table_id,