Wykrywanie imienia i nazwiska kelnera oraz info czy sesja dostala sie do aplikacji

This commit is contained in:
2026-05-28 20:46:35 +02:00
parent a72b5afcc7
commit d374723fd6
12 changed files with 467 additions and 30 deletions

View File

@@ -3,12 +3,62 @@ 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';
$kdsSecret = 'karczma_kuchnia';
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$secret = isset($_GET['kds_secret']) ? trim((string) $_GET['kds_secret']) : '';
if ($secret !== $kdsSecret) {
http_response_code(403);
echo json_encode([
'status' => 'error',
'message' => 'Forbidden',
], JSON_UNESCAPED_UNICODE);
exit;
}
try {
$pdo = getAnalyticsPdo();
$stmt = $pdo->query("
SELECT
id,
table_id,
message_type,
message_text,
otwierajacy_imie,
otwierajacy_nazwisko,
api_sent,
status_kds,
created_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
");
$rows = $stmt->fetchAll();
echo json_encode([
'status' => 'success',
'count' => count($rows),
'data' => $rows,
], JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
http_response_code(500);
echo json_encode([
'status' => 'error',
'message' => 'Queue fetch failed',
], JSON_UNESCAPED_UNICODE);
}
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode([
'status' => 'error',
'message' => 'Method not allowed'
'message' => 'Method not allowed',
], JSON_UNESCAPED_UNICODE);
exit;
}
@@ -19,22 +69,24 @@ if (!is_array($data)) {
http_response_code(400);
echo json_encode([
'status' => 'error',
'message' => 'Invalid JSON payload'
'message' => 'Invalid JSON payload',
], JSON_UNESCAPED_UNICODE);
exit;
}
$tableId = isset($data['tableId']) ? trim((string)$data['tableId']) : '';
$messageType = isset($data['messageType']) ? trim((string)$data['messageType']) : '';
$messageText = isset($data['messageText']) ? trim((string)$data['messageText']) : '';
$qrHash = isset($data['qrHash']) ? trim((string)$data['qrHash']) : '';
$tableId = isset($data['tableId']) ? trim((string) $data['tableId']) : '';
$messageType = isset($data['messageType']) ? trim((string) $data['messageType']) : '';
$messageText = isset($data['messageText']) ? trim((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']) : '';
$allowedTypes = ['waiter_call', 'bill_request'];
if ($messageType === '' || !in_array($messageType, $allowedTypes, true)) {
http_response_code(422);
echo json_encode([
'status' => 'error',
'message' => 'Invalid messageType'
'message' => 'Invalid messageType',
], JSON_UNESCAPED_UNICODE);
exit;
}
@@ -50,7 +102,7 @@ if ($tableId === '') {
http_response_code(422);
echo json_encode([
'status' => 'error',
'message' => 'tableId is required'
'message' => 'tableId is required',
], JSON_UNESCAPED_UNICODE);
exit;
}
@@ -59,14 +111,33 @@ if ($messageText === '') {
http_response_code(422);
echo json_encode([
'status' => 'error',
'message' => 'messageText is required'
'message' => 'messageText is required',
], JSON_UNESCAPED_UNICODE);
exit;
}
if ($otwierajacyImie === '' && $otwierajacyNazwisko === '' && isset($conn)) {
$operator = resolveOperatorForTable($conn, strtolower($tableId));
if ($otwierajacyImie === '' && $operator['imie'] !== '') {
$otwierajacyImie = $operator['imie'];
}
if ($otwierajacyNazwisko === '' && $operator['nazwisko'] !== '') {
$otwierajacyNazwisko = $operator['nazwisko'];
}
if ($otwierajacyImie === '' && $otwierajacyNazwisko === '' && $operator['nick'] !== '') {
$otwierajacyImie = $operator['nick'];
}
}
if (strlen($tableId) > 32) {
$tableId = substr($tableId, 0, 32);
}
if (strlen($otwierajacyImie) > 100) {
$otwierajacyImie = substr($otwierajacyImie, 0, 100);
}
if (strlen($otwierajacyNazwisko) > 100) {
$otwierajacyNazwisko = substr($otwierajacyNazwisko, 0, 100);
}
try {
$pdo = getAnalyticsPdo();
@@ -75,6 +146,8 @@ try {
table_id,
message_type,
message_text,
otwierajacy_imie,
otwierajacy_nazwisko,
api_sent,
status_kds,
created_at
@@ -82,6 +155,8 @@ try {
:table_id,
:message_type,
:message_text,
:otwierajacy_imie,
:otwierajacy_nazwisko,
0,
0,
NOW(3)
@@ -92,17 +167,18 @@ try {
':table_id' => $tableId,
':message_type' => $messageType,
':message_text' => $messageText,
':otwierajacy_imie' => $otwierajacyImie !== '' ? $otwierajacyImie : null,
':otwierajacy_nazwisko' => $otwierajacyNazwisko !== '' ? $otwierajacyNazwisko : null,
]);
echo json_encode([
'status' => 'success',
'id' => (int)$pdo->lastInsertId()
'id' => (int) $pdo->lastInsertId(),
], JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
http_response_code(500);
echo json_encode([
'status' => 'error',
'message' => 'Queue insert failed'
'message' => 'Queue insert failed',
], JSON_UNESCAPED_UNICODE);
}